diff --git a/package-lock.json b/package-lock.json index 5444c121..fbb2794a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@multiversx/sdk-core", - "version": "11.2.1", + "version": "11.3.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@multiversx/sdk-core", - "version": "11.2.1", + "version": "11.3.0", "license": "MIT", "dependencies": { "@multiversx/sdk-transaction-decoder": "1.0.2", diff --git a/package.json b/package.json index 89bbd6e5..24d7bfc2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@multiversx/sdk-core", - "version": "11.2.1", + "version": "11.3.0", "description": "MultiversX SDK for JavaScript and TypeScript", "main": "out/index.js", "types": "out/index.d.js", diff --git a/src/proto/serializer.spec.ts b/src/proto/serializer.spec.ts index 3ba47bbc..6d2a513b 100644 --- a/src/proto/serializer.spec.ts +++ b/src/proto/serializer.spec.ts @@ -1,12 +1,11 @@ import { assert } from "chai"; -import { ProtoSerializer } from "./serializer"; -import { Transaction } from "../transaction"; -import { loadTestWallets, TestWallet } from "../testutils"; -import { Signature } from "../signature"; import { TransactionVersion } from "../networkParams"; -import { TransactionPayload } from "../transactionPayload"; -import BigNumber from "bignumber.js"; +import { Signature } from "../signature"; +import { loadTestWallets, TestWallet } from "../testutils"; import { TokenPayment } from "../tokenPayment"; +import { Transaction } from "../transaction"; +import { TransactionPayload } from "../transactionPayload"; +import { ProtoSerializer } from "./serializer"; describe("serialize transactions", () => { let wallets: Record; @@ -69,7 +68,7 @@ describe("serialize transactions", () => { it("with data, with large value", async () => { let transaction = new Transaction({ nonce: 92, - value: new BigNumber("123456789000000000000000000000"), + value: "123456789000000000000000000000", sender: wallets.alice.address, receiver: wallets.bob.address, gasLimit: 100000, @@ -86,7 +85,7 @@ describe("serialize transactions", () => { it("with nonce = 0", async () => { let transaction = new Transaction({ nonce: 0, - value: new BigNumber("0"), + value: "0", sender: wallets.alice.address, receiver: wallets.bob.address, gasLimit: 80000, diff --git a/src/transaction.spec.ts b/src/transaction.spec.ts index 4b1fad9b..f6f2f9be 100644 --- a/src/transaction.spec.ts +++ b/src/transaction.spec.ts @@ -1,10 +1,10 @@ -import { assert } from "chai"; import BigNumber from "bignumber.js"; -import { Transaction } from "./transaction"; +import { assert } from "chai"; import { TransactionOptions, TransactionVersion } from "./networkParams"; -import { TransactionPayload } from "./transactionPayload"; import { loadTestWallets, TestWallet } from "./testutils"; import { TokenPayment } from "./tokenPayment"; +import { Transaction } from "./transaction"; +import { TransactionPayload } from "./transactionPayload"; describe("test transaction construction", async () => { @@ -187,8 +187,8 @@ describe("test transaction construction", async () => { const sender = wallets.alice.address; const transaction = new Transaction({ nonce: 90, - value: new BigNumber("1000000000000000000"), - sender: wallets.alice.address, + value: "123456789000000000000000000000", + sender: sender, receiver: wallets.bob.address, gasPrice: minGasPrice, gasLimit: 80000, @@ -200,4 +200,36 @@ describe("test transaction construction", async () => { const restoredTransaction = Transaction.fromPlainObject(plainObject); assert.deepEqual(restoredTransaction, transaction); }); + + it("should handle large values", () => { + const tx1 = new Transaction({ + value: "123456789000000000000000000000", + sender: wallets.alice.address, + receiver: wallets.bob.address, + gasLimit: 50000, + chainID: "local-testnet" + }); + assert.equal(tx1.getValue().toString(), "123456789000000000000000000000"); + + const tx2 = new Transaction({ + value: TokenPayment.egldFromBigInteger("123456789000000000000000000000"), + sender: wallets.alice.address, + receiver: wallets.bob.address, + gasLimit: 50000, + chainID: "local-testnet" + }); + assert.equal(tx2.getValue().toString(), "123456789000000000000000000000"); + + const tx3 = new Transaction({ + // Passing a BigNumber is not recommended. + // However, ITransactionValue interface is permissive, and developers may mistakenly pass such objects as values. + // TokenPayment objects or simple strings (see above) are preferred, instead. + value: new BigNumber("123456789000000000000000000000"), + sender: wallets.alice.address, + receiver: wallets.bob.address, + gasLimit: 50000, + chainID: "local-testnet" + }); + assert.equal(tx3.getValue().toString(), "123456789000000000000000000000"); + }); }); diff --git a/src/transaction.ts b/src/transaction.ts index 9aded117..af639a1e 100644 --- a/src/transaction.ts +++ b/src/transaction.ts @@ -108,7 +108,7 @@ export class Transaction { options?: TransactionOptions; }) { this.nonce = nonce || 0; - this.value = value || 0; + this.value = value ? new BigNumber(value.toString()).toFixed(0) : 0; this.sender = sender; this.receiver = receiver; this.gasPrice = gasPrice || TRANSACTION_MIN_GAS_PRICE; @@ -242,7 +242,7 @@ export class Transaction { static fromPlainObject(plainObjectTransaction: IPlainTransactionObject): Transaction { const tx = new Transaction({ nonce: Number(plainObjectTransaction.nonce), - value: new BigNumber(plainObjectTransaction.value), + value: new BigNumber(plainObjectTransaction.value).toFixed(0), receiver: Address.fromString(plainObjectTransaction.receiver), sender: Address.fromString(plainObjectTransaction.sender), gasPrice: Number(plainObjectTransaction.gasPrice),