Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Update transaction factories to match the sdk-specs #339

Merged
merged 7 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/transactionIntent.ts → src/draftTransaction.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BigNumber } from "bignumber.js";

export class TransactionIntent {
export class DraftTransaction {
CiprianDraghici marked this conversation as resolved.
Show resolved Hide resolved
public sender: string;
public receiver: string;
public gasLimit: BigNumber.Value;
Expand Down
22 changes: 11 additions & 11 deletions src/smartcontracts/smartContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ 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";
const createKeccakHash = require("keccak");

Expand Down Expand Up @@ -115,16 +115,16 @@ 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 scIntentFactory = new SmartContractTransactionsFactory({
config: config,
abi: this.abi
});

const bytecode = Buffer.from(code.toString(), 'hex');
const metadataAsJson = this.getMetadataPropertiesAsObject(codeMetadata);

const intent = scIntentFactory.createTransactionIntentForDeploy({
const intent = scIntentFactory.createTransactionForDeploy({
sender: deployer,
bytecode: bytecode,
gasLimit: gasLimit.valueOf(),
Expand Down Expand Up @@ -177,16 +177,16 @@ export class SmartContract implements ISmartContract {

this.ensureHasAddress();

const config = new TransactionIntentsFactoryConfig(chainID.valueOf());
const scIntentFactory = new SmartContractTransactionIntentsFactory({
const config = new TransactionsFactoryConfig(chainID.valueOf());
Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps we do not need chain ID on the factory config?

const scIntentFactory = new SmartContractTransactionsFactory({
config: config,
abi: this.abi
});

const bytecode = Uint8Array.from(Buffer.from(code.toString(), 'hex'));
const metadataAsJson = this.getMetadataPropertiesAsObject(codeMetadata);

const intent = scIntentFactory.createTransactionIntentForUpgrade({
const intent = scIntentFactory.createTransactionForUpgrade({
Copy link

@CiprianDraghici CiprianDraghici Oct 17, 2023

Choose a reason for hiding this comment

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

Transaction.fromDraft function could be used here. Try to see where else can be used

sender: caller,
contract: this.getAddress(),
bytecode: bytecode,
Expand Down Expand Up @@ -217,16 +217,16 @@ export class SmartContract implements ISmartContract {

this.ensureHasAddress();

const config = new TransactionIntentsFactoryConfig(chainID.valueOf());
const scIntentFactory = new SmartContractTransactionIntentsFactory({
const config = new TransactionsFactoryConfig(chainID.valueOf());
const scIntentFactory = new SmartContractTransactionsFactory({
config: config,
abi: this.abi
});

args = args || [];
value = value || 0;

const intent = scIntentFactory.createTransactionIntentForExecute({
const intent = scIntentFactory.createTransactionForExecute({
sender: caller,
contractAddress: receiver ? receiver : this.getAddress(),
functionName: func.toString(),
Expand Down
19 changes: 19 additions & 0 deletions src/transaction.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand All @@ -17,6 +18,24 @@ describe("test transaction construction", async () => {
wallets = await loadTestWallets();
});

it.only("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(), "");
Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps also add asserts for the other empty fields (e.g. nonce, gas price, signature etc.)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

added new asserts.

});

it("with no data, no value", async () => {
let transaction = new Transaction({
nonce: 89,
Expand Down
15 changes: 15 additions & 0 deletions src/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -152,6 +153,20 @@ export class Transaction {
this.hash = TransactionHash.empty();
}

/**
* Creates a new Transaction object from a DraftTransaction.
*/
Comment on lines +156 to +158
Copy link
Contributor

Choose a reason for hiding this comment

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

Formatting / indentation.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed it

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: "",
Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, this is a bit unfortunate at this moment (since chainID is required on the Transaction).

value: draft.value ?? 0,
data: draft.data ? new TransactionPayload(Buffer.from(draft.data)) : new TransactionPayload()
Copy link
Contributor

Choose a reason for hiding this comment

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

new TransactionPayload() accepts buffers, as well.

})
}

getNonce(): INonce {
return this.nonce;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
import BigNumber from "bignumber.js";
import { Address } from "../address";
import { DelegationTransactionIntentsFactory } from "./delegationTransactionIntentsFactory";
import { DelegationTransactionsFactory } from "./delegationTransactionsFactory";
import { assert } from "chai";
import { DELEGATION_MANAGER_SC_ADDRESS } from "../constants";
import { ValidatorPublicKey } from "@multiversx/sdk-wallet-next";
import { TransactionIntentsFactoryConfig } from "./transactionIntentsFactoryConfig";
import { TransactionsFactoryConfig } from "./transactionsFactoryConfig";

describe("test delegation intents factory", function () {
const config = new TransactionIntentsFactoryConfig("D");
const delegationFactory = new DelegationTransactionIntentsFactory(config);
describe("test delegation transactions factory", function () {
const config = new TransactionsFactoryConfig("D");
const delegationFactory = new DelegationTransactionsFactory(config);

it("should create intent for new delegation contract", async function () {
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 intent = delegationFactory.createTransactionIntentForNewDelegationContract({
const intent = delegationFactory.createTransactionForNewDelegationContract({
sender: sender,
totalDelegationCap: delagationCap,
serviceFee: serviceFee,
value: value
amount: value
});

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

it("should create intent for adding nodes", async function () {
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"));
Expand All @@ -40,7 +40,7 @@ describe("test delegation intents factory", function () {
getSignature: () => Buffer.from("81109fa1c8d3dc7b6c2d6e65206cc0bc1a83c9b2d1eb91a601d66ad32def430827d5eb52917bd2b0d04ce195738db216", "hex")
}

const intent = delegationFactory.createTransactionIntentForAddingNodes({
const intent = delegationFactory.createTransactionForAddingNodes({
sender: sender,
delegationContract: delegationContract,
publicKeys: [publicKey],
Expand All @@ -54,7 +54,7 @@ describe("test delegation intents factory", function () {
assert.equal(intent.value, 0);
});

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

Expand All @@ -64,7 +64,7 @@ describe("test delegation intents factory", function () {
}
};

const intent = delegationFactory.createTransactionIntentForRemovingNodes({
const intent = delegationFactory.createTransactionForRemovingNodes({
sender: sender,
delegationContract: delegationContract,
publicKeys: [publicKey]
Expand All @@ -77,7 +77,7 @@ describe("test delegation intents factory", function () {
assert.equal(intent.value, 0);
});

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

Expand All @@ -87,7 +87,7 @@ describe("test delegation intents factory", function () {
}
};

const intent = delegationFactory.createTransactionIntentForStakingNodes({
const intent = delegationFactory.createTransactionForStakingNodes({
sender: sender,
delegationContract: delegationContract,
publicKeys: [publicKey]
Expand All @@ -100,7 +100,7 @@ describe("test delegation intents factory", function () {
assert.equal(intent.value, 0);
});

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

Expand All @@ -110,7 +110,7 @@ describe("test delegation intents factory", function () {
}
};

const intent = delegationFactory.createTransactionIntentForUnbondingNodes({
const intent = delegationFactory.createTransactionForUnbondingNodes({
sender: sender,
delegationContract: delegationContract,
publicKeys: [publicKey]
Expand All @@ -124,7 +124,7 @@ describe("test delegation intents factory", function () {
assert.equal(intent.gasLimit.valueOf(), 12080000);
});

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

Expand All @@ -134,7 +134,7 @@ describe("test delegation intents factory", function () {
}
};

const intent = delegationFactory.createTransactionIntentForUnstakingNodes({
const intent = delegationFactory.createTransactionForUnstakingNodes({
sender: sender,
delegationContract: delegationContract,
publicKeys: [publicKey]
Expand All @@ -148,7 +148,7 @@ describe("test delegation intents factory", function () {
assert.equal(intent.gasLimit.valueOf(), 12081500);
});

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

Expand All @@ -158,7 +158,7 @@ describe("test delegation intents factory", function () {
}
};

const intent = delegationFactory.createTransactionIntentForUnjailingNodes({
const intent = delegationFactory.createTransactionForUnjailingNodes({
sender: sender,
delegationContract: delegationContract,
publicKeys: [publicKey]
Expand All @@ -171,12 +171,12 @@ describe("test delegation intents factory", function () {
assert.equal(intent.value, 0);
});

it("should create intent for changing service fee", async function () {
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 intent = delegationFactory.createTransactionIntentForChangingServiceFee({
const intent = delegationFactory.createTransactionForChangingServiceFee({
sender: sender,
delegationContract: delegationContract,
serviceFee: serviceFee
Expand All @@ -189,12 +189,12 @@ describe("test delegation intents factory", function () {
assert.equal(intent.value, 0);
});

it("should create intent for changing delegation cap", async function () {
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 intent = delegationFactory.createTransactionIntentForModifyingDelegationCap({
const intent = delegationFactory.createTransactionForModifyingDelegationCap({
sender: sender,
delegationContract: delegationContract,
delegationCap: delegationCap
Expand All @@ -207,11 +207,11 @@ describe("test delegation intents factory", function () {
assert.equal(intent.value, 0);
});

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

const intent = delegationFactory.createTransactionIntentForSettingAutomaticActivation({
const intent = delegationFactory.createTransactionForSettingAutomaticActivation({
sender: sender,
delegationContract: delegationContract
});
Expand All @@ -223,11 +223,11 @@ describe("test delegation intents factory", function () {
assert.equal(intent.value, 0);
});

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

const intent = delegationFactory.createTransactionIntentForUnsettingAutomaticActivation({
const intent = delegationFactory.createTransactionForUnsettingAutomaticActivation({
sender: sender,
delegationContract: delegationContract
});
Expand All @@ -239,11 +239,11 @@ describe("test delegation intents factory", function () {
assert.equal(intent.value, 0);
});

it("should create intent for setting cap check on redelegate rewards", async function () {
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 intent = delegationFactory.createTransactionIntentForSettingCapCheckOnRedelegateRewards({
const intent = delegationFactory.createTransactionForSettingCapCheckOnRedelegateRewards({
sender: sender,
delegationContract: delegationContract
});
Expand All @@ -255,11 +255,11 @@ describe("test delegation intents factory", function () {
assert.equal(intent.value, 0);
});

it("should create intent for unsetting cap check on redelegate rewards", async function () {
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 intent = delegationFactory.createTransactionIntentForUnsettingCapCheckOnRedelegateRewards({
const intent = delegationFactory.createTransactionForUnsettingCapCheckOnRedelegateRewards({
sender: sender,
delegationContract: delegationContract
});
Expand All @@ -271,11 +271,11 @@ describe("test delegation intents factory", function () {
assert.equal(intent.value, 0);
});

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

const intent = delegationFactory.createTransactionIntentForSettingMetadata({
const intent = delegationFactory.createTransactionForSettingMetadata({
sender: sender,
delegationContract: delegationContract,
name: "name",
Expand Down
Loading
Loading