From 0388311a786fade048b409b649c8855eee7229fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrei=20B=C4=83ncioiu?= Date: Sun, 24 Mar 2024 20:42:17 +0200 Subject: [PATCH 1/5] Adjust constructor of the new "TokenTransfer" class. --- src/tokens.ts | 6 +++--- src/transaction.local.net.spec.ts | 2 +- .../smartContractTransactionsFactory.spec.ts | 12 ++++++------ .../transferTransactionsFactory.spec.ts | 8 ++++---- 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/tokens.ts b/src/tokens.ts index 361800ac..342eda2f 100644 --- a/src/tokens.ts +++ b/src/tokens.ts @@ -14,9 +14,9 @@ export class NextTokenTransfer { token: Token; amount: bigint; - constructor(token: Token, amount: bigint) { - this.token = token; - this.amount = amount; + constructor(options: { token: Token; amount: bigint }) { + this.token = options.token; + this.amount = options.amount; } } diff --git a/src/transaction.local.net.spec.ts b/src/transaction.local.net.spec.ts index e7d76718..d7aa0c56 100644 --- a/src/transaction.local.net.spec.ts +++ b/src/transaction.local.net.spec.ts @@ -140,7 +140,7 @@ describe("test transaction", function () { Logger.trace(JSON.stringify(await provider.simulateTransaction(transactionTwo), null, 4)); }); - it("should create transaction using the NextTokenTransferFactory", async function () { + it("should create transaction using the TokenTransferFactory", async function () { this.timeout(70000); const provider = createLocalnetProvider(); diff --git a/src/transactionsFactories/smartContractTransactionsFactory.spec.ts b/src/transactionsFactories/smartContractTransactionsFactory.spec.ts index 74fb1803..6fcf9b87 100644 --- a/src/transactionsFactories/smartContractTransactionsFactory.spec.ts +++ b/src/transactionsFactories/smartContractTransactionsFactory.spec.ts @@ -152,7 +152,7 @@ describe("test smart contract transactions factory", function () { const gasLimit = 6000000n; const args = [new U32Value(7)]; const token = new Token("FOO-6ce17b", 0n); - const transfer = new NextTokenTransfer(token, 10n); + const transfer = new NextTokenTransfer({ token, amount: 10n }); const transaction = smartContractFactory.createTransactionForExecute({ sender: sender, @@ -189,9 +189,9 @@ describe("test smart contract transactions factory", function () { const args = [new U32Value(7)]; const fooToken = new Token("FOO-6ce17b", 0n); - const fooTransfer = new NextTokenTransfer(fooToken, 10n); + const fooTransfer = new NextTokenTransfer({ token: fooToken, amount: 10n }); const barToken = new Token("BAR-5bc08f", 0n); - const barTransfer = new NextTokenTransfer(barToken, 3140n); + const barTransfer = new NextTokenTransfer({ token: barToken, amount: 3140n }); const transaction = smartContractFactory.createTransactionForExecute({ sender: sender, @@ -235,7 +235,7 @@ describe("test smart contract transactions factory", function () { const args = [new U32Value(7)]; const token = new Token("NFT-123456", 1n); - const transfer = new NextTokenTransfer(token, 1n); + const transfer = new NextTokenTransfer({ token, amount: 1n }); const transaction = smartContractFactory.createTransactionForExecute({ sender: sender, @@ -280,9 +280,9 @@ describe("test smart contract transactions factory", function () { const args = [new U32Value(7)]; const firstToken = new Token("NFT-123456", 1n); - const firstTransfer = new NextTokenTransfer(firstToken, 1n); + const firstTransfer = new NextTokenTransfer({ token: firstToken, amount: 1n }); const secondToken = new Token("NFT-123456", 42n); - const secondTransfer = new NextTokenTransfer(secondToken, 1n); + const secondTransfer = new NextTokenTransfer({ token: secondToken, amount: 1n }); const transaction = smartContractFactory.createTransactionForExecute({ sender: sender, diff --git a/src/transactionsFactories/transferTransactionsFactory.spec.ts b/src/transactionsFactories/transferTransactionsFactory.spec.ts index 78af0890..240c2cc1 100644 --- a/src/transactionsFactories/transferTransactionsFactory.spec.ts +++ b/src/transactionsFactories/transferTransactionsFactory.spec.ts @@ -62,7 +62,7 @@ describe("test transfer transcations factory", function () { it("should create 'Transaction' for esdt transfer", async () => { const fooToken = new Token("FOO-123456", 0n); - const transfer = new NextTokenTransfer(fooToken, 1000000n); + const transfer = new NextTokenTransfer({ token: fooToken, amount: 1000000n }); const transaction = transferFactory.createTransactionForESDTTokenTransfer({ sender: alice, @@ -79,7 +79,7 @@ describe("test transfer transcations factory", function () { it("should create 'Transaction' for nft transfer", async () => { const nft = new Token("NFT-123456", 10n); - const transfer = new NextTokenTransfer(nft, 1n); + const transfer = new NextTokenTransfer({ token: nft, amount: 1n }); const transaction = transferFactory.createTransactionForESDTTokenTransfer({ sender: alice, @@ -99,10 +99,10 @@ describe("test transfer transcations factory", function () { it("should create 'Transaction' for multiple nft transfers", async () => { const firstNft = new Token("NFT-123456", 10n); - const firstTransfer = new NextTokenTransfer(firstNft, 1n); + const firstTransfer = new NextTokenTransfer({ token: firstNft, amount: 1n }); const secondNft = new Token("TEST-987654", 1n); - const secondTransfer = new NextTokenTransfer(secondNft, 1n); + const secondTransfer = new NextTokenTransfer({ token: secondNft, amount: 1n }); const transaction = transferFactory.createTransactionForESDTTokenTransfer({ sender: alice, From 3d82525969fc92572843f958b7c8d75c0dd3c3d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrei=20B=C4=83ncioiu?= Date: Sun, 24 Mar 2024 20:46:02 +0200 Subject: [PATCH 2/5] Minor constraints on fields: readonly. --- src/tokens.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/tokens.ts b/src/tokens.ts index 342eda2f..6a5e3ef3 100644 --- a/src/tokens.ts +++ b/src/tokens.ts @@ -1,8 +1,8 @@ import { ErrInvalidTokenIdentifier } from "./errors"; export class Token { - identifier: string; - nonce: bigint; + readonly identifier: string; + readonly nonce: bigint; constructor(identifier: string, nonce: bigint) { this.identifier = identifier; @@ -11,8 +11,8 @@ export class Token { } export class NextTokenTransfer { - token: Token; - amount: bigint; + readonly token: Token; + readonly amount: bigint; constructor(options: { token: Token; amount: bigint }) { this.token = options.token; From 72da628204b2c8a2ad712d28fc845caf8a47f6fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrei=20B=C4=83ncioiu?= Date: Sun, 24 Mar 2024 20:54:48 +0200 Subject: [PATCH 3/5] Adjust constructor. Use "options" pattern. --- src/tokens.spec.ts | 4 ++-- src/tokens.ts | 6 +++--- .../smartContractTransactionsFactory.spec.ts | 12 ++++++------ .../transferTransactionsFactory.spec.ts | 8 ++++---- 4 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/tokens.spec.ts b/src/tokens.spec.ts index e7949b56..09341765 100644 --- a/src/tokens.spec.ts +++ b/src/tokens.spec.ts @@ -5,8 +5,8 @@ describe("test token computer", async () => { const tokenComputer = new TokenComputer(); it("should test if token is fungible", async () => { - const fungibleToken = new Token("TEST-123456", 0n); - const nonFungibleToken = new Token("NFT-987654", 7n); + const fungibleToken = new Token({ identifier: "TEST-123456", nonce: 0n }); + const nonFungibleToken = new Token({ identifier: "NFT-987654", nonce: 7n }); assert.equal(tokenComputer.isFungible(fungibleToken), true); assert.equal(tokenComputer.isFungible(nonFungibleToken), false); diff --git a/src/tokens.ts b/src/tokens.ts index 6a5e3ef3..d3bab272 100644 --- a/src/tokens.ts +++ b/src/tokens.ts @@ -4,9 +4,9 @@ export class Token { readonly identifier: string; readonly nonce: bigint; - constructor(identifier: string, nonce: bigint) { - this.identifier = identifier; - this.nonce = nonce; + constructor(options: { identifier: string; nonce: bigint }) { + this.identifier = options.identifier; + this.nonce = options.nonce; } } diff --git a/src/transactionsFactories/smartContractTransactionsFactory.spec.ts b/src/transactionsFactories/smartContractTransactionsFactory.spec.ts index 6fcf9b87..cc301787 100644 --- a/src/transactionsFactories/smartContractTransactionsFactory.spec.ts +++ b/src/transactionsFactories/smartContractTransactionsFactory.spec.ts @@ -151,7 +151,7 @@ describe("test smart contract transactions factory", function () { const func = "add"; const gasLimit = 6000000n; const args = [new U32Value(7)]; - const token = new Token("FOO-6ce17b", 0n); + const token = new Token({ identifier: "FOO-6ce17b", nonce: 0n }); const transfer = new NextTokenTransfer({ token, amount: 10n }); const transaction = smartContractFactory.createTransactionForExecute({ @@ -188,9 +188,9 @@ describe("test smart contract transactions factory", function () { const gasLimit = 6000000n; const args = [new U32Value(7)]; - const fooToken = new Token("FOO-6ce17b", 0n); + const fooToken = new Token({ identifier: "FOO-6ce17b", nonce: 0n }); const fooTransfer = new NextTokenTransfer({ token: fooToken, amount: 10n }); - const barToken = new Token("BAR-5bc08f", 0n); + const barToken = new Token({ identifier: "BAR-5bc08f", nonce: 0n }); const barTransfer = new NextTokenTransfer({ token: barToken, amount: 3140n }); const transaction = smartContractFactory.createTransactionForExecute({ @@ -234,7 +234,7 @@ describe("test smart contract transactions factory", function () { const gasLimit = 6000000n; const args = [new U32Value(7)]; - const token = new Token("NFT-123456", 1n); + const token = new Token({ identifier: "NFT-123456", nonce: 1n }); const transfer = new NextTokenTransfer({ token, amount: 1n }); const transaction = smartContractFactory.createTransactionForExecute({ @@ -279,9 +279,9 @@ describe("test smart contract transactions factory", function () { const gasLimit = 6000000n; const args = [new U32Value(7)]; - const firstToken = new Token("NFT-123456", 1n); + const firstToken = new Token({ identifier: "NFT-123456", nonce: 1n }); const firstTransfer = new NextTokenTransfer({ token: firstToken, amount: 1n }); - const secondToken = new Token("NFT-123456", 42n); + const secondToken = new Token({ identifier: "NFT-123456", nonce: 42n }); const secondTransfer = new NextTokenTransfer({ token: secondToken, amount: 1n }); const transaction = smartContractFactory.createTransactionForExecute({ diff --git a/src/transactionsFactories/transferTransactionsFactory.spec.ts b/src/transactionsFactories/transferTransactionsFactory.spec.ts index 240c2cc1..93f7002f 100644 --- a/src/transactionsFactories/transferTransactionsFactory.spec.ts +++ b/src/transactionsFactories/transferTransactionsFactory.spec.ts @@ -61,7 +61,7 @@ describe("test transfer transcations factory", function () { }); it("should create 'Transaction' for esdt transfer", async () => { - const fooToken = new Token("FOO-123456", 0n); + const fooToken = new Token({ identifier: "FOO-123456", nonce: 0n }); const transfer = new NextTokenTransfer({ token: fooToken, amount: 1000000n }); const transaction = transferFactory.createTransactionForESDTTokenTransfer({ @@ -78,7 +78,7 @@ describe("test transfer transcations factory", function () { }); it("should create 'Transaction' for nft transfer", async () => { - const nft = new Token("NFT-123456", 10n); + const nft = new Token({ identifier: "NFT-123456", nonce: 10n }); const transfer = new NextTokenTransfer({ token: nft, amount: 1n }); const transaction = transferFactory.createTransactionForESDTTokenTransfer({ @@ -98,10 +98,10 @@ describe("test transfer transcations factory", function () { }); it("should create 'Transaction' for multiple nft transfers", async () => { - const firstNft = new Token("NFT-123456", 10n); + const firstNft = new Token({ identifier: "NFT-123456", nonce: 10n }); const firstTransfer = new NextTokenTransfer({ token: firstNft, amount: 1n }); - const secondNft = new Token("TEST-987654", 1n); + const secondNft = new Token({ identifier: "TEST-987654", nonce: 1n }); const secondTransfer = new NextTokenTransfer({ token: secondNft, amount: 1n }); const transaction = transferFactory.createTransactionForESDTTokenTransfer({ From 0ec447f1c55fd5672750f010276f2f9ff3e3a8e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrei=20B=C4=83ncioiu?= Date: Sun, 24 Mar 2024 21:29:24 +0200 Subject: [PATCH 4/5] Merge legacy "TokenTransfer" into "NextTokenTransfer". --- src/index.ts | 3 +- src/proto/serializer.spec.ts | 2 +- src/relayedTransactionV1Builder.spec.ts | 2 +- src/smartcontracts/interaction.spec.ts | 2 +- src/smartcontracts/interactionChecker.spec.ts | 2 +- src/tokenTransfer.spec.ts | 66 ------ src/tokenTransfer.ts | 141 ----------- src/tokenTransferBuilders.spec.ts | 2 +- src/tokenTransferBuilders.ts | 2 +- src/tokens.spec.ts | 68 +++++- src/tokens.ts | 219 +++++++++++++++++- src/transaction.local.net.spec.ts | 2 +- src/transaction.spec.ts | 2 +- .../smartContractTransactionsFactory.spec.ts | 14 +- .../smartContractTransactionsFactory.ts | 4 +- .../tokenTransfersDataBuilder.ts | 8 +- .../transferTransactionsFactory.spec.ts | 10 +- .../transferTransactionsFactory.ts | 6 +- src/transferTransactionsFactory.spec.ts | 2 +- 19 files changed, 311 insertions(+), 246 deletions(-) delete mode 100644 src/tokenTransfer.spec.ts delete mode 100644 src/tokenTransfer.ts diff --git a/src/index.ts b/src/index.ts index 3043ba6c..0ce32cab 100644 --- a/src/index.ts +++ b/src/index.ts @@ -14,10 +14,9 @@ export * from "./relayedTransactionV2Builder"; export * from "./signableMessage"; export * from "./smartcontracts"; export * from "./tokenOperations"; -export * from "./tokenTransfer"; export * from "./tokens"; export * from "./transaction"; +export * from "./transactionComputer"; export * from "./transactionPayload"; export * from "./transactionWatcher"; export * from "./utils"; -export * from "./transactionComputer"; diff --git a/src/proto/serializer.spec.ts b/src/proto/serializer.spec.ts index 174f8af2..f92acf0e 100644 --- a/src/proto/serializer.spec.ts +++ b/src/proto/serializer.spec.ts @@ -3,7 +3,7 @@ import { Address } from "../address"; import { TransactionVersion } from "../networkParams"; import { Signature } from "../signature"; import { loadTestWallets, TestWallet } from "../testutils"; -import { TokenTransfer } from "../tokenTransfer"; +import { TokenTransfer } from "../tokens"; import { Transaction } from "../transaction"; import { TransactionPayload } from "../transactionPayload"; import { ProtoSerializer } from "./serializer"; diff --git a/src/relayedTransactionV1Builder.spec.ts b/src/relayedTransactionV1Builder.spec.ts index aeb5a060..55bbc9c0 100644 --- a/src/relayedTransactionV1Builder.spec.ts +++ b/src/relayedTransactionV1Builder.spec.ts @@ -4,7 +4,7 @@ import * as errors from "./errors"; import { TransactionOptions, TransactionVersion } from "./networkParams"; import { RelayedTransactionV1Builder } from "./relayedTransactionV1Builder"; import { TestWallet, loadTestWallets } from "./testutils"; -import { TokenTransfer } from "./tokenTransfer"; +import { TokenTransfer } from "./tokens"; import { Transaction } from "./transaction"; import { TransactionPayload } from "./transactionPayload"; diff --git a/src/smartcontracts/interaction.spec.ts b/src/smartcontracts/interaction.spec.ts index fbad250e..b005ddf1 100644 --- a/src/smartcontracts/interaction.spec.ts +++ b/src/smartcontracts/interaction.spec.ts @@ -10,7 +10,7 @@ import { TestWallet } from "../testutils"; import { ContractController } from "../testutils/contractController"; -import { TokenTransfer } from "../tokenTransfer"; +import { TokenTransfer } from "../tokens"; import { ContractFunction } from "./function"; import { Interaction } from "./interaction"; import { ReturnCode } from "./returnCode"; diff --git a/src/smartcontracts/interactionChecker.spec.ts b/src/smartcontracts/interactionChecker.spec.ts index 157ba290..00f51117 100644 --- a/src/smartcontracts/interactionChecker.spec.ts +++ b/src/smartcontracts/interactionChecker.spec.ts @@ -2,7 +2,7 @@ import { assert } from "chai"; import { Address } from "../address"; import * as errors from "../errors"; import { loadAbiRegistry } from "../testutils"; -import { TokenTransfer } from "../tokenTransfer"; +import { TokenTransfer } from "../tokens"; import { Interaction } from "./interaction"; import { InteractionChecker } from "./interactionChecker"; import { SmartContract } from "./smartContract"; diff --git a/src/tokenTransfer.spec.ts b/src/tokenTransfer.spec.ts deleted file mode 100644 index 274121a7..00000000 --- a/src/tokenTransfer.spec.ts +++ /dev/null @@ -1,66 +0,0 @@ -import { assert } from "chai"; -import { TokenTransfer } from "./tokenTransfer"; - -describe("test token transfer", () => { - it("should work with EGLD", () => { - assert.equal(TokenTransfer.egldFromAmount("1").toString(), "1000000000000000000"); - assert.equal(TokenTransfer.egldFromAmount("10").toString(), "10000000000000000000"); - assert.equal(TokenTransfer.egldFromAmount("100").toString(), "100000000000000000000"); - assert.equal(TokenTransfer.egldFromAmount("1000").toString(), "1000000000000000000000"); - assert.equal(TokenTransfer.egldFromAmount("0.1").toString(), "100000000000000000"); - assert.equal(TokenTransfer.egldFromAmount("0.123456789").toString(), "123456789000000000"); - assert.equal(TokenTransfer.egldFromAmount("0.123456789123456789").toString(), "123456789123456789"); - assert.equal(TokenTransfer.egldFromAmount("0.123456789123456789777").toString(), "123456789123456789"); - assert.equal(TokenTransfer.egldFromAmount("0.123456789123456789777777888888").toString(), "123456789123456789"); - - assert.equal(TokenTransfer.egldFromAmount(0.1).toPrettyString(), "0.100000000000000000 EGLD"); - assert.equal(TokenTransfer.egldFromAmount(1).toPrettyString(), "1.000000000000000000 EGLD"); - assert.equal(TokenTransfer.egldFromAmount(10).toPrettyString(), "10.000000000000000000 EGLD"); - assert.equal(TokenTransfer.egldFromAmount(100).toPrettyString(), "100.000000000000000000 EGLD"); - assert.equal(TokenTransfer.egldFromAmount(1000).toPrettyString(), "1000.000000000000000000 EGLD"); - assert.equal(TokenTransfer.egldFromAmount("0.123456789").toPrettyString(), "0.123456789000000000 EGLD"); - assert.equal( - TokenTransfer.egldFromAmount("0.123456789123456789777777888888").toPrettyString(), - "0.123456789123456789 EGLD", - ); - - assert.equal(TokenTransfer.egldFromBigInteger("1").toString(), "1"); - assert.equal(TokenTransfer.egldFromBigInteger("1").toPrettyString(), "0.000000000000000001 EGLD"); - assert.isTrue(TokenTransfer.egldFromAmount("1").isEgld()); - }); - - it("should work with USDC", () => { - const identifier = "USDC-c76f1f"; - const numDecimals = 6; - - assert.equal(TokenTransfer.fungibleFromAmount(identifier, "1", numDecimals).toString(), "1000000"); - assert.equal(TokenTransfer.fungibleFromAmount(identifier, "0.1", numDecimals).toString(), "100000"); - assert.equal(TokenTransfer.fungibleFromAmount(identifier, "0.123456789", numDecimals).toString(), "123456"); - assert.equal(TokenTransfer.fungibleFromBigInteger(identifier, "1000000", numDecimals).toString(), "1000000"); - assert.equal( - TokenTransfer.fungibleFromBigInteger(identifier, "1000000", numDecimals).toPrettyString(), - "1.000000 USDC-c76f1f", - ); - }); - - it("should work with MetaESDT", () => { - const identifier = "MEXFARML-28d646"; - const numDecimals = 18; - const nonce = 12345678; - const transfer = TokenTransfer.metaEsdtFromAmount(identifier, nonce, "0.1", numDecimals); - - assert.equal(transfer.tokenIdentifier, identifier); - assert.equal(transfer.nonce, nonce); - assert.equal(transfer.toString(), "100000000000000000"); - }); - - it("should work with NFTs", () => { - const identifier = "TEST-38f249"; - const nonce = 1; - const transfer = TokenTransfer.nonFungible(identifier, nonce); - - assert.equal(transfer.tokenIdentifier, identifier); - assert.equal(transfer.nonce, nonce); - assert.equal(transfer.toPrettyString(), "1 TEST-38f249"); - }); -}); diff --git a/src/tokenTransfer.ts b/src/tokenTransfer.ts deleted file mode 100644 index ab81d89a..00000000 --- a/src/tokenTransfer.ts +++ /dev/null @@ -1,141 +0,0 @@ -import BigNumber from "bignumber.js"; -import { ErrInvalidArgument } from "./errors"; - -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 }); - -interface ITokenTransferOptions { - tokenIdentifier: string; - nonce: number; - amountAsBigInteger: BigNumber.Value; - numDecimals?: number; -} - -export class TokenTransfer { - readonly tokenIdentifier: string; - readonly nonce: number; - readonly amountAsBigInteger: BigNumber; - readonly numDecimals: number; - - public constructor(options: ITokenTransferOptions) { - const amount = new BigNumber(options.amountAsBigInteger); - if (!amount.isInteger() || amount.isNegative()) { - throw new ErrInvalidArgument(`bad amountAsBigInteger: ${options.amountAsBigInteger}`); - } - - this.tokenIdentifier = options.tokenIdentifier; - this.nonce = options.nonce; - this.amountAsBigInteger = amount; - this.numDecimals = options.numDecimals || 0; - } - - static egldFromAmount(amount: BigNumber.Value) { - const amountAsBigInteger = new BigNumber(amount).shiftedBy(EGLDNumDecimals).decimalPlaces(0); - return this.egldFromBigInteger(amountAsBigInteger); - } - - static egldFromBigInteger(amountAsBigInteger: BigNumber.Value) { - return new TokenTransfer({ - tokenIdentifier: EGLDTokenIdentifier, - nonce: 0, - amountAsBigInteger, - numDecimals: EGLDNumDecimals, - }); - } - - static fungibleFromAmount(tokenIdentifier: string, amount: BigNumber.Value, numDecimals: number) { - const amountAsBigInteger = new BigNumber(amount).shiftedBy(numDecimals).decimalPlaces(0); - return this.fungibleFromBigInteger(tokenIdentifier, amountAsBigInteger, numDecimals); - } - - static fungibleFromBigInteger( - tokenIdentifier: string, - amountAsBigInteger: BigNumber.Value, - numDecimals: number = 0, - ) { - return new TokenTransfer({ - tokenIdentifier, - nonce: 0, - amountAsBigInteger, - numDecimals, - }); - } - - static nonFungible(tokenIdentifier: string, nonce: number) { - return new TokenTransfer({ - tokenIdentifier, - nonce, - amountAsBigInteger: 1, - numDecimals: 0, - }); - } - - static semiFungible(tokenIdentifier: string, nonce: number, quantity: number) { - return new TokenTransfer({ - tokenIdentifier, - nonce, - amountAsBigInteger: quantity, - numDecimals: 0, - }); - } - - static metaEsdtFromAmount(tokenIdentifier: string, nonce: number, amount: BigNumber.Value, numDecimals: number) { - const amountAsBigInteger = new BigNumber(amount).shiftedBy(numDecimals).decimalPlaces(0); - return this.metaEsdtFromBigInteger(tokenIdentifier, nonce, amountAsBigInteger, numDecimals); - } - - static metaEsdtFromBigInteger( - tokenIdentifier: string, - nonce: number, - amountAsBigInteger: BigNumber.Value, - numDecimals = 0, - ) { - return new TokenTransfer({ - tokenIdentifier, - nonce, - amountAsBigInteger, - numDecimals, - }); - } - - toString() { - return this.amountAsBigInteger.toFixed(0); - } - - valueOf(): BigNumber { - return this.amountAsBigInteger; - } - - toPrettyString(): string { - return `${this.toAmount()} ${this.tokenIdentifier}`; - } - - private toAmount(): string { - return this.amountAsBigInteger.shiftedBy(-this.numDecimals).toFixed(this.numDecimals); - } - - isEgld(): boolean { - return this.tokenIdentifier == EGLDTokenIdentifier; - } - - isFungible(): boolean { - return this.nonce == 0; - } -} - -/** - * @deprecated use {@link TokenTransfer} instead. - */ -export class TokenPayment extends TokenTransfer { - constructor(tokenIdentifier: string, nonce: number, amountAsBigInteger: BigNumber.Value, numDecimals: number) { - super({ - tokenIdentifier, - nonce, - amountAsBigInteger, - numDecimals, - }); - } -} diff --git a/src/tokenTransferBuilders.spec.ts b/src/tokenTransferBuilders.spec.ts index abddcb37..e3fd02ad 100644 --- a/src/tokenTransferBuilders.spec.ts +++ b/src/tokenTransferBuilders.spec.ts @@ -1,6 +1,6 @@ import { assert } from "chai"; import { Address } from "./address"; -import { TokenTransfer } from "./tokenTransfer"; +import { TokenTransfer } from "./tokens"; import { ESDTNFTTransferPayloadBuilder, ESDTTransferPayloadBuilder, diff --git a/src/tokenTransferBuilders.ts b/src/tokenTransferBuilders.ts index edc47641..1c6f7009 100644 --- a/src/tokenTransferBuilders.ts +++ b/src/tokenTransferBuilders.ts @@ -2,7 +2,7 @@ import { Address } from "./address"; import { IAddress, ITokenTransfer } from "./interface"; import { ArgSerializer } from "./smartcontracts/argSerializer"; import { AddressValue, BigUIntValue, BytesValue, TypedValue, U16Value, U64Value } from "./smartcontracts/typesystem"; -import { TokenTransfer } from "./tokenTransfer"; +import { TokenTransfer } from "./tokens"; import { TransactionPayload } from "./transactionPayload"; /** diff --git a/src/tokens.spec.ts b/src/tokens.spec.ts index 09341765..f3214b11 100644 --- a/src/tokens.spec.ts +++ b/src/tokens.spec.ts @@ -1,7 +1,7 @@ import { assert } from "chai"; -import { Token, TokenComputer } from "./tokens"; +import { Token, TokenComputer, TokenTransfer } from "./tokens"; -describe("test token computer", async () => { +describe("test tokens and token computer", async () => { const tokenComputer = new TokenComputer(); it("should test if token is fungible", async () => { @@ -32,3 +32,67 @@ describe("test token computer", async () => { assert.equal(identifier, "FNG-123456"); }); }); + +describe("test token transfer (legacy)", () => { + it("should work with EGLD", () => { + assert.equal(TokenTransfer.egldFromAmount("1").toString(), "1000000000000000000"); + assert.equal(TokenTransfer.egldFromAmount("10").toString(), "10000000000000000000"); + assert.equal(TokenTransfer.egldFromAmount("100").toString(), "100000000000000000000"); + assert.equal(TokenTransfer.egldFromAmount("1000").toString(), "1000000000000000000000"); + assert.equal(TokenTransfer.egldFromAmount("0.1").toString(), "100000000000000000"); + assert.equal(TokenTransfer.egldFromAmount("0.123456789").toString(), "123456789000000000"); + assert.equal(TokenTransfer.egldFromAmount("0.123456789123456789").toString(), "123456789123456789"); + assert.equal(TokenTransfer.egldFromAmount("0.123456789123456789777").toString(), "123456789123456789"); + assert.equal(TokenTransfer.egldFromAmount("0.123456789123456789777777888888").toString(), "123456789123456789"); + + assert.equal(TokenTransfer.egldFromAmount(0.1).toPrettyString(), "0.100000000000000000 EGLD"); + assert.equal(TokenTransfer.egldFromAmount(1).toPrettyString(), "1.000000000000000000 EGLD"); + assert.equal(TokenTransfer.egldFromAmount(10).toPrettyString(), "10.000000000000000000 EGLD"); + assert.equal(TokenTransfer.egldFromAmount(100).toPrettyString(), "100.000000000000000000 EGLD"); + assert.equal(TokenTransfer.egldFromAmount(1000).toPrettyString(), "1000.000000000000000000 EGLD"); + assert.equal(TokenTransfer.egldFromAmount("0.123456789").toPrettyString(), "0.123456789000000000 EGLD"); + assert.equal( + TokenTransfer.egldFromAmount("0.123456789123456789777777888888").toPrettyString(), + "0.123456789123456789 EGLD", + ); + + assert.equal(TokenTransfer.egldFromBigInteger("1").toString(), "1"); + assert.equal(TokenTransfer.egldFromBigInteger("1").toPrettyString(), "0.000000000000000001 EGLD"); + assert.isTrue(TokenTransfer.egldFromAmount("1").isEgld()); + }); + + it("should work with USDC (legacy)", () => { + const identifier = "USDC-c76f1f"; + const numDecimals = 6; + + assert.equal(TokenTransfer.fungibleFromAmount(identifier, "1", numDecimals).toString(), "1000000"); + assert.equal(TokenTransfer.fungibleFromAmount(identifier, "0.1", numDecimals).toString(), "100000"); + assert.equal(TokenTransfer.fungibleFromAmount(identifier, "0.123456789", numDecimals).toString(), "123456"); + assert.equal(TokenTransfer.fungibleFromBigInteger(identifier, "1000000", numDecimals).toString(), "1000000"); + assert.equal( + TokenTransfer.fungibleFromBigInteger(identifier, "1000000", numDecimals).toPrettyString(), + "1.000000 USDC-c76f1f", + ); + }); + + it("should work with MetaESDT (legacy)", () => { + const identifier = "MEXFARML-28d646"; + const numDecimals = 18; + const nonce = 12345678; + const transfer = TokenTransfer.metaEsdtFromAmount(identifier, nonce, "0.1", numDecimals); + + assert.equal(transfer.tokenIdentifier, identifier); + assert.equal(transfer.nonce, nonce); + assert.equal(transfer.toString(), "100000000000000000"); + }); + + it("should work with NFTs (legacy)", () => { + const identifier = "TEST-38f249"; + const nonce = 1; + const transfer = TokenTransfer.nonFungible(identifier, nonce); + + assert.equal(transfer.tokenIdentifier, identifier); + assert.equal(transfer.nonce, nonce); + assert.equal(transfer.toPrettyString(), "1 TEST-38f249"); + }); +}); diff --git a/src/tokens.ts b/src/tokens.ts index d3bab272..d1195c3f 100644 --- a/src/tokens.ts +++ b/src/tokens.ts @@ -1,4 +1,20 @@ -import { ErrInvalidTokenIdentifier } from "./errors"; +import BigNumber from "bignumber.js"; +import { ErrInvalidArgument, ErrInvalidTokenIdentifier } from "./errors"; + +// Legacy constants: +const EGLDTokenIdentifier = "EGLD"; +const EGLDNumDecimals = 18; + +// Legacy configuration. +// 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 }); + +interface ILegacyTokenTransferOptions { + tokenIdentifier: string; + nonce: number; + amountAsBigInteger: BigNumber.Value; + numDecimals?: number; +} export class Token { readonly identifier: string; @@ -10,13 +26,192 @@ export class Token { } } -export class NextTokenTransfer { +export class TokenTransfer { readonly token: Token; readonly amount: bigint; - constructor(options: { token: Token; amount: bigint }) { - this.token = options.token; - this.amount = options.amount; + /** + * Legacy field. Use "token.identifier" instead. + */ + readonly tokenIdentifier: string; + + /** + * Legacy field. Use "token.nonce" instead. + */ + readonly nonce: number; + + /** + * Legacy field. Use "amount" instead. + */ + readonly amountAsBigInteger: BigNumber; + + /** + * Legacy field. The number of decimals is not a concern of "sdk-core". + * For formatting and parsing amounts, use "sdk-dapp" or "bignumber.js" directly. + */ + readonly numDecimals: number; + + constructor(options: { token: Token; amount: bigint } | ILegacyTokenTransferOptions) { + if (this.isLegacyTokenTransferOptions(options)) { + const amount = new BigNumber(options.amountAsBigInteger); + if (!amount.isInteger() || amount.isNegative()) { + throw new ErrInvalidArgument(`bad amountAsBigInteger: ${options.amountAsBigInteger}`); + } + + this.tokenIdentifier = options.tokenIdentifier; + this.nonce = options.nonce; + this.amountAsBigInteger = amount; + this.numDecimals = options.numDecimals || 0; + + this.token = new Token({ + identifier: options.tokenIdentifier, + nonce: BigInt(options.nonce), + }); + + this.amount = BigInt(this.amountAsBigInteger.toFixed(0)); + } else { + this.token = options.token; + this.amount = options.amount; + + this.tokenIdentifier = options.token.identifier; + this.nonce = Number(options.token.nonce); + this.amountAsBigInteger = new BigNumber(this.amount.toString()); + this.numDecimals = 0; + } + } + + private isLegacyTokenTransferOptions(options: any): options is ILegacyTokenTransferOptions { + return options.tokenIdentifier !== undefined; + } + + /** + * Legacy function. Use the constructor instead: new TokenTransfer({ token, amount }); + */ + static egldFromAmount(amount: BigNumber.Value) { + const amountAsBigInteger = new BigNumber(amount).shiftedBy(EGLDNumDecimals).decimalPlaces(0); + return this.egldFromBigInteger(amountAsBigInteger); + } + + /** + * Legacy function. Use the constructor instead: new TokenTransfer({ token, amount }); + */ + static egldFromBigInteger(amountAsBigInteger: BigNumber.Value) { + return new TokenTransfer({ + tokenIdentifier: EGLDTokenIdentifier, + nonce: 0, + amountAsBigInteger, + numDecimals: EGLDNumDecimals, + }); + } + + /** + * Legacy function. Use the constructor instead: new TokenTransfer({ token, amount }); + */ + static fungibleFromAmount(tokenIdentifier: string, amount: BigNumber.Value, numDecimals: number) { + const amountAsBigInteger = new BigNumber(amount).shiftedBy(numDecimals).decimalPlaces(0); + return this.fungibleFromBigInteger(tokenIdentifier, amountAsBigInteger, numDecimals); + } + + /** + * Legacy function. Use the constructor instead: new TokenTransfer({ token, amount }); + */ + static fungibleFromBigInteger( + tokenIdentifier: string, + amountAsBigInteger: BigNumber.Value, + numDecimals: number = 0, + ) { + return new TokenTransfer({ + tokenIdentifier, + nonce: 0, + amountAsBigInteger, + numDecimals, + }); + } + + /** + * Legacy function. Use the constructor instead: new TokenTransfer({ token, amount }); + */ + static nonFungible(tokenIdentifier: string, nonce: number) { + return new TokenTransfer({ + tokenIdentifier, + nonce, + amountAsBigInteger: 1, + numDecimals: 0, + }); + } + + /** + * Legacy function. Use the constructor instead: new TokenTransfer({ token, amount }); + */ + static semiFungible(tokenIdentifier: string, nonce: number, quantity: number) { + return new TokenTransfer({ + tokenIdentifier, + nonce, + amountAsBigInteger: quantity, + numDecimals: 0, + }); + } + + /** + * Legacy function. Use the constructor instead: new TokenTransfer({ token, amount }); + */ + static metaEsdtFromAmount(tokenIdentifier: string, nonce: number, amount: BigNumber.Value, numDecimals: number) { + const amountAsBigInteger = new BigNumber(amount).shiftedBy(numDecimals).decimalPlaces(0); + return this.metaEsdtFromBigInteger(tokenIdentifier, nonce, amountAsBigInteger, numDecimals); + } + + /** + * Legacy function. Use the constructor instead: new TokenTransfer({ token, amount }); + */ + static metaEsdtFromBigInteger( + tokenIdentifier: string, + nonce: number, + amountAsBigInteger: BigNumber.Value, + numDecimals = 0, + ) { + return new TokenTransfer({ + tokenIdentifier, + nonce, + amountAsBigInteger, + numDecimals, + }); + } + + toString() { + return this.amount.toString(); + } + + /** + * Legacy function. Use the "amount" field instead. + */ + valueOf(): BigNumber { + return new BigNumber(this.amount.toString()); + } + + /** + * Legacy function. For formatting and parsing amounts, use "sdk-dapp" or "bignumber.js" directly. + */ + toPrettyString(): string { + return `${this.toAmount()} ${this.tokenIdentifier}`; + } + + private toAmount(): string { + return this.amountAsBigInteger.shiftedBy(-this.numDecimals).toFixed(this.numDecimals); + } + + /** + * Legacy function. Within your code, don't mix native values (EGLD) and custom (ESDT) tokens. + * See "TransferTransactionsFactory.createTransactionForNativeTokenTransfer()" vs. "TransferTransactionsFactory.createTransactionForESDTTokenTransfer()". + */ + isEgld(): boolean { + return this.token.identifier == EGLDTokenIdentifier; + } + + /** + * Legacy function. Use "TokenComputer.isFungible(token)" instead. + */ + isFungible(): boolean { + return this.token.nonce == 0n; } } @@ -99,3 +294,17 @@ export class TokenComputer { function decodeUnsignedNumber(arg: Buffer): number { return arg.readUIntBE(0, arg.length); } + +/** + * @deprecated use {@link TokenTransfer} instead. + */ +export class TokenPayment extends TokenTransfer { + constructor(tokenIdentifier: string, nonce: number, amountAsBigInteger: BigNumber.Value, numDecimals: number) { + super({ + tokenIdentifier, + nonce, + amountAsBigInteger, + numDecimals, + }); + } +} diff --git a/src/transaction.local.net.spec.ts b/src/transaction.local.net.spec.ts index d7aa0c56..502d6be0 100644 --- a/src/transaction.local.net.spec.ts +++ b/src/transaction.local.net.spec.ts @@ -3,7 +3,7 @@ import { assert } from "chai"; import { Logger } from "./logger"; import { loadTestWallets, TestWallet } from "./testutils"; import { createLocalnetProvider } from "./testutils/networkProviders"; -import { TokenTransfer } from "./tokenTransfer"; +import { TokenTransfer } from "./tokens"; import { Transaction } from "./transaction"; import { TransactionPayload } from "./transactionPayload"; import { TransactionWatcher } from "./transactionWatcher"; diff --git a/src/transaction.spec.ts b/src/transaction.spec.ts index 74a28151..37679de7 100644 --- a/src/transaction.spec.ts +++ b/src/transaction.spec.ts @@ -5,7 +5,7 @@ import { MIN_TRANSACTION_VERSION_THAT_SUPPORTS_OPTIONS } from "./constants"; import { TransactionOptions, TransactionVersion } from "./networkParams"; import { ProtoSerializer } from "./proto"; import { TestWallet, loadTestWallets } from "./testutils"; -import { TokenTransfer } from "./tokenTransfer"; +import { TokenTransfer } from "./tokens"; import { Transaction } from "./transaction"; import { TransactionComputer } from "./transactionComputer"; import { TransactionPayload } from "./transactionPayload"; diff --git a/src/transactionsFactories/smartContractTransactionsFactory.spec.ts b/src/transactionsFactories/smartContractTransactionsFactory.spec.ts index cc301787..f7f440f6 100644 --- a/src/transactionsFactories/smartContractTransactionsFactory.spec.ts +++ b/src/transactionsFactories/smartContractTransactionsFactory.spec.ts @@ -6,7 +6,7 @@ import { U32Value } from "../smartcontracts"; import { Code } from "../smartcontracts/code"; import { AbiRegistry } from "../smartcontracts/typesystem/abiRegistry"; import { loadAbiRegistry, loadContractCode } from "../testutils/utils"; -import { NextTokenTransfer, Token, TokenComputer } from "../tokens"; +import { Token, TokenComputer, TokenTransfer } from "../tokens"; import { SmartContractTransactionsFactory } from "./smartContractTransactionsFactory"; import { TransactionsFactoryConfig } from "./transactionsFactoryConfig"; @@ -152,7 +152,7 @@ describe("test smart contract transactions factory", function () { const gasLimit = 6000000n; const args = [new U32Value(7)]; const token = new Token({ identifier: "FOO-6ce17b", nonce: 0n }); - const transfer = new NextTokenTransfer({ token, amount: 10n }); + const transfer = new TokenTransfer({ token, amount: 10n }); const transaction = smartContractFactory.createTransactionForExecute({ sender: sender, @@ -189,9 +189,9 @@ describe("test smart contract transactions factory", function () { const args = [new U32Value(7)]; const fooToken = new Token({ identifier: "FOO-6ce17b", nonce: 0n }); - const fooTransfer = new NextTokenTransfer({ token: fooToken, amount: 10n }); + const fooTransfer = new TokenTransfer({ token: fooToken, amount: 10n }); const barToken = new Token({ identifier: "BAR-5bc08f", nonce: 0n }); - const barTransfer = new NextTokenTransfer({ token: barToken, amount: 3140n }); + const barTransfer = new TokenTransfer({ token: barToken, amount: 3140n }); const transaction = smartContractFactory.createTransactionForExecute({ sender: sender, @@ -235,7 +235,7 @@ describe("test smart contract transactions factory", function () { const args = [new U32Value(7)]; const token = new Token({ identifier: "NFT-123456", nonce: 1n }); - const transfer = new NextTokenTransfer({ token, amount: 1n }); + const transfer = new TokenTransfer({ token, amount: 1n }); const transaction = smartContractFactory.createTransactionForExecute({ sender: sender, @@ -280,9 +280,9 @@ describe("test smart contract transactions factory", function () { const args = [new U32Value(7)]; const firstToken = new Token({ identifier: "NFT-123456", nonce: 1n }); - const firstTransfer = new NextTokenTransfer({ token: firstToken, amount: 1n }); + const firstTransfer = new TokenTransfer({ token: firstToken, amount: 1n }); const secondToken = new Token({ identifier: "NFT-123456", nonce: 42n }); - const secondTransfer = new NextTokenTransfer({ token: secondToken, amount: 1n }); + const secondTransfer = new TokenTransfer({ token: secondToken, amount: 1n }); const transaction = smartContractFactory.createTransactionForExecute({ sender: sender, diff --git a/src/transactionsFactories/smartContractTransactionsFactory.ts b/src/transactionsFactories/smartContractTransactionsFactory.ts index c2e4efbd..6f95273b 100644 --- a/src/transactionsFactories/smartContractTransactionsFactory.ts +++ b/src/transactionsFactories/smartContractTransactionsFactory.ts @@ -4,7 +4,7 @@ import { Err, ErrBadUsage } from "../errors"; import { IAddress } from "../interface"; import { ArgSerializer, CodeMetadata, ContractFunction, EndpointDefinition } from "../smartcontracts"; import { NativeSerializer } from "../smartcontracts/nativeSerializer"; -import { NextTokenTransfer, Token } from "../tokens"; +import { Token, TokenTransfer } from "../tokens"; import { Transaction } from "../transaction"; import { byteArrayToHex, utf8ToHex } from "../utils.codec"; import { TokenTransfersDataBuilder } from "./tokenTransfersDataBuilder"; @@ -82,7 +82,7 @@ export class SmartContractTransactionsFactory { gasLimit: bigint; args?: any[]; nativeTransferAmount?: bigint; - tokenTransfers?: NextTokenTransfer[]; + tokenTransfers?: TokenTransfer[]; }): Transaction { const args = options.args || []; const tokenTransfer = options.tokenTransfers || []; diff --git a/src/transactionsFactories/tokenTransfersDataBuilder.ts b/src/transactionsFactories/tokenTransfersDataBuilder.ts index 593f9275..fbbf07f6 100644 --- a/src/transactionsFactories/tokenTransfersDataBuilder.ts +++ b/src/transactionsFactories/tokenTransfersDataBuilder.ts @@ -1,5 +1,5 @@ import { IAddress } from "../interface"; -import { NextTokenTransfer, TokenComputer } from "../tokens"; +import { TokenComputer, TokenTransfer } from "../tokens"; import { addressToHex, numberToPaddedHex, utf8ToHex } from "../utils.codec"; export class TokenTransfersDataBuilder { @@ -9,13 +9,13 @@ export class TokenTransfersDataBuilder { this.tokenComputer = new TokenComputer(); } - buildArgsForESDTTransfer(transfer: NextTokenTransfer): string[] { + buildArgsForESDTTransfer(transfer: TokenTransfer): string[] { let args = ["ESDTTransfer"]; args.push(...[utf8ToHex(transfer.token.identifier), numberToPaddedHex(transfer.amount)]); return args; } - buildArgsForSingleESDTNFTTransfer(transfer: NextTokenTransfer, receiver: IAddress) { + buildArgsForSingleESDTNFTTransfer(transfer: TokenTransfer, receiver: IAddress) { let args = ["ESDTNFTTransfer"]; const token = transfer.token; @@ -32,7 +32,7 @@ export class TokenTransfersDataBuilder { return args; } - buildArgsForMultiESDTNFTTransfer(receiver: IAddress, transfers: NextTokenTransfer[]) { + buildArgsForMultiESDTNFTTransfer(receiver: IAddress, transfers: TokenTransfer[]) { let args = ["MultiESDTNFTTransfer", addressToHex(receiver), numberToPaddedHex(transfers.length)]; for (let transfer of transfers) { diff --git a/src/transactionsFactories/transferTransactionsFactory.spec.ts b/src/transactionsFactories/transferTransactionsFactory.spec.ts index 93f7002f..ab7f676e 100644 --- a/src/transactionsFactories/transferTransactionsFactory.spec.ts +++ b/src/transactionsFactories/transferTransactionsFactory.spec.ts @@ -1,7 +1,7 @@ import { assert } from "chai"; import { Address } from "../address"; import { ErrBadUsage } from "../errors"; -import { NextTokenTransfer, Token, TokenComputer } from "../tokens"; +import { Token, TokenComputer, TokenTransfer } from "../tokens"; import { TransactionsFactoryConfig } from "./transactionsFactoryConfig"; import { TransferTransactionsFactory } from "./transferTransactionsFactory"; @@ -62,7 +62,7 @@ describe("test transfer transcations factory", function () { it("should create 'Transaction' for esdt transfer", async () => { const fooToken = new Token({ identifier: "FOO-123456", nonce: 0n }); - const transfer = new NextTokenTransfer({ token: fooToken, amount: 1000000n }); + const transfer = new TokenTransfer({ token: fooToken, amount: 1000000n }); const transaction = transferFactory.createTransactionForESDTTokenTransfer({ sender: alice, @@ -79,7 +79,7 @@ describe("test transfer transcations factory", function () { it("should create 'Transaction' for nft transfer", async () => { const nft = new Token({ identifier: "NFT-123456", nonce: 10n }); - const transfer = new NextTokenTransfer({ token: nft, amount: 1n }); + const transfer = new TokenTransfer({ token: nft, amount: 1n }); const transaction = transferFactory.createTransactionForESDTTokenTransfer({ sender: alice, @@ -99,10 +99,10 @@ describe("test transfer transcations factory", function () { it("should create 'Transaction' for multiple nft transfers", async () => { const firstNft = new Token({ identifier: "NFT-123456", nonce: 10n }); - const firstTransfer = new NextTokenTransfer({ token: firstNft, amount: 1n }); + const firstTransfer = new TokenTransfer({ token: firstNft, amount: 1n }); const secondNft = new Token({ identifier: "TEST-987654", nonce: 1n }); - const secondTransfer = new NextTokenTransfer({ token: secondNft, amount: 1n }); + const secondTransfer = new TokenTransfer({ token: secondNft, amount: 1n }); const transaction = transferFactory.createTransactionForESDTTokenTransfer({ sender: alice, diff --git a/src/transactionsFactories/transferTransactionsFactory.ts b/src/transactionsFactories/transferTransactionsFactory.ts index 5d897346..f0cc7d93 100644 --- a/src/transactionsFactories/transferTransactionsFactory.ts +++ b/src/transactionsFactories/transferTransactionsFactory.ts @@ -18,7 +18,7 @@ import { U16Value, U64Value, } from "../smartcontracts"; -import { NextTokenTransfer, Token } from "../tokens"; +import { Token, TokenTransfer } from "../tokens"; import { Transaction } from "../transaction"; import { TransactionPayload } from "../transactionPayload"; import { TokenTransfersDataBuilder } from "./tokenTransfersDataBuilder"; @@ -124,7 +124,7 @@ export class TransferTransactionsFactory { createTransactionForESDTTokenTransfer(options: { sender: IAddress; receiver: IAddress; - tokenTransfers: NextTokenTransfer[]; + tokenTransfers: TokenTransfer[]; }): Transaction { this.ensureMembersAreDefined(); @@ -339,7 +339,7 @@ export class TransferTransactionsFactory { private createSingleESDTTransferTransaction(options: { sender: IAddress; receiver: IAddress; - tokenTransfers: NextTokenTransfer[]; + tokenTransfers: TokenTransfer[]; }): Transaction { this.ensureMembersAreDefined(); diff --git a/src/transferTransactionsFactory.spec.ts b/src/transferTransactionsFactory.spec.ts index abc64867..66b91fe1 100644 --- a/src/transferTransactionsFactory.spec.ts +++ b/src/transferTransactionsFactory.spec.ts @@ -1,7 +1,7 @@ import { assert } from "chai"; import { Address } from "./address"; import { GasEstimator } from "./gasEstimator"; -import { TokenTransfer } from "./tokenTransfer"; +import { TokenTransfer } from "./tokens"; import { TransactionPayload } from "./transactionPayload"; import { TransferTransactionsFactory } from "./transactionsFactories/transferTransactionsFactory"; From 9773a3a4f1f0bd57ef67fb5e39305fca42f1ce8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrei=20B=C4=83ncioiu?= Date: Sun, 24 Mar 2024 23:46:13 +0200 Subject: [PATCH 5/5] Fix after self review. --- src/tokens.ts | 4 ++++ src/transactionsOutcomeParsers/index.ts | 4 +++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/tokens.ts b/src/tokens.ts index d1195c3f..07e082b2 100644 --- a/src/tokens.ts +++ b/src/tokens.ts @@ -53,6 +53,7 @@ export class TokenTransfer { constructor(options: { token: Token; amount: bigint } | ILegacyTokenTransferOptions) { if (this.isLegacyTokenTransferOptions(options)) { + // Handle legacy fields. const amount = new BigNumber(options.amountAsBigInteger); if (!amount.isInteger() || amount.isNegative()) { throw new ErrInvalidArgument(`bad amountAsBigInteger: ${options.amountAsBigInteger}`); @@ -63,6 +64,7 @@ export class TokenTransfer { this.amountAsBigInteger = amount; this.numDecimals = options.numDecimals || 0; + // Handle new fields. this.token = new Token({ identifier: options.tokenIdentifier, nonce: BigInt(options.nonce), @@ -70,9 +72,11 @@ export class TokenTransfer { this.amount = BigInt(this.amountAsBigInteger.toFixed(0)); } else { + // Handle new fields. this.token = options.token; this.amount = options.amount; + // Handle legacy fields. this.tokenIdentifier = options.token.identifier; this.nonce = Number(options.token.nonce); this.amountAsBigInteger = new BigNumber(this.amount.toString()); diff --git a/src/transactionsOutcomeParsers/index.ts b/src/transactionsOutcomeParsers/index.ts index e95e9836..b50f8ba9 100644 --- a/src/transactionsOutcomeParsers/index.ts +++ b/src/transactionsOutcomeParsers/index.ts @@ -1,3 +1,5 @@ +export * from "./delegationTransactionsOutcomeParser"; export * from "./resources"; +export * from "./smartContractTransactionsOutcomeParser"; export * from "./tokenManagementTransactionsOutcomeParser"; -export * from "./delegationTransactionsOutcomeParser"; +export * from "./transactionEventsParser";