Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delegation transaction intents factory #328

Merged
merged 8 commits into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from 6 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
1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ export const ESDT_TRANSFER_VALUE = "0";
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"

Original file line number Diff line number Diff line change
@@ -0,0 +1,301 @@
import BigNumber from "bignumber.js";
import { Address } from "../address";
import { DelegationTransactionIntentsFactory } from "./delegationTransactionIntentsFactory";
import { assert } from "chai";
import { DELEGATION_MANAGER_SC_ADDRESS } from "../constants";
import { ValidatorPublicKey } from "@multiversx/sdk-wallet-next";
import { SignableMessage } from "../signableMessage";

describe("test delegation intents factory", function () {
const delegationFactory = new DelegationTransactionIntentsFactory({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Created the config file.

chainID: "D",
minGasLimit: 50000,
gasLimitPerByte: 1500,
gasLimitStake: 5000000,
gasLimitUnstake: 5000000,
gasLimitUnbond: 5000000,
gasLimitCreateDelegationContract: 50000000,
gasLimitDelegationOperations: 1000000,
additionalGasLimitPerValidatorNode: 6000000,
additionalGasLimitForDelegationOperations: 10000000
});

it("should build intent for new delegation contract", async function () {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now I see, we can change "should build" to "should create" (optional, "philosophical").

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed to should create

const sender = Address.fromBech32("erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2");
const delagationCap = new BigNumber("5000000000000000000000");
const serviceFee = new BigNumber(10);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can also use raw strings and numbers, and the factory methods should be able to handle them appropriately. Perhaps do this in some tests? E.g. directly pass "5000000000000000000000".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed this test, works as expected.

const value = new BigNumber("1250000000000000000000");

const intent = delegationFactory.createTransactionIntentForNewDelegationContract({
sender: sender,
totalDelegationCap: delagationCap,
serviceFee: serviceFee,
value: value
});

assert.equal(intent.sender, "erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2");
assert.equal(intent.receiver, DELEGATION_MANAGER_SC_ADDRESS);
assert.isDefined(intent.data);
assert.deepEqual(intent.data, Buffer.from("createNewDelegationContract@010f0cf064dd59200000@0a"));
assert.equal(intent.gasLimit.valueOf(), 60126500);
assert.equal(intent.value, value);
});

it("should build 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 intent = delegationFactory.createTransactionIntentForAddingNodes({
sender: sender,
delegationContract: delegationContract,
publicKeys: [publicKey],
signedMessages: [signedMessage.getSignature()]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since it's a mock message, we can do:

const mockMessage = {
    getSignature: () => Buffer.from("...", hex)
}

Should work. Then, we'll remove new SignableMessage() and applySignature() from the test.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

});

assert.equal(intent.sender, "erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2");
assert.equal(intent.receiver, "erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqtllllls002zgc");
assert.isDefined(intent.data);
assert.deepEqual(intent.data, Buffer.from("addNodes@e7beaa95b3877f47348df4dd1cb578a4f7cabf7a20bfeefe5cdd263878ff132b765e04fef6f40c93512b666c47ed7719b8902f6c922c04247989b7137e837cc81a62e54712471c97a2ddab75aa9c2f58f813ed4c0fa722bde0ab718bff382208@81109fa1c8d3dc7b6c2d6e65206cc0bc1a83c9b2d1eb91a601d66ad32def430827d5eb52917bd2b0d04ce195738db216"));
assert.equal(intent.value, 0);
});

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

const publicKey = {
hex(): string {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀

return Buffer.from("notavalidblskeyhexencoded").toString("hex");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to be a happy unit test. Perhaps use a valid BLS key, so that there is no ambiguity for the reader?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And below, as well? Actually, a dummy value should do, e.g. abba, but "notavalidblskeyhexencoded" is somehow misleading.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now using abba in test

}
};

const intent = delegationFactory.createTransactionIntentForRemovingNodes({
sender: sender,
delegationContract: delegationContract,
publicKeys: [publicKey]
});

assert.equal(intent.sender, "erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2");
assert.equal(intent.receiver, "erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqtllllls002zgc");
assert.isDefined(intent.data);
assert.deepEqual(intent.data, Buffer.from("removeNodes@6e6f746176616c6964626c736b6579686578656e636f646564"));
assert.equal(intent.value, 0);
});

it("should build 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");
}
};

const intent = delegationFactory.createTransactionIntentForStakingNodes({
sender: sender,
delegationContract: delegationContract,
publicKeys: [publicKey]
});

assert.equal(intent.sender, "erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2");
assert.equal(intent.receiver, "erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqtllllls002zgc");
assert.isDefined(intent.data);
assert.deepEqual(intent.data, Buffer.from("stakeNodes@6e6f746176616c6964626c736b6579686578656e636f646564"));
assert.equal(intent.value, 0);
});

it("should build 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");
}
};

const intent = delegationFactory.createTransactionIntentForUnbondingNodes({
sender: sender,
delegationContract: delegationContract,
publicKeys: [publicKey]
});

