Skip to content

Commit

Permalink
Merge pull request #257 from multiversx/large-value-13
Browse files Browse the repository at this point in the history
Handle passing BigNumber as transaction values (not recommeded)
  • Loading branch information
andreibancioiu authored Feb 13, 2023
2 parents 37a4188 + 859f5a2 commit 5c72787
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 18 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
15 changes: 7 additions & 8 deletions src/proto/serializer.spec.ts
Original file line number Diff line number Diff line change
@@ -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<string, TestWallet>;
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down
42 changes: 37 additions & 5 deletions src/transaction.spec.ts
Original file line number Diff line number Diff line change
@@ -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 () => {
Expand Down Expand Up @@ -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,
Expand All @@ -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");
});
});
4 changes: 2 additions & 2 deletions src/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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),
Expand Down

0 comments on commit 5c72787

Please sign in to comment.