From 6bb3ca4a0da2c60116a07fe26494b76750e6ef01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrei=20B=C4=83ncioiu?= Date: Mon, 13 Feb 2023 09:22:25 +0200 Subject: [PATCH 1/7] Configure BigNumber's EXPONENTIAL_AT. --- src/tokenPayment.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tokenPayment.ts b/src/tokenPayment.ts index 5552dbd2..cb2c8bf3 100644 --- a/src/tokenPayment.ts +++ b/src/tokenPayment.ts @@ -5,7 +5,7 @@ const EGLDTokenIdentifier = "EGLD"; const EGLDNumDecimals = 18; // Note: this will actually set the default rounding mode for all BigNumber objects in the environment (in the application / dApp). -BigNumber.set({ ROUNDING_MODE: 1 }); +BigNumber.set({ ROUNDING_MODE: 1, EXPONENTIAL_AT: 128 }); export class TokenPayment { readonly tokenIdentifier: string; From 3cfbdf5f75360f47bf3fc133ae6736a17111220c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrei=20B=C4=83ncioiu?= Date: Mon, 13 Feb 2023 09:23:24 +0200 Subject: [PATCH 2/7] Adjust unit tests, so that BigNumber's aren't passed as value anymore - they were serving as bad examples. --- src/proto/serializer.spec.ts | 15 +++++++-------- src/transaction.spec.ts | 7 +++---- 2 files changed, 10 insertions(+), 12 deletions(-) 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..fb68e5ce 100644 --- a/src/transaction.spec.ts +++ b/src/transaction.spec.ts @@ -1,10 +1,9 @@ import { assert } from "chai"; -import BigNumber from "bignumber.js"; -import { Transaction } from "./transaction"; 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,7 +186,7 @@ describe("test transaction construction", async () => { const sender = wallets.alice.address; const transaction = new Transaction({ nonce: 90, - value: new BigNumber("1000000000000000000"), + value: "123456789000000000000000000000", sender: wallets.alice.address, receiver: wallets.bob.address, gasPrice: minGasPrice, From adeb672eb39f946115d8e29d5e18c8e4bf544ef8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrei=20B=C4=83ncioiu?= Date: Mon, 13 Feb 2023 09:24:23 +0200 Subject: [PATCH 3/7] Handle cases when developers still pass BigNumber objects as values (not recommended). --- src/transaction.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/transaction.ts b/src/transaction.ts index 9aded117..3517c321 100644 --- a/src/transaction.ts +++ b/src/transaction.ts @@ -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), From d4a1c7b6a9f89098dce64cfbbd6ae230d0ced9a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrei=20B=C4=83ncioiu?= Date: Mon, 13 Feb 2023 09:31:56 +0200 Subject: [PATCH 4/7] Handle cases where users pass BigNumber as tx.value. --- src/transaction.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/transaction.ts b/src/transaction.ts index 3517c321..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; From 1e97054da842fa1a068fc6e2fe664a57ec67a6e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrei=20B=C4=83ncioiu?= Date: Mon, 13 Feb 2023 09:56:57 +0200 Subject: [PATCH 5/7] Add more tests. Undo configuring the "EXPONENTIAL_AT". --- src/tokenPayment.ts | 2 +- src/transaction.spec.ts | 36 +++++++++++++++++++++++++++++++++++- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/src/tokenPayment.ts b/src/tokenPayment.ts index cb2c8bf3..5552dbd2 100644 --- a/src/tokenPayment.ts +++ b/src/tokenPayment.ts @@ -5,7 +5,7 @@ const EGLDTokenIdentifier = "EGLD"; const EGLDNumDecimals = 18; // Note: this will actually set the default rounding mode for all BigNumber objects in the environment (in the application / dApp). -BigNumber.set({ ROUNDING_MODE: 1, EXPONENTIAL_AT: 128 }); +BigNumber.set({ ROUNDING_MODE: 1 }); export class TokenPayment { readonly tokenIdentifier: string; diff --git a/src/transaction.spec.ts b/src/transaction.spec.ts index fb68e5ce..2a170bcd 100644 --- a/src/transaction.spec.ts +++ b/src/transaction.spec.ts @@ -1,3 +1,4 @@ +import BigNumber from "bignumber.js"; import { assert } from "chai"; import { TransactionOptions, TransactionVersion } from "./networkParams"; import { loadTestWallets, TestWallet } from "./testutils"; @@ -187,7 +188,7 @@ describe("test transaction construction", async () => { const transaction = new Transaction({ nonce: 90, value: "123456789000000000000000000000", - sender: wallets.alice.address, + sender: sender, receiver: wallets.bob.address, gasPrice: minGasPrice, gasLimit: 80000, @@ -199,4 +200,37 @@ 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"); + }); + }); From 29db4a387752c37449a814f99351e81f6923b58c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrei=20B=C4=83ncioiu?= Date: Mon, 13 Feb 2023 10:14:02 +0200 Subject: [PATCH 6/7] Formatting. --- src/transaction.spec.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/transaction.spec.ts b/src/transaction.spec.ts index 2a170bcd..f6f2f9be 100644 --- a/src/transaction.spec.ts +++ b/src/transaction.spec.ts @@ -232,5 +232,4 @@ describe("test transaction construction", async () => { }); assert.equal(tx3.getValue().toString(), "123456789000000000000000000000"); }); - }); From 859f5a26140fbd305c3a5c91abec4c932ce863f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrei=20B=C4=83ncioiu?= Date: Mon, 13 Feb 2023 10:27:41 +0200 Subject: [PATCH 7/7] Bump version. --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) 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",