Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion src/api/embedded/plasma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {BigNumber, BigNumberish} from "../../utilities/bignumber.js";
import { MEMORY_POOL_PAGE_SIZE, RPC_MAX_PAGE_SIZE } from "../../zenon.js";
import { Api } from "../base.js";
import { Plasma } from "../../embedded/plasma.js";
import { FusionEntryList, GetRequiredPowParam, GetRequiredPowResponse, PlasmaInfo } from "../../model/embedded/plasma.js";
import { FusionEntryList, GetRequiredPowParam, GetRequiredPowResponse, PlasmaInfo, PlasmaVariables } from "../../model/embedded/plasma.js";
import { AccountBlockTemplate } from "../../model/nom/accountBlock.js";
import { Address, PLASMA_ADDRESS, Hash, QSR_ZTS } from "../../model/primitives/index.js";

Expand Down Expand Up @@ -53,6 +53,11 @@ export class PlasmaApi extends Api {
return GetRequiredPowResponse.fromJson(response);
}

async getVariables(): Promise<PlasmaVariables> {
const response = await this.client.sendRequest("embedded.plasma.getVariables", []);
return PlasmaVariables.fromJson(response!);
}

//
// Contract methods

Expand All @@ -77,4 +82,25 @@ export class PlasmaApi extends Api {
])
);
}

setVariables(
maxBasePlasmaInMomentum: number,
fusedPlasmaTarget: number,
powPlasmaTarget: number,
maxPriceChangePercent: number,
priceChangeDenominator: number
): AccountBlockTemplate {
return AccountBlockTemplate.callContract(
PLASMA_ADDRESS,
QSR_ZTS,
BigNumber.from(0),
Plasma.abi.encodeFunctionData("SetVariables", [
maxBasePlasmaInMomentum,
fusedPlasmaTarget,
powPlasmaTarget,
maxPriceChangePercent,
priceChangeDenominator
])
);
}
}
14 changes: 14 additions & 0 deletions src/embedded/plasma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ export class Plasma extends EmbeddedContract {
{"type":"function","name":"CancelFuse","inputs":[
{"name":"id","type":"hash"}
]},
{"type":"function","name":"SetVariables", "inputs":[
{"name":"maxBasePlasmaInMomentum","type":"uint64"},
{"name":"fusedPlasmaTarget","type":"uint64"},
{"name":"powPlasmaTarget","type":"uint64"},
{"name":"maxPriceChangePercent","type":"uint8"},
{"name":"priceChangeDenominator","type":"uint8"}
]},

{"type":"variable","name":"fusionInfo","inputs":[
{"name":"amount","type":"uint256"},
Expand All @@ -17,6 +24,13 @@ export class Plasma extends EmbeddedContract {
]},
{"type":"variable","name":"fusedAmount","inputs":[
{"name":"amount","type":"uint256"}
]},
{"type":"variable","name":"plasmaVariables","inputs":[
{"name":"maxBasePlasmaInMomentum","type":"uint64"},
{"name":"fusedPlasmaTarget","type":"uint64"},
{"name":"powPlasmaTarget","type":"uint64"},
{"name":"maxPriceChangePercent","type":"uint8"},
{"name":"priceChangeDenominator","type":"uint8"}
]}
]`;
}
22 changes: 22 additions & 0 deletions src/model/embedded/plasma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,25 @@ export class GetRequiredPowResponse extends Model {
}
}

export class PlasmaVariables extends Model {
constructor(
public maxBasePlasmaInMomentum: number,
public fusedPlasmaTarget: number,
public powPlasmaTarget: number,
public maxPriceChangePercent: number,
public priceChangeDenominator: number
) {
super()
}

static fromJson(json: {[key: string]: any}): PlasmaVariables {
return new PlasmaVariables(
json.MaxBasePlasmaInMomentum,
json.FusedPlasmaTarget,
json.PowPlasmaTarget,
json.MaxPriceChangePercent,
json.PriceChangeDenominator
);
}
}

12 changes: 9 additions & 3 deletions src/model/nom/momentum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ export class Momentum extends Model {
public changesHash: Hash,
public publicKey: string,
public signature: string,
public producer: Address
public producer: Address,
public nextFusionPrice?: number,
public nextWorkPrice?: number
){
super()
}
Expand All @@ -34,7 +36,9 @@ export class Momentum extends Model {
Hash.parse(json.changesHash),
json.publicKey || "",
json.signature || "",
Address.parse(json.producer)
Address.parse(json.producer),
json.nextFusionPrice,
json.nextWorkPrice
);
}

Expand All @@ -51,7 +55,9 @@ export class Momentum extends Model {
changesHash: this.changesHash?.toString(),
publicKey: this.publicKey,
signature: this.signature,
producer: this.producer.toString()
producer: this.producer.toString(),
nextFusionPrice: this.nextFusionPrice,
nextWorkPrice: this.nextWorkPrice
};
}
}
Expand Down
50 changes: 47 additions & 3 deletions test/api/embedded/plasma.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import {
FusionEntryList,
GetRequiredPowParam,
GetRequiredPowResponse,
PlasmaInfo
PlasmaInfo,
PlasmaVariables
} from "../../../src/model/embedded/plasma.js";
import { AccountBlockTemplate } from "../../../src/model/nom/index.js";
import {
Expand Down Expand Up @@ -37,8 +38,7 @@ const makeFusionEntryListJson = (overrides: Record<string, any> = {}) => ({
qsrAmount: "100",
beneficiary: ADDRESS,
expirationHeight: 123,
id: HASH_A,
isRevocable: true
id: HASH_A
}],
...overrides
});
Expand Down Expand Up @@ -144,4 +144,48 @@ describe("PlasmaApi", () => {
.to.equal(Buffer.from(arrayify(expectedData)).toString("hex"));
});
});

describe("getVariables", () => {
it("should fetch and parse plasma variables", async () => {
const mockResponse = {
MaxBasePlasmaInMomentum: 4200000,
FusedPlasmaTarget: 1050000,
PowPlasmaTarget: 1050000,
MaxPriceChangePercent: 10,
PriceChangeDenominator: 20
};
mockClient.setMockResponse("embedded.plasma.getVariables", mockResponse);

const result = await plasmaApi.getVariables();

const lastCall = mockClient.getLastCall();
expect(lastCall).to.exist;
expect(lastCall!.method).to.equal("embedded.plasma.getVariables");
expect(lastCall!.parameters).to.deep.equal([]);

expect(result).to.be.instanceOf(PlasmaVariables);
expect(result.maxBasePlasmaInMomentum).to.equal(4200000);
expect(result.fusedPlasmaTarget).to.equal(1050000);
expect(result.powPlasmaTarget).to.equal(1050000);
expect(result.maxPriceChangePercent).to.equal(10);
expect(result.priceChangeDenominator).to.equal(20);
});
});

describe("setVariables", () => {
it("should build a set variables block", async () => {
const template = await plasmaApi.setVariables(4200000, 1050000, 1050000, 10, 20);

const expectedData = PlasmaContract.abi.encodeFunctionData("SetVariables", [
4200000, 1050000, 1050000, 10, 20
]);

expect(template).to.be.instanceOf(AccountBlockTemplate);
expect(template.toAddress.toString()).to.equal(PLASMA_ADDRESS.toString());
expect(template.tokenStandard.toString()).to.equal(QSR_ZTS.toString());
expect(template.amount.toString()).to.equal("0");
expect(template.data.toString("hex"))
.to.equal(Buffer.from(arrayify(expectedData)).toString("hex"));
});
});
});
Loading