diff --git a/src/transactionIntent.ts b/src/draftTransaction.ts similarity index 80% rename from src/transactionIntent.ts rename to src/draftTransaction.ts index 43cb9e36..f1554af7 100644 --- a/src/transactionIntent.ts +++ b/src/draftTransaction.ts @@ -1,6 +1,6 @@ import { BigNumber } from "bignumber.js"; -export class TransactionIntent { +export class DraftTransaction { public sender: string; public receiver: string; public gasLimit: BigNumber.Value; @@ -17,7 +17,7 @@ export class TransactionIntent { this.sender = options.sender; this.receiver = options.receiver; this.gasLimit = options.gasLimit; - this.value = options.value; - this.data = options.data; + this.value = options.value ?? 0; + this.data = options.data ?? new Uint8Array(); } } diff --git a/src/smartcontracts/interaction.spec.ts b/src/smartcontracts/interaction.spec.ts index 2fb119d1..9c43dae9 100644 --- a/src/smartcontracts/interaction.spec.ts +++ b/src/smartcontracts/interaction.spec.ts @@ -33,6 +33,7 @@ describe("test smart contract interactor", function () { let interaction = new Interaction(contract, dummyFunction, []); let transaction = interaction + .withSender(alice.address) .withNonce(7) .withValue(TokenTransfer.egldFromAmount(1)) .withGasLimit(20000000) @@ -63,6 +64,7 @@ describe("test smart contract interactor", function () { // ESDT, single let transaction = new Interaction(contract, dummyFunction, []) + .withSender(alice) .withSingleESDTTransfer(TokenFoo(10)) .buildTransaction(); @@ -180,7 +182,7 @@ describe("test smart contract interactor", function () { assert.isTrue(queryCode.equals(ReturnCode.Ok)); // Execute, do not wait for execution - let transaction = interaction.withNonce(0).buildTransaction(); + let transaction = interaction.withSender(alice.address).withNonce(0).buildTransaction(); transaction.setSender(alice.address); await alice.signer.sign(transaction); await provider.sendTransaction(transaction); @@ -235,7 +237,7 @@ describe("test smart contract interactor", function () { assert.deepEqual(counterValue!.valueOf(), new BigNumber(7)); - let incrementTransaction = incrementInteraction.withNonce(14).buildTransaction(); + let incrementTransaction = incrementInteraction.withSender(alice.address).withNonce(14).buildTransaction(); await alice.signer.sign(incrementTransaction); provider.mockGetTransactionWithAnyHashAsNotarizedWithOneResult("@6f6b@08"); let { bundle: { firstValue: valueAfterIncrement } } = await controller.execute(incrementInteraction, incrementTransaction); @@ -243,7 +245,7 @@ describe("test smart contract interactor", function () { // Decrement three times (simulate three parallel broadcasts). Wait for execution of the latter (third transaction). Return fake "5". // Decrement #1 - let decrementTransaction = decrementInteraction.withNonce(15).buildTransaction(); + let decrementTransaction = decrementInteraction.withSender(alice.address).withNonce(15).buildTransaction(); await alice.signer.sign(decrementTransaction); await provider.sendTransaction(decrementTransaction); // Decrement #2 @@ -292,7 +294,7 @@ describe("test smart contract interactor", function () { ); // start() - let startTransaction = startInteraction.withNonce(14).buildTransaction(); + let startTransaction = startInteraction.withSender(alice.address).withNonce(14).buildTransaction(); await alice.signer.sign(startTransaction); provider.mockGetTransactionWithAnyHashAsNotarizedWithOneResult("@6f6b"); let { bundle: { returnCode: startReturnCode, values: startReturnValues } } = await controller.execute(startInteraction, startTransaction); @@ -302,7 +304,7 @@ describe("test smart contract interactor", function () { assert.lengthOf(startReturnValues, 0); // status() (this is a view function, but for the sake of the test, we'll execute it) - let statusTransaction = statusInteraction.withNonce(15).buildTransaction(); + let statusTransaction = statusInteraction.withSender(alice.address).withNonce(15).buildTransaction(); await alice.signer.sign(statusTransaction); provider.mockGetTransactionWithAnyHashAsNotarizedWithOneResult("@6f6b@01"); let { bundle: { returnCode: statusReturnCode, values: statusReturnValues, firstValue: statusFirstValue } } = await controller.execute(statusInteraction, statusTransaction); @@ -313,7 +315,7 @@ describe("test smart contract interactor", function () { assert.deepEqual(statusFirstValue!.valueOf(), { name: "Running", fields: [] }); // lotteryInfo() (this is a view function, but for the sake of the test, we'll execute it) - let getLotteryInfoTransaction = getLotteryInfoInteraction.withNonce(15).buildTransaction(); + let getLotteryInfoTransaction = getLotteryInfoInteraction.withSender(alice.address).withNonce(15).buildTransaction(); await alice.signer.sign(getLotteryInfoTransaction); provider.mockGetTransactionWithAnyHashAsNotarizedWithOneResult("@6f6b@0000000b6c75636b792d746f6b656e000000010100000000000000005fc2b9dbffffffff00000001640000000a140ec80fa7ee88000000"); let { bundle: { returnCode: infoReturnCode, values: infoReturnValues, firstValue: infoFirstValue } } = await controller.execute(getLotteryInfoInteraction, getLotteryInfoTransaction); diff --git a/src/smartcontracts/smartContract.ts b/src/smartcontracts/smartContract.ts index 0b6d7124..58005cbd 100644 --- a/src/smartcontracts/smartContract.ts +++ b/src/smartcontracts/smartContract.ts @@ -14,9 +14,10 @@ import { NativeSerializer } from "./nativeSerializer"; import { Query } from "./query"; import { ArwenVirtualMachine, ContractCallPayloadBuilder, ContractUpgradePayloadBuilder } from "./transactionPayloadBuilders"; import { EndpointDefinition, TypedValue } from "./typesystem"; -import { SmartContractTransactionIntentsFactory } from "../transactionIntentsFactories/smartContractTransactionIntentsFactory"; -import { TransactionIntentsFactoryConfig } from "../transactionIntentsFactories/transactionIntentsFactoryConfig"; +import { SmartContractTransactionsFactory } from "../transactionsFactories/smartContractTransactionsFactory"; +import { TransactionsFactoryConfig } from "../transactionsFactories/transactionsFactoryConfig"; import { TransactionPayload } from "../transactionPayload"; +import { TRANSACTION_MIN_GAS_PRICE } from "../constants"; const createKeccakHash = require("keccak"); interface IAbi { @@ -115,8 +116,8 @@ export class SmartContract implements ISmartContract { deploy({ deployer, code, codeMetadata, initArguments, value, gasLimit, gasPrice, chainID }: DeployArguments): Transaction { Compatibility.guardAddressIsSetAndNonZero(deployer, "'deployer' of SmartContract.deploy()", "pass the actual address to deploy()"); - const config = new TransactionIntentsFactoryConfig(chainID.valueOf()); - const scIntentFactory = new SmartContractTransactionIntentsFactory({ + const config = new TransactionsFactoryConfig(chainID.valueOf()); + const scDraftTransactionFactory = new SmartContractTransactionsFactory({ config: config, abi: this.abi }); @@ -124,7 +125,7 @@ export class SmartContract implements ISmartContract { const bytecode = Buffer.from(code.toString(), 'hex'); const metadataAsJson = this.getMetadataPropertiesAsObject(codeMetadata); - const intent = scIntentFactory.createTransactionIntentForDeploy({ + const draftTx = scDraftTransactionFactory.createTransactionForDeploy({ sender: deployer, bytecode: bytecode, gasLimit: gasLimit.valueOf(), @@ -135,15 +136,12 @@ export class SmartContract implements ISmartContract { isPayableBySmartContract: metadataAsJson.payableBySc }); - return new Transaction({ - receiver: Address.fromBech32(intent.receiver), - sender: Address.fromBech32(intent.sender), - value: value, - gasLimit: new BigNumber(intent.gasLimit).toNumber(), - gasPrice: gasPrice, - data: new TransactionPayload(Buffer.from(intent.data!)), - chainID: chainID - }); + let transaction = Transaction.fromDraft(draftTx); + transaction.setChainID(chainID); + transaction.setValue(value ?? 0); + transaction.setGasPrice(gasPrice ?? TRANSACTION_MIN_GAS_PRICE) + + return transaction; } private getMetadataPropertiesAsObject(codeMetadata?: ICodeMetadata): { @@ -177,8 +175,8 @@ export class SmartContract implements ISmartContract { this.ensureHasAddress(); - const config = new TransactionIntentsFactoryConfig(chainID.valueOf()); - const scIntentFactory = new SmartContractTransactionIntentsFactory({ + const config = new TransactionsFactoryConfig(chainID.valueOf()); + const scDraftTransactionFactory = new SmartContractTransactionsFactory({ config: config, abi: this.abi }); @@ -186,7 +184,7 @@ export class SmartContract implements ISmartContract { const bytecode = Uint8Array.from(Buffer.from(code.toString(), 'hex')); const metadataAsJson = this.getMetadataPropertiesAsObject(codeMetadata); - const intent = scIntentFactory.createTransactionIntentForUpgrade({ + const draftTx = scDraftTransactionFactory.createTransactionForUpgrade({ sender: caller, contract: this.getAddress(), bytecode: bytecode, @@ -198,15 +196,12 @@ export class SmartContract implements ISmartContract { isPayableBySmartContract: metadataAsJson.payableBySc }) - return new Transaction({ - sender: Address.fromBech32(intent.sender), - receiver: Address.fromBech32(intent.receiver), - value: value, - gasLimit: new BigNumber(intent.gasLimit).toNumber(), - gasPrice: gasPrice, - data: new TransactionPayload(Buffer.from(intent.data!)), - chainID: chainID - }); + let transaction = Transaction.fromDraft(draftTx); + transaction.setChainID(chainID); + transaction.setValue(value ?? 0); + transaction.setGasPrice(gasPrice ?? TRANSACTION_MIN_GAS_PRICE) + + return transaction; } /** @@ -217,8 +212,8 @@ export class SmartContract implements ISmartContract { this.ensureHasAddress(); - const config = new TransactionIntentsFactoryConfig(chainID.valueOf()); - const scIntentFactory = new SmartContractTransactionIntentsFactory({ + const config = new TransactionsFactoryConfig(chainID.valueOf()); + const scDraftTransactionFactory = new SmartContractTransactionsFactory({ config: config, abi: this.abi }); @@ -226,7 +221,7 @@ export class SmartContract implements ISmartContract { args = args || []; value = value || 0; - const intent = scIntentFactory.createTransactionIntentForExecute({ + const draftTx = scDraftTransactionFactory.createTransactionForExecute({ sender: caller, contractAddress: receiver ? receiver : this.getAddress(), functionName: func.toString(), @@ -234,15 +229,12 @@ export class SmartContract implements ISmartContract { args: args }) - return new Transaction({ - sender: caller, - receiver: Address.fromBech32(intent.receiver), - value: value, - gasLimit: new BigNumber(intent.gasLimit).toNumber(), - gasPrice: gasPrice, - data: new TransactionPayload(Buffer.from(intent.data!)), - chainID: chainID - }); + let transaction = Transaction.fromDraft(draftTx); + transaction.setChainID(chainID); + transaction.setValue(value); + transaction.setGasPrice(gasPrice ?? TRANSACTION_MIN_GAS_PRICE) + + return transaction; } createQuery({ func, args, value, caller }: QueryArguments): Query { diff --git a/src/transaction.spec.ts b/src/transaction.spec.ts index 52dd1f75..8d635852 100644 --- a/src/transaction.spec.ts +++ b/src/transaction.spec.ts @@ -6,6 +6,7 @@ import { TestWallet, loadTestWallets } from "./testutils"; import { TokenTransfer } from "./tokenTransfer"; import { Transaction } from "./transaction"; import { TransactionPayload } from "./transactionPayload"; +import { DraftTransaction } from "./draftTransaction"; describe("test transaction construction", async () => { @@ -17,6 +18,24 @@ describe("test transaction construction", async () => { wallets = await loadTestWallets(); }); + it("create transaction from draft transaction", async () => { + const draftTransaction = new DraftTransaction({ + sender: "erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th", + receiver: "erd1spyavw0956vq68xj8y4tenjpq2wd5a9p2c6j8gsz7ztyrnpxrruqzu66jx", + gasLimit: 56000, + value: "1000000000000000000", + data: Buffer.from("test") + }); + + const transaction = Transaction.fromDraft(draftTransaction); + assert.deepEqual(transaction.getSender(), Address.fromBech32("erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th")); + assert.deepEqual(transaction.getReceiver(), Address.fromBech32("erd1spyavw0956vq68xj8y4tenjpq2wd5a9p2c6j8gsz7ztyrnpxrruqzu66jx")); + assert.equal(transaction.getGasLimit().valueOf(), 56000); + assert.equal(transaction.getValue().toString(), "1000000000000000000"); + assert.equal(transaction.getData().toString(), "test"); + assert.equal(transaction.getChainID().valueOf(), ""); + }); + it("with no data, no value", async () => { let transaction = new Transaction({ nonce: 89, diff --git a/src/transaction.ts b/src/transaction.ts index 092aae7d..01b947b2 100644 --- a/src/transaction.ts +++ b/src/transaction.ts @@ -11,6 +11,7 @@ import { ProtoSerializer } from "./proto"; import { Signature } from "./signature"; import { TransactionPayload } from "./transactionPayload"; import { guardNotEmpty } from "./utils"; +import { DraftTransaction } from "./draftTransaction"; const createTransactionHasher = require("blake2b"); const TRANSACTION_HASH_LENGTH = 32; @@ -152,6 +153,20 @@ export class Transaction { this.hash = TransactionHash.empty(); } + /** + * Creates a new Transaction object from a DraftTransaction. + */ + static fromDraft(draft: DraftTransaction): Transaction { + return new Transaction({ + sender: Address.fromBech32(draft.sender), + receiver: Address.fromBech32(draft.receiver), + gasLimit: new BigNumber(draft.gasLimit).toNumber(), + chainID: "", + value: draft.value ?? 0, + data: draft.data ? new TransactionPayload(Buffer.from(draft.data)) : new TransactionPayload() + }) + } + getNonce(): INonce { return this.nonce; } diff --git a/src/transactionIntentsFactories/delegationTransactionIntentsFactory.spec.ts b/src/transactionIntentsFactories/delegationTransactionIntentsFactory.spec.ts deleted file mode 100644 index 34bd7df4..00000000 --- a/src/transactionIntentsFactories/delegationTransactionIntentsFactory.spec.ts +++ /dev/null @@ -1,292 +0,0 @@ -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 { TransactionIntentsFactoryConfig } from "./transactionIntentsFactoryConfig"; - -describe("test delegation intents factory", function () { - const config = new TransactionIntentsFactoryConfig("D"); - const delegationFactory = new DelegationTransactionIntentsFactory(config); - - it("should create intent for new delegation contract", async function () { - const sender = Address.fromBech32("erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2"); - const delagationCap = "5000000000000000000000"; - const serviceFee = 10; - 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 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 mockMessage = { - getSignature: () => Buffer.from("81109fa1c8d3dc7b6c2d6e65206cc0bc1a83c9b2d1eb91a601d66ad32def430827d5eb52917bd2b0d04ce195738db216", "hex") - } - - const intent = delegationFactory.createTransactionIntentForAddingNodes({ - sender: sender, - delegationContract: delegationContract, - publicKeys: [publicKey], - signedMessages: [mockMessage.getSignature()] - }); - - 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 create intent for removing nodes", async function () { - const sender = Address.fromBech32("erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2"); - const delegationContract = Address.fromBech32("erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqtllllls002zgc"); - - const publicKey = { - hex(): string { - return Buffer.from("abba").toString("hex"); - } - }; - - 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@61626261")); - assert.equal(intent.value, 0); - }); - - 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("abba").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@61626261")); - assert.equal(intent.value, 0); - }); - - 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("abba").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@61626261")); - assert.equal(intent.value, 0); - assert.equal(intent.gasLimit.valueOf(), 12080000); - }); - - 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("abba").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@61626261")); - assert.equal(intent.value, 0); - assert.equal(intent.gasLimit.valueOf(), 12081500); - }); - - 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("abba").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@61626261")); - assert.equal(intent.value, 0); - }); - - 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); - - 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 create 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 create 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 create 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 create 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 create 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 create 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); - }); -}); diff --git a/src/transactionIntentsFactories/tokenManagementTransactionIntentsFactory.spec.ts b/src/transactionIntentsFactories/tokenManagementTransactionIntentsFactory.spec.ts deleted file mode 100644 index b6fa00a1..00000000 --- a/src/transactionIntentsFactories/tokenManagementTransactionIntentsFactory.spec.ts +++ /dev/null @@ -1,152 +0,0 @@ -import { assert } from "chai"; -import { loadTestWallets, TestWallet } from "../testutils"; -import { TokenManagementTransactionIntentsFactory } from "./tokenManagementTransactionIntentsFactory"; -import { TransactionIntentsFactoryConfig } from "./transactionIntentsFactoryConfig"; -import BigNumber from "bignumber.js"; -import { ESDT_CONTRACT_ADDRESS } from "../constants"; - -describe("test token management transaction intents factory", () => { - let frank: TestWallet, grace: TestWallet; - let factory: TokenManagementTransactionIntentsFactory; - let config: TransactionIntentsFactoryConfig; - - before(async function () { - ({ frank, grace } = await loadTestWallets()); - config = new TransactionIntentsFactoryConfig("T"); - factory = new TokenManagementTransactionIntentsFactory(config); - }); - - it("should create transaction intent for registering and setting roles", () => { - const intent = factory.createTransactionIntentForRegisteringAndSettingRoles({ - sender: frank.address, - tokenName: "TEST", - tokenTicker: "TEST", - tokenType: "FNG", - numDecimals: 2 - }); - - assert.deepEqual(intent.data, Buffer.from("registerAndSetAllRoles@54455354@54455354@464e47@02")); - assert.equal(intent.sender, frank.address.toString()); - assert.equal(intent.receiver, "erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqzllls8a5w6u"); - assert.deepEqual(intent.value, config.issueCost); - assert.deepEqual(intent.gasLimit, new BigNumber("60125000")); - }); - - it("should create transaction intent for issuing fungible token", () => { - const intent = factory.createTransactionIntentForIssuingFungible({ - sender: frank.address, - tokenName: "FRANK", - tokenTicker: "FRANK", - initialSupply: 100, - numDecimals: 0, - canFreeze: true, - canWipe: true, - canPause: true, - canChangeOwner: true, - canUpgrade: true, - canAddSpecialRoles: true - }); - - assert.deepEqual(intent.data, Buffer.from("issue@4652414e4b@4652414e4b@64@@63616e467265657a65@74727565@63616e57697065@74727565@63616e5061757365@74727565@63616e4368616e67654f776e6572@74727565@63616e55706772616465@74727565@63616e4164645370656369616c526f6c6573@74727565")); - assert.equal(intent.sender, frank.address.toString()); - assert.equal(intent.receiver, ESDT_CONTRACT_ADDRESS); - assert.deepEqual(intent.value, config.issueCost); - }); - - it("should create transaction intent for issuing semi-fungible token", () => { - const intent = factory.createTransactionIntentForIssuingSemiFungible({ - sender: frank.address, - tokenName: "FRANK", - tokenTicker: "FRANK", - canFreeze: true, - canWipe: true, - canPause: true, - canTransferNFTCreateRole: true, - canChangeOwner: true, - canUpgrade: true, - canAddSpecialRoles: true - }); - - assert.deepEqual(intent.data, Buffer.from("issueSemiFungible@4652414e4b@4652414e4b@63616e467265657a65@74727565@63616e57697065@74727565@63616e5061757365@74727565@63616e5472616e736665724e4654437265617465526f6c65@74727565@63616e4368616e67654f776e6572@74727565@63616e55706772616465@74727565@63616e4164645370656369616c526f6c6573@74727565")); - assert.equal(intent.sender, frank.address.toString()); - assert.equal(intent.receiver, ESDT_CONTRACT_ADDRESS); - assert.deepEqual(intent.value, config.issueCost); - }); - - it("should create transaction intent for issuing non-fungible token", () => { - const intent = factory.createTransactionIntentForIssuingNonFungible({ - sender: frank.address, - tokenName: "FRANK", - tokenTicker: "FRANK", - canFreeze: true, - canWipe: true, - canPause: true, - canTransferNFTCreateRole: true, - canChangeOwner: true, - canUpgrade: true, - canAddSpecialRoles: true - }); - - assert.deepEqual(intent.data, Buffer.from("issueNonFungible@4652414e4b@4652414e4b@63616e467265657a65@74727565@63616e57697065@74727565@63616e5061757365@74727565@63616e5472616e736665724e4654437265617465526f6c65@74727565@63616e4368616e67654f776e6572@74727565@63616e55706772616465@74727565@63616e4164645370656369616c526f6c6573@74727565")); - assert.equal(intent.sender, frank.address.toString()); - assert.equal(intent.receiver, ESDT_CONTRACT_ADDRESS); - assert.deepEqual(intent.value, config.issueCost); - }); - - it("should create transaction intent for registering metaEsdt", () => { - const intent = factory.createTransactionIntentForRegisteringMetaESDT({ - sender: frank.address, - tokenName: "FRANK", - tokenTicker: "FRANK", - numDecimals: 10, - canFreeze: true, - canWipe: true, - canPause: true, - canTransferNFTCreateRole: true, - canChangeOwner: true, - canUpgrade: true, - canAddSpecialRoles: true - }); - - assert.deepEqual(intent.data, Buffer.from("registerMetaESDT@4652414e4b@4652414e4b@0a@63616e467265657a65@74727565@63616e57697065@74727565@63616e5061757365@74727565@63616e5472616e736665724e4654437265617465526f6c65@74727565@63616e4368616e67654f776e6572@74727565@63616e55706772616465@74727565@63616e4164645370656369616c526f6c6573@74727565")); - assert.equal(intent.sender, frank.address.toString()); - assert.equal(intent.receiver, ESDT_CONTRACT_ADDRESS); - assert.deepEqual(intent.value, config.issueCost); - }); - - it("should create transaction intent for setting spcial role on non-fungible token", () => { - const intent = factory.createTransactionIntentForSettingSpecialRoleOnNonFungibleToken({ - sender: frank.address, - user: grace.address, - tokenIdentifier: "FRANK-11ce3e", - addRoleNFTCreate: true, - addRoleNFTBurn: false, - addRoleNFTUpdateAttributes: true, - addRoleNFTAddURI: true, - addRoleESDTTransferRole: false - }); - - assert.deepEqual(intent.data, Buffer.from("setSpecialRole@4652414e4b2d313163653365@1e8a8b6b49de5b7be10aaa158a5a6a4abb4b56cc08f524bb5e6cd5f211ad3e13@45534454526f6c654e4654437265617465@45534454526f6c654e465455706461746541747472696275746573@45534454526f6c654e4654416464555249")); - assert.equal(intent.sender, frank.address.toString()); - assert.equal(intent.receiver, ESDT_CONTRACT_ADDRESS); - assert.equal(intent.value, 0); - }); - - it("should create transaction intent for creating nft", () => { - const intent = factory.createTransactionIntentForCreatingNFT({ - sender: grace.address, - tokenIdentifier: "FRANK-aa9e8d", - initialQuantity: 1, - name: `test`, - royalties: 1000, - hash: "abba", - attributes: Buffer.from("test"), - uris: ["a", "b"] - }); - - assert.deepEqual(intent.data, Buffer.from("ESDTNFTCreate@4652414e4b2d616139653864@01@74657374@03e8@61626261@74657374@61@62")); - assert.equal(intent.sender, grace.address.toString()); - assert.equal(intent.receiver, grace.address.toString()); - assert.equal(intent.value, 0); - }); -}); diff --git a/src/transactionsFactories/delegationTransactionsFactory.spec.ts b/src/transactionsFactories/delegationTransactionsFactory.spec.ts new file mode 100644 index 00000000..1afc908a --- /dev/null +++ b/src/transactionsFactories/delegationTransactionsFactory.spec.ts @@ -0,0 +1,292 @@ +import BigNumber from "bignumber.js"; +import { Address } from "../address"; +import { DelegationTransactionsFactory } from "./delegationTransactionsFactory"; +import { assert } from "chai"; +import { DELEGATION_MANAGER_SC_ADDRESS } from "../constants"; +import { ValidatorPublicKey } from "@multiversx/sdk-wallet-next"; +import { TransactionsFactoryConfig } from "./transactionsFactoryConfig"; + +describe("test delegation transactions factory", function () { + const config = new TransactionsFactoryConfig("D"); + const delegationFactory = new DelegationTransactionsFactory(config); + + it("should create draft transaction for new delegation contract", async function () { + const sender = Address.fromBech32("erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2"); + const delagationCap = "5000000000000000000000"; + const serviceFee = 10; + const value = new BigNumber("1250000000000000000000"); + + const draft = delegationFactory.createTransactionForNewDelegationContract({ + sender: sender, + totalDelegationCap: delagationCap, + serviceFee: serviceFee, + amount: value + }); + + assert.equal(draft.sender, "erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2"); + assert.equal(draft.receiver, DELEGATION_MANAGER_SC_ADDRESS); + assert.isDefined(draft.data); + assert.deepEqual(draft.data, Buffer.from("createNewDelegationContract@010f0cf064dd59200000@0a")); + assert.equal(draft.gasLimit.valueOf(), 60126500); + assert.equal(draft.value, value); + }); + + it("should create draft transaction for adding nodes", async function () { + const sender = Address.fromBech32("erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2"); + const delegationContract = Address.fromBech32("erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqtllllls002zgc"); + const publicKey = new ValidatorPublicKey(Buffer.from("e7beaa95b3877f47348df4dd1cb578a4f7cabf7a20bfeefe5cdd263878ff132b765e04fef6f40c93512b666c47ed7719b8902f6c922c04247989b7137e837cc81a62e54712471c97a2ddab75aa9c2f58f813ed4c0fa722bde0ab718bff382208", "hex")); + + const mockMessage = { + getSignature: () => Buffer.from("81109fa1c8d3dc7b6c2d6e65206cc0bc1a83c9b2d1eb91a601d66ad32def430827d5eb52917bd2b0d04ce195738db216", "hex") + } + + const draft = delegationFactory.createTransactionForAddingNodes({ + sender: sender, + delegationContract: delegationContract, + publicKeys: [publicKey], + signedMessages: [mockMessage.getSignature()] + }); + + assert.equal(draft.sender, "erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2"); + assert.equal(draft.receiver, "erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqtllllls002zgc"); + assert.isDefined(draft.data); + assert.deepEqual(draft.data, Buffer.from("addNodes@e7beaa95b3877f47348df4dd1cb578a4f7cabf7a20bfeefe5cdd263878ff132b765e04fef6f40c93512b666c47ed7719b8902f6c922c04247989b7137e837cc81a62e54712471c97a2ddab75aa9c2f58f813ed4c0fa722bde0ab718bff382208@81109fa1c8d3dc7b6c2d6e65206cc0bc1a83c9b2d1eb91a601d66ad32def430827d5eb52917bd2b0d04ce195738db216")); + assert.equal(draft.value, 0); + }); + + it("should create draft transaction for removing nodes", async function () { + const sender = Address.fromBech32("erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2"); + const delegationContract = Address.fromBech32("erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqtllllls002zgc"); + + const publicKey = { + hex(): string { + return Buffer.from("abba").toString("hex"); + } + }; + + const draft = delegationFactory.createTransactionForRemovingNodes({ + sender: sender, + delegationContract: delegationContract, + publicKeys: [publicKey] + }); + + assert.equal(draft.sender, "erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2"); + assert.equal(draft.receiver, "erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqtllllls002zgc"); + assert.isDefined(draft.data); + assert.deepEqual(draft.data, Buffer.from("removeNodes@61626261")); + assert.equal(draft.value, 0); + }); + + it("should create draft transaction for staking nodes", async function () { + const sender = Address.fromBech32("erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2"); + const delegationContract = Address.fromBech32("erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqtllllls002zgc"); + + const publicKey = { + hex(): string { + return Buffer.from("abba").toString("hex"); + } + }; + + const draft = delegationFactory.createTransactionForStakingNodes({ + sender: sender, + delegationContract: delegationContract, + publicKeys: [publicKey] + }); + + assert.equal(draft.sender, "erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2"); + assert.equal(draft.receiver, "erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqtllllls002zgc"); + assert.isDefined(draft.data); + assert.deepEqual(draft.data, Buffer.from("stakeNodes@61626261")); + assert.equal(draft.value, 0); + }); + + it("should create draft transaction for unbonding nodes", async function () { + const sender = Address.fromBech32("erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2"); + const delegationContract = Address.fromBech32("erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqtllllls002zgc"); + + const publicKey = { + hex(): string { + return Buffer.from("abba").toString("hex"); + } + }; + + const draft = delegationFactory.createTransactionForUnbondingNodes({ + sender: sender, + delegationContract: delegationContract, + publicKeys: [publicKey] + }); + + assert.equal(draft.sender, "erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2"); + assert.equal(draft.receiver, "erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqtllllls002zgc"); + assert.isDefined(draft.data); + assert.deepEqual(draft.data, Buffer.from("unBondNodes@61626261")); + assert.equal(draft.value, 0); + assert.equal(draft.gasLimit.valueOf(), 12080000); + }); + + it("should create draft transaction for unstaking nodes", async function () { + const sender = Address.fromBech32("erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2"); + const delegationContract = Address.fromBech32("erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqtllllls002zgc"); + + const publicKey = { + hex(): string { + return Buffer.from("abba").toString("hex"); + } + }; + + const draft = delegationFactory.createTransactionForUnstakingNodes({ + sender: sender, + delegationContract: delegationContract, + publicKeys: [publicKey] + }); + + assert.equal(draft.sender, "erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2"); + assert.equal(draft.receiver, "erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqtllllls002zgc"); + assert.isDefined(draft.data); + assert.deepEqual(draft.data, Buffer.from("unStakeNodes@61626261")); + assert.equal(draft.value, 0); + assert.equal(draft.gasLimit.valueOf(), 12081500); + }); + + it("should create draft transaction for unjailing nodes", async function () { + const sender = Address.fromBech32("erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2"); + const delegationContract = Address.fromBech32("erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqtllllls002zgc"); + + const publicKey = { + hex(): string { + return Buffer.from("abba").toString("hex"); + } + }; + + const draft = delegationFactory.createTransactionForUnjailingNodes({ + sender: sender, + delegationContract: delegationContract, + publicKeys: [publicKey] + }); + + assert.equal(draft.sender, "erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2"); + assert.equal(draft.receiver, "erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqtllllls002zgc"); + assert.isDefined(draft.data); + assert.deepEqual(draft.data, Buffer.from("unJailNodes@61626261")); + assert.equal(draft.value, 0); + }); + + it("should create draft transaction for changing service fee", async function () { + const sender = Address.fromBech32("erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2"); + const delegationContract = Address.fromBech32("erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqtllllls002zgc"); + const serviceFee = new BigNumber(10); + + const draft = delegationFactory.createTransactionForChangingServiceFee({ + sender: sender, + delegationContract: delegationContract, + serviceFee: serviceFee + }); + + assert.equal(draft.sender, "erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2"); + assert.equal(draft.receiver, "erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqtllllls002zgc"); + assert.isDefined(draft.data); + assert.deepEqual(draft.data, Buffer.from("changeServiceFee@0a")); + assert.equal(draft.value, 0); + }); + + it("should create draft transaction for changing delegation cap", async function () { + const sender = Address.fromBech32("erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2"); + const delegationContract = Address.fromBech32("erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqtllllls002zgc"); + const delegationCap = new BigNumber("5000000000000000000000"); + + const draft = delegationFactory.createTransactionForModifyingDelegationCap({ + sender: sender, + delegationContract: delegationContract, + delegationCap: delegationCap + }); + + assert.equal(draft.sender, "erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2"); + assert.equal(draft.receiver, "erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqtllllls002zgc"); + assert.isDefined(draft.data); + assert.deepEqual(draft.data, Buffer.from("modifyTotalDelegationCap@010f0cf064dd59200000")); + assert.equal(draft.value, 0); + }); + + it("should create draft transaction for setting automatic activation", async function () { + const sender = Address.fromBech32("erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2"); + const delegationContract = Address.fromBech32("erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqtllllls002zgc"); + + const draft = delegationFactory.createTransactionForSettingAutomaticActivation({ + sender: sender, + delegationContract: delegationContract + }); + + assert.equal(draft.sender, "erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2"); + assert.equal(draft.receiver, "erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqtllllls002zgc"); + assert.isDefined(draft.data); + assert.deepEqual(draft.data, Buffer.from("setAutomaticActivation@74727565")); + assert.equal(draft.value, 0); + }); + + it("should create draft transaction for unsetting automatic activation", async function () { + const sender = Address.fromBech32("erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2"); + const delegationContract = Address.fromBech32("erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqtllllls002zgc"); + + const draft = delegationFactory.createTransactionForUnsettingAutomaticActivation({ + sender: sender, + delegationContract: delegationContract + }); + + assert.equal(draft.sender, "erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2"); + assert.equal(draft.receiver, "erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqtllllls002zgc"); + assert.isDefined(draft.data); + assert.deepEqual(draft.data, Buffer.from("setAutomaticActivation@66616c7365")); + assert.equal(draft.value, 0); + }); + + it("should create draft transaction for setting cap check on redelegate rewards", async function () { + const sender = Address.fromBech32("erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2"); + const delegationContract = Address.fromBech32("erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqtllllls002zgc"); + + const draft = delegationFactory.createTransactionForSettingCapCheckOnRedelegateRewards({ + sender: sender, + delegationContract: delegationContract + }); + + assert.equal(draft.sender, "erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2"); + assert.equal(draft.receiver, "erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqtllllls002zgc"); + assert.isDefined(draft.data); + assert.deepEqual(draft.data, Buffer.from("setCheckCapOnReDelegateRewards@74727565")); + assert.equal(draft.value, 0); + }); + + it("should create draft transaction for unsetting cap check on redelegate rewards", async function () { + const sender = Address.fromBech32("erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2"); + const delegationContract = Address.fromBech32("erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqtllllls002zgc"); + + const draft = delegationFactory.createTransactionForUnsettingCapCheckOnRedelegateRewards({ + sender: sender, + delegationContract: delegationContract + }); + + assert.equal(draft.sender, "erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2"); + assert.equal(draft.receiver, "erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqtllllls002zgc"); + assert.isDefined(draft.data); + assert.deepEqual(draft.data, Buffer.from("setCheckCapOnReDelegateRewards@66616c7365")); + assert.equal(draft.value, 0); + }); + + it("should create draft transaction for setting metadata", async function () { + const sender = Address.fromBech32("erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2"); + const delegationContract = Address.fromBech32("erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqtllllls002zgc"); + + const draft = delegationFactory.createTransactionForSettingMetadata({ + sender: sender, + delegationContract: delegationContract, + name: "name", + website: "website", + identifier: "identifier" + }); + + assert.equal(draft.sender, "erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2"); + assert.equal(draft.receiver, "erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqtllllls002zgc"); + assert.isDefined(draft.data); + assert.deepEqual(draft.data, Buffer.from("setMetaData@6e616d65@77656273697465@6964656e746966696572")); + assert.equal(draft.value, 0); + }); +}); diff --git a/src/transactionIntentsFactories/delegationTransactionIntentsFactory.ts b/src/transactionsFactories/delegationTransactionsFactory.ts similarity index 83% rename from src/transactionIntentsFactories/delegationTransactionIntentsFactory.ts rename to src/transactionsFactories/delegationTransactionsFactory.ts index 1a8cc5be..fe7133a0 100644 --- a/src/transactionIntentsFactories/delegationTransactionIntentsFactory.ts +++ b/src/transactionsFactories/delegationTransactionsFactory.ts @@ -1,8 +1,8 @@ import { IAddress } from "../interface"; import { BigNumber } from "bignumber.js"; import { numberToPaddedHex, byteArrayToHex, utf8ToHex } from "../utils.codec"; -import { TransactionIntent } from "../transactionIntent"; -import { TransactionIntentBuilder } from "./transactionIntentBuilder"; +import { DraftTransaction } from "../draftTransaction"; +import { DraftTransactionBuilder } from "./draftTransactionBuilder"; import { Address } from "../address"; import { DELEGATION_MANAGER_SC_ADDRESS } from "../constants"; import { Err } from "../errors"; @@ -24,19 +24,19 @@ interface IValidatorPublicKey { hex(): string; } -export class DelegationTransactionIntentsFactory { +export class DelegationTransactionsFactory { private readonly config: Config; constructor(config: Config) { this.config = config; } - createTransactionIntentForNewDelegationContract(options: { + createTransactionForNewDelegationContract(options: { sender: IAddress, totalDelegationCap: BigNumber.Value, serviceFee: BigNumber.Value, - value: BigNumber.Value - }): TransactionIntent { + amount: BigNumber.Value + }): DraftTransaction { const dataParts = [ "createNewDelegationContract", numberToPaddedHex(options.totalDelegationCap.toString()), @@ -45,23 +45,23 @@ export class DelegationTransactionIntentsFactory { const executionGasLimit = new BigNumber(this.config.gasLimitCreateDelegationContract).plus(this.config.additionalGasLimitForDelegationOperations); - return new TransactionIntentBuilder({ + return new DraftTransactionBuilder({ config: this.config, sender: options.sender, receiver: Address.fromBech32(DELEGATION_MANAGER_SC_ADDRESS), dataParts: dataParts, gasLimit: executionGasLimit, addDataMovementGas: true, - value: options.value + amount: options.amount }).build(); } - createTransactionIntentForAddingNodes(options: { + createTransactionForAddingNodes(options: { sender: IAddress, delegationContract: IAddress, publicKeys: IValidatorPublicKey[], signedMessages: Uint8Array[] - }): TransactionIntent { + }): DraftTransaction { if (options.publicKeys.length !== options.signedMessages.length) { throw new Err("The number of public keys should match the number of signed messages"); } @@ -73,7 +73,7 @@ export class DelegationTransactionIntentsFactory { dataParts.push(...[options.publicKeys[i].hex(), byteArrayToHex(options.signedMessages[i])]); } - return new TransactionIntentBuilder({ + return new DraftTransactionBuilder({ config: this.config, sender: options.sender, receiver: options.delegationContract, @@ -83,11 +83,11 @@ export class DelegationTransactionIntentsFactory { }).build(); } - createTransactionIntentForRemovingNodes(options: { + createTransactionForRemovingNodes(options: { sender: IAddress, delegationContract: IAddress, publicKeys: IValidatorPublicKey[] - }): TransactionIntent { + }): DraftTransaction { const dataParts = ["removeNodes"]; for (const key of options.publicKeys) { @@ -96,7 +96,7 @@ export class DelegationTransactionIntentsFactory { const numNodes = options.publicKeys.length; - return new TransactionIntentBuilder({ + return new DraftTransactionBuilder({ config: this.config, sender: options.sender, receiver: options.delegationContract, @@ -106,11 +106,11 @@ export class DelegationTransactionIntentsFactory { }).build(); } - createTransactionIntentForStakingNodes(options: { + createTransactionForStakingNodes(options: { sender: IAddress, delegationContract: IAddress, publicKeys: IValidatorPublicKey[] - }): TransactionIntent { + }): DraftTransaction { let dataParts = ["stakeNodes"]; for (const key of options.publicKeys) { @@ -123,7 +123,7 @@ export class DelegationTransactionIntentsFactory { this.config.gasLimitDelegationOperations ); - return new TransactionIntentBuilder({ + return new DraftTransactionBuilder({ config: this.config, sender: options.sender, receiver: options.delegationContract, @@ -133,11 +133,11 @@ export class DelegationTransactionIntentsFactory { }).build(); } - createTransactionIntentForUnbondingNodes(options: { + createTransactionForUnbondingNodes(options: { sender: IAddress, delegationContract: IAddress, publicKeys: IValidatorPublicKey[] - }): TransactionIntent { + }): DraftTransaction { let dataParts = ["unBondNodes"]; for (const key of options.publicKeys) { @@ -150,7 +150,7 @@ export class DelegationTransactionIntentsFactory { this.config.gasLimitUnbond ).plus(this.config.gasLimitDelegationOperations); - return new TransactionIntentBuilder({ + return new DraftTransactionBuilder({ config: this.config, sender: options.sender, receiver: options.delegationContract, @@ -160,11 +160,11 @@ export class DelegationTransactionIntentsFactory { }).build(); } - createTransactionIntentForUnstakingNodes(options: { + createTransactionForUnstakingNodes(options: { sender: IAddress, delegationContract: IAddress, publicKeys: IValidatorPublicKey[] - }): TransactionIntent { + }): DraftTransaction { let dataParts = ["unStakeNodes"]; for (const key of options.publicKeys) { @@ -177,7 +177,7 @@ export class DelegationTransactionIntentsFactory { this.config.gasLimitUnstake ).plus(this.config.gasLimitDelegationOperations); - return new TransactionIntentBuilder({ + return new DraftTransactionBuilder({ config: this.config, sender: options.sender, receiver: options.delegationContract, @@ -187,11 +187,11 @@ export class DelegationTransactionIntentsFactory { }).build(); } - createTransactionIntentForUnjailingNodes(options: { + createTransactionForUnjailingNodes(options: { sender: IAddress, delegationContract: IAddress, publicKeys: IValidatorPublicKey[] - }): TransactionIntent { + }): DraftTransaction { const dataParts = ["unJailNodes"]; for (const key of options.publicKeys) { @@ -200,7 +200,7 @@ export class DelegationTransactionIntentsFactory { const numNodes = options.publicKeys.length; - return new TransactionIntentBuilder({ + return new DraftTransactionBuilder({ config: this.config, sender: options.sender, receiver: options.delegationContract, @@ -210,17 +210,17 @@ export class DelegationTransactionIntentsFactory { }).build(); } - createTransactionIntentForChangingServiceFee(options: { + createTransactionForChangingServiceFee(options: { sender: IAddress, delegationContract: IAddress, serviceFee: BigNumber.Value - }): TransactionIntent { + }): DraftTransaction { const dataParts = [ "changeServiceFee", numberToPaddedHex(options.serviceFee) ]; - return new TransactionIntentBuilder({ + return new DraftTransactionBuilder({ config: this.config, sender: options.sender, receiver: options.delegationContract, @@ -230,17 +230,17 @@ export class DelegationTransactionIntentsFactory { }).build(); } - createTransactionIntentForModifyingDelegationCap(options: { + createTransactionForModifyingDelegationCap(options: { sender: IAddress, delegationContract: IAddress, delegationCap: BigNumber.Value - }): TransactionIntent { + }): DraftTransaction { const dataParts = [ "modifyTotalDelegationCap", numberToPaddedHex(options.delegationCap) ]; - return new TransactionIntentBuilder({ + return new DraftTransactionBuilder({ config: this.config, sender: options.sender, receiver: options.delegationContract, @@ -250,16 +250,16 @@ export class DelegationTransactionIntentsFactory { }).build(); } - createTransactionIntentForSettingAutomaticActivation(options: { + createTransactionForSettingAutomaticActivation(options: { sender: IAddress, delegationContract: IAddress - }): TransactionIntent { + }): DraftTransaction { const dataParts = [ "setAutomaticActivation", utf8ToHex("true") ]; - return new TransactionIntentBuilder({ + return new DraftTransactionBuilder({ config: this.config, sender: options.sender, receiver: options.delegationContract, @@ -269,16 +269,16 @@ export class DelegationTransactionIntentsFactory { }).build(); } - createTransactionIntentForUnsettingAutomaticActivation(options: { + createTransactionForUnsettingAutomaticActivation(options: { sender: IAddress, delegationContract: IAddress - }): TransactionIntent { + }): DraftTransaction { const dataParts = [ "setAutomaticActivation", utf8ToHex("false") ]; - return new TransactionIntentBuilder({ + return new DraftTransactionBuilder({ config: this.config, sender: options.sender, receiver: options.delegationContract, @@ -288,16 +288,16 @@ export class DelegationTransactionIntentsFactory { }).build(); } - createTransactionIntentForSettingCapCheckOnRedelegateRewards(options: { + createTransactionForSettingCapCheckOnRedelegateRewards(options: { sender: IAddress, delegationContract: IAddress - }): TransactionIntent { + }): DraftTransaction { const dataParts = [ "setCheckCapOnReDelegateRewards", utf8ToHex("true") ]; - return new TransactionIntentBuilder({ + return new DraftTransactionBuilder({ config: this.config, sender: options.sender, receiver: options.delegationContract, @@ -307,16 +307,16 @@ export class DelegationTransactionIntentsFactory { }).build(); } - createTransactionIntentForUnsettingCapCheckOnRedelegateRewards(options: { + createTransactionForUnsettingCapCheckOnRedelegateRewards(options: { sender: IAddress, delegationContract: IAddress - }): TransactionIntent { + }): DraftTransaction { const dataParts = [ "setCheckCapOnReDelegateRewards", utf8ToHex("false") ]; - return new TransactionIntentBuilder({ + return new DraftTransactionBuilder({ config: this.config, sender: options.sender, receiver: options.delegationContract, @@ -326,13 +326,13 @@ export class DelegationTransactionIntentsFactory { }).build(); } - createTransactionIntentForSettingMetadata(options: { + createTransactionForSettingMetadata(options: { sender: IAddress, delegationContract: IAddress, name: string, website: string, identifier: string - }): TransactionIntent { + }): DraftTransaction { const dataParts = [ "setMetaData", utf8ToHex(options.name), @@ -340,7 +340,7 @@ export class DelegationTransactionIntentsFactory { utf8ToHex(options.identifier) ]; - return new TransactionIntentBuilder({ + return new DraftTransactionBuilder({ config: this.config, sender: options.sender, receiver: options.delegationContract, diff --git a/src/transactionIntentsFactories/transactionIntentBuilder.ts b/src/transactionsFactories/draftTransactionBuilder.ts similarity index 85% rename from src/transactionIntentsFactories/transactionIntentBuilder.ts rename to src/transactionsFactories/draftTransactionBuilder.ts index 00076017..99d57c5a 100644 --- a/src/transactionIntentsFactories/transactionIntentBuilder.ts +++ b/src/transactionsFactories/draftTransactionBuilder.ts @@ -2,21 +2,21 @@ import { BigNumber } from "bignumber.js"; import { IAddress, ITransactionPayload } from "../interface"; import { ARGUMENTS_SEPARATOR } from "../constants"; import { TransactionPayload } from "../transactionPayload"; -import { TransactionIntent } from "../transactionIntent"; +import { DraftTransaction } from "../draftTransaction"; interface Config { minGasLimit: BigNumber.Value; gasLimitPerByte: BigNumber.Value; } -export class TransactionIntentBuilder { +export class DraftTransactionBuilder { private config: Config; private sender: IAddress; private receiver: IAddress; private dataParts: string[]; private providedGasLimit: BigNumber; private addDataMovementGas: boolean; - private value?: BigNumber.Value; + private amount?: BigNumber.Value; constructor(options: { config: Config, @@ -25,7 +25,7 @@ export class TransactionIntentBuilder { dataParts: string[], gasLimit: BigNumber.Value, addDataMovementGas: boolean, - value?: BigNumber.Value + amount?: BigNumber.Value }) { this.config = options.config; this.sender = options.sender; @@ -33,7 +33,7 @@ export class TransactionIntentBuilder { this.dataParts = options.dataParts; this.providedGasLimit = new BigNumber(options.gasLimit); this.addDataMovementGas = options.addDataMovementGas; - this.value = options.value; + this.amount = options.amount; } private computeGasLimit(payload: ITransactionPayload): BigNumber.Value { @@ -51,15 +51,15 @@ export class TransactionIntentBuilder { return new TransactionPayload(data); } - build(): TransactionIntent { + build(): DraftTransaction { const data = this.buildTransactionPayload() const gasLimit = this.computeGasLimit(data); - return new TransactionIntent({ + return new DraftTransaction({ sender: this.sender.bech32(), receiver: this.receiver.bech32(), gasLimit: gasLimit, - value: this.value || 0, + value: this.amount || 0, data: data.valueOf() }) } diff --git a/src/transactionIntentsFactories/smartContractTransactionIntentsFactory.spec.ts b/src/transactionsFactories/smartContractTransactionsFactory.spec.ts similarity index 56% rename from src/transactionIntentsFactories/smartContractTransactionIntentsFactory.spec.ts rename to src/transactionsFactories/smartContractTransactionsFactory.spec.ts index 8f21e42a..874845ff 100644 --- a/src/transactionIntentsFactories/smartContractTransactionIntentsFactory.spec.ts +++ b/src/transactionsFactories/smartContractTransactionsFactory.spec.ts @@ -1,5 +1,5 @@ import { assert, expect } from "chai"; -import { SmartContractTransactionIntentsFactory } from "./smartContractTransactionIntentsFactory"; +import { SmartContractTransactionsFactory } from "./smartContractTransactionsFactory"; import { Address } from "../address"; import { Code } from "../smartcontracts/code"; import { AbiRegistry } from "../smartcontracts/typesystem/abiRegistry"; @@ -7,24 +7,24 @@ import { U32Value } from "../smartcontracts"; import { CONTRACT_DEPLOY_ADDRESS } from "../constants"; import { loadContractCode, loadAbiRegistry } from "../testutils/utils"; import { Err } from "../errors"; -import { TransactionIntentsFactoryConfig } from "./transactionIntentsFactoryConfig"; +import { TransactionsFactoryConfig } from "./transactionsFactoryConfig"; -describe("test smart contract intents factory", function () { - const config = new TransactionIntentsFactoryConfig("D"); - let factory: SmartContractTransactionIntentsFactory; - let abiAwareFactory: SmartContractTransactionIntentsFactory; +describe("test smart contract transactions factory", function () { + const config = new TransactionsFactoryConfig("D"); + let factory: SmartContractTransactionsFactory; + let abiAwareFactory: SmartContractTransactionsFactory; let adderByteCode: Code; let abiRegistry: AbiRegistry; before(async function () { - factory = new SmartContractTransactionIntentsFactory({ + factory = new SmartContractTransactionsFactory({ config: config }); adderByteCode = await loadContractCode("src/testdata/adder.wasm"); abiRegistry = await loadAbiRegistry("src/testdata/adder.abi.json"); - abiAwareFactory = new SmartContractTransactionIntentsFactory({ + abiAwareFactory = new SmartContractTransactionsFactory({ config: config, abi: abiRegistry }, @@ -36,7 +36,7 @@ describe("test smart contract intents factory", function () { const gasLimit = 6000000; const args = [0]; - assert.throws(() => factory.createTransactionIntentForDeploy({ + assert.throws(() => factory.createTransactionForDeploy({ sender: sender, bytecode: adderByteCode.valueOf(), gasLimit: gasLimit, @@ -44,50 +44,50 @@ describe("test smart contract intents factory", function () { }), Err, "Can't convert args to TypedValues"); }); - it("should create intent for deploy", async function () { + it("should create draft transaction for deploy", async function () { const sender = Address.fromBech32("erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"); const gasLimit = 6000000; const args = [new U32Value(0)]; - const deployIntent = factory.createTransactionIntentForDeploy({ + const deployDraft = factory.createTransactionForDeploy({ sender: sender, bytecode: adderByteCode.valueOf(), gasLimit: gasLimit, args: args }); - const abiDeployIntent = abiAwareFactory.createTransactionIntentForDeploy({ + const abiDeployDraft = abiAwareFactory.createTransactionForDeploy({ sender: sender, bytecode: adderByteCode.valueOf(), gasLimit: gasLimit, args: args }); - assert.equal(deployIntent.sender, "erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"); - assert.equal(deployIntent.receiver, CONTRACT_DEPLOY_ADDRESS); - assert.isDefined(deployIntent.data); - expect(deployIntent.data!.length).to.be.greaterThan(0); + assert.equal(deployDraft.sender, "erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"); + assert.equal(deployDraft.receiver, CONTRACT_DEPLOY_ADDRESS); + assert.isDefined(deployDraft.data); + expect(deployDraft.data!.length).to.be.greaterThan(0); - assert.equal(deployIntent.gasLimit.valueOf(), gasLimit); - assert.equal(deployIntent.value, 0); + assert.equal(deployDraft.gasLimit.valueOf(), gasLimit); + assert.equal(deployDraft.value, 0); - assert.deepEqual(deployIntent, abiDeployIntent); + assert.deepEqual(deployDraft, abiDeployDraft); }); - it("should create intent for execute", async function () { + it("should create draft transaction for execute", async function () { const sender = Address.fromBech32("erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"); const contract = Address.fromBech32("erd1qqqqqqqqqqqqqpgqhy6nl6zq07rnzry8uyh6rtyq0uzgtk3e69fqgtz9l4"); const func = "add"; const gasLimit = 6000000; const args = [new U32Value(7)]; - const deployIntent = factory.createTransactionIntentForExecute({ + const executeDraft = factory.createTransactionForExecute({ sender: sender, contractAddress: contract, functionName: func, gasLimit: gasLimit, args: args }); - const abiDeployIntent = abiAwareFactory.createTransactionIntentForExecute({ + const abiExecuteDraft = abiAwareFactory.createTransactionForExecute({ sender: sender, contractAddress: contract, functionName: func, @@ -95,25 +95,25 @@ describe("test smart contract intents factory", function () { args: args }); - assert.equal(deployIntent.sender, "erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"); - assert.equal(deployIntent.receiver, "erd1qqqqqqqqqqqqqpgqhy6nl6zq07rnzry8uyh6rtyq0uzgtk3e69fqgtz9l4"); + assert.equal(executeDraft.sender, "erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"); + assert.equal(executeDraft.receiver, "erd1qqqqqqqqqqqqqpgqhy6nl6zq07rnzry8uyh6rtyq0uzgtk3e69fqgtz9l4"); - assert.isDefined(deployIntent.data); - assert.deepEqual(deployIntent.data, Buffer.from("add@07")); + assert.isDefined(executeDraft.data); + assert.deepEqual(executeDraft.data, Buffer.from("add@07")); - assert.equal(deployIntent.gasLimit.valueOf(), gasLimit); - assert.equal(deployIntent.value, 0); + assert.equal(executeDraft.gasLimit.valueOf(), gasLimit); + assert.equal(executeDraft.value, 0); - assert.deepEqual(deployIntent, abiDeployIntent); + assert.deepEqual(executeDraft, abiExecuteDraft); }); - it("should create intent for upgrade", async function () { + it("should create draft transaction for upgrade", async function () { const sender = Address.fromBech32("erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"); const contract = Address.fromBech32("erd1qqqqqqqqqqqqqpgqhy6nl6zq07rnzry8uyh6rtyq0uzgtk3e69fqgtz9l4"); const gasLimit = 6000000; const args = [new U32Value(0)]; - const deployIntent = factory.createTransactionIntentForUpgrade({ + const upgradeDraft = factory.createTransactionForUpgrade({ sender: sender, contract: contract, bytecode: adderByteCode.valueOf(), @@ -121,7 +121,7 @@ describe("test smart contract intents factory", function () { args: args }); - const abiDeployIntent = abiAwareFactory.createTransactionIntentForUpgrade({ + const abiUpgradeDraft = abiAwareFactory.createTransactionForUpgrade({ sender: sender, contract: contract, bytecode: adderByteCode.valueOf(), @@ -129,14 +129,14 @@ describe("test smart contract intents factory", function () { args: args }); - assert.equal(deployIntent.sender, "erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"); - assert.equal(deployIntent.receiver, "erd1qqqqqqqqqqqqqpgqhy6nl6zq07rnzry8uyh6rtyq0uzgtk3e69fqgtz9l4"); - assert.isDefined(deployIntent.data); - assert.isTrue(Buffer.from(deployIntent.data!).toString().startsWith("upgradeContract@")); + assert.equal(upgradeDraft.sender, "erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"); + assert.equal(upgradeDraft.receiver, "erd1qqqqqqqqqqqqqpgqhy6nl6zq07rnzry8uyh6rtyq0uzgtk3e69fqgtz9l4"); + assert.isDefined(upgradeDraft.data); + assert.isTrue(Buffer.from(upgradeDraft.data!).toString().startsWith("upgradeContract@")); - assert.equal(deployIntent.gasLimit.valueOf(), gasLimit); - assert.equal(deployIntent.value, 0); + assert.equal(upgradeDraft.gasLimit.valueOf(), gasLimit); + assert.equal(upgradeDraft.value, 0); - assert.deepEqual(deployIntent, abiDeployIntent); + assert.deepEqual(upgradeDraft, abiUpgradeDraft); }); }); diff --git a/src/transactionIntentsFactories/smartContractTransactionIntentsFactory.ts b/src/transactionsFactories/smartContractTransactionsFactory.ts similarity index 81% rename from src/transactionIntentsFactories/smartContractTransactionIntentsFactory.ts rename to src/transactionsFactories/smartContractTransactionsFactory.ts index 82e69e7c..b358fc0b 100644 --- a/src/transactionIntentsFactories/smartContractTransactionIntentsFactory.ts +++ b/src/transactionsFactories/smartContractTransactionsFactory.ts @@ -1,13 +1,13 @@ import { BigNumber } from "bignumber.js"; import { IAddress } from "../interface"; -import { TransactionIntent } from "../transactionIntent"; -import { AbiRegistry, ArgSerializer, CodeMetadata, ContractFunction, EndpointDefinition } from "../smartcontracts"; +import { DraftTransaction } from "../draftTransaction"; +import { ArgSerializer, CodeMetadata, ContractFunction, EndpointDefinition } from "../smartcontracts"; import { byteArrayToHex } from "../utils.codec"; import { CONTRACT_DEPLOY_ADDRESS, VM_TYPE_WASM_VM } from "../constants"; import { NativeSerializer } from "../smartcontracts/nativeSerializer"; import { Err } from "../errors"; import { Address } from "../address"; -import { TransactionIntentBuilder } from "./transactionIntentBuilder"; +import { DraftTransactionBuilder } from "./draftTransactionBuilder"; interface Config { chainID: string; @@ -22,7 +22,7 @@ interface Abi { } -export class SmartContractTransactionIntentsFactory { +export class SmartContractTransactionsFactory { private readonly config: Config; private readonly abiRegistry?: Abi; @@ -37,16 +37,19 @@ export class SmartContractTransactionIntentsFactory { this.abiRegistry = abi; } - createTransactionIntentForDeploy(options: { + createTransactionForDeploy(options: { sender: IAddress, bytecode: Uint8Array, gasLimit: BigNumber.Value, args?: any[], + nativeTransferAmount?: BigNumber.Value, isUpgradeable?: boolean, isReadable?: boolean, isPayable?: boolean, isPayableBySmartContract?: boolean - }): TransactionIntent { + }): DraftTransaction { + const nativeTransferAmount = options.nativeTransferAmount ?? 0; + const isUpgradeable = options.isUpgradeable ?? true; const isReadable = options.isReadable ?? true; const isPayable = options.isPayable ?? false; @@ -64,31 +67,32 @@ export class SmartContractTransactionIntentsFactory { const preparedArgs = this.argsToDataParts(args, this.abiRegistry?.constructorDefinition) parts = parts.concat(preparedArgs); - return new TransactionIntentBuilder({ + return new DraftTransactionBuilder({ config: this.config, sender: options.sender, receiver: Address.fromBech32(CONTRACT_DEPLOY_ADDRESS), dataParts: parts, gasLimit: options.gasLimit, - addDataMovementGas: false + addDataMovementGas: false, + amount: nativeTransferAmount }).build(); } - createTransactionIntentForExecute(options: { + createTransactionForExecute(options: { sender: IAddress, contractAddress: IAddress, functionName: string, gasLimit: BigNumber.Value, args?: any[] } - ): TransactionIntent { + ): DraftTransaction { const args = options.args || []; let parts: string[] = [options.functionName]; const preparedArgs = this.argsToDataParts(args, this.abiRegistry?.getEndpoint(options.functionName)); parts = parts.concat(preparedArgs); - return new TransactionIntentBuilder({ + return new DraftTransactionBuilder({ config: this.config, sender: options.sender, receiver: options.contractAddress, @@ -98,18 +102,21 @@ export class SmartContractTransactionIntentsFactory { }).build(); } - createTransactionIntentForUpgrade(options: { + createTransactionForUpgrade(options: { sender: IAddress, contract: IAddress, bytecode: Uint8Array, gasLimit: BigNumber.Value, args?: any[], + nativeTransferAmount?: BigNumber.Value, isUpgradeable?: boolean, isReadable?: boolean, isPayable?: boolean, isPayableBySmartContract?: boolean } - ): TransactionIntent { + ): DraftTransaction { + const nativeTransferAmount = options.nativeTransferAmount ?? 0; + const isUpgradeable = options.isUpgradeable ?? true; const isReadable = options.isReadable ?? true; const isPayable = options.isPayable ?? false; @@ -127,13 +134,14 @@ export class SmartContractTransactionIntentsFactory { const preparedArgs = this.argsToDataParts(args, this.abiRegistry?.constructorDefinition) parts = parts.concat(preparedArgs); - return new TransactionIntentBuilder({ + return new DraftTransactionBuilder({ config: this.config, sender: options.sender, receiver: options.contract, dataParts: parts, gasLimit: options.gasLimit, - addDataMovementGas: false + addDataMovementGas: false, + amount: nativeTransferAmount }).build(); } diff --git a/src/transactionsFactories/tokenManagementTransactionIntentsFactory.spec.ts b/src/transactionsFactories/tokenManagementTransactionIntentsFactory.spec.ts new file mode 100644 index 00000000..01b49506 --- /dev/null +++ b/src/transactionsFactories/tokenManagementTransactionIntentsFactory.spec.ts @@ -0,0 +1,153 @@ +import { assert } from "chai"; +import { loadTestWallets, TestWallet } from "../testutils"; +import { TokenManagementTransactionsFactory } from "./tokenManagementTransactionsFactory"; +import { TransactionsFactoryConfig } from "./transactionsFactoryConfig"; +import BigNumber from "bignumber.js"; +import { ESDT_CONTRACT_ADDRESS } from "../constants"; + +describe("test token management transactions factory", () => { + let frank: TestWallet, grace: TestWallet; + let factory: TokenManagementTransactionsFactory; + let config: TransactionsFactoryConfig; + + before(async function () { + ({ frank, grace } = await loadTestWallets()); + config = new TransactionsFactoryConfig("T"); + factory = new TokenManagementTransactionsFactory(config); + }); + + it("should create draft transaction for registering and setting roles", () => { + const draft = factory.createTransactionForRegisteringAndSettingRoles({ + sender: frank.address, + tokenName: "TEST", + tokenTicker: "TEST", + tokenType: "FNG", + numDecimals: 2 + }); + + assert.deepEqual(draft.data, Buffer.from("registerAndSetAllRoles@54455354@54455354@464e47@02")); + assert.equal(draft.sender, frank.address.toString()); + assert.equal(draft.receiver, "erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqzllls8a5w6u"); + assert.deepEqual(draft.value, config.issueCost); + assert.deepEqual(draft.gasLimit, new BigNumber("60125000")); + }); + + it("should create draft transaction for issuing fungible token", () => { + const draft = factory.createTransactionForIssuingFungible({ + sender: frank.address, + tokenName: "FRANK", + tokenTicker: "FRANK", + initialSupply: 100, + numDecimals: 0, + canFreeze: true, + canWipe: true, + canPause: true, + canTransferNFTCreateRole: false, + canChangeOwner: true, + canUpgrade: true, + canAddSpecialRoles: true + }); + + assert.deepEqual(draft.data, Buffer.from("issue@4652414e4b@4652414e4b@64@@63616e467265657a65@74727565@63616e57697065@74727565@63616e5061757365@74727565@63616e4368616e67654f776e6572@74727565@63616e55706772616465@74727565@63616e4164645370656369616c526f6c6573@74727565")); + assert.equal(draft.sender, frank.address.toString()); + assert.equal(draft.receiver, ESDT_CONTRACT_ADDRESS); + assert.deepEqual(draft.value, config.issueCost); + }); + + it("should create draft transaction for issuing semi-fungible token", () => { + const draft = factory.createTransactionForIssuingSemiFungible({ + sender: frank.address, + tokenName: "FRANK", + tokenTicker: "FRANK", + canFreeze: true, + canWipe: true, + canPause: true, + canTransferNFTCreateRole: true, + canChangeOwner: true, + canUpgrade: true, + canAddSpecialRoles: true + }); + + assert.deepEqual(draft.data, Buffer.from("issueSemiFungible@4652414e4b@4652414e4b@63616e467265657a65@74727565@63616e57697065@74727565@63616e5061757365@74727565@63616e5472616e736665724e4654437265617465526f6c65@74727565@63616e4368616e67654f776e6572@74727565@63616e55706772616465@74727565@63616e4164645370656369616c526f6c6573@74727565")); + assert.equal(draft.sender, frank.address.toString()); + assert.equal(draft.receiver, ESDT_CONTRACT_ADDRESS); + assert.deepEqual(draft.value, config.issueCost); + }); + + it("should create draft transaction for issuing non-fungible token", () => { + const draft = factory.createTransactionForIssuingNonFungible({ + sender: frank.address, + tokenName: "FRANK", + tokenTicker: "FRANK", + canFreeze: true, + canWipe: true, + canPause: true, + canTransferNFTCreateRole: true, + canChangeOwner: true, + canUpgrade: true, + canAddSpecialRoles: true + }); + + assert.deepEqual(draft.data, Buffer.from("issueNonFungible@4652414e4b@4652414e4b@63616e467265657a65@74727565@63616e57697065@74727565@63616e5061757365@74727565@63616e5472616e736665724e4654437265617465526f6c65@74727565@63616e4368616e67654f776e6572@74727565@63616e55706772616465@74727565@63616e4164645370656369616c526f6c6573@74727565")); + assert.equal(draft.sender, frank.address.toString()); + assert.equal(draft.receiver, ESDT_CONTRACT_ADDRESS); + assert.deepEqual(draft.value, config.issueCost); + }); + + it("should create draft transaction for registering metaEsdt", () => { + const draft = factory.createTransactionForRegisteringMetaESDT({ + sender: frank.address, + tokenName: "FRANK", + tokenTicker: "FRANK", + numDecimals: 10, + canFreeze: true, + canWipe: true, + canPause: true, + canTransferNFTCreateRole: true, + canChangeOwner: true, + canUpgrade: true, + canAddSpecialRoles: true + }); + + assert.deepEqual(draft.data, Buffer.from("registerMetaESDT@4652414e4b@4652414e4b@0a@63616e467265657a65@74727565@63616e57697065@74727565@63616e5061757365@74727565@63616e5472616e736665724e4654437265617465526f6c65@74727565@63616e4368616e67654f776e6572@74727565@63616e55706772616465@74727565@63616e4164645370656369616c526f6c6573@74727565")); + assert.equal(draft.sender, frank.address.toString()); + assert.equal(draft.receiver, ESDT_CONTRACT_ADDRESS); + assert.deepEqual(draft.value, config.issueCost); + }); + + it("should create draft transaction for setting spcial role on non-fungible token", () => { + const draft = factory.createTransactionForSettingSpecialRoleOnNonFungibleToken({ + sender: frank.address, + user: grace.address, + tokenIdentifier: "FRANK-11ce3e", + addRoleNFTCreate: true, + addRoleNFTBurn: false, + addRoleNFTUpdateAttributes: true, + addRoleNFTAddURI: true, + addRoleESDTTransferRole: false + }); + + assert.deepEqual(draft.data, Buffer.from("setSpecialRole@4652414e4b2d313163653365@1e8a8b6b49de5b7be10aaa158a5a6a4abb4b56cc08f524bb5e6cd5f211ad3e13@45534454526f6c654e4654437265617465@45534454526f6c654e465455706461746541747472696275746573@45534454526f6c654e4654416464555249")); + assert.equal(draft.sender, frank.address.toString()); + assert.equal(draft.receiver, ESDT_CONTRACT_ADDRESS); + assert.equal(draft.value, 0); + }); + + it("should create draft transaction for creating nft", () => { + const draft = factory.createTransactionForCreatingNFT({ + sender: grace.address, + tokenIdentifier: "FRANK-aa9e8d", + initialQuantity: 1, + name: `test`, + royalties: 1000, + hash: "abba", + attributes: Buffer.from("test"), + uris: ["a", "b"] + }); + + assert.deepEqual(draft.data, Buffer.from("ESDTNFTCreate@4652414e4b2d616139653864@01@74657374@03e8@61626261@74657374@61@62")); + assert.equal(draft.sender, grace.address.toString()); + assert.equal(draft.receiver, grace.address.toString()); + assert.equal(draft.value, 0); + }); +}); diff --git a/src/transactionIntentsFactories/tokenManagementTransactionIntentsFactory.ts b/src/transactionsFactories/tokenManagementTransactionsFactory.ts similarity index 81% rename from src/transactionIntentsFactories/tokenManagementTransactionIntentsFactory.ts rename to src/transactionsFactories/tokenManagementTransactionsFactory.ts index 5c603202..1dc1709e 100644 --- a/src/transactionIntentsFactories/tokenManagementTransactionIntentsFactory.ts +++ b/src/transactionsFactories/tokenManagementTransactionsFactory.ts @@ -1,6 +1,6 @@ import BigNumber from "bignumber.js"; -import { TransactionIntent } from "../transactionIntent"; -import { TransactionIntentBuilder } from "./transactionIntentBuilder"; +import { DraftTransaction } from "../draftTransaction"; +import { DraftTransactionBuilder } from "./draftTransactionBuilder"; import { IAddress } from "../interface"; import { utf8ToHex, bigIntToHex, addressToHex, byteArrayToHex } from "../utils.codec"; import { ESDT_CONTRACT_ADDRESS } from "../constants"; @@ -29,7 +29,7 @@ interface Config { type RegisterAndSetAllRolesTokenType = "NFT" | "SFT" | "META" | "FNG"; -export class TokenManagementTransactionIntentsFactory { +export class TokenManagementTransactionsFactory { private readonly config: Config; private readonly trueAsHex: string; @@ -38,7 +38,7 @@ export class TokenManagementTransactionIntentsFactory { this.trueAsHex = utf8ToHex("true"); } - createTransactionIntentForIssuingFungible(options: { + createTransactionForIssuingFungible(options: { sender: IAddress, tokenName: string, tokenTicker: string, @@ -47,10 +47,11 @@ export class TokenManagementTransactionIntentsFactory { canFreeze: boolean, canWipe: boolean, canPause: boolean, + canTransferNFTCreateRole: boolean, canChangeOwner: boolean, canUpgrade: boolean, canAddSpecialRoles: boolean - }): TransactionIntent { + }): DraftTransaction { this.notifyAboutUnsettingBurnRoleGlobally(); const dataParts = [ @@ -62,23 +63,24 @@ export class TokenManagementTransactionIntentsFactory { ...(options.canFreeze ? [utf8ToHex("canFreeze"), this.trueAsHex] : []), ...(options.canWipe ? [utf8ToHex("canWipe"), this.trueAsHex] : []), ...(options.canPause ? [utf8ToHex("canPause"), this.trueAsHex] : []), + ...(options.canTransferNFTCreateRole ? [utf8ToHex("canTransferNFTCreateRole"), this.trueAsHex] : []), ...(options.canChangeOwner ? [utf8ToHex("canChangeOwner"), this.trueAsHex] : []), - ...(options.canUpgrade ? [utf8ToHex("canUpgrade"), this.trueAsHex] : []), - ...(options.canAddSpecialRoles ? [utf8ToHex("canAddSpecialRoles"), this.trueAsHex] : []), + ...[utf8ToHex("canUpgrade"), utf8ToHex(String(options.canUpgrade).toLowerCase())], + ...[utf8ToHex("canAddSpecialRoles"), utf8ToHex(String(options.canAddSpecialRoles).toLowerCase())] ]; - return new TransactionIntentBuilder({ + return new DraftTransactionBuilder({ config: this.config, sender: options.sender, receiver: Address.fromBech32(ESDT_CONTRACT_ADDRESS), dataParts: dataParts, gasLimit: this.config.gasLimitIssue, addDataMovementGas: true, - value: this.config.issueCost + amount: this.config.issueCost }).build(); } - createTransactionIntentForIssuingSemiFungible(options: { + createTransactionForIssuingSemiFungible(options: { sender: IAddress; tokenName: string; tokenTicker: string; @@ -89,7 +91,7 @@ export class TokenManagementTransactionIntentsFactory { canChangeOwner: boolean; canUpgrade: boolean; canAddSpecialRoles: boolean; - }): TransactionIntent { + }): DraftTransaction { this.notifyAboutUnsettingBurnRoleGlobally(); const dataParts = [ @@ -101,22 +103,22 @@ export class TokenManagementTransactionIntentsFactory { ...(options.canPause ? [utf8ToHex("canPause"), this.trueAsHex] : []), ...(options.canTransferNFTCreateRole ? [utf8ToHex("canTransferNFTCreateRole"), this.trueAsHex] : []), ...(options.canChangeOwner ? [utf8ToHex("canChangeOwner"), this.trueAsHex] : []), - ...(options.canUpgrade ? [utf8ToHex("canUpgrade"), this.trueAsHex] : []), - ...(options.canAddSpecialRoles ? [utf8ToHex("canAddSpecialRoles"), this.trueAsHex] : []), + ...[utf8ToHex("canUpgrade"), utf8ToHex(String(options.canUpgrade).toLowerCase())], + ...[utf8ToHex("canAddSpecialRoles"), utf8ToHex(String(options.canAddSpecialRoles).toLowerCase())] ]; - return new TransactionIntentBuilder({ + return new DraftTransactionBuilder({ config: this.config, sender: options.sender, receiver: Address.fromBech32(ESDT_CONTRACT_ADDRESS), dataParts: dataParts, gasLimit: this.config.gasLimitIssue, addDataMovementGas: true, - value: this.config.issueCost + amount: this.config.issueCost }).build(); } - createTransactionIntentForIssuingNonFungible(options: { + createTransactionForIssuingNonFungible(options: { sender: IAddress; tokenName: string; tokenTicker: string; @@ -127,7 +129,7 @@ export class TokenManagementTransactionIntentsFactory { canChangeOwner: boolean; canUpgrade: boolean; canAddSpecialRoles: boolean; - }): TransactionIntent { + }): DraftTransaction { this.notifyAboutUnsettingBurnRoleGlobally(); const dataParts = [ @@ -139,22 +141,22 @@ export class TokenManagementTransactionIntentsFactory { ...(options.canPause ? [utf8ToHex("canPause"), this.trueAsHex] : []), ...(options.canTransferNFTCreateRole ? [utf8ToHex("canTransferNFTCreateRole"), this.trueAsHex] : []), ...(options.canChangeOwner ? [utf8ToHex("canChangeOwner"), this.trueAsHex] : []), - ...(options.canUpgrade ? [utf8ToHex("canUpgrade"), this.trueAsHex] : []), - ...(options.canAddSpecialRoles ? [utf8ToHex("canAddSpecialRoles"), this.trueAsHex] : []), + ...[utf8ToHex("canUpgrade"), utf8ToHex(String(options.canUpgrade).toLowerCase())], + ...[utf8ToHex("canAddSpecialRoles"), utf8ToHex(String(options.canAddSpecialRoles).toLowerCase())] ]; - return new TransactionIntentBuilder({ + return new DraftTransactionBuilder({ config: this.config, sender: options.sender, receiver: Address.fromBech32(ESDT_CONTRACT_ADDRESS), dataParts: dataParts, gasLimit: this.config.gasLimitIssue, addDataMovementGas: true, - value: this.config.issueCost + amount: this.config.issueCost }).build(); } - createTransactionIntentForRegisteringMetaESDT(options: { + createTransactionForRegisteringMetaESDT(options: { sender: IAddress; tokenName: string; tokenTicker: string; @@ -166,7 +168,7 @@ export class TokenManagementTransactionIntentsFactory { canChangeOwner: boolean; canUpgrade: boolean; canAddSpecialRoles: boolean; - }): TransactionIntent { + }): DraftTransaction { this.notifyAboutUnsettingBurnRoleGlobally(); const dataParts = [ @@ -183,24 +185,24 @@ export class TokenManagementTransactionIntentsFactory { ...(options.canAddSpecialRoles ? [utf8ToHex("canAddSpecialRoles"), this.trueAsHex] : []), ]; - return new TransactionIntentBuilder({ + return new DraftTransactionBuilder({ config: this.config, sender: options.sender, receiver: Address.fromBech32(ESDT_CONTRACT_ADDRESS), dataParts: dataParts, gasLimit: this.config.gasLimitIssue, addDataMovementGas: true, - value: this.config.issueCost + amount: this.config.issueCost }).build(); } - createTransactionIntentForRegisteringAndSettingRoles(options: { + createTransactionForRegisteringAndSettingRoles(options: { sender: IAddress; tokenName: string; tokenTicker: string; tokenType: RegisterAndSetAllRolesTokenType; numDecimals: BigNumber.Value; - }): TransactionIntent { + }): DraftTransaction { this.notifyAboutUnsettingBurnRoleGlobally(); const dataParts = [ @@ -211,27 +213,27 @@ export class TokenManagementTransactionIntentsFactory { bigIntToHex(options.numDecimals) ]; - return new TransactionIntentBuilder({ + return new DraftTransactionBuilder({ config: this.config, sender: options.sender, receiver: Address.fromBech32(ESDT_CONTRACT_ADDRESS), dataParts: dataParts, gasLimit: this.config.gasLimitIssue, addDataMovementGas: true, - value: this.config.issueCost + amount: this.config.issueCost }).build(); } - createTransactionIntentForSettingBurnRoleGlobally(options: { + createTransactionForSettingBurnRoleGlobally(options: { sender: IAddress, tokenIdentifier: string - }): TransactionIntent { + }): DraftTransaction { const dataParts = [ "setBurnRoleGlobally", utf8ToHex(options.tokenIdentifier) ]; - return new TransactionIntentBuilder({ + return new DraftTransactionBuilder({ config: this.config, sender: options.sender, receiver: Address.fromBech32(ESDT_CONTRACT_ADDRESS), @@ -241,16 +243,16 @@ export class TokenManagementTransactionIntentsFactory { }).build(); } - createTransactionIntentForUnsettingBurnRoleGlobally(options: { + createTransactionForUnsettingBurnRoleGlobally(options: { sender: IAddress, tokenIdentifier: string - }): TransactionIntent { + }): DraftTransaction { const dataParts = [ "unsetBurnRoleGlobally", utf8ToHex(options.tokenIdentifier) ]; - return new TransactionIntentBuilder({ + return new DraftTransactionBuilder({ config: this.config, sender: options.sender, receiver: Address.fromBech32(ESDT_CONTRACT_ADDRESS), @@ -260,13 +262,13 @@ export class TokenManagementTransactionIntentsFactory { }).build(); } - createTransactionIntentForSettingSpecialRoleOnFungibleToken(options: { + createTransactionForSettingSpecialRoleOnFungibleToken(options: { sender: IAddress; user: IAddress; tokenIdentifier: string; addRoleLocalMint: boolean; addRoleLocalBurn: boolean; - }): TransactionIntent { + }): DraftTransaction { const dataParts = [ "setSpecialRole", utf8ToHex(options.tokenIdentifier), @@ -275,7 +277,7 @@ export class TokenManagementTransactionIntentsFactory { ...(options.addRoleLocalBurn ? [utf8ToHex("ESDTRoleLocalBurn")] : []), ]; - return new TransactionIntentBuilder({ + return new DraftTransactionBuilder({ config: this.config, sender: options.sender, receiver: Address.fromBech32(ESDT_CONTRACT_ADDRESS), @@ -285,7 +287,7 @@ export class TokenManagementTransactionIntentsFactory { }).build(); } - createTransactionIntentForSettingSpecialRoleOnSemiFungibleToken(options: { + createTransactionForSettingSpecialRoleOnSemiFungibleToken(options: { sender: IAddress; user: IAddress; tokenIdentifier: string; @@ -293,7 +295,7 @@ export class TokenManagementTransactionIntentsFactory { addRoleNFTBurn: boolean; addRoleNFTAddQuantity: boolean; addRoleESDTTransferRole: boolean; - }): TransactionIntent { + }): DraftTransaction { const dataParts = [ "setSpecialRole", utf8ToHex(options.tokenIdentifier), @@ -304,7 +306,7 @@ export class TokenManagementTransactionIntentsFactory { ...(options.addRoleESDTTransferRole ? [utf8ToHex("ESDTTransferRole")] : []), ]; - return new TransactionIntentBuilder({ + return new DraftTransactionBuilder({ config: this.config, sender: options.sender, receiver: Address.fromBech32(ESDT_CONTRACT_ADDRESS), @@ -314,7 +316,7 @@ export class TokenManagementTransactionIntentsFactory { }).build(); } - createTransactionIntentForSettingSpecialRoleOnMetaESDT(options: { + createTransactionForSettingSpecialRoleOnMetaESDT(options: { sender: IAddress; user: IAddress; tokenIdentifier: string; @@ -322,11 +324,11 @@ export class TokenManagementTransactionIntentsFactory { addRoleNFTBurn: boolean; addRoleNFTAddQuantity: boolean; addRoleESDTTransferRole: boolean; - }): TransactionIntent { - return this.createTransactionIntentForSettingSpecialRoleOnSemiFungibleToken(options); + }): DraftTransaction { + return this.createTransactionForSettingSpecialRoleOnSemiFungibleToken(options); } - createTransactionIntentForSettingSpecialRoleOnNonFungibleToken(options: { + createTransactionForSettingSpecialRoleOnNonFungibleToken(options: { sender: IAddress; user: IAddress; tokenIdentifier: string; @@ -335,7 +337,7 @@ export class TokenManagementTransactionIntentsFactory { addRoleNFTUpdateAttributes: boolean; addRoleNFTAddURI: boolean; addRoleESDTTransferRole: boolean; - }): TransactionIntent { + }): DraftTransaction { const dataParts = [ "setSpecialRole", utf8ToHex(options.tokenIdentifier), @@ -347,7 +349,7 @@ export class TokenManagementTransactionIntentsFactory { ...(options.addRoleESDTTransferRole ? [utf8ToHex("ESDTTransferRole")] : []), ]; - return new TransactionIntentBuilder({ + return new DraftTransactionBuilder({ config: this.config, sender: options.sender, receiver: Address.fromBech32(ESDT_CONTRACT_ADDRESS), @@ -357,7 +359,7 @@ export class TokenManagementTransactionIntentsFactory { }).build(); } - createTransactionIntentForCreatingNFT(options: { + createTransactionForCreatingNFT(options: { sender: IAddress; tokenIdentifier: string; initialQuantity: BigNumber.Value; @@ -366,7 +368,7 @@ export class TokenManagementTransactionIntentsFactory { hash: string; attributes: Uint8Array; uris: string[]; - }): TransactionIntent { + }): DraftTransaction { const dataParts = [ "ESDTNFTCreate", utf8ToHex(options.tokenIdentifier), @@ -382,7 +384,7 @@ export class TokenManagementTransactionIntentsFactory { const nftData = options.name + options.hash + options.attributes + options.uris.join(""); const storageGasLimit = new BigNumber(this.config.gasLimitPerByte).multipliedBy(nftData.length); - return new TransactionIntentBuilder({ + return new DraftTransactionBuilder({ config: this.config, sender: options.sender, receiver: options.sender, @@ -392,16 +394,16 @@ export class TokenManagementTransactionIntentsFactory { }).build(); } - createTransactionIntentForPausing(options: { + createTransactionForPausing(options: { sender: IAddress; tokenIdentifier: string; - }): TransactionIntent { + }): DraftTransaction { const dataParts = [ "pause", utf8ToHex(options.tokenIdentifier) ]; - return new TransactionIntentBuilder({ + return new DraftTransactionBuilder({ config: this.config, sender: options.sender, receiver: options.sender, @@ -411,16 +413,16 @@ export class TokenManagementTransactionIntentsFactory { }).build(); } - createTransactionIntentForUnpausing(options: { + createTransactionForUnpausing(options: { sender: IAddress; tokenIdentifier: string; - }): TransactionIntent { + }): DraftTransaction { const dataParts = [ "unPause", utf8ToHex(options.tokenIdentifier) ]; - return new TransactionIntentBuilder({ + return new DraftTransactionBuilder({ config: this.config, sender: options.sender, receiver: options.sender, @@ -430,18 +432,18 @@ export class TokenManagementTransactionIntentsFactory { }).build(); } - createTransactionIntentForFreezing(options: { + createTransactionForFreezing(options: { sender: IAddress; user: IAddress; tokenIdentifier: string; - }): TransactionIntent { + }): DraftTransaction { const dataParts = [ "freeze", utf8ToHex(options.tokenIdentifier), addressToHex(options.user) ]; - return new TransactionIntentBuilder({ + return new DraftTransactionBuilder({ config: this.config, sender: options.sender, receiver: options.sender, @@ -451,18 +453,18 @@ export class TokenManagementTransactionIntentsFactory { }).build(); } - createTransactionIntentForUnfreezing(options: { + createTransactionForUnfreezing(options: { sender: IAddress; user: IAddress; tokenIdentifier: string; - }): TransactionIntent { + }): DraftTransaction { const dataParts = [ "UnFreeze", utf8ToHex(options.tokenIdentifier), addressToHex(options.user) ]; - return new TransactionIntentBuilder({ + return new DraftTransactionBuilder({ config: this.config, sender: options.sender, receiver: options.sender, @@ -472,18 +474,18 @@ export class TokenManagementTransactionIntentsFactory { }).build(); } - createTransactionIntentForWiping(options: { + createTransactionForWiping(options: { sender: IAddress; user: IAddress; tokenIdentifier: string; - }): TransactionIntent { + }): DraftTransaction { const dataParts = [ "wipe", utf8ToHex(options.tokenIdentifier), addressToHex(options.user) ]; - return new TransactionIntentBuilder({ + return new DraftTransactionBuilder({ config: this.config, sender: options.sender, receiver: options.sender, @@ -493,18 +495,18 @@ export class TokenManagementTransactionIntentsFactory { }).build(); } - createTransactionIntentForLocalMint(options: { + createTransactionForLocalMint(options: { sender: IAddress; tokenIdentifier: string; supplyToMint: BigNumber.Value; - }): TransactionIntent { + }): DraftTransaction { const dataParts = [ "ESDTLocalMint", utf8ToHex(options.tokenIdentifier), bigIntToHex(options.supplyToMint), ]; - return new TransactionIntentBuilder({ + return new DraftTransactionBuilder({ config: this.config, sender: options.sender, receiver: options.sender, @@ -514,18 +516,18 @@ export class TokenManagementTransactionIntentsFactory { }).build(); } - createTransactionIntentForLocalBurning(options: { + createTransactionForLocalBurning(options: { sender: IAddress; tokenIdentifier: string; - supplyToMint: BigNumber.Value; - }): TransactionIntent { + supplyToBurn: BigNumber.Value; + }): DraftTransaction { const dataParts = [ "ESDTLocalBurn", utf8ToHex(options.tokenIdentifier), - bigIntToHex(options.supplyToMint), + bigIntToHex(options.supplyToBurn), ]; - return new TransactionIntentBuilder({ + return new DraftTransactionBuilder({ config: this.config, sender: options.sender, receiver: options.sender, @@ -535,12 +537,12 @@ export class TokenManagementTransactionIntentsFactory { }).build(); } - createTransactionIntentForUpdatingAttributes(options: { + createTransactionForUpdatingAttributes(options: { sender: IAddress; tokenIdentifier: string; tokenNonce: BigNumber.Value; attributes: Uint8Array - }): TransactionIntent { + }): DraftTransaction { const dataParts = [ "ESDTNFTUpdateAttributes", utf8ToHex(options.tokenIdentifier), @@ -548,7 +550,7 @@ export class TokenManagementTransactionIntentsFactory { byteArrayToHex(options.attributes), ]; - return new TransactionIntentBuilder({ + return new DraftTransactionBuilder({ config: this.config, sender: options.sender, receiver: options.sender, @@ -558,12 +560,12 @@ export class TokenManagementTransactionIntentsFactory { }).build(); } - createTransactionIntentForAddingQuantity(options: { + createTransactionForAddingQuantity(options: { sender: IAddress; tokenIdentifier: string; tokenNonce: BigNumber.Value; quantityToAdd: BigNumber.Value - }): TransactionIntent { + }): DraftTransaction { const dataParts = [ "ESDTNFTAddQuantity", utf8ToHex(options.tokenIdentifier), @@ -571,7 +573,7 @@ export class TokenManagementTransactionIntentsFactory { bigIntToHex(options.quantityToAdd) ]; - return new TransactionIntentBuilder({ + return new DraftTransactionBuilder({ config: this.config, sender: options.sender, receiver: options.sender, @@ -581,12 +583,12 @@ export class TokenManagementTransactionIntentsFactory { }).build(); } - createTransactionIntentForBurningQuantity(options: { + createTransactionForBurningQuantity(options: { sender: IAddress; tokenIdentifier: string; tokenNonce: BigNumber.Value; quantityToBurn: BigNumber.Value - }): TransactionIntent { + }): DraftTransaction { const dataParts = [ "ESDTNFTBurn", utf8ToHex(options.tokenIdentifier), @@ -594,7 +596,7 @@ export class TokenManagementTransactionIntentsFactory { bigIntToHex(options.quantityToBurn) ]; - return new TransactionIntentBuilder({ + return new DraftTransactionBuilder({ config: this.config, sender: options.sender, receiver: options.sender, diff --git a/src/transactionIntentsFactories/transactionIntentsFactoryConfig.ts b/src/transactionsFactories/transactionsFactoryConfig.ts similarity index 98% rename from src/transactionIntentsFactories/transactionIntentsFactoryConfig.ts rename to src/transactionsFactories/transactionsFactoryConfig.ts index d8d1c9c9..b3dca70f 100644 --- a/src/transactionIntentsFactories/transactionIntentsFactoryConfig.ts +++ b/src/transactionsFactories/transactionsFactoryConfig.ts @@ -1,7 +1,7 @@ import { BigNumber } from "bignumber.js"; import { DEFAULT_HRP } from "../constants"; -export class TransactionIntentsFactoryConfig { +export class TransactionsFactoryConfig { chainID: string; addressHrp: string; minGasLimit: BigNumber.Value;