assert.equal(intent.sender, "erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2");
assert.equal(intent.receiver, "erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqtllllls002zgc");
assert.isDefined(intent.data);
assert.deepEqual(intent.data, Buffer.from("unBondNodes@6e6f746176616c6964626c736b6579686578656e636f646564"));
assert.equal(intent.value, 0);
assert.equal(intent.gasLimit.valueOf(), 12143000);
});

it("should build 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");
}
};

const intent = delegationFactory.createTransactionIntentForUnstakingNodes({
sender: sender,
delegationContract: delegationContract,
publicKeys: [publicKey]
});

assert.equal(intent.sender, "erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2");
assert.equal(intent.receiver, "erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqtllllls002zgc");
assert.isDefined(intent.data);
assert.deepEqual(intent.data, Buffer.from("unStakeNodes@6e6f746176616c6964626c736b6579686578656e636f646564"));
assert.equal(intent.value, 0);
assert.equal(intent.gasLimit.valueOf(), 12144500);
});

it("should build 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");
}
};

const intent = delegationFactory.createTransactionIntentForUnjailingNodes({
sender: sender,
delegationContract: delegationContract,
publicKeys: [publicKey]
});

assert.equal(intent.sender, "erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2");
assert.equal(intent.receiver, "erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqtllllls002zgc");
assert.isDefined(intent.data);
assert.deepEqual(intent.data, Buffer.from("unJailNodes@6e6f746176616c6964626c736b6579686578656e636f646564"));
assert.equal(intent.value, 0);
});

it("should build intent for changing service fee", async function () {
const sender = Address.fromBech32("erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2");
const delegationContract = Address.fromBech32("erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqtllllls002zgc");
const serviceFee = new BigNumber(10);

const intent = delegationFactory.createTransactionIntentForChangingServiceFee({
sender: sender,
delegationContract: delegationContract,
serviceFee: serviceFee
});

assert.equal(intent.sender, "erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2");
assert.equal(intent.receiver, "erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqtllllls002zgc");
assert.isDefined(intent.data);
assert.deepEqual(intent.data, Buffer.from("changeServiceFee@0a"));
assert.equal(intent.value, 0);
});

it("should build intent for changing delegation cap", async function () {
const sender = Address.fromBech32("erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2");
const delegationContract = Address.fromBech32("erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqtllllls002zgc");
const delegationCap = new BigNumber("5000000000000000000000");

const intent = delegationFactory.createTransactionIntentForModifyingDelegationCap({
sender: sender,
delegationContract: delegationContract,
delegationCap: delegationCap
});

assert.equal(intent.sender, "erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2");
assert.equal(intent.receiver, "erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqtllllls002zgc");
assert.isDefined(intent.data);
assert.deepEqual(intent.data, Buffer.from("modifyTotalDelegationCap@010f0cf064dd59200000"));
assert.equal(intent.value, 0);
});

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

const intent = delegationFactory.createTransactionIntentForSettingAutomaticActivation({
sender: sender,
delegationContract: delegationContract
});

assert.equal(intent.sender, "erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2");
assert.equal(intent.receiver, "erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqtllllls002zgc");
assert.isDefined(intent.data);
assert.deepEqual(intent.data, Buffer.from("setAutomaticActivation@74727565"));
assert.equal(intent.value, 0);
});

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

const intent = delegationFactory.createTransactionIntentForUnsettingAutomaticActivation({
sender: sender,
delegationContract: delegationContract
});

assert.equal(intent.sender, "erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2");
assert.equal(intent.receiver, "erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqtllllls002zgc");
assert.isDefined(intent.data);
assert.deepEqual(intent.data, Buffer.from("setAutomaticActivation@66616c7365"));
assert.equal(intent.value, 0);
});

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

const intent = delegationFactory.createTransactionIntentForSettingCapCheckOnRedelegateRewards({
sender: sender,
delegationContract: delegationContract
});

assert.equal(intent.sender, "erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2");
assert.equal(intent.receiver, "erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqtllllls002zgc");
assert.isDefined(intent.data);
assert.deepEqual(intent.data, Buffer.from("setCheckCapOnReDelegateRewards@74727565"));
assert.equal(intent.value, 0);
});

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

const intent = delegationFactory.createTransactionIntentForUnsettingCapCheckOnRedelegateRewards({
sender: sender,
delegationContract: delegationContract
});

assert.equal(intent.sender, "erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2");
assert.equal(intent.receiver, "erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqtllllls002zgc");
assert.isDefined(intent.data);
assert.deepEqual(intent.data, Buffer.from("setCheckCapOnReDelegateRewards@66616c7365"));
assert.equal(intent.value, 0);
});

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

const intent = delegationFactory.createTransactionIntentForSettingMetadata({
sender: sender,
delegationContract: delegationContract,
name: "name",
website: "website",
identifier: "identifier"
});

assert.equal(intent.sender, "erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2");
assert.equal(intent.receiver, "erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqtllllls002zgc");
assert.isDefined(intent.data);
assert.deepEqual(intent.data, Buffer.from("setMetaData@6e616d65@77656273697465@6964656e746966696572"));
assert.equal(intent.value, 0);
});
});
Loading