diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 5881ae129..26d36b49b 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -5,7 +5,6 @@ name: Build and Test on: pull_request: - branches: [ main, development, feat/*, release/* ] workflow_dispatch: jobs: diff --git a/src/abi/interaction.local.net.spec.ts b/src/abi/interaction.local.net.spec.ts index 68d119c79..2af44644e 100644 --- a/src/abi/interaction.local.net.spec.ts +++ b/src/abi/interaction.local.net.spec.ts @@ -9,7 +9,7 @@ import { ContractController } from "../testutils/contractController"; import { createLocalnetProvider } from "../testutils/networkProviders"; import { Transaction } from "../transaction"; import { TransactionComputer } from "../transactionComputer"; -import { TransactionsFactoryConfig } from "../transactionsFactories"; +import { TransactionsFactoryConfig } from "../transactionsFactoryConfig"; import { TransactionWatcher } from "../transactionWatcher"; import { Interaction } from "./interaction"; import { ResultsParser } from "./resultsParser"; diff --git a/src/abi/interaction.ts b/src/abi/interaction.ts index 8519308d7..0cca6ae47 100644 --- a/src/abi/interaction.ts +++ b/src/abi/interaction.ts @@ -6,7 +6,7 @@ import { IAddress, IChainID, IGasLimit, IGasPrice, INonce, ITokenTransfer, ITran import { SmartContractTransactionsFactory } from "../smartContracts"; import { TokenTransfer } from "../tokens"; import { Transaction } from "../transaction"; -import { TransactionsFactoryConfig } from "../transactionsFactories"; +import { TransactionsFactoryConfig } from "../transactionsFactoryConfig"; import { ContractFunction } from "./function"; import { InteractionChecker } from "./interactionChecker"; import { CallArguments } from "./interface"; diff --git a/src/abi/smartContract.local.net.spec.ts b/src/abi/smartContract.local.net.spec.ts index 1bde9a611..9266c0eae 100644 --- a/src/abi/smartContract.local.net.spec.ts +++ b/src/abi/smartContract.local.net.spec.ts @@ -8,7 +8,7 @@ import { prepareDeployment } from "../testutils"; import { createLocalnetProvider } from "../testutils/networkProviders"; import { loadTestWallets, TestWallet } from "../testutils/wallets"; import { TransactionComputer } from "../transactionComputer"; -import { TransactionsFactoryConfig } from "../transactionsFactories"; +import { TransactionsFactoryConfig } from "../transactionsFactoryConfig"; import { TransactionWatcher } from "../transactionWatcher"; import { decodeUnsignedNumber } from "./codec"; import { ContractFunction } from "./function"; diff --git a/src/abi/smartContract.ts b/src/abi/smartContract.ts index ff8122bf4..6b1b4db08 100644 --- a/src/abi/smartContract.ts +++ b/src/abi/smartContract.ts @@ -4,7 +4,7 @@ import { TRANSACTION_MIN_GAS_PRICE } from "../constants"; import { ErrContractHasNoAddress } from "../errors"; import { INonce } from "../interface"; import { Transaction } from "../transaction"; -import { TransactionsFactoryConfig } from "../transactionsFactories/transactionsFactoryConfig"; +import { TransactionsFactoryConfig } from "../transactionsFactoryConfig"; import { guardValueIsSet } from "../utils"; import { SmartContractTransactionsFactory } from "./../smartContracts/smartContractTransactionsFactory"; import { CodeMetadata } from "./codeMetadata"; diff --git a/src/abi/smartContractResults.local.net.spec.ts b/src/abi/smartContractResults.local.net.spec.ts index 5f74c6809..f9e5c1005 100644 --- a/src/abi/smartContractResults.local.net.spec.ts +++ b/src/abi/smartContractResults.local.net.spec.ts @@ -4,7 +4,7 @@ import { SmartContractTransactionsFactory } from "../smartContracts"; import { loadTestWallets, prepareDeployment, TestWallet } from "../testutils"; import { createLocalnetProvider } from "../testutils/networkProviders"; import { TransactionComputer } from "../transactionComputer"; -import { TransactionsFactoryConfig } from "../transactionsFactories/transactionsFactoryConfig"; +import { TransactionsFactoryConfig } from "../transactionsFactoryConfig"; import { TransactionWatcher } from "../transactionWatcher"; import { ContractFunction } from "./function"; import { ResultsParser } from "./resultsParser"; diff --git a/src/accountManagement/accountController.ts b/src/accountManagement/accountController.ts index 91ca55150..a73b92b8f 100644 --- a/src/accountManagement/accountController.ts +++ b/src/accountManagement/accountController.ts @@ -1,7 +1,7 @@ import { IAccount } from "../accounts/interfaces"; import { Transaction } from "../transaction"; import { TransactionComputer } from "../transactionComputer"; -import { TransactionsFactoryConfig } from "../transactionsFactories"; +import { TransactionsFactoryConfig } from "../transactionsFactoryConfig"; import { AccountTransactionsFactory } from "./accountTransactionsFactory"; import { SaveKeyValueInput, SetGuardianInput } from "./resources"; diff --git a/src/accountManagement/accountTransactionsFactory.spec.ts b/src/accountManagement/accountTransactionsFactory.spec.ts index 78660dd6f..77ce1a041 100644 --- a/src/accountManagement/accountTransactionsFactory.spec.ts +++ b/src/accountManagement/accountTransactionsFactory.spec.ts @@ -1,6 +1,6 @@ import { assert } from "chai"; import { Address } from "../address"; -import { TransactionsFactoryConfig } from "../transactionsFactories"; +import { TransactionsFactoryConfig } from "../transactionsFactoryConfig"; import { AccountTransactionsFactory } from "./accountTransactionsFactory"; describe("test account transactions factory", function () { diff --git a/src/accountManagement/accountTransactionsFactory.ts b/src/accountManagement/accountTransactionsFactory.ts index f9bbc5a16..8fdeed7db 100644 --- a/src/accountManagement/accountTransactionsFactory.ts +++ b/src/accountManagement/accountTransactionsFactory.ts @@ -1,7 +1,7 @@ import { Address } from "../address"; import { IAddress } from "../interface"; import { Transaction } from "../transaction"; -import { TransactionBuilder } from "../transactionsFactories/transactionBuilder"; +import { TransactionBuilder } from "../transactionBuilder"; import { SaveKeyValueInput, SetGuardianInput } from "./resources"; interface IConfig { diff --git a/src/delegation/delegationController.ts b/src/delegation/delegationController.ts index e8ad9c787..01ebbef58 100644 --- a/src/delegation/delegationController.ts +++ b/src/delegation/delegationController.ts @@ -3,7 +3,7 @@ import { ITransactionOnNetwork } from "../interfaceOfNetwork"; import { INetworkProvider } from "../networkProviders/interface"; import { Transaction } from "../transaction"; import { TransactionComputer } from "../transactionComputer"; -import { TransactionsFactoryConfig } from "../transactionsFactories"; +import { TransactionsFactoryConfig } from "../transactionsFactoryConfig"; import { TransactionWatcher } from "../transactionWatcher"; import { DelegationTransactionsFactory } from "./delegationTransactionsFactory"; import { DelegationTransactionsOutcomeParser } from "./delegationTransactionsOutcomeParser"; diff --git a/src/delegation/delegationTransactionsFactory.spec.ts b/src/delegation/delegationTransactionsFactory.spec.ts index 67f21a49e..6a9e53f88 100644 --- a/src/delegation/delegationTransactionsFactory.spec.ts +++ b/src/delegation/delegationTransactionsFactory.spec.ts @@ -1,7 +1,7 @@ import { assert } from "chai"; import { Address } from "../address"; import { DELEGATION_MANAGER_SC_ADDRESS_HEX } from "../constants"; -import { TransactionsFactoryConfig } from "../transactionsFactories"; +import { TransactionsFactoryConfig } from "../transactionsFactoryConfig"; import { ValidatorPublicKey } from "../wallet"; import { DelegationTransactionsFactory } from "./delegationTransactionsFactory"; diff --git a/src/delegation/delegationTransactionsFactory.ts b/src/delegation/delegationTransactionsFactory.ts index 193521827..2c5f94374 100644 --- a/src/delegation/delegationTransactionsFactory.ts +++ b/src/delegation/delegationTransactionsFactory.ts @@ -4,7 +4,7 @@ import { DELEGATION_MANAGER_SC_ADDRESS_HEX } from "../constants"; import { Err } from "../errors"; import { IAddress } from "../interface"; import { Transaction } from "../transaction"; -import { TransactionBuilder } from "../transactionsFactories/transactionBuilder"; +import { TransactionBuilder } from "../transactionBuilder"; import * as resources from "./resources"; interface IConfig { diff --git a/src/index.ts b/src/index.ts index 7b89d4c2c..7207227d3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -34,8 +34,8 @@ export * from "./tokens"; export * from "./transaction"; export * from "./transactionComputer"; export * from "./transactionPayload"; -export * from "./transactionsFactories"; export * from "./transactionsOutcomeParsers"; export * from "./transactionWatcher"; +export * from "./transfers"; export * from "./utils"; export * from "./wallet"; diff --git a/src/relayed/relayedController.ts b/src/relayed/relayedController.ts index 2b48500e0..3a219c470 100644 --- a/src/relayed/relayedController.ts +++ b/src/relayed/relayedController.ts @@ -1,7 +1,7 @@ import { IAccount } from "../accounts/interfaces"; import { Transaction } from "../transaction"; import { TransactionComputer } from "../transactionComputer"; -import { TransactionsFactoryConfig } from "../transactionsFactories"; +import { TransactionsFactoryConfig } from "../transactionsFactoryConfig"; import { RelayedTransactionsFactory } from "./relayedTransactionsFactory"; import { RelayedV1TransactionInput, RelayedV2TransactionInput } from "./resources"; diff --git a/src/relayed/relayedTransactionsFactory.spec.ts b/src/relayed/relayedTransactionsFactory.spec.ts index cbd41f337..0be7b0d43 100644 --- a/src/relayed/relayedTransactionsFactory.spec.ts +++ b/src/relayed/relayedTransactionsFactory.spec.ts @@ -2,7 +2,7 @@ import { assert } from "chai"; import { TestWallet, loadTestWallets } from "../testutils"; import { Transaction } from "../transaction"; import { TransactionComputer } from "../transactionComputer"; -import { TransactionsFactoryConfig } from "../transactionsFactories/transactionsFactoryConfig"; +import { TransactionsFactoryConfig } from "../transactionsFactoryConfig"; import { RelayedTransactionsFactory } from "./relayedTransactionsFactory"; describe("test relayed transactions factory", function () { diff --git a/src/smartContracts/smartContractController.ts b/src/smartContracts/smartContractController.ts index 8c4fb1790..a469b39d0 100644 --- a/src/smartContracts/smartContractController.ts +++ b/src/smartContracts/smartContractController.ts @@ -7,7 +7,7 @@ import { INetworkProvider } from "../networkProviders/interface"; import { SmartContractQueriesController } from "../smartContractQueriesController"; import { Transaction } from "../transaction"; import { TransactionComputer } from "../transactionComputer"; -import { TransactionsFactoryConfig } from "../transactionsFactories"; +import { TransactionsFactoryConfig } from "../transactionsFactoryConfig"; import { SmartContractTransactionsOutcomeParser } from "../transactionsOutcomeParsers"; import { TransactionWatcher } from "../transactionWatcher"; import * as resources from "./resources"; diff --git a/src/smartContracts/smartContractTransactionsFactory.spec.ts b/src/smartContracts/smartContractTransactionsFactory.spec.ts index b2c88ff97..eae01d4c0 100644 --- a/src/smartContracts/smartContractTransactionsFactory.spec.ts +++ b/src/smartContracts/smartContractTransactionsFactory.spec.ts @@ -4,7 +4,7 @@ import { Address } from "../address"; import { Err } from "../errors"; import { loadAbiRegistry, loadContractCode } from "../testutils/utils"; import { Token, TokenTransfer } from "../tokens"; -import { TransactionsFactoryConfig } from "../transactionsFactories"; +import { TransactionsFactoryConfig } from "../transactionsFactoryConfig"; import { SmartContractTransactionsFactory } from "./smartContractTransactionsFactory"; describe("test smart contract transactions factory", function () { diff --git a/src/smartContracts/smartContractTransactionsFactory.ts b/src/smartContracts/smartContractTransactionsFactory.ts index c5731dcc8..899904d0c 100644 --- a/src/smartContracts/smartContractTransactionsFactory.ts +++ b/src/smartContracts/smartContractTransactionsFactory.ts @@ -6,9 +6,9 @@ import { CONTRACT_DEPLOY_ADDRESS_HEX, VM_TYPE_WASM_VM } from "../constants"; import { Err } from "../errors"; import { Logger } from "../logger"; import { TokenComputer, TokenTransfer } from "../tokens"; +import { TokenTransfersDataBuilder } from "../tokenTransfersDataBuilder"; import { Transaction } from "../transaction"; -import { TokenTransfersDataBuilder } from "../transactionsFactories/tokenTransfersDataBuilder"; -import { TransactionBuilder } from "../transactionsFactories/transactionBuilder"; +import { TransactionBuilder } from "../transactionBuilder"; import { byteArrayToHex, utf8ToHex } from "../utils.codec"; import * as resources from "./resources"; diff --git a/src/tokenManagement/tokenManagementController.ts b/src/tokenManagement/tokenManagementController.ts index 9a36baf01..e5d0f219f 100644 --- a/src/tokenManagement/tokenManagementController.ts +++ b/src/tokenManagement/tokenManagementController.ts @@ -4,7 +4,7 @@ import { INetworkProvider } from "../networkProviders/interface"; import { IESDTIssueOutcome } from "../tokenOperations"; import { Transaction } from "../transaction"; import { TransactionComputer } from "../transactionComputer"; -import { TransactionsFactoryConfig } from "../transactionsFactories"; +import { TransactionsFactoryConfig } from "../transactionsFactoryConfig"; import { TokenManagementTransactionsOutcomeParser } from "../transactionsOutcomeParsers"; import { TransactionWatcher } from "../transactionWatcher"; import * as resources from "./resources"; diff --git a/src/tokenManagement/tokenManagementTransactionFactory.spec.ts b/src/tokenManagement/tokenManagementTransactionFactory.spec.ts index 2c2d432cc..9808d41ae 100644 --- a/src/tokenManagement/tokenManagementTransactionFactory.spec.ts +++ b/src/tokenManagement/tokenManagementTransactionFactory.spec.ts @@ -2,7 +2,7 @@ import { assert } from "chai"; import { Address } from "../address"; import { ESDT_CONTRACT_ADDRESS_HEX } from "../constants"; import { loadTestWallets, TestWallet } from "../testutils"; -import { TransactionsFactoryConfig } from "../transactionsFactories"; +import { TransactionsFactoryConfig } from "../transactionsFactoryConfig"; import { TokenManagementTransactionsFactory } from "./tokenManagementTransactionsFactory"; describe("test token management transactions factory", () => { diff --git a/src/tokenManagement/tokenManagementTransactionsFactory.ts b/src/tokenManagement/tokenManagementTransactionsFactory.ts index 261912046..2b161ee2b 100644 --- a/src/tokenManagement/tokenManagementTransactionsFactory.ts +++ b/src/tokenManagement/tokenManagementTransactionsFactory.ts @@ -5,7 +5,7 @@ import { ErrBadUsage } from "../errors"; import { IAddress } from "../interface"; import { Logger } from "../logger"; import { Transaction } from "../transaction"; -import { TransactionBuilder } from "../transactionsFactories/transactionBuilder"; +import { TransactionBuilder } from "../transactionBuilder"; import * as resources from "./resources"; interface IConfig { diff --git a/src/transactionsFactories/tokenTransfersDataBuilder.ts b/src/tokenTransfersDataBuilder.ts similarity index 92% rename from src/transactionsFactories/tokenTransfersDataBuilder.ts rename to src/tokenTransfersDataBuilder.ts index 75e8b2409..17e416d13 100644 --- a/src/transactionsFactories/tokenTransfersDataBuilder.ts +++ b/src/tokenTransfersDataBuilder.ts @@ -1,6 +1,6 @@ -import { AddressValue, ArgSerializer, BigUIntValue, TokenIdentifierValue, TypedValue, U32Value } from "../abi"; -import { IAddress } from "../interface"; -import { TokenComputer, TokenTransfer } from "../tokens"; +import { AddressValue, ArgSerializer, BigUIntValue, TokenIdentifierValue, TypedValue, U32Value } from "./abi"; +import { IAddress } from "./interface"; +import { TokenComputer, TokenTransfer } from "./tokens"; export class TokenTransfersDataBuilder { private tokenComputer: TokenComputer; diff --git a/src/transaction.local.net.spec.ts b/src/transaction.local.net.spec.ts index ed8dd0ab7..1ac7e632e 100644 --- a/src/transaction.local.net.spec.ts +++ b/src/transaction.local.net.spec.ts @@ -7,9 +7,9 @@ import { TokenTransfer } from "./tokens"; import { Transaction } from "./transaction"; import { TransactionComputer } from "./transactionComputer"; import { TransactionPayload } from "./transactionPayload"; +import { TransactionsFactoryConfig } from "./transactionsFactoryConfig"; import { TransactionWatcher } from "./transactionWatcher"; -import { TransactionsFactoryConfig } from "./transactionsFactories/transactionsFactoryConfig"; -import { TransferTransactionsFactory } from "./transactionsFactories/transferTransactionsFactory"; +import { TransferTransactionsFactory } from "./transfers/transferTransactionsFactory"; describe("test transaction", function () { let alice: TestWallet, bob: TestWallet; @@ -156,8 +156,7 @@ describe("test transaction", function () { await bob.sync(provider); const initialBalanceOfBob = new BigNumber(bob.account.balance.toString()); - const transaction = factory.createTransactionForNativeTokenTransfer({ - sender: alice.address, + const transaction = factory.createTransactionForNativeTokenTransfer(alice.address, { receiver: bob.address, nativeAmount: 42000000000000000000n, }); diff --git a/src/transactionsFactories/transactionBuilder.ts b/src/transactionBuilder.ts similarity index 89% rename from src/transactionsFactories/transactionBuilder.ts rename to src/transactionBuilder.ts index 6c163e816..d4b684a6b 100644 --- a/src/transactionsFactories/transactionBuilder.ts +++ b/src/transactionBuilder.ts @@ -1,7 +1,7 @@ -import { ARGUMENTS_SEPARATOR } from "../constants"; -import { IAddress, ITransactionPayload } from "../interface"; -import { Transaction } from "../transaction"; -import { TransactionPayload } from "../transactionPayload"; +import { ARGUMENTS_SEPARATOR } from "./constants"; +import { IAddress, ITransactionPayload } from "./interface"; +import { Transaction } from "./transaction"; +import { TransactionPayload } from "./transactionPayload"; interface Config { chainID: string; diff --git a/src/transactionsFactories/transactionsFactoryConfig.ts b/src/transactionsFactoryConfig.ts similarity index 98% rename from src/transactionsFactories/transactionsFactoryConfig.ts rename to src/transactionsFactoryConfig.ts index c1d21df92..1ad0007f1 100644 --- a/src/transactionsFactories/transactionsFactoryConfig.ts +++ b/src/transactionsFactoryConfig.ts @@ -1,4 +1,4 @@ -import { LibraryConfig } from "../config"; +import { LibraryConfig } from "./config"; export class TransactionsFactoryConfig { chainID: string; diff --git a/src/transferTransactionsFactory.spec.ts b/src/transferTransactionsFactory.spec.ts deleted file mode 100644 index 39e6f96d1..000000000 --- a/src/transferTransactionsFactory.spec.ts +++ /dev/null @@ -1,128 +0,0 @@ -import { assert } from "chai"; -import { Address } from "./address"; -import { GasEstimator } from "./gasEstimator"; -import { TokenTransfer } from "./tokens"; -import { TransactionPayload } from "./transactionPayload"; -import { TransferTransactionsFactory } from "./transactionsFactories/transferTransactionsFactory"; - -describe("test transaction factory", () => { - const factory = new TransferTransactionsFactory(new GasEstimator()); - - it("should create EGLD transfers", () => { - const transactionWithData = factory.createEGLDTransfer({ - value: TokenTransfer.egldFromAmount(10.5), - sender: Address.fromBech32("erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"), - receiver: new Address("erd1dc3yzxxeq69wvf583gw0h67td226gu2ahpk3k50qdgzzym8npltq7ndgha"), - data: new TransactionPayload("hello"), - chainID: "D", - }); - - assert.equal( - transactionWithData.getSender().bech32(), - "erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th", - ); - assert.equal( - transactionWithData.getReceiver().bech32(), - "erd1dc3yzxxeq69wvf583gw0h67td226gu2ahpk3k50qdgzzym8npltq7ndgha", - ); - assert.equal(transactionWithData.getValue(), "10500000000000000000"); - assert.equal(transactionWithData.getGasLimit(), 50000 + 5 * 1500); - assert.equal(transactionWithData.getData().toString(), "hello"); - assert.equal(transactionWithData.getChainID(), "D"); - - const transactionWithoutData = factory.createEGLDTransfer({ - value: TokenTransfer.egldFromAmount(10.5), - sender: Address.fromBech32("erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"), - receiver: new Address("erd1dc3yzxxeq69wvf583gw0h67td226gu2ahpk3k50qdgzzym8npltq7ndgha"), - chainID: "D", - }); - - assert.equal( - transactionWithoutData.getSender().bech32(), - "erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th", - ); - assert.equal( - transactionWithoutData.getReceiver().bech32(), - "erd1dc3yzxxeq69wvf583gw0h67td226gu2ahpk3k50qdgzzym8npltq7ndgha", - ); - assert.equal(transactionWithoutData.getValue(), "10500000000000000000"); - assert.equal(transactionWithoutData.getGasLimit(), 50000); - assert.equal(transactionWithoutData.getData().toString(), ""); - assert.equal(transactionWithoutData.getChainID(), "D"); - }); - - it("should create ESDT transfers", () => { - const transaction = factory.createESDTTransfer({ - tokenTransfer: TokenTransfer.fungibleFromAmount("TEST-8b028f", "100.00", 2), - sender: Address.fromBech32("erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"), - receiver: Address.fromBech32("erd1dc3yzxxeq69wvf583gw0h67td226gu2ahpk3k50qdgzzym8npltq7ndgha"), - chainID: "D", - }); - - assert.equal( - transaction.getSender().bech32(), - "erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th", - ); - assert.equal( - transaction.getReceiver().bech32(), - "erd1dc3yzxxeq69wvf583gw0h67td226gu2ahpk3k50qdgzzym8npltq7ndgha", - ); - assert.equal(transaction.getValue(), ""); - assert.equal(transaction.getGasLimit(), 50000 + 40 * 1500 + 200000 + 100000); - assert.equal(transaction.getData().toString(), "ESDTTransfer@544553542d386230323866@2710"); - assert.equal(transaction.getChainID(), "D"); - }); - - it("should create ESDTNFT transfers", () => { - const transaction = factory.createESDTNFTTransfer({ - tokenTransfer: TokenTransfer.nonFungible("TEST-38f249", 1), - destination: new Address("erd1spyavw0956vq68xj8y4tenjpq2wd5a9p2c6j8gsz7ztyrnpxrruqzu66jx"), - sender: new Address("erd1dc3yzxxeq69wvf583gw0h67td226gu2ahpk3k50qdgzzym8npltq7ndgha"), - chainID: "D", - }); - - assert.equal( - transaction.getSender().bech32(), - "erd1dc3yzxxeq69wvf583gw0h67td226gu2ahpk3k50qdgzzym8npltq7ndgha", - ); - assert.equal( - transaction.getReceiver().bech32(), - "erd1dc3yzxxeq69wvf583gw0h67td226gu2ahpk3k50qdgzzym8npltq7ndgha", - ); - assert.equal(transaction.getValue(), ""); - assert.equal(transaction.getGasLimit(), 50000 + 109 * 1500 + 200000 + 800000); - assert.equal( - transaction.getData().toString(), - "ESDTNFTTransfer@544553542d333866323439@01@01@8049d639e5a6980d1cd2392abcce41029cda74a1563523a202f09641cc2618f8", - ); - assert.equal(transaction.getChainID(), "D"); - }); - - it("should create Multi ESDTNFT transfers", () => { - const transaction = factory.createMultiESDTNFTTransfer({ - tokenTransfers: [ - TokenTransfer.nonFungible("FOO-38f249", 1), - TokenTransfer.fungibleFromAmount("BAR-c80d29", "10.00", 18), - ], - destination: new Address("erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"), - sender: new Address("erd1dc3yzxxeq69wvf583gw0h67td226gu2ahpk3k50qdgzzym8npltq7ndgha"), - chainID: "D", - }); - - assert.equal( - transaction.getSender().bech32(), - "erd1dc3yzxxeq69wvf583gw0h67td226gu2ahpk3k50qdgzzym8npltq7ndgha", - ); - assert.equal( - transaction.getReceiver().bech32(), - "erd1dc3yzxxeq69wvf583gw0h67td226gu2ahpk3k50qdgzzym8npltq7ndgha", - ); - assert.equal(transaction.getValue(), ""); - assert.equal(transaction.getGasLimit(), 50000 + 154 * 1500 + (200000 + 800000) * 2); - assert.equal( - transaction.getData().toString(), - "MultiESDTNFTTransfer@0139472eff6886771a982f3083da5d421f24c29181e63888228dc81ca60d69e1@02@464f4f2d333866323439@01@01@4241522d633830643239@@8ac7230489e80000", - ); - assert.equal(transaction.getChainID(), "D"); - }); -}); diff --git a/src/transactionsFactories/index.ts b/src/transfers/index.ts similarity index 51% rename from src/transactionsFactories/index.ts rename to src/transfers/index.ts index 505dff76b..1542bb8ce 100644 --- a/src/transactionsFactories/index.ts +++ b/src/transfers/index.ts @@ -1,2 +1,2 @@ -export * from "./transactionsFactoryConfig"; +export * from "./transfersControllers"; export * from "./transferTransactionsFactory"; diff --git a/src/transfers/resources.ts b/src/transfers/resources.ts new file mode 100644 index 000000000..7a6aa975b --- /dev/null +++ b/src/transfers/resources.ts @@ -0,0 +1,20 @@ +import { Address } from "../address"; +import { TokenTransfer } from "../tokens"; + +export type NativeTokenTransferInput = { + receiver: Address; + nativeAmount?: bigint; + data?: Uint8Array; +}; + +export type CustomTokenTransferInput = { + receiver: Address; + tokenTransfers: TokenTransfer[]; +}; + +export type CreateTransferTransactionInput = { + receiver: Address; + nativeAmount?: bigint; + tokenTransfers?: TokenTransfer[]; + data?: Uint8Array; +}; diff --git a/src/transactionsFactories/transferTransactionsFactory.spec.ts b/src/transfers/transferTransactionsFactory.spec.ts similarity index 93% rename from src/transactionsFactories/transferTransactionsFactory.spec.ts rename to src/transfers/transferTransactionsFactory.spec.ts index 6a7581c3e..b4cda39f9 100644 --- a/src/transactionsFactories/transferTransactionsFactory.spec.ts +++ b/src/transfers/transferTransactionsFactory.spec.ts @@ -3,7 +3,7 @@ import { Address } from "../address"; import { EGLD_IDENTIFIER_FOR_MULTI_ESDTNFT_TRANSFER } from "../constants"; import { ErrBadUsage } from "../errors"; import { Token, TokenTransfer } from "../tokens"; -import { TransactionsFactoryConfig } from "./transactionsFactoryConfig"; +import { TransactionsFactoryConfig } from "../transactionsFactoryConfig"; import { TransferTransactionsFactory } from "./transferTransactionsFactory"; describe("test transfer transactions factory", function () { @@ -20,8 +20,7 @@ describe("test transfer transactions factory", function () { assert.throw( () => { - transferFactory.createTransactionForESDTTokenTransfer({ - sender: alice, + transferFactory.createTransactionForESDTTokenTransfer(alice, { receiver: bob, tokenTransfers: transfers, }); @@ -32,8 +31,7 @@ describe("test transfer transactions factory", function () { }); it("should create 'Transaction' for native token transfer without data", async () => { - const transaction = transferFactory.createTransactionForNativeTokenTransfer({ - sender: alice, + const transaction = transferFactory.createTransactionForNativeTokenTransfer(alice, { receiver: bob, nativeAmount: 1000000000000000000n, }); @@ -46,8 +44,7 @@ describe("test transfer transactions factory", function () { }); it("should create 'Transaction' for native token transfer with data", async () => { - const transaction = transferFactory.createTransactionForNativeTokenTransfer({ - sender: alice, + const transaction = transferFactory.createTransactionForNativeTokenTransfer(alice, { receiver: bob, nativeAmount: 1000000000000000000n, data: Buffer.from("test data"), @@ -64,8 +61,7 @@ describe("test transfer transactions factory", function () { const fooToken = new Token({ identifier: "FOO-123456", nonce: 0n }); const transfer = new TokenTransfer({ token: fooToken, amount: 1000000n }); - const transaction = transferFactory.createTransactionForESDTTokenTransfer({ - sender: alice, + const transaction = transferFactory.createTransactionForESDTTokenTransfer(alice, { receiver: bob, tokenTransfers: [transfer], }); @@ -81,8 +77,7 @@ describe("test transfer transactions factory", function () { const nft = new Token({ identifier: "NFT-123456", nonce: 10n }); const transfer = new TokenTransfer({ token: nft, amount: 1n }); - const transaction = transferFactory.createTransactionForESDTTokenTransfer({ - sender: alice, + const transaction = transferFactory.createTransactionForESDTTokenTransfer(alice, { receiver: bob, tokenTransfers: [transfer], }); @@ -101,8 +96,7 @@ describe("test transfer transactions factory", function () { const nft = new Token({ identifier: "t0-NFT-123456", nonce: 10n }); const transfer = new TokenTransfer({ token: nft, amount: 1n }); - const transaction = transferFactory.createTransactionForESDTTokenTransfer({ - sender: alice, + const transaction = transferFactory.createTransactionForESDTTokenTransfer(alice, { receiver: bob, tokenTransfers: [transfer], }); @@ -124,8 +118,7 @@ describe("test transfer transactions factory", function () { const secondNft = new Token({ identifier: "TEST-987654", nonce: 1n }); const secondTransfer = new TokenTransfer({ token: secondNft, amount: 1n }); - const transaction = transferFactory.createTransactionForESDTTokenTransfer({ - sender: alice, + const transaction = transferFactory.createTransactionForESDTTokenTransfer(alice, { receiver: bob, tokenTransfers: [firstTransfer, secondTransfer], }); @@ -139,8 +132,7 @@ describe("test transfer transactions factory", function () { "MultiESDTNFTTransfer@8049d639e5a6980d1cd2392abcce41029cda74a1563523a202f09641cc2618f8@02@4e46542d313233343536@0a@01@544553542d393837363534@01@01", ); - const secondTransaction = transferFactory.createTransactionForTransfer({ - sender: alice, + const secondTransaction = transferFactory.createTransactionForTransfer(alice, { receiver: bob, tokenTransfers: [firstTransfer, secondTransfer], }); @@ -155,8 +147,7 @@ describe("test transfer transactions factory", function () { const secondNft = new Token({ identifier: "t0-TEST-987654", nonce: 1n }); const secondTransfer = new TokenTransfer({ token: secondNft, amount: 1n }); - const transaction = transferFactory.createTransactionForESDTTokenTransfer({ - sender: alice, + const transaction = transferFactory.createTransactionForESDTTokenTransfer(alice, { receiver: bob, tokenTransfers: [firstTransfer, secondTransfer], }); @@ -170,8 +161,7 @@ describe("test transfer transactions factory", function () { "MultiESDTNFTTransfer@8049d639e5a6980d1cd2392abcce41029cda74a1563523a202f09641cc2618f8@02@74302d4e46542d313233343536@0a@01@74302d544553542d393837363534@01@01", ); - const secondTransaction = transferFactory.createTransactionForTransfer({ - sender: alice, + const secondTransaction = transferFactory.createTransactionForTransfer(alice, { receiver: bob, tokenTransfers: [firstTransfer, secondTransfer], }); @@ -184,8 +174,7 @@ describe("test transfer transactions factory", function () { const nft = new Token({ identifier: "NFT-123456", nonce: 10n }); const transfer = new TokenTransfer({ token: nft, amount: 1n }); - transferFactory.createTransactionForTransfer({ - sender: alice, + transferFactory.createTransactionForTransfer(alice, { receiver: bob, tokenTransfers: [transfer], data: Buffer.from("test"), @@ -194,8 +183,7 @@ describe("test transfer transactions factory", function () { }); it("should create transaction for native transfers", async () => { - const transaction = transferFactory.createTransactionForTransfer({ - sender: alice, + const transaction = transferFactory.createTransactionForTransfer(alice, { receiver: bob, nativeAmount: 1000000000000000000n, }); @@ -207,8 +195,7 @@ describe("test transfer transactions factory", function () { }); it("should create transaction for native transfers and set data field", async () => { - const transaction = transferFactory.createTransactionForTransfer({ - sender: alice, + const transaction = transferFactory.createTransactionForTransfer(alice, { receiver: bob, nativeAmount: 1000000000000000000n, data: Buffer.from("hello"), @@ -222,8 +209,7 @@ describe("test transfer transactions factory", function () { }); it("should create transaction for notarizing", async () => { - const transaction = transferFactory.createTransactionForTransfer({ - sender: alice, + const transaction = transferFactory.createTransactionForTransfer(alice, { receiver: bob, data: Buffer.from("hello"), }); @@ -241,8 +227,7 @@ describe("test transfer transactions factory", function () { const secondNft = new Token({ identifier: "TEST-987654", nonce: 1n }); const secondTransfer = new TokenTransfer({ token: secondNft, amount: 1n }); - const transaction = transferFactory.createTransactionForTransfer({ - sender: alice, + const transaction = transferFactory.createTransactionForTransfer(alice, { receiver: bob, nativeAmount: 1000000000000000000n, tokenTransfers: [firstTransfer, secondTransfer], @@ -265,8 +250,7 @@ describe("test transfer transactions factory", function () { const secondNft = new Token({ identifier: "t0-TEST-987654", nonce: 1n }); const secondTransfer = new TokenTransfer({ token: secondNft, amount: 1n }); - const transaction = transferFactory.createTransactionForTransfer({ - sender: alice, + const transaction = transferFactory.createTransactionForTransfer(alice, { receiver: bob, nativeAmount: 1000000000000000000n, tokenTransfers: [firstTransfer, secondTransfer], @@ -286,8 +270,7 @@ describe("test transfer transactions factory", function () { const firstNft = new Token({ identifier: EGLD_IDENTIFIER_FOR_MULTI_ESDTNFT_TRANSFER }); const firstTransfer = new TokenTransfer({ token: firstNft, amount: 1000000000000000000n }); - const transaction = transferFactory.createTransactionForESDTTokenTransfer({ - sender: alice, + const transaction = transferFactory.createTransactionForESDTTokenTransfer(alice, { receiver: bob, tokenTransfers: [firstTransfer], }); diff --git a/src/transactionsFactories/transferTransactionsFactory.ts b/src/transfers/transferTransactionsFactory.ts similarity index 91% rename from src/transactionsFactories/transferTransactionsFactory.ts rename to src/transfers/transferTransactionsFactory.ts index b4e43c079..8bb64c41d 100644 --- a/src/transactionsFactories/transferTransactionsFactory.ts +++ b/src/transfers/transferTransactionsFactory.ts @@ -12,10 +12,11 @@ import { ITransactionValue, } from "../interface"; import { TokenComputer, TokenTransfer } from "../tokens"; +import { TokenTransfersDataBuilder } from "../tokenTransfersDataBuilder"; import { Transaction } from "../transaction"; +import { TransactionBuilder } from "../transactionBuilder"; import { TransactionPayload } from "../transactionPayload"; -import { TokenTransfersDataBuilder } from "./tokenTransfersDataBuilder"; -import { TransactionBuilder } from "./transactionBuilder"; +import * as resources from "./resources"; const ADDITIONAL_GAS_FOR_ESDT_TRANSFER = 100000; const ADDITIONAL_GAS_FOR_ESDT_NFT_TRANSFER = 800000; @@ -81,31 +82,25 @@ export class TransferTransactionsFactory { } } - createTransactionForNativeTokenTransfer(options: { - sender: IAddress; - receiver: IAddress; - nativeAmount: bigint; - data?: Uint8Array; - }): Transaction { + createTransactionForNativeTokenTransfer( + sender: IAddress, + options: resources.NativeTokenTransferInput, + ): Transaction { this.ensureConfigIsDefined(); const data = options.data || new Uint8Array(); return new Transaction({ - sender: options.sender.bech32(), + sender: sender.bech32(), receiver: options.receiver.bech32(), chainID: this.config!.chainID, gasLimit: this.computeGasForMoveBalance(this.config!, data), data: data, - value: options.nativeAmount, + value: options.nativeAmount ?? BigInt(0), }); } - createTransactionForESDTTokenTransfer(options: { - sender: IAddress; - receiver: IAddress; - tokenTransfers: TokenTransfer[]; - }): Transaction { + createTransactionForESDTTokenTransfer(sender: IAddress, options: resources.CustomTokenTransferInput): Transaction { this.ensureConfigIsDefined(); const numberOfTransfers = options.tokenTransfers.length; @@ -115,7 +110,7 @@ export class TransferTransactionsFactory { } if (numberOfTransfers === 1) { - return this.createSingleESDTTransferTransaction(options); + return this.createSingleESDTTransferTransaction(sender, options); } const { dataParts, extraGasForTransfer } = this.buildMultiESDTNFTTransferData( @@ -125,21 +120,15 @@ export class TransferTransactionsFactory { return new TransactionBuilder({ config: this.config!, - sender: options.sender, - receiver: options.sender, + sender: sender, + receiver: sender, dataParts: dataParts, gasLimit: extraGasForTransfer, addDataMovementGas: true, }).build(); } - createTransactionForTransfer(options: { - sender: IAddress; - receiver: IAddress; - nativeAmount?: bigint; - tokenTransfers?: TokenTransfer[]; - data?: Uint8Array; - }): Transaction { + createTransactionForTransfer(sender: IAddress, options: resources.CreateTransferTransactionInput): Transaction { const nativeAmount = options.nativeAmount ?? 0n; let tokenTransfers = options.tokenTransfers ? [...options.tokenTransfers] : []; const numberOfTokens = tokenTransfers.length; @@ -149,8 +138,7 @@ export class TransferTransactionsFactory { } if ((nativeAmount && numberOfTokens === 0) || options.data) { - return this.createTransactionForNativeTokenTransfer({ - sender: options.sender, + return this.createTransactionForNativeTokenTransfer(sender, { receiver: options.receiver, nativeAmount: nativeAmount, data: options.data, @@ -162,8 +150,7 @@ export class TransferTransactionsFactory { tokenTransfers.push(nativeTransfer); } - return this.createTransactionForESDTTokenTransfer({ - sender: options.sender, + return this.createTransactionForESDTTokenTransfer(sender, { receiver: options.receiver, tokenTransfers: tokenTransfers, }); @@ -348,20 +335,24 @@ export class TransferTransactionsFactory { }); } - private createSingleESDTTransferTransaction(options: { - sender: IAddress; - receiver: IAddress; - tokenTransfers: TokenTransfer[]; - }): Transaction { + private createSingleESDTTransferTransaction( + sender: IAddress, + options: { + receiver: IAddress; + tokenTransfers: TokenTransfer[]; + }, + ): Transaction { this.ensureConfigIsDefined(); const transfer = options.tokenTransfers[0]; - - const { dataParts, extraGasForTransfer, receiver } = this.buildTransferData(transfer, options); + const { dataParts, extraGasForTransfer, receiver } = this.buildTransferData(transfer, { + sender, + receiver: options.receiver, + }); return new TransactionBuilder({ config: this.config!, - sender: options.sender, + sender: sender, receiver: receiver, dataParts: dataParts, gasLimit: extraGasForTransfer, diff --git a/src/transfers/transfersControllers.ts b/src/transfers/transfersControllers.ts new file mode 100644 index 000000000..74a0e3085 --- /dev/null +++ b/src/transfers/transfersControllers.ts @@ -0,0 +1,55 @@ +import { IAccount } from "../accounts/interfaces"; +import { Transaction } from "../transaction"; +import { TransactionComputer } from "../transactionComputer"; +import { TransactionsFactoryConfig } from "../transactionsFactoryConfig"; +import * as resources from "./resources"; +import { TransferTransactionsFactory } from "./transferTransactionsFactory"; + +export class TransfersController { + private factory: TransferTransactionsFactory; + private txComputer: TransactionComputer; + + constructor(options: { chainID: string }) { + this.factory = new TransferTransactionsFactory({ config: new TransactionsFactoryConfig(options) }); + this.txComputer = new TransactionComputer(); + } + + async createTransactionForNativeTokenTransfer( + sender: IAccount, + nonce: bigint, + options: resources.NativeTokenTransferInput, + ): Promise { + const transaction = this.factory.createTransactionForNativeTokenTransfer(sender.address, options); + + transaction.nonce = nonce; + transaction.signature = await sender.sign(this.txComputer.computeBytesForSigning(transaction)); + + return transaction; + } + + async createTransactionForEsdtTokenTransfer( + sender: IAccount, + nonce: bigint, + options: resources.CustomTokenTransferInput, + ): Promise { + const transaction = this.factory.createTransactionForESDTTokenTransfer(sender.address, options); + + transaction.nonce = nonce; + transaction.signature = await sender.sign(this.txComputer.computeBytesForSigning(transaction)); + + return transaction; + } + + async createTransactionForTransfer( + sender: IAccount, + nonce: bigint, + options: resources.CreateTransferTransactionInput, + ): Promise { + const transaction = this.factory.createTransactionForTransfer(sender.address, options); + + transaction.nonce = nonce; + transaction.signature = await sender.sign(this.txComputer.computeBytesForSigning(transaction)); + + return transaction; + } +}