Skip to content

Commit

Permalink
fixes after review
Browse files Browse the repository at this point in the history
  • Loading branch information
popenta committed Sep 11, 2023
1 parent 5a0813c commit 41604d0
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 82 deletions.
1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ export const ARGUMENTS_SEPARATOR = "@";
export const VM_TYPE_WASM_VM = new Uint8Array([0x05, 0x00]);
export const CONTRACT_DEPLOY_ADDRESS = "erd1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq6gq4hu";
export const DELEGATION_MANAGER_SC_ADDRESS = "erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqylllslmq6y6";
export const DEFAULT_HRP = "erd";
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,16 @@ import { DelegationTransactionIntentsFactory } from "./delegationTransactionInte
import { assert } from "chai";
import { DELEGATION_MANAGER_SC_ADDRESS } from "../constants";
import { ValidatorPublicKey } from "@multiversx/sdk-wallet-next";
import { SignableMessage } from "../signableMessage";
import { TransactionIntentsFactoryConfig } from "./transactionIntentsFactoryConfig";

describe("test delegation intents factory", function () {
const delegationFactory = new DelegationTransactionIntentsFactory({
chainID: "D",
minGasLimit: 50000,
gasLimitPerByte: 1500,
gasLimitStake: 5000000,
gasLimitUnstake: 5000000,
gasLimitUnbond: 5000000,
gasLimitCreateDelegationContract: 50000000,
gasLimitDelegationOperations: 1000000,
additionalGasLimitPerValidatorNode: 6000000,
additionalGasLimitForDelegationOperations: 10000000
});
const config = new TransactionIntentsFactoryConfig("D");
const delegationFactory = new DelegationTransactionIntentsFactory(config);

it("should build intent for new delegation contract", async function () {
it("should create intent for new delegation contract", async function () {
const sender = Address.fromBech32("erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2");
const delagationCap = new BigNumber("5000000000000000000000");
const serviceFee = new BigNumber(10);
const delagationCap = "5000000000000000000000";
const serviceFee = 10;
const value = new BigNumber("1250000000000000000000");

const intent = delegationFactory.createTransactionIntentForNewDelegationContract({
Expand All @@ -41,19 +31,20 @@ describe("test delegation intents factory", function () {
assert.equal(intent.value, value);
});

it("should build intent for adding nodes", async function () {
it("should create intent for adding nodes", async function () {
const sender = Address.fromBech32("erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2");
const delegationContract = Address.fromBech32("erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqtllllls002zgc");
const publicKey = new ValidatorPublicKey(Buffer.from("e7beaa95b3877f47348df4dd1cb578a4f7cabf7a20bfeefe5cdd263878ff132b765e04fef6f40c93512b666c47ed7719b8902f6c922c04247989b7137e837cc81a62e54712471c97a2ddab75aa9c2f58f813ed4c0fa722bde0ab718bff382208", "hex"));

const signedMessage = new SignableMessage();
signedMessage.applySignature(Buffer.from("81109fa1c8d3dc7b6c2d6e65206cc0bc1a83c9b2d1eb91a601d66ad32def430827d5eb52917bd2b0d04ce195738db216", "hex"));
const mockMessage = {
getSignature: () => Buffer.from("81109fa1c8d3dc7b6c2d6e65206cc0bc1a83c9b2d1eb91a601d66ad32def430827d5eb52917bd2b0d04ce195738db216", "hex")
}

const intent = delegationFactory.createTransactionIntentForAddingNodes({
sender: sender,
delegationContract: delegationContract,
publicKeys: [publicKey],
signedMessages: [signedMessage.getSignature()]
signedMessages: [mockMessage.getSignature()]
});

assert.equal(intent.sender, "erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2");
Expand All @@ -63,13 +54,13 @@ describe("test delegation intents factory", function () {
assert.equal(intent.value, 0);
});

it("should build intent for removing nodes", async function () {
it("should create intent for removing nodes", async function () {
const sender = Address.fromBech32("erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2");
const delegationContract = Address.fromBech32("erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqtllllls002zgc");

const publicKey = {
hex(): string {
return Buffer.from("notavalidblskeyhexencoded").toString("hex");
return Buffer.from("abba").toString("hex");
}
};

Expand All @@ -82,17 +73,17 @@ describe("test delegation intents factory", function () {
assert.equal(intent.sender, "erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2");
assert.equal(intent.receiver, "erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqtllllls002zgc");
assert.isDefined(intent.data);
assert.deepEqual(intent.data, Buffer.from("removeNodes@6e6f746176616c6964626c736b6579686578656e636f646564"));
assert.deepEqual(intent.data, Buffer.from("removeNodes@61626261"));
assert.equal(intent.value, 0);
});

it("should build intent for staking nodes", async function () {
it("should create intent for staking nodes", async function () {
const sender = Address.fromBech32("erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2");
const delegationContract = Address.fromBech32("erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqtllllls002zgc");

const publicKey = {
hex(): string {
return Buffer.from("notavalidblskeyhexencoded").toString("hex");
return Buffer.from("abba").toString("hex");
}
};

Expand All @@ -105,17 +96,17 @@ describe("test delegation intents factory", function () {
assert.equal(intent.sender, "erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2");
assert.equal(intent.receiver, "erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqtllllls002zgc");
assert.isDefined(intent.data);
assert.deepEqual(intent.data, Buffer.from("stakeNodes@6e6f746176616c6964626c736b6579686578656e636f646564"));
assert.deepEqual(intent.data, Buffer.from("stakeNodes@61626261"));
assert.equal(intent.value, 0);
});

it("should build intent for unbonding nodes", async function () {
it("should create intent for unbonding nodes", async function () {
const sender = Address.fromBech32("erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2");
const delegationContract = Address.fromBech32("erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqtllllls002zgc");

const publicKey = {
hex(): string {
return Buffer.from("notavalidblskeyhexencoded").toString("hex");
return Buffer.from("abba").toString("hex");
}
};

Expand All @@ -128,18 +119,18 @@ describe("test delegation intents factory", function () {
assert.equal(intent.sender, "erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2");
assert.equal(intent.receiver, "erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqtllllls002zgc");
assert.isDefined(intent.data);
assert.deepEqual(intent.data, Buffer.from("unBondNodes@6e6f746176616c6964626c736b6579686578656e636f646564"));
assert.deepEqual(intent.data, Buffer.from("unBondNodes@61626261"));
assert.equal(intent.value, 0);
assert.equal(intent.gasLimit.valueOf(), 12143000);
assert.equal(intent.gasLimit.valueOf(), 12080000);
});

it("should build intent for unstaking nodes", async function () {
it("should create intent for unstaking nodes", async function () {
const sender = Address.fromBech32("erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2");
const delegationContract = Address.fromBech32("erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqtllllls002zgc");

const publicKey = {
hex(): string {
return Buffer.from("notavalidblskeyhexencoded").toString("hex");
return Buffer.from("abba").toString("hex");
}
};

Expand All @@ -152,18 +143,18 @@ describe("test delegation intents factory", function () {
assert.equal(intent.sender, "erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2");
assert.equal(intent.receiver, "erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqtllllls002zgc");
assert.isDefined(intent.data);
assert.deepEqual(intent.data, Buffer.from("unStakeNodes@6e6f746176616c6964626c736b6579686578656e636f646564"));
assert.deepEqual(intent.data, Buffer.from("unStakeNodes@61626261"));
assert.equal(intent.value, 0);
assert.equal(intent.gasLimit.valueOf(), 12144500);
assert.equal(intent.gasLimit.valueOf(), 12081500);
});

it("should build intent for unjailing nodes", async function () {
it("should create intent for unjailing nodes", async function () {
const sender = Address.fromBech32("erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2");
const delegationContract = Address.fromBech32("erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqtllllls002zgc");

const publicKey = {
hex(): string {
return Buffer.from("notavalidblskeyhexencoded").toString("hex");
return Buffer.from("abba").toString("hex");
}
};

Expand All @@ -176,11 +167,11 @@ describe("test delegation intents factory", function () {
assert.equal(intent.sender, "erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2");
assert.equal(intent.receiver, "erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqtllllls002zgc");
assert.isDefined(intent.data);
assert.deepEqual(intent.data, Buffer.from("unJailNodes@6e6f746176616c6964626c736b6579686578656e636f646564"));
assert.deepEqual(intent.data, Buffer.from("unJailNodes@61626261"));
assert.equal(intent.value, 0);
});

it("should build intent for changing service fee", async function () {
it("should create intent for changing service fee", async function () {
const sender = Address.fromBech32("erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2");
const delegationContract = Address.fromBech32("erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqtllllls002zgc");
const serviceFee = new BigNumber(10);
Expand All @@ -198,7 +189,7 @@ describe("test delegation intents factory", function () {
assert.equal(intent.value, 0);
});

it("should build intent for changing delegation cap", async function () {
it("should create intent for changing delegation cap", async function () {
const sender = Address.fromBech32("erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2");
const delegationContract = Address.fromBech32("erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqtllllls002zgc");
const delegationCap = new BigNumber("5000000000000000000000");
Expand All @@ -216,7 +207,7 @@ describe("test delegation intents factory", function () {
assert.equal(intent.value, 0);
});

it("should build intent for setting automatic activation", async function () {
it("should create intent for setting automatic activation", async function () {
const sender = Address.fromBech32("erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2");
const delegationContract = Address.fromBech32("erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqtllllls002zgc");

Expand All @@ -232,7 +223,7 @@ describe("test delegation intents factory", function () {
assert.equal(intent.value, 0);
});

it("should build intent for unsetting automatic activation", async function () {
it("should create intent for unsetting automatic activation", async function () {
const sender = Address.fromBech32("erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2");
const delegationContract = Address.fromBech32("erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqtllllls002zgc");

Expand All @@ -248,7 +239,7 @@ describe("test delegation intents factory", function () {
assert.equal(intent.value, 0);
});

it("should build intent for setting cap check on redelegate rewards", async function () {
it("should create intent for setting cap check on redelegate rewards", async function () {
const sender = Address.fromBech32("erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2");
const delegationContract = Address.fromBech32("erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqtllllls002zgc");

Expand All @@ -264,7 +255,7 @@ describe("test delegation intents factory", function () {
assert.equal(intent.value, 0);
});

it("should build intent for unsetting cap check on redelegate rewards", async function () {
it("should create intent for unsetting cap check on redelegate rewards", async function () {
const sender = Address.fromBech32("erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2");
const delegationContract = Address.fromBech32("erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqtllllls002zgc");

Expand All @@ -280,7 +271,7 @@ describe("test delegation intents factory", function () {
assert.equal(intent.value, 0);
});

it("should build intent for setting metadata", async function () {
it("should create intent for setting metadata", async function () {
const sender = Address.fromBech32("erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2");
const delegationContract = Address.fromBech32("erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqtllllls002zgc");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ interface Config {
additionalGasLimitForDelegationOperations: BigNumber.Value;
}

interface ValidatorPublicKey {
interface IValidatorPublicKey {
hex(): string;
}

Expand Down Expand Up @@ -58,7 +58,7 @@ export class DelegationTransactionIntentsFactory {
createTransactionIntentForAddingNodes(options: {
sender: IAddress,
delegationContract: IAddress,
publicKeys: ValidatorPublicKey[],
publicKeys: IValidatorPublicKey[],
signedMessages: Uint8Array[]
}): TransactionIntent {
if (options.publicKeys.length !== options.signedMessages.length) {
Expand All @@ -67,10 +67,9 @@ export class DelegationTransactionIntentsFactory {

const numNodes = options.publicKeys.length;

let dataParts = ["addNodes"];
const dataParts = ["addNodes"];
for (let i = 0; i < numNodes; i++) {
dataParts = dataParts.concat(options.publicKeys[i].hex());
dataParts = dataParts.concat(byteArrayToHex(options.signedMessages[i]));
dataParts.push(...[options.publicKeys[i].hex(), byteArrayToHex(options.signedMessages[i])]);
}

return new TransactionIntentBuilder({
Expand All @@ -82,20 +81,15 @@ export class DelegationTransactionIntentsFactory {
}).build();
}

private computeExecutionGasLimitForNodesManagement(numNodes: number): BigNumber.Value {
const additionalGasForAllNodes = new BigNumber(this.config.additionalGasLimitPerValidatorNode).multipliedBy(numNodes);
return new BigNumber(this.config.gasLimitDelegationOperations).plus(additionalGasForAllNodes);
}

createTransactionIntentForRemovingNodes(options: {
sender: IAddress,
delegationContract: IAddress,
publicKeys: ValidatorPublicKey[]
publicKeys: IValidatorPublicKey[]
}): TransactionIntent {
let dataParts = ["removeNodes"];
const dataParts = ["removeNodes"];

for (const key of options.publicKeys) {
dataParts = dataParts.concat(key.hex());
dataParts.push(key.hex());
}

const numNodes = options.publicKeys.length;
Expand All @@ -112,7 +106,7 @@ export class DelegationTransactionIntentsFactory {
createTransactionIntentForStakingNodes(options: {
sender: IAddress,
delegationContract: IAddress,
publicKeys: ValidatorPublicKey[]
publicKeys: IValidatorPublicKey[]
}): TransactionIntent {
let dataParts = ["stakeNodes"];

Expand All @@ -121,10 +115,10 @@ export class DelegationTransactionIntentsFactory {
}

const numNodes = options.publicKeys.length;
const executionGasLimit = new BigNumber(numNodes).multipliedBy(
this.config.additionalGasLimitPerValidatorNode).plus(
this.config.gasLimitStake
).plus(this.config.gasLimitDelegationOperations);
const additionalGasForAllNodes = new BigNumber(numNodes).multipliedBy(this.config.additionalGasLimitPerValidatorNode);
const executionGasLimit = additionalGasForAllNodes.plus(this.config.gasLimitStake).plus(
this.config.gasLimitDelegationOperations
);

return new TransactionIntentBuilder({
config: this.config,
Expand All @@ -138,7 +132,7 @@ export class DelegationTransactionIntentsFactory {
createTransactionIntentForUnbondingNodes(options: {
sender: IAddress,
delegationContract: IAddress,
publicKeys: ValidatorPublicKey[]
publicKeys: IValidatorPublicKey[]
}): TransactionIntent {
let dataParts = ["unBondNodes"];

Expand All @@ -164,7 +158,7 @@ export class DelegationTransactionIntentsFactory {
createTransactionIntentForUnstakingNodes(options: {
sender: IAddress,
delegationContract: IAddress,
publicKeys: ValidatorPublicKey[]
publicKeys: IValidatorPublicKey[]
}): TransactionIntent {
let dataParts = ["unStakeNodes"];

Expand All @@ -190,12 +184,12 @@ export class DelegationTransactionIntentsFactory {
createTransactionIntentForUnjailingNodes(options: {
sender: IAddress,
delegationContract: IAddress,
publicKeys: ValidatorPublicKey[]
publicKeys: IValidatorPublicKey[]
}): TransactionIntent {
let dataParts = ["unJailNodes"];
const dataParts = ["unJailNodes"];

for (const key of options.publicKeys) {
dataParts = dataParts.concat(key.hex());
dataParts.push(key.hex());
}

const numNodes = options.publicKeys.length;
Expand Down Expand Up @@ -341,4 +335,9 @@ export class DelegationTransactionIntentsFactory {
executionGasLimit: new BigNumber(this.config.gasLimitDelegationOperations).plus(this.config.additionalGasLimitForDelegationOperations)
}).build();
}

private computeExecutionGasLimitForNodesManagement(numNodes: number): BigNumber.Value {
const additionalGasForAllNodes = new BigNumber(this.config.additionalGasLimitPerValidatorNode).multipliedBy(numNodes);
return new BigNumber(this.config.gasLimitDelegationOperations).plus(additionalGasForAllNodes);
}
}
Loading

0 comments on commit 41604d0

Please sign in to comment.