diff --git a/src/abi/interaction.local.net.spec.ts b/src/abi/interaction.local.net.spec.ts index 7dbecbb0..f306f2c8 100644 --- a/src/abi/interaction.local.net.spec.ts +++ b/src/abi/interaction.local.net.spec.ts @@ -139,7 +139,7 @@ describe("test smart contract interactor", function () { contract: contract, deployer: alice, codePath: "src/testdata/basic-features.wasm", - gasLimit: 600000000, + gasLimit: 600000000n, initArguments: [], chainID: network.ChainID, }); diff --git a/src/abi/interaction.spec.ts b/src/abi/interaction.spec.ts index 2a88bf55..db210faf 100644 --- a/src/abi/interaction.spec.ts +++ b/src/abi/interaction.spec.ts @@ -59,7 +59,7 @@ describe("test smart contract interactor", function () { const hexBar = "4241522d356263303866"; const hexLKMEX = "4c4b4d45582d616162393130"; const hexNFT = "4d4f532d623962346232"; - const hexContractAddress = new Address(contract.getAddress().bech32()).hex(); + const hexContractAddress = new Address(contract.getAddress().toBech32()).hex(); const hexDummyFunction = "64756d6d79"; // ESDT, single @@ -76,8 +76,8 @@ describe("test smart contract interactor", function () { .withSingleESDTNFTTransfer(LKMEX(123456, 123.456)) .buildTransaction(); - assert.equal(transaction.getSender().bech32(), alice.bech32()); - assert.equal(transaction.getReceiver().bech32(), alice.bech32()); + assert.equal(transaction.getSender().toBech32(), alice.toBech32()); + assert.equal(transaction.getReceiver().toBech32(), alice.toBech32()); assert.equal( transaction.getData().toString(), `ESDTNFTTransfer@${hexLKMEX}@01e240@06b14bd1e6eea00000@${hexContractAddress}@${hexDummyFunction}`, @@ -89,8 +89,8 @@ describe("test smart contract interactor", function () { .withSender(alice) .buildTransaction(); - assert.equal(transaction.getSender().bech32(), alice.bech32()); - assert.equal(transaction.getReceiver().bech32(), alice.bech32()); + assert.equal(transaction.getSender().toBech32(), alice.toBech32()); + assert.equal(transaction.getReceiver().toBech32(), alice.toBech32()); assert.equal( transaction.getData().toString(), `ESDTNFTTransfer@${hexLKMEX}@01e240@06b14bd1e6eea00000@${hexContractAddress}@${hexDummyFunction}`, @@ -102,8 +102,8 @@ describe("test smart contract interactor", function () { .withSingleESDTNFTTransfer(nonFungibleToken(1)) .buildTransaction(); - assert.equal(transaction.getSender().bech32(), alice.bech32()); - assert.equal(transaction.getReceiver().bech32(), alice.bech32()); + assert.equal(transaction.getSender().toBech32(), alice.toBech32()); + assert.equal(transaction.getReceiver().toBech32(), alice.toBech32()); assert.equal( transaction.getData().toString(), `ESDTNFTTransfer@${hexNFT}@01@01@${hexContractAddress}@${hexDummyFunction}`, @@ -115,8 +115,8 @@ describe("test smart contract interactor", function () { .withSender(alice) .buildTransaction(); - assert.equal(transaction.getSender().bech32(), alice.bech32()); - assert.equal(transaction.getReceiver().bech32(), alice.bech32()); + assert.equal(transaction.getSender().toBech32(), alice.toBech32()); + assert.equal(transaction.getReceiver().toBech32(), alice.toBech32()); assert.equal( transaction.getData().toString(), `ESDTNFTTransfer@${hexNFT}@01@01@${hexContractAddress}@${hexDummyFunction}`, @@ -128,8 +128,8 @@ describe("test smart contract interactor", function () { .withMultiESDTNFTTransfer([TokenFoo(3), TokenBar(3.14)]) .buildTransaction(); - assert.equal(transaction.getSender().bech32(), alice.bech32()); - assert.equal(transaction.getReceiver().bech32(), alice.bech32()); + assert.equal(transaction.getSender().toBech32(), alice.toBech32()); + assert.equal(transaction.getReceiver().toBech32(), alice.toBech32()); assert.equal( transaction.getData().toString(), `MultiESDTNFTTransfer@${hexContractAddress}@02@${hexFoo}@@03@${hexBar}@@0c44@${hexDummyFunction}`, @@ -141,8 +141,8 @@ describe("test smart contract interactor", function () { .withSender(alice) .buildTransaction(); - assert.equal(transaction.getSender().bech32(), alice.bech32()); - assert.equal(transaction.getReceiver().bech32(), alice.bech32()); + assert.equal(transaction.getSender().toBech32(), alice.toBech32()); + assert.equal(transaction.getReceiver().toBech32(), alice.toBech32()); assert.equal( transaction.getData().toString(), `MultiESDTNFTTransfer@${hexContractAddress}@02@${hexFoo}@@03@${hexBar}@@0c44@${hexDummyFunction}`, @@ -154,8 +154,8 @@ describe("test smart contract interactor", function () { .withMultiESDTNFTTransfer([nonFungibleToken(1), nonFungibleToken(42)]) .buildTransaction(); - assert.equal(transaction.getSender().bech32(), alice.bech32()); - assert.equal(transaction.getReceiver().bech32(), alice.bech32()); + assert.equal(transaction.getSender().toBech32(), alice.toBech32()); + assert.equal(transaction.getReceiver().toBech32(), alice.toBech32()); assert.equal( transaction.getData().toString(), `MultiESDTNFTTransfer@${hexContractAddress}@02@${hexNFT}@01@01@${hexNFT}@2a@01@${hexDummyFunction}`, @@ -167,8 +167,8 @@ describe("test smart contract interactor", function () { .withSender(alice) .buildTransaction(); - assert.equal(transaction.getSender().bech32(), alice.bech32()); - assert.equal(transaction.getReceiver().bech32(), alice.bech32()); + assert.equal(transaction.getSender().toBech32(), alice.toBech32()); + assert.equal(transaction.getReceiver().toBech32(), alice.toBech32()); }); it("should create transaction, with ABI, with transfer & execute", async function () { @@ -190,8 +190,8 @@ describe("test smart contract interactor", function () { transaction, new Transaction({ chainID: "T", - sender: alice.toBech32(), - receiver: dummyAddress.toBech32(), + sender: alice, + receiver: dummyAddress, data: Buffer.from("ESDTTransfer@464f4f2d616263646566@64@676574556c74696d617465416e73776572"), gasLimit: 543210n, value: 0n, diff --git a/src/abi/interface.ts b/src/abi/interface.ts index e51c5b70..9fc39656 100644 --- a/src/abi/interface.ts +++ b/src/abi/interface.ts @@ -1,5 +1,5 @@ import { Address } from "../address"; -import { IChainID, IGasLimit, IGasPrice, ITransactionValue } from "../interface"; +import { ITransactionValue } from "../interface"; import { Transaction } from "../transaction"; import { ReturnCode } from "./returnCode"; import { TypedValue } from "./typesystem"; @@ -33,10 +33,10 @@ export interface DeployArguments { code: ICode; codeMetadata?: ICodeMetadata; initArguments?: any[]; - value?: ITransactionValue; - gasLimit: IGasLimit; - gasPrice?: IGasPrice; - chainID: IChainID; + value?: bigint; + gasLimit: bigint; + gasPrice?: bigint; + chainID: string; deployer: Address; } @@ -44,21 +44,21 @@ export interface UpgradeArguments { code: ICode; codeMetadata?: ICodeMetadata; initArguments?: any[]; - value?: ITransactionValue; - gasLimit: IGasLimit; - gasPrice?: IGasPrice; - chainID: IChainID; + value?: bigint; + gasLimit: bigint; + gasPrice?: bigint; + chainID: string; caller: Address; } export interface CallArguments { func: IContractFunction; args?: any[]; - value?: ITransactionValue; - gasLimit: IGasLimit; + value?: bigint; + gasLimit: bigint; receiver?: Address; - gasPrice?: IGasPrice; - chainID: IChainID; + gasPrice?: bigint; + chainID: string; caller: Address; } diff --git a/src/abi/nativeSerializer.ts b/src/abi/nativeSerializer.ts index e1c10c61..85ba5b7c 100644 --- a/src/abi/nativeSerializer.ts +++ b/src/abi/nativeSerializer.ts @@ -67,7 +67,7 @@ import { export namespace NativeTypes { export type NativeBuffer = Buffer | string; export type NativeBytes = Buffer | { valueOf(): Buffer } | string; - export type NativeAddress = string | Buffer | IAddress | { getAddress(): IAddress }; + export type NativeAddress = string | Buffer | Address | { getAddress(): IAddress }; export type NativeBigNumber = BigNumber.Value | bigint; } @@ -403,9 +403,9 @@ export namespace NativeSerializer { export function convertNativeToAddress( native: NativeTypes.NativeAddress, errorContext: ArgumentErrorContext, - ): IAddress { + ): Address { if ((native).bech32) { - return native; + return
native; } if ((native).getAddress) { return (native).getAddress(); diff --git a/src/abi/smartContract.spec.ts b/src/abi/smartContract.spec.ts index abbcef78..cc494451 100644 --- a/src/abi/smartContract.spec.ts +++ b/src/abi/smartContract.spec.ts @@ -29,10 +29,13 @@ describe("test contract", () => { let owner = new Address("93ee6143cdc10ce79f15b2a6c2ad38e9b6021c72a1779051f47154fd54cfbd5e"); let firstContractAddress = SmartContract.computeAddress(owner, 0); - assert.equal(firstContractAddress.bech32(), "erd1qqqqqqqqqqqqqpgqhdjjyq8dr7v5yq9tv6v5vt9tfvd00vg7h40q6779zn"); + assert.equal(firstContractAddress.toBech32(), "erd1qqqqqqqqqqqqqpgqhdjjyq8dr7v5yq9tv6v5vt9tfvd00vg7h40q6779zn"); let secondContractAddress = SmartContract.computeAddress(owner, 1); - assert.equal(secondContractAddress.bech32(), "erd1qqqqqqqqqqqqqpgqde8eqjywyu6zlxjxuxqfg5kgtmn3setxh40qen8egy"); + assert.equal( + secondContractAddress.toBech32(), + "erd1qqqqqqqqqqqqqpgqde8eqjywyu6zlxjxuxqfg5kgtmn3setxh40qen8egy", + ); }); it("should deploy", async () => { @@ -42,7 +45,7 @@ describe("test contract", () => { let contract = new SmartContract(); let deployTransaction = contract.deploy({ code: Code.fromBuffer(Buffer.from([1, 2, 3, 4])), - gasLimit: 1000000, + gasLimit: 1000000n, chainID: chainID, deployer: alice.address, }); @@ -60,7 +63,10 @@ describe("test contract", () => { // Compute & set the contract address contract.setAddress(SmartContract.computeAddress(alice.address, 42)); - assert.equal(contract.getAddress().bech32(), "erd1qqqqqqqqqqqqqpgq3ytm9m8dpeud35v3us20vsafp77smqghd8ss4jtm0q"); + assert.equal( + contract.getAddress().toBech32(), + "erd1qqqqqqqqqqqqqpgq3ytm9m8dpeud35v3us20vsafp77smqghd8ss4jtm0q", + ); // Sign the transaction deployTransaction.applySignature(await alice.signer.sign(deployTransaction.serializeForSigning())); @@ -97,7 +103,7 @@ describe("test contract", () => { let callTransactionOne = contract.call({ func: new ContractFunction("helloEarth"), args: [new U32Value(5), BytesValue.fromHex("0123")], - gasLimit: 150000, + gasLimit: 150000n, chainID: chainID, caller: alice.address, }); @@ -105,7 +111,7 @@ describe("test contract", () => { let callTransactionTwo = contract.call({ func: new ContractFunction("helloMars"), args: [new U32Value(5), BytesValue.fromHex("0123")], - gasLimit: 1500000, + gasLimit: 1500000n, chainID: chainID, caller: alice.address, }); @@ -161,7 +167,7 @@ describe("test contract", () => { let deployTransaction = contract.upgrade({ code: Code.fromBuffer(Buffer.from([1, 2, 3, 4])), - gasLimit: 1000000, + gasLimit: 1000000n, chainID: chainID, caller: alice.address, }); @@ -233,7 +239,7 @@ describe("test contract", () => { func: "foo", args: [new U8Value(1), new U8Value(2), new U8Value(3)], chainID: "D", - gasLimit: 1000000, + gasLimit: 1000000n, caller: callerAddress, }); }, "Wrong number of arguments for endpoint foo: expected between 0 and 1 arguments, have 3"); @@ -243,7 +249,7 @@ describe("test contract", () => { func: "foo", args: [[new U8Value(1), new U8Value(2), new U8Value(3)]], chainID: "D", - gasLimit: 1000000, + gasLimit: 1000000n, caller: callerAddress, }); @@ -252,7 +258,7 @@ describe("test contract", () => { func: "foo", args: [[1, 2, 3]], chainID: "D", - gasLimit: 1000000, + gasLimit: 1000000n, caller: callerAddress, }); @@ -302,7 +308,7 @@ describe("test contract", () => { func: "foo", args: [new U8Value(1), new U8Value(2), new U8Value(3)], chainID: "D", - gasLimit: 1000000, + gasLimit: 1000000n, caller: callerAddress, }); }, "Invalid argument: Wrong argument type for endpoint foo: typed value provided; expected variadic type, have U8Value"); @@ -312,7 +318,7 @@ describe("test contract", () => { func: "foo", args: [VariadicValue.fromItems(new U8Value(1), new U8Value(2), new U8Value(3))], chainID: "D", - gasLimit: 1000000, + gasLimit: 1000000n, caller: callerAddress, }); @@ -321,7 +327,7 @@ describe("test contract", () => { func: "foo", args: [1, 2, 3], chainID: "D", - gasLimit: 1000000, + gasLimit: 1000000n, caller: callerAddress, }); diff --git a/src/abi/smartContract.ts b/src/abi/smartContract.ts index 6b1b4db0..b8fb5fe7 100644 --- a/src/abi/smartContract.ts +++ b/src/abi/smartContract.ts @@ -247,12 +247,12 @@ export class SmartContract implements ISmartContract { }); args = args || []; - value = value || 0; + value = value || 0n; const transaction = factory.createTransactionForExecute(caller, { contract: receiver ? receiver : this.getAddress(), function: func.toString(), - gasLimit: BigInt(gasLimit.valueOf()), + gasLimit: gasLimit, arguments: args, }); @@ -276,7 +276,7 @@ export class SmartContract implements ISmartContract { } private ensureHasAddress() { - if (!this.getAddress().bech32()) { + if (!this.getAddress().toBech32()) { throw new ErrContractHasNoAddress(); } } @@ -289,7 +289,7 @@ export class SmartContract implements ISmartContract { * @param nonce The owner nonce used for the deployment transaction */ static computeAddress(owner: Address, nonce: INonce): Address { - const deployer = Address.fromBech32(owner.bech32()); + const deployer = Address.fromBech32(owner.toBech32()); const addressComputer = new AddressComputer(); return addressComputer.computeContractAddress(deployer, BigInt(nonce.valueOf())); } diff --git a/src/abi/smartContractResults.local.net.spec.ts b/src/abi/smartContractResults.local.net.spec.ts index 87201fa5..a2f34ad0 100644 --- a/src/abi/smartContractResults.local.net.spec.ts +++ b/src/abi/smartContractResults.local.net.spec.ts @@ -42,7 +42,7 @@ describe("fetch transactions from local testnet", function () { contract: contract, deployer: alice, codePath: "src/testdata/counter.wasm", - gasLimit: 3000000, + gasLimit: 3000000n, initArguments: [], chainID: network.ChainID, }); @@ -50,7 +50,7 @@ describe("fetch transactions from local testnet", function () { // ++ let transactionIncrement = contract.call({ func: new ContractFunction("increment"), - gasLimit: 3000000, + gasLimit: 3000000n, chainID: network.ChainID, caller: alice.address, }); diff --git a/src/abi/typesystem/address.ts b/src/abi/typesystem/address.ts index ef8db6d0..7fc34fe9 100644 --- a/src/abi/typesystem/address.ts +++ b/src/abi/typesystem/address.ts @@ -1,5 +1,4 @@ import { Address } from "../../address"; -import { IAddress } from "../../interface"; import { PrimitiveType, PrimitiveValue } from "./types"; export class AddressType extends PrimitiveType { @@ -21,9 +20,9 @@ export class AddressValue extends PrimitiveValue { static ClassName = "AddressValue"; private readonly value: Address; - constructor(value: IAddress) { + constructor(value: Address) { super(new AddressType()); - this.value = Address.newFromBech32(value.bech32()); + this.value = Address.newFromBech32(value.toBech32()); } getClassName(): string { diff --git a/src/accountManagement/accountTransactionsFactory.spec.ts b/src/accountManagement/accountTransactionsFactory.spec.ts index 77ce1a04..43129eb6 100644 --- a/src/accountManagement/accountTransactionsFactory.spec.ts +++ b/src/accountManagement/accountTransactionsFactory.spec.ts @@ -15,8 +15,14 @@ describe("test account transactions factory", function () { keyValuePairs: keyValuePairs, }); - assert.equal(transaction.sender, "erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"); - assert.equal(transaction.receiver, "erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"); + assert.deepEqual( + transaction.sender, + Address.newFromBech32("erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"), + ); + assert.deepEqual( + transaction.receiver, + Address.newFromBech32("erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"), + ); assert.equal(Buffer.from(transaction.data).toString(), "SaveKeyValue@6b657930@76616c756530"); assert.equal(transaction.value, 0n); assert.equal(transaction.chainID, config.chainID); @@ -24,8 +30,8 @@ describe("test account transactions factory", function () { }); it("should create 'Transaction' for setting guardian", async function () { - const sender = Address.fromBech32("erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"); - const guardian = Address.fromBech32("erd1spyavw0956vq68xj8y4tenjpq2wd5a9p2c6j8gsz7ztyrnpxrruqzu66jx"); + const sender = Address.newFromBech32("erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"); + const guardian = Address.newFromBech32("erd1spyavw0956vq68xj8y4tenjpq2wd5a9p2c6j8gsz7ztyrnpxrruqzu66jx"); const serviceID = "MultiversXTCSService"; const transaction = factory.createTransactionForSettingGuardian(sender, { @@ -33,8 +39,14 @@ describe("test account transactions factory", function () { serviceID: serviceID, }); - assert.equal(transaction.sender, "erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"); - assert.equal(transaction.receiver, "erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"); + assert.deepEqual( + transaction.sender, + Address.newFromBech32("erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"), + ); + assert.deepEqual( + transaction.receiver, + Address.newFromBech32("erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"), + ); assert.equal( Buffer.from(transaction.data).toString(), "SetGuardian@8049d639e5a6980d1cd2392abcce41029cda74a1563523a202f09641cc2618f8@4d756c7469766572735854435353657276696365", @@ -49,8 +61,14 @@ describe("test account transactions factory", function () { const transaction = factory.createTransactionForGuardingAccount(sender); - assert.equal(transaction.sender, "erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"); - assert.equal(transaction.receiver, "erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"); + assert.deepEqual( + transaction.sender, + Address.newFromBech32("erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"), + ); + assert.deepEqual( + transaction.receiver, + Address.newFromBech32("erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"), + ); assert.equal(Buffer.from(transaction.data).toString(), "GuardAccount"); assert.equal(transaction.value, 0n); assert.equal(transaction.chainID, config.chainID); @@ -62,8 +80,14 @@ describe("test account transactions factory", function () { const transaction = factory.createTransactionForUnguardingAccount(sender); - assert.equal(transaction.sender, "erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"); - assert.equal(transaction.receiver, "erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"); + assert.deepEqual( + transaction.sender, + Address.newFromBech32("erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"), + ); + assert.deepEqual( + transaction.receiver, + Address.newFromBech32("erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"), + ); assert.equal(Buffer.from(transaction.data).toString(), "UnGuardAccount"); assert.equal(transaction.value, 0n); assert.equal(transaction.chainID, config.chainID); diff --git a/src/accountManagement/accountTransactionsFactory.ts b/src/accountManagement/accountTransactionsFactory.ts index 8fdeed7d..37b17662 100644 --- a/src/accountManagement/accountTransactionsFactory.ts +++ b/src/accountManagement/accountTransactionsFactory.ts @@ -1,5 +1,4 @@ import { Address } from "../address"; -import { IAddress } from "../interface"; import { Transaction } from "../transaction"; import { TransactionBuilder } from "../transactionBuilder"; import { SaveKeyValueInput, SetGuardianInput } from "./resources"; @@ -23,7 +22,7 @@ export class AccountTransactionsFactory { this.config = options.config; } - createTransactionForSavingKeyValue(sender: IAddress, options: SaveKeyValueInput): Transaction { + createTransactionForSavingKeyValue(sender: Address, options: SaveKeyValueInput): Transaction { const functionName = "SaveKeyValue"; const keyValueParts = this.computeDataPartsForSavingKeyValue(options.keyValuePairs); const dataParts = [functionName, ...keyValueParts]; @@ -61,7 +60,7 @@ export class AccountTransactionsFactory { return dataParts; } - createTransactionForSettingGuardian(sender: IAddress, options: SetGuardianInput): Transaction { + createTransactionForSettingGuardian(sender: Address, options: SetGuardianInput): Transaction { const dataParts = [ "SetGuardian", Address.fromBech32(options.guardianAddress.bech32()).toHex(), @@ -78,7 +77,7 @@ export class AccountTransactionsFactory { }).build(); } - createTransactionForGuardingAccount(sender: IAddress): Transaction { + createTransactionForGuardingAccount(sender: Address): Transaction { const dataParts = ["GuardAccount"]; return new TransactionBuilder({ @@ -91,7 +90,7 @@ export class AccountTransactionsFactory { }).build(); } - createTransactionForUnguardingAccount(sender: IAddress): Transaction { + createTransactionForUnguardingAccount(sender: Address): Transaction { const dataParts = ["UnGuardAccount"]; return new TransactionBuilder({ diff --git a/src/accounts/account.ts b/src/accounts/account.ts index 126e194d..4f6060a3 100644 --- a/src/accounts/account.ts +++ b/src/accounts/account.ts @@ -52,7 +52,7 @@ export class Account implements IAccount { */ toJSON(): any { return { - address: this.address.bech32(), + address: this.address.toBech32(), nonce: this.nonce.valueOf(), }; } diff --git a/src/address.spec.ts b/src/address.spec.ts index 46552556..ee754675 100644 --- a/src/address.spec.ts +++ b/src/address.spec.ts @@ -35,7 +35,7 @@ describe("test address", () => { const nobody = Address.empty(); assert.isEmpty(nobody.hex()); - assert.isEmpty(nobody.bech32()); + assert.isEmpty(nobody.toBech32()); assert.deepEqual(nobody.toJSON(), { bech32: "", pubkey: "" }); }); diff --git a/src/compatibility.ts b/src/compatibility.ts index 9caf1977..7fb18b5a 100644 --- a/src/compatibility.ts +++ b/src/compatibility.ts @@ -13,7 +13,7 @@ export class Compatibility { console.warn( `${context}: address should be set; ${resolution}. In the future, this will throw an exception instead of emitting a WARN.`, ); - } else if (address.bech32() == Address.Zero().bech32()) { + } else if (address.bech32() == Address.Zero().toBech32()) { console.warn( `${context}: address should not be the 'zero' address (also known as the 'contracts deployment address'); ${resolution}. In the future, this will throw an exception instead of emitting a WARN.`, ); diff --git a/src/converters/transactionsConverter.ts b/src/converters/transactionsConverter.ts index 92cb6f75..ba6d7649 100644 --- a/src/converters/transactionsConverter.ts +++ b/src/converters/transactionsConverter.ts @@ -1,13 +1,14 @@ -import { IPlainTransactionObject, ITransaction } from "../interface"; +import { Address } from "../address"; +import { IPlainTransactionObject } from "../interface"; import { Transaction } from "../transaction"; export class TransactionsConverter { - public transactionToPlainObject(transaction: ITransaction): IPlainTransactionObject { + public transactionToPlainObject(transaction: Transaction): IPlainTransactionObject { const plainObject = { nonce: Number(transaction.nonce), value: transaction.value.toString(), - receiver: transaction.receiver, - sender: transaction.sender, + receiver: transaction.receiver.toBech32(), + sender: transaction.sender.toBech32(), senderUsername: this.toBase64OrUndefined(transaction.senderUsername), receiverUsername: this.toBase64OrUndefined(transaction.receiverUsername), gasPrice: Number(transaction.gasPrice), @@ -16,7 +17,7 @@ export class TransactionsConverter { chainID: transaction.chainID.valueOf(), version: transaction.version, options: transaction.options == 0 ? undefined : transaction.options, - guardian: transaction.guardian ? transaction.guardian : undefined, + guardian: transaction.guardian.isEmpty() ? undefined : transaction.guardian.toBech32(), signature: this.toHexOrUndefined(transaction.signature), guardianSignature: this.toHexOrUndefined(transaction.guardianSignature), }; @@ -36,11 +37,11 @@ export class TransactionsConverter { const transaction = new Transaction({ nonce: BigInt(object.nonce), value: BigInt(object.value || ""), - receiver: object.receiver, + receiver: Address.newFromBech32(object.receiver), receiverUsername: this.bufferFromBase64(object.receiverUsername).toString(), - sender: object.sender, + sender: Address.newFromBech32(object.sender), senderUsername: this.bufferFromBase64(object.senderUsername).toString(), - guardian: object.guardian, + guardian: object.guardian ? Address.newFromBech32(object.guardian) : Address.empty(), gasPrice: BigInt(object.gasPrice), gasLimit: BigInt(object.gasLimit), data: this.bufferFromBase64(object.data), diff --git a/src/converters/transactionsConverters.spec.ts b/src/converters/transactionsConverters.spec.ts index 2d91adf0..fe1fe20b 100644 --- a/src/converters/transactionsConverters.spec.ts +++ b/src/converters/transactionsConverters.spec.ts @@ -1,4 +1,5 @@ import { assert } from "chai"; +import { Address } from "../address"; import { Transaction } from "../transaction"; import { TransactionsConverter } from "./transactionsConverter"; @@ -7,14 +8,14 @@ describe("test transactions converter", async () => { const converter = new TransactionsConverter(); const transaction = new Transaction({ - nonce: 90, + nonce: 90n, value: BigInt("123456789000000000000000000000"), - sender: "erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th", - receiver: "erd1spyavw0956vq68xj8y4tenjpq2wd5a9p2c6j8gsz7ztyrnpxrruqzu66jx", + sender: Address.newFromBech32("erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"), + receiver: Address.newFromBech32("erd1spyavw0956vq68xj8y4tenjpq2wd5a9p2c6j8gsz7ztyrnpxrruqzu66jx"), senderUsername: "alice", receiverUsername: "bob", - gasPrice: 1000000000, - gasLimit: 80000, + gasPrice: 1000000000n, + gasLimit: 80000n, data: Buffer.from("hello"), chainID: "localnet", version: 2, diff --git a/src/delegation/delegationTransactionsFactory.spec.ts b/src/delegation/delegationTransactionsFactory.spec.ts index 6a9e53f8..65a11930 100644 --- a/src/delegation/delegationTransactionsFactory.spec.ts +++ b/src/delegation/delegationTransactionsFactory.spec.ts @@ -21,11 +21,11 @@ describe("test delegation transactions factory", function () { amount: value, }); - assert.equal(transaction.sender, "erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2"); - assert.equal( - transaction.receiver, - Address.fromHex(DELEGATION_MANAGER_SC_ADDRESS_HEX, config.addressHrp).bech32(), + assert.deepEqual( + transaction.sender, + Address.newFromBech32("erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2"), ); + assert.deepEqual(transaction.receiver, Address.fromHex(DELEGATION_MANAGER_SC_ADDRESS_HEX, config.addressHrp)); assert.isDefined(transaction.data); assert.deepEqual(transaction.data, Buffer.from("createNewDelegationContract@010f0cf064dd59200000@0a")); assert.equal(transaction.gasLimit, 60126500n); @@ -35,7 +35,7 @@ describe("test delegation transactions factory", function () { it("should create 'Transaction' for adding nodes", async function () { const sender = Address.fromBech32("erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2"); - const delegationContract = Address.fromBech32("erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqtllllls002zgc"); + const delegationContract = Address.newFromHex(DELEGATION_MANAGER_SC_ADDRESS_HEX); const publicKey = new ValidatorPublicKey( Buffer.from( "e7beaa95b3877f47348df4dd1cb578a4f7cabf7a20bfeefe5cdd263878ff132b765e04fef6f40c93512b666c47ed7719b8902f6c922c04247989b7137e837cc81a62e54712471c97a2ddab75aa9c2f58f813ed4c0fa722bde0ab718bff382208", @@ -57,8 +57,11 @@ describe("test delegation transactions factory", function () { signedMessages: [mockMessage.getSignature()], }); - assert.equal(transaction.sender, "erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2"); - assert.equal(transaction.receiver, "erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqtllllls002zgc"); + assert.deepEqual( + transaction.sender, + Address.newFromBech32("erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2"), + ); + assert.deepEqual(transaction.receiver, Address.newFromHex(DELEGATION_MANAGER_SC_ADDRESS_HEX)); assert.isDefined(transaction.data); assert.deepEqual( transaction.data, @@ -71,7 +74,7 @@ describe("test delegation transactions factory", function () { it("should create 'Transaction' for removing nodes", async function () { const sender = Address.fromBech32("erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2"); - const delegationContract = Address.fromBech32("erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqtllllls002zgc"); + const delegationContract = Address.newFromHex(DELEGATION_MANAGER_SC_ADDRESS_HEX); const publicKey = new ValidatorPublicKey( Buffer.from( @@ -84,8 +87,11 @@ describe("test delegation transactions factory", function () { publicKeys: [publicKey], }); - assert.equal(transaction.sender, "erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2"); - assert.equal(transaction.receiver, "erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqtllllls002zgc"); + assert.deepEqual( + transaction.sender, + Address.newFromBech32("erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2"), + ); + assert.deepEqual(transaction.receiver, Address.newFromHex(DELEGATION_MANAGER_SC_ADDRESS_HEX)); assert.isDefined(transaction.data); assert.deepEqual( transaction.data, @@ -98,7 +104,7 @@ describe("test delegation transactions factory", function () { it("should create 'Transaction' for staking nodes", async function () { const sender = Address.fromBech32("erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2"); - const delegationContract = Address.fromBech32("erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqtllllls002zgc"); + const delegationContract = Address.newFromHex(DELEGATION_MANAGER_SC_ADDRESS_HEX); const publicKey = new ValidatorPublicKey( Buffer.from( "be2e593ff10899a2ee8e1d5c8094e36c9f48e04b87e129991ff09475808743e07bb41bf6e7bc1463fa554c4b46594b98", @@ -110,8 +116,11 @@ describe("test delegation transactions factory", function () { publicKeys: [publicKey], }); - assert.equal(transaction.sender, "erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2"); - assert.equal(transaction.receiver, "erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqtllllls002zgc"); + assert.deepEqual( + transaction.sender, + Address.newFromBech32("erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2"), + ); + assert.deepEqual(transaction.receiver, Address.newFromHex(DELEGATION_MANAGER_SC_ADDRESS_HEX)); assert.isDefined(transaction.data); assert.deepEqual( transaction.data, @@ -124,7 +133,7 @@ describe("test delegation transactions factory", function () { it("should create 'Transaction' for unbonding nodes", async function () { const sender = Address.fromBech32("erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2"); - const delegationContract = Address.fromBech32("erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqtllllls002zgc"); + const delegationContract = Address.newFromHex(DELEGATION_MANAGER_SC_ADDRESS_HEX); const publicKey = new ValidatorPublicKey( Buffer.from( "be2e593ff10899a2ee8e1d5c8094e36c9f48e04b87e129991ff09475808743e07bb41bf6e7bc1463fa554c4b46594b98", @@ -136,8 +145,11 @@ describe("test delegation transactions factory", function () { publicKeys: [publicKey], }); - assert.equal(transaction.sender, "erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2"); - assert.equal(transaction.receiver, "erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqtllllls002zgc"); + assert.deepEqual( + transaction.sender, + Address.newFromBech32("erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2"), + ); + assert.deepEqual(transaction.receiver, Address.newFromHex(DELEGATION_MANAGER_SC_ADDRESS_HEX)); assert.isDefined(transaction.data); assert.deepEqual( transaction.data, @@ -151,7 +163,7 @@ describe("test delegation transactions factory", function () { it("should create 'Transaction' for unstaking nodes", async function () { const sender = Address.fromBech32("erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2"); - const delegationContract = Address.fromBech32("erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqtllllls002zgc"); + const delegationContract = Address.newFromHex(DELEGATION_MANAGER_SC_ADDRESS_HEX); const publicKey = new ValidatorPublicKey( Buffer.from( "be2e593ff10899a2ee8e1d5c8094e36c9f48e04b87e129991ff09475808743e07bb41bf6e7bc1463fa554c4b46594b98", @@ -163,8 +175,11 @@ describe("test delegation transactions factory", function () { publicKeys: [publicKey], }); - assert.equal(transaction.sender, "erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2"); - assert.equal(transaction.receiver, "erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqtllllls002zgc"); + assert.deepEqual( + transaction.sender, + Address.newFromBech32("erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2"), + ); + assert.deepEqual(transaction.receiver, Address.newFromHex(DELEGATION_MANAGER_SC_ADDRESS_HEX)); assert.isDefined(transaction.data); assert.deepEqual( transaction.data, @@ -177,8 +192,8 @@ describe("test delegation transactions factory", function () { }); it("should create 'Transaction' for unjailing nodes", async function () { - const sender = Address.fromBech32("erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2"); - const delegationContract = Address.fromBech32("erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqtllllls002zgc"); + const sender = Address.newFromBech32("erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2"); + const delegationContract = Address.newFromHex(DELEGATION_MANAGER_SC_ADDRESS_HEX); const publicKey = new ValidatorPublicKey( Buffer.from( "be2e593ff10899a2ee8e1d5c8094e36c9f48e04b87e129991ff09475808743e07bb41bf6e7bc1463fa554c4b46594b98", @@ -190,8 +205,11 @@ describe("test delegation transactions factory", function () { publicKeys: [publicKey], amount: 25000000000000000000n, }); - assert.equal(transaction.sender, "erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2"); - assert.equal(transaction.receiver, "erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqtllllls002zgc"); + assert.deepEqual( + transaction.sender, + Address.newFromBech32("erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2"), + ); + assert.deepEqual(transaction.receiver, Address.newFromHex(DELEGATION_MANAGER_SC_ADDRESS_HEX)); assert.isDefined(transaction.data); assert.deepEqual( transaction.data, @@ -203,8 +221,8 @@ describe("test delegation transactions factory", function () { }); it("should create 'Transaction' for changing service fee", async function () { - const sender = Address.fromBech32("erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2"); - const delegationContract = Address.fromBech32("erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqtllllls002zgc"); + const sender = Address.newFromBech32("erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2"); + const delegationContract = Address.newFromHex(DELEGATION_MANAGER_SC_ADDRESS_HEX); const serviceFee = 10n; const transaction = delegationFactory.createTransactionForChangingServiceFee(sender, { @@ -212,8 +230,11 @@ describe("test delegation transactions factory", function () { serviceFee: serviceFee, }); - assert.equal(transaction.sender, "erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2"); - assert.equal(transaction.receiver, "erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqtllllls002zgc"); + assert.deepEqual( + transaction.sender, + Address.newFromBech32("erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2"), + ); + assert.deepEqual(transaction.receiver, Address.newFromHex(DELEGATION_MANAGER_SC_ADDRESS_HEX)); assert.isDefined(transaction.data); assert.deepEqual(transaction.data, Buffer.from("changeServiceFee@0a")); assert.equal(transaction.value, 0n); @@ -221,7 +242,7 @@ describe("test delegation transactions factory", function () { it("should create 'Transaction' for changing delegation cap", async function () { const sender = Address.fromBech32("erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2"); - const delegationContract = Address.fromBech32("erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqtllllls002zgc"); + const delegationContract = Address.newFromHex(DELEGATION_MANAGER_SC_ADDRESS_HEX); const delegationCap = 5000000000000000000000n; const transaction = delegationFactory.createTransactionForModifyingDelegationCap(sender, { @@ -229,8 +250,11 @@ describe("test delegation transactions factory", function () { delegationCap: delegationCap, }); - assert.equal(transaction.sender, "erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2"); - assert.equal(transaction.receiver, "erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqtllllls002zgc"); + assert.deepEqual( + transaction.sender, + Address.newFromBech32("erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2"), + ); + assert.deepEqual(transaction.receiver, Address.newFromHex(DELEGATION_MANAGER_SC_ADDRESS_HEX)); assert.isDefined(transaction.data); assert.deepEqual(transaction.data, Buffer.from("modifyTotalDelegationCap@010f0cf064dd59200000")); assert.equal(transaction.value, 0n); @@ -238,14 +262,17 @@ describe("test delegation transactions factory", function () { it("should create 'Transaction' for setting automatic activation", async function () { const sender = Address.fromBech32("erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2"); - const delegationContract = Address.fromBech32("erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqtllllls002zgc"); + const delegationContract = Address.newFromHex(DELEGATION_MANAGER_SC_ADDRESS_HEX); const transaction = delegationFactory.createTransactionForSettingAutomaticActivation(sender, { delegationContract: delegationContract, }); - assert.equal(transaction.sender, "erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2"); - assert.equal(transaction.receiver, "erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqtllllls002zgc"); + assert.deepEqual( + transaction.sender, + Address.newFromBech32("erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2"), + ); + assert.deepEqual(transaction.receiver, Address.newFromHex(DELEGATION_MANAGER_SC_ADDRESS_HEX)); assert.isDefined(transaction.data); assert.deepEqual(transaction.data, Buffer.from("setAutomaticActivation@74727565")); assert.equal(transaction.value, 0n); @@ -253,14 +280,17 @@ describe("test delegation transactions factory", function () { it("should create 'Transaction' for unsetting automatic activation", async function () { const sender = Address.fromBech32("erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2"); - const delegationContract = Address.fromBech32("erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqtllllls002zgc"); + const delegationContract = Address.newFromHex(DELEGATION_MANAGER_SC_ADDRESS_HEX); const transaction = delegationFactory.createTransactionForUnsettingAutomaticActivation(sender, { delegationContract: delegationContract, }); - assert.equal(transaction.sender, "erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2"); - assert.equal(transaction.receiver, "erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqtllllls002zgc"); + assert.deepEqual( + transaction.sender, + Address.newFromBech32("erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2"), + ); + assert.deepEqual(transaction.receiver, Address.newFromHex(DELEGATION_MANAGER_SC_ADDRESS_HEX)); assert.isDefined(transaction.data); assert.deepEqual(transaction.data, Buffer.from("setAutomaticActivation@66616c7365")); assert.equal(transaction.value, 0n); @@ -268,14 +298,17 @@ describe("test delegation transactions factory", function () { it("should create 'Transaction' for setting cap check on redelegate rewards", async function () { const sender = Address.fromBech32("erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2"); - const delegationContract = Address.fromBech32("erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqtllllls002zgc"); + const delegationContract = Address.newFromHex(DELEGATION_MANAGER_SC_ADDRESS_HEX); const transaction = delegationFactory.createTransactionForSettingCapCheckOnRedelegateRewards(sender, { delegationContract: delegationContract, }); - assert.equal(transaction.sender, "erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2"); - assert.equal(transaction.receiver, "erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqtllllls002zgc"); + assert.deepEqual( + transaction.sender, + Address.newFromBech32("erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2"), + ); + assert.deepEqual(transaction.receiver, Address.newFromHex(DELEGATION_MANAGER_SC_ADDRESS_HEX)); assert.isDefined(transaction.data); assert.deepEqual(transaction.data, Buffer.from("setCheckCapOnReDelegateRewards@74727565")); assert.equal(transaction.value, 0n); @@ -283,22 +316,25 @@ describe("test delegation transactions factory", function () { it("should create 'Transaction' for unsetting cap check on redelegate rewards", async function () { const sender = Address.fromBech32("erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2"); - const delegationContract = Address.fromBech32("erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqtllllls002zgc"); + const delegationContract = Address.newFromHex(DELEGATION_MANAGER_SC_ADDRESS_HEX); const transaction = delegationFactory.createTransactionForUnsettingCapCheckOnRedelegateRewards(sender, { delegationContract: delegationContract, }); - assert.equal(transaction.sender, "erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2"); - assert.equal(transaction.receiver, "erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqtllllls002zgc"); + assert.deepEqual( + transaction.sender, + Address.newFromBech32("erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2"), + ); + assert.deepEqual(transaction.receiver, Address.newFromHex(DELEGATION_MANAGER_SC_ADDRESS_HEX)); assert.isDefined(transaction.data); assert.deepEqual(transaction.data, Buffer.from("setCheckCapOnReDelegateRewards@66616c7365")); assert.equal(transaction.value, 0n); }); it("should create 'Transaction' for setting metadata", async function () { - const sender = Address.fromBech32("erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2"); - const delegationContract = Address.fromBech32("erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqtllllls002zgc"); + const sender = Address.newFromBech32("erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2"); + const delegationContract = Address.newFromHex(DELEGATION_MANAGER_SC_ADDRESS_HEX); const transaction = delegationFactory.createTransactionForSettingMetadata(sender, { delegationContract: delegationContract, @@ -307,8 +343,11 @@ describe("test delegation transactions factory", function () { identifier: "identifier", }); - assert.equal(transaction.sender, "erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2"); - assert.equal(transaction.receiver, "erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqtllllls002zgc"); + assert.deepEqual( + transaction.sender, + Address.newFromBech32("erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2"), + ); + assert.deepEqual(transaction.receiver, Address.newFromHex(DELEGATION_MANAGER_SC_ADDRESS_HEX)); assert.isDefined(transaction.data); assert.deepEqual(transaction.data, Buffer.from("setMetaData@6e616d65@77656273697465@6964656e746966696572")); assert.equal(transaction.value, 0n); diff --git a/src/delegation/delegationTransactionsFactory.ts b/src/delegation/delegationTransactionsFactory.ts index 8aa26e38..bd71fcd3 100644 --- a/src/delegation/delegationTransactionsFactory.ts +++ b/src/delegation/delegationTransactionsFactory.ts @@ -2,7 +2,6 @@ import { ArgSerializer, BigUIntValue, BytesValue, StringValue } from "../abi"; import { Address } from "../address"; import { DELEGATION_MANAGER_SC_ADDRESS_HEX } from "../constants"; import { Err } from "../errors"; -import { IAddress } from "../interface"; import { Transaction } from "../transaction"; import { TransactionBuilder } from "../transactionBuilder"; import * as resources from "./resources"; @@ -36,7 +35,7 @@ export class DelegationTransactionsFactory { } createTransactionForNewDelegationContract( - sender: IAddress, + sender: Address, options: resources.NewDelegationContractInput, ): Transaction { const dataParts = [ @@ -61,7 +60,7 @@ export class DelegationTransactionsFactory { }).build(); } - createTransactionForAddingNodes(sender: IAddress, options: resources.AddNodesInput): Transaction { + createTransactionForAddingNodes(sender: Address, options: resources.AddNodesInput): Transaction { if (options.publicKeys.length !== options.signedMessages.length) { throw new Err("The number of public keys should match the number of signed messages"); } @@ -88,7 +87,7 @@ export class DelegationTransactionsFactory { }).build(); } - createTransactionForRemovingNodes(sender: IAddress, options: resources.ManageNodesInput): Transaction { + createTransactionForRemovingNodes(sender: Address, options: resources.ManageNodesInput): Transaction { const dataParts = ["removeNodes"]; for (const key of options.publicKeys) { @@ -106,7 +105,7 @@ export class DelegationTransactionsFactory { }).build(); } - createTransactionForStakingNodes(sender: IAddress, options: resources.ManageNodesInput): Transaction { + createTransactionForStakingNodes(sender: Address, options: resources.ManageNodesInput): Transaction { let dataParts = ["stakeNodes"]; for (const key of options.publicKeys) { @@ -129,7 +128,7 @@ export class DelegationTransactionsFactory { }).build(); } - createTransactionForUnbondingNodes(sender: IAddress, options: resources.ManageNodesInput): Transaction { + createTransactionForUnbondingNodes(sender: Address, options: resources.ManageNodesInput): Transaction { let dataParts = ["unBondNodes"]; for (const key of options.publicKeys) { @@ -152,7 +151,7 @@ export class DelegationTransactionsFactory { }).build(); } - createTransactionForUnstakingNodes(sender: IAddress, options: resources.ManageNodesInput): Transaction { + createTransactionForUnstakingNodes(sender: Address, options: resources.ManageNodesInput): Transaction { let dataParts = ["unStakeNodes"]; for (const key of options.publicKeys) { @@ -175,7 +174,7 @@ export class DelegationTransactionsFactory { }).build(); } - createTransactionForUnjailingNodes(sender: IAddress, options: resources.UnjailingNodesInput): Transaction { + createTransactionForUnjailingNodes(sender: Address, options: resources.UnjailingNodesInput): Transaction { const dataParts = ["unJailNodes"]; for (const key of options.publicKeys) { @@ -194,7 +193,7 @@ export class DelegationTransactionsFactory { }).build(); } - createTransactionForChangingServiceFee(sender: IAddress, options: resources.ChangeServiceFee): Transaction { + createTransactionForChangingServiceFee(sender: Address, options: resources.ChangeServiceFee): Transaction { const dataParts = [ "changeServiceFee", this.argSerializer.valuesToStrings([new BigUIntValue(options.serviceFee)])[0], @@ -213,7 +212,7 @@ export class DelegationTransactionsFactory { } createTransactionForModifyingDelegationCap( - sender: IAddress, + sender: Address, options: resources.ModifyDelegationCapInput, ): Transaction { const dataParts = [ @@ -234,7 +233,7 @@ export class DelegationTransactionsFactory { } createTransactionForSettingAutomaticActivation( - sender: IAddress, + sender: Address, options: resources.ManageDelegationContractInput, ): Transaction { const dataParts = ["setAutomaticActivation", this.argSerializer.valuesToStrings([new StringValue("true")])[0]]; @@ -252,7 +251,7 @@ export class DelegationTransactionsFactory { } createTransactionForUnsettingAutomaticActivation( - sender: IAddress, + sender: Address, options: resources.ManageDelegationContractInput, ): Transaction { const dataParts = ["setAutomaticActivation", this.argSerializer.valuesToStrings([new StringValue("false")])[0]]; @@ -270,7 +269,7 @@ export class DelegationTransactionsFactory { } createTransactionForSettingCapCheckOnRedelegateRewards( - sender: IAddress, + sender: Address, options: resources.ManageDelegationContractInput, ): Transaction { const dataParts = [ @@ -291,7 +290,7 @@ export class DelegationTransactionsFactory { } createTransactionForUnsettingCapCheckOnRedelegateRewards( - sender: IAddress, + sender: Address, options: resources.ManageDelegationContractInput, ): Transaction { const dataParts = [ @@ -311,7 +310,7 @@ export class DelegationTransactionsFactory { }).build(); } - createTransactionForSettingMetadata(sender: IAddress, options: resources.SetContractMetadataInput): Transaction { + createTransactionForSettingMetadata(sender: Address, options: resources.SetContractMetadataInput): Transaction { const dataParts = [ "setMetaData", ...this.argSerializer.valuesToStrings([ @@ -334,29 +333,29 @@ export class DelegationTransactionsFactory { }).build(); } - createTransactionForWithdrawing(_sender: IAddress, _options: resources.ManageDelegationContractInput): Transaction { + createTransactionForWithdrawing(_sender: Address, _options: resources.ManageDelegationContractInput): Transaction { throw new Error("Method not implemented."); } - createTransactionForUndelegating(_sender: IAddress, _options: resources.DelegateActionsInput): Transaction { + createTransactionForUndelegating(_sender: Address, _options: resources.DelegateActionsInput): Transaction { throw new Error("Method not implemented."); } createTransactionForRedelegatingRewards( - _sender: IAddress, + _sender: Address, _options: resources.ManageDelegationContractInput, ): Transaction { throw new Error("Method not implemented."); } createTransactionForClaimingRewards( - _sender: IAddress, + _sender: Address, _options: resources.ManageDelegationContractInput, ): Transaction { throw new Error("Method not implemented."); } - createTransactionForDelegating(_sender: IAddress, _options: resources.DelegateActionsInput): Transaction { + createTransactionForDelegating(_sender: Address, _options: resources.DelegateActionsInput): Transaction { throw new Error("Method not implemented."); } diff --git a/src/entrypoints/entrypoints.spec.ts b/src/entrypoints/entrypoints.spec.ts index f0a85039..e2173e05 100644 --- a/src/entrypoints/entrypoints.spec.ts +++ b/src/entrypoints/entrypoints.spec.ts @@ -115,7 +115,7 @@ describe("TestEntrypoint", () => { ); assert.equal(relayedTransaction.chainID, "D"); assert.deepEqual( - relayedTransaction.data, + Buffer.from(relayedTransaction.data), Buffer.from( "relayedTxV2@0139472eff6886771a982f3083da5d421f24c29181e63888228dc81ca60d69e1@012fd1@68656c6c6f@c1eed3ac766d6b94aa53a1348d38eac8db60be0a1b2d0873247b61b8b25bbcb45bf9c1518227bcadd5044d4c027bdb935e0164243b2b2df9a5b250a10aca260e", ), diff --git a/src/entrypoints/entrypoints.ts b/src/entrypoints/entrypoints.ts index 76e72254..7ee11e04 100644 --- a/src/entrypoints/entrypoints.ts +++ b/src/entrypoints/entrypoints.ts @@ -40,7 +40,7 @@ class NetworkEntrypoint { } verifyTransactionSignature(transaction: Transaction): boolean { - const verifier = UserVerifier.fromAddress(Address.fromBech32(transaction.sender)); + const verifier = UserVerifier.fromAddress(transaction.sender); const txComputer = new TransactionComputer(); return verifier.verify(txComputer.computeBytesForVerifying(transaction), transaction.signature); } diff --git a/src/interface.ts b/src/interface.ts index a3fb3b2c..d1258c1e 100644 --- a/src/interface.ts +++ b/src/interface.ts @@ -82,21 +82,3 @@ export interface ITokenTransfer { readonly amountAsBigInteger: BigNumber.Value; valueOf(): BigNumber.Value; } - -export interface ITransaction { - sender: string; - receiver: string; - gasLimit: bigint; - chainID: string; - nonce: bigint; - value: bigint; - senderUsername: string; - receiverUsername: string; - gasPrice: bigint; - data: Uint8Array; - version: number; - options: number; - guardian: string; - signature: Uint8Array; - guardianSignature: Uint8Array; -} diff --git a/src/message.ts b/src/message.ts index 8b2dbfce..4d10df6c 100644 --- a/src/message.ts +++ b/src/message.ts @@ -65,7 +65,7 @@ export class MessageComputer { return { message: Buffer.from(message.data).toString("hex"), signature: message.signature ? Buffer.from(message.signature).toString("hex") : "", - address: message.address ? message.address.bech32() : "", + address: message.address ? message.address.toBech32() : "", version: message.version, signer: message.signer, }; diff --git a/src/networkProviders/apiNetworkProvider.ts b/src/networkProviders/apiNetworkProvider.ts index d6445a09..7b2bd6a4 100644 --- a/src/networkProviders/apiNetworkProvider.ts +++ b/src/networkProviders/apiNetworkProvider.ts @@ -1,6 +1,6 @@ import { ErrContractQuery, ErrNetworkProvider } from "../errors"; -import { ITransaction } from "../interface"; import { SmartContractQuery, SmartContractQueryResponse } from "../smartContractQuery"; +import { Transaction } from "../transaction"; import { TransactionOnNetwork, prepareTransactionForBroadcasting } from "../transactionOnNetwork"; import { TransactionStatus } from "../transactionStatus"; import { getAxios } from "../utils"; @@ -147,17 +147,17 @@ export class ApiNetworkProvider implements INetworkProvider { return status; } - async sendTransaction(tx: ITransaction): Promise { + async sendTransaction(tx: Transaction): Promise { const transaction = prepareTransactionForBroadcasting(tx); const response = await this.doPostGeneric("transactions", transaction); return response.txHash; } - async sendTransactions(txs: ITransaction[]): Promise { + async sendTransactions(txs: Transaction[]): Promise { return await this.backingProxyNetworkProvider.sendTransactions(txs); } - async simulateTransaction(tx: ITransaction): Promise { + async simulateTransaction(tx: Transaction): Promise { return await this.backingProxyNetworkProvider.simulateTransaction(tx); } diff --git a/src/networkProviders/interface.ts b/src/networkProviders/interface.ts index 675f27db..2ddd3b5b 100644 --- a/src/networkProviders/interface.ts +++ b/src/networkProviders/interface.ts @@ -1,5 +1,5 @@ -import { ITransaction } from "../interface"; import { SmartContractQuery, SmartContractQueryResponse } from "../smartContractQuery"; +import { Transaction } from "../transaction"; import { TransactionOnNetwork } from "../transactionOnNetwork"; import { TransactionStatus } from "../transactionStatus"; import { AccountOnNetwork } from "./accounts"; @@ -79,18 +79,18 @@ export interface INetworkProvider { /** * Broadcasts an already-signed transaction. */ - sendTransaction(tx: ITransaction): Promise; + sendTransaction(tx: Transaction): Promise; /** * Broadcasts a list of already-signed transactions. */ - sendTransactions(txs: ITransaction[]): Promise; + sendTransactions(txs: Transaction[]): Promise; /** * Simulates the processing of an already-signed transaction. * */ - simulateTransaction(tx: ITransaction): Promise; + simulateTransaction(tx: Transaction): Promise; /** * Queries a Smart Contract - runs a pure function defined by the contract and returns its results. diff --git a/src/networkProviders/providers.dev.net.spec.ts b/src/networkProviders/providers.dev.net.spec.ts index 25870368..3c5a09e7 100644 --- a/src/networkProviders/providers.dev.net.spec.ts +++ b/src/networkProviders/providers.dev.net.spec.ts @@ -1,8 +1,8 @@ import { AxiosHeaders } from "axios"; import { assert } from "chai"; import { Address } from "../address"; -import { ITransaction } from "../interface"; import { SmartContractQuery } from "../smartContractQuery"; +import { Transaction } from "../transaction"; import { TransactionOnNetwork } from "../transactionOnNetwork"; import { ApiNetworkProvider } from "./apiNetworkProvider"; import { INetworkProvider } from "./interface"; @@ -175,7 +175,7 @@ describe("test network providers on devnet: Proxy and API", function () { let proxyItemResponse = await proxyProvider.getNonFungibleTokenOfAccount(dan, item.collection, item.nonce); removeInconsistencyForNonFungibleTokenOfAccount(apiItemResponse, proxyItemResponse); - assert.deepEqual(apiResponse, proxyResponse, `user: ${dan.bech32()}, token: ${item.identifier}`); + assert.deepEqual(apiResponse, proxyResponse, `user: ${dan.toBech32()}, token: ${item.identifier}`); }); // TODO: Strive to have as little differences as possible between Proxy and API. @@ -196,10 +196,10 @@ describe("test network providers on devnet: Proxy and API", function () { this.timeout(5000); const txs = [ - { + new Transaction({ nonce: 103n, - receiver: "erd1487vz5m4zpxjyqw4flwa3xhnkzg4yrr3mkzf5sf0zgt94hjprc8qazcccl", - sender: "erd1487vz5m4zpxjyqw4flwa3xhnkzg4yrr3mkzf5sf0zgt94hjprc8qazcccl", + receiver: Address.newFromBech32("erd1487vz5m4zpxjyqw4flwa3xhnkzg4yrr3mkzf5sf0zgt94hjprc8qazcccl"), + sender: Address.newFromBech32("erd1487vz5m4zpxjyqw4flwa3xhnkzg4yrr3mkzf5sf0zgt94hjprc8qazcccl"), gasPrice: 1000000000n, gasLimit: 50000n, chainID: "D", @@ -208,19 +208,12 @@ describe("test network providers on devnet: Proxy and API", function () { "498d5abb9f8eb69cc75f24320e8929dadbfa855ffac220d5e92175a83be68e0437801af3a1411e3d839738230097a1c38da5c8c4df3f345defc5d40300675900", "hex", ), - senderUsername: "", - receiverUsername: "", - guardian: "", - guardianSignature: new Uint8Array(), - options: 0, - data: new Uint8Array(), - value: 0n, - }, - - { + }), + + new Transaction({ nonce: 104n, - receiver: "erd1487vz5m4zpxjyqw4flwa3xhnkzg4yrr3mkzf5sf0zgt94hjprc8qazcccl", - sender: "erd1487vz5m4zpxjyqw4flwa3xhnkzg4yrr3mkzf5sf0zgt94hjprc8qazcccl", + receiver: Address.newFromBech32("erd1487vz5m4zpxjyqw4flwa3xhnkzg4yrr3mkzf5sf0zgt94hjprc8qazcccl"), + sender: Address.newFromBech32("erd1487vz5m4zpxjyqw4flwa3xhnkzg4yrr3mkzf5sf0zgt94hjprc8qazcccl"), gasPrice: 1000000000n, gasLimit: 50000n, chainID: "D", @@ -229,31 +222,15 @@ describe("test network providers on devnet: Proxy and API", function () { "341a2f3b738fbd20692e3bbd1cb36cb5f4ce9c0a9acc0cf4322269c0fcf34fd6bb59cd94062a9a4730e47f41b1ef3e29b69c6ab2a2a4dca9c9a7724681bc1708", "hex", ), - value: 0n, - senderUsername: "", - receiverUsername: "", - guardian: "", - guardianSignature: new Uint8Array(), - options: 0, - data: new Uint8Array(), - }, - { + }), + new Transaction({ nonce: 77n, chainID: "D", - receiver: "erd1487vz5m4zpxjyqw4flwa3xhnkzg4yrr3mkzf5sf0zgt94hjprc8qazcccl", - sender: "erd1487vz5m4zpxjyqw4flwa3xhnkzg4yrr3mkzf5sf0zgt94hjprc8qazcccl", + receiver: Address.newFromBech32("erd1487vz5m4zpxjyqw4flwa3xhnkzg4yrr3mkzf5sf0zgt94hjprc8qazcccl"), + sender: Address.newFromBech32("erd1487vz5m4zpxjyqw4flwa3xhnkzg4yrr3mkzf5sf0zgt94hjprc8qazcccl"), gasLimit: 50000n, gasPrice: 1000000000n, - value: 0n, - senderUsername: "", - receiverUsername: "", - guardian: "", - guardianSignature: new Uint8Array(), - options: 0, - data: new Uint8Array(), - version: 1, - signature: new Uint8Array(), - }, + }), ]; const expectedHashes = [ @@ -431,11 +408,11 @@ describe("test network providers on devnet: Proxy and API", function () { it("should send both `Transaction` ", async function () { this.timeout(50000); - const transaction: ITransaction = { + const transaction = new Transaction({ nonce: BigInt(8), value: BigInt(0), - receiver: "erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th", - sender: "erd1zztjf9fhwvuvquzsllknq4qcmffwad6n0hjtn5dyzytr5tgz7uas0mkgrq", + receiver: Address.newFromBech32("erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"), + sender: Address.newFromBech32("erd1zztjf9fhwvuvquzsllknq4qcmffwad6n0hjtn5dyzytr5tgz7uas0mkgrq"), data: new Uint8Array(Buffer.from("test")), gasPrice: BigInt(1000000000), gasLimit: BigInt(80000), @@ -445,12 +422,7 @@ describe("test network providers on devnet: Proxy and API", function () { "3fa42d97b4f85442850340a11411a3cbd63885e06ff3f84c7a75d0ef59c780f7a18aa4f331cf460300bc8bd99352aea10b7c3bc17e40287337ae9f9842470205", "hex", ), - senderUsername: "", - receiverUsername: "", - guardian: "", - guardianSignature: new Uint8Array(), - options: 0, - }; + }); const apiTxNextHash = await apiProvider.sendTransaction(transaction); diff --git a/src/networkProviders/proxyNetworkProvider.ts b/src/networkProviders/proxyNetworkProvider.ts index 1451b905..056f02c8 100644 --- a/src/networkProviders/proxyNetworkProvider.ts +++ b/src/networkProviders/proxyNetworkProvider.ts @@ -1,8 +1,8 @@ import { Address } from "../address"; import { ESDT_CONTRACT_ADDRESS_HEX } from "../constants"; import { ErrContractQuery, ErrNetworkProvider } from "../errors"; -import { ITransaction } from "../interface"; import { SmartContractQuery, SmartContractQueryResponse } from "../smartContractQuery"; +import { Transaction } from "../transaction"; import { TransactionOnNetwork, prepareTransactionForBroadcasting } from "../transactionOnNetwork"; import { TransactionStatus } from "../transactionStatus"; import { getAxios } from "../utils"; @@ -137,13 +137,13 @@ export class ProxyNetworkProvider implements INetworkProvider { return status; } - async sendTransaction(tx: ITransaction): Promise { + async sendTransaction(tx: Transaction): Promise { const transaction = prepareTransactionForBroadcasting(tx); const response = await this.doPostGeneric("transaction/send", transaction); return response.txHash; } - async sendTransactions(txs: ITransaction[]): Promise { + async sendTransactions(txs: Transaction[]): Promise { const data = txs.map((tx) => prepareTransactionForBroadcasting(tx)); const response = await this.doPostGeneric("transaction/send-multiple", data); @@ -156,7 +156,7 @@ export class ProxyNetworkProvider implements INetworkProvider { return hashes; } - async simulateTransaction(tx: ITransaction): Promise { + async simulateTransaction(tx: Transaction): Promise { const transaction = prepareTransactionForBroadcasting(tx); const response = await this.doPostGeneric("transaction/simulate", transaction); return response; diff --git a/src/proto/serializer.spec.ts b/src/proto/serializer.spec.ts index 97a88b5c..9a6b693e 100644 --- a/src/proto/serializer.spec.ts +++ b/src/proto/serializer.spec.ts @@ -1,11 +1,9 @@ import { assert } from "chai"; import { Address } from "../address"; -import { TransactionVersion } from "../networkParams"; import { Signature } from "../signature"; import { loadTestWallets, TestWallet } from "../testutils"; import { TokenTransfer } from "../tokens"; import { Transaction } from "../transaction"; -import { TransactionPayload } from "../transactionPayload"; import { ProtoSerializer } from "./serializer"; describe("serialize transactions", () => { @@ -18,11 +16,11 @@ describe("serialize transactions", () => { it("with no data, no value", async () => { let transaction = new Transaction({ - nonce: 89, - value: 0, + nonce: 89n, + value: 0n, sender: wallets.alice.address, receiver: wallets.bob.address, - gasLimit: 50000, + gasLimit: 50000n, chainID: "local-testnet", }); @@ -38,12 +36,12 @@ describe("serialize transactions", () => { it("with data, no value", async () => { let transaction = new Transaction({ - nonce: 90, - value: 0, + nonce: 90n, + value: 0n, sender: wallets.alice.address, receiver: wallets.bob.address, - gasLimit: 80000, - data: new TransactionPayload("hello"), + gasLimit: 80000n, + data: Buffer.from("hello"), chainID: "local-testnet", }); @@ -59,12 +57,12 @@ describe("serialize transactions", () => { it("with data, with value", async () => { let transaction = new Transaction({ - nonce: 91, - value: TokenTransfer.egldFromAmount(10), + nonce: 91n, + value: TokenTransfer.egldFromAmount(10).amount, sender: wallets.alice.address, receiver: wallets.bob.address, - gasLimit: 100000, - data: new TransactionPayload("for the book"), + gasLimit: 100000n, + data: Buffer.from("for the book"), chainID: "local-testnet", }); @@ -80,12 +78,12 @@ describe("serialize transactions", () => { it("with data, with large value", async () => { let transaction = new Transaction({ - nonce: 92, - value: "123456789000000000000000000000", + nonce: 92n, + value: 123456789000000000000000000000n, sender: wallets.alice.address, receiver: wallets.bob.address, - gasLimit: 100000, - data: new TransactionPayload("for the spaceship"), + gasLimit: 100000n, + data: Buffer.from("for the spaceship"), chainID: "local-testnet", }); @@ -101,14 +99,14 @@ describe("serialize transactions", () => { it("with nonce = 0", async () => { let transaction = new Transaction({ - nonce: 0, - value: "0", + nonce: 0n, + value: 0n, sender: wallets.alice.address, receiver: wallets.bob.address, - gasLimit: 80000, - data: new TransactionPayload("hello"), + gasLimit: 80000n, + data: Buffer.from("hello"), chainID: "local-testnet", - version: new TransactionVersion(1), + version: 1, }); transaction.applySignature( @@ -126,13 +124,13 @@ describe("serialize transactions", () => { it("with usernames", async () => { const transaction = new Transaction({ - nonce: 204, - value: "1000000000000000000", + nonce: 204n, + value: 1000000000000000000n, sender: Address.fromBech32("erd1k2s324ww2g0yj38qn2ch2jwctdy8mnfxep94q9arncc6xecg3xaq6mjse8"), receiver: Address.fromBech32("erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"), senderUsername: "carol", receiverUsername: "alice", - gasLimit: 50000, + gasLimit: 50000n, chainID: "T", }); diff --git a/src/proto/serializer.ts b/src/proto/serializer.ts index c690b5e4..f3842617 100644 --- a/src/proto/serializer.ts +++ b/src/proto/serializer.ts @@ -3,7 +3,6 @@ import { bigIntToBuffer } from "../abi/codec/utils"; import { Address } from "../address"; import { TRANSACTION_OPTIONS_DEFAULT, TRANSACTION_OPTIONS_TX_GUARDED } from "../constants"; import * as errors from "../errors"; -import { ITransaction, ITransactionValue } from "../interface"; import { Transaction } from "../transaction"; /** @@ -25,11 +24,11 @@ export class ProtoSerializer { return buffer; } - private convertToProtoMessage(transaction: ITransaction) { + private convertToProtoMessage(transaction: Transaction) { const proto = require("./compiled").proto; - const receiverPubkey = new Address(transaction.receiver).getPublicKey(); - const senderPubkey = new Address(transaction.sender).getPublicKey(); + const receiverPubkey = transaction.receiver.getPublicKey(); + const senderPubkey = transaction.sender.getPublicKey(); let protoTransaction = new proto.Transaction({ // mx-chain-go's serializer handles nonce == 0 differently, thus we treat 0 as "undefined". @@ -66,7 +65,7 @@ export class ProtoSerializer { /** * Custom serialization, compatible with mx-chain-go. */ - private serializeTransactionValue(transactionValue: ITransactionValue): Buffer { + private serializeTransactionValue(transactionValue: bigint): Buffer { let value = new BigNumber(transactionValue.toString()); if (value.isZero()) { return Buffer.from([0, 0]); @@ -79,13 +78,13 @@ export class ProtoSerializer { return buffer; } - private isGuardedTransaction(transaction: ITransaction): boolean { - const hasGuardian = transaction.guardian.length > 0; + private isGuardedTransaction(transaction: Transaction): boolean { + const hasGuardian = !transaction.guardian.isEmpty(); const hasGuardianSignature = transaction.guardianSignature.length > 0; return this.isWithGuardian(transaction) && hasGuardian && hasGuardianSignature; } - private isWithGuardian(transaction: ITransaction): boolean { + private isWithGuardian(transaction: Transaction): boolean { return (transaction.options & TRANSACTION_OPTIONS_TX_GUARDED) == TRANSACTION_OPTIONS_TX_GUARDED; } diff --git a/src/relayed/relayedTransactionsFactory.spec.ts b/src/relayed/relayedTransactionsFactory.spec.ts index 0be7b0d4..aed994fa 100644 --- a/src/relayed/relayedTransactionsFactory.spec.ts +++ b/src/relayed/relayedTransactionsFactory.spec.ts @@ -1,4 +1,5 @@ import { assert } from "chai"; +import { Address } from "../address"; import { TestWallet, loadTestWallets } from "../testutils"; import { Transaction } from "../transaction"; import { TransactionComputer } from "../transactionComputer"; @@ -17,8 +18,8 @@ describe("test relayed transactions factory", function () { it("should throw exception when creating relayed v1 transaction with invalid inner transaction", async function () { let innerTransaction = new Transaction({ - sender: alice.address.bech32(), - receiver: "erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqzllls8a5w6u", + sender: alice.address, + receiver: Address.newFromBech32("erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqzllls8a5w6u"), gasLimit: 10000000n, data: Buffer.from("getContractConfig"), chainID: config.chainID, @@ -40,8 +41,8 @@ describe("test relayed transactions factory", function () { it("should create relayed v1 transaction", async function () { let innerTransaction = new Transaction({ - sender: bob.address.bech32(), - receiver: "erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqzllls8a5w6u", + sender: bob.address, + receiver: Address.newFromBech32("erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqzllls8a5w6u"), gasLimit: 60000000n, data: Buffer.from("getContractConfig"), chainID: config.chainID, @@ -71,8 +72,8 @@ describe("test relayed transactions factory", function () { it("should create relayed v1 transaction with usernames", async function () { let innerTransaction = new Transaction({ - sender: carol.address.bech32(), - receiver: alice.address.bech32(), + sender: carol.address, + receiver: alice.address, gasLimit: 50000n, chainID: config.chainID, nonce: 208n, @@ -104,8 +105,8 @@ describe("test relayed transactions factory", function () { it("should create relayed v1 transaction with big value", async function () { let innerTransaction = new Transaction({ - sender: carol.address.bech32(), - receiver: alice.address.bech32(), + sender: carol.address, + receiver: alice.address, gasLimit: 50000n, chainID: config.chainID, nonce: 208n, @@ -137,15 +138,15 @@ describe("test relayed transactions factory", function () { it("should create relayed v1 transaction with guarded inner transaction", async function () { let innerTransaction = new Transaction({ - sender: bob.address.bech32(), - receiver: "erd1qqqqqqqqqqqqqpgq54tsxmej537z9leghvp69hfu4f8gg5eu396q83gnnz", + sender: bob.address, + receiver: Address.newFromBech32("erd1qqqqqqqqqqqqqpgq54tsxmej537z9leghvp69hfu4f8gg5eu396q83gnnz"), gasLimit: 60000000n, chainID: config.chainID, data: Buffer.from("getContractConfig"), nonce: 198n, version: 2, options: 2, - guardian: grace.address.bech32(), + guardian: grace.address, }); const serializedInnerTransaction = transactionComputer.computeBytesForSigning(innerTransaction); @@ -172,15 +173,15 @@ describe("test relayed transactions factory", function () { it("should create guarded relayed v1 transaction with guarded inner transaction", async function () { let innerTransaction = new Transaction({ - sender: bob.address.bech32(), - receiver: "erd1qqqqqqqqqqqqqpgq54tsxmej537z9leghvp69hfu4f8gg5eu396q83gnnz", + sender: bob.address, + receiver: Address.newFromBech32("erd1qqqqqqqqqqqqqpgq54tsxmej537z9leghvp69hfu4f8gg5eu396q83gnnz"), gasLimit: 60000000n, chainID: config.chainID, data: Buffer.from("addNumber"), nonce: 198n, version: 2, options: 2, - guardian: grace.address.bech32(), + guardian: grace.address, }); const serializedInnerTransaction = transactionComputer.computeBytesForSigning(innerTransaction); @@ -192,7 +193,7 @@ describe("test relayed transactions factory", function () { }); relayedTransaction.nonce = 2627n; relayedTransaction.options = 2; - relayedTransaction.guardian = frank.address.bech32(); + relayedTransaction.guardian = frank.address; const serializedRelayedTransaction = transactionComputer.computeBytesForSigning(relayedTransaction); relayedTransaction.signature = await alice.signer.sign(serializedRelayedTransaction); @@ -210,8 +211,8 @@ describe("test relayed transactions factory", function () { it("should throw exception when creating relayed v2 transaction with invalid inner transaction", async function () { let innerTransaction = new Transaction({ - sender: bob.address.bech32(), - receiver: bob.address.bech32(), + sender: bob.address, + receiver: bob.address, gasLimit: 50000n, chainID: config.chainID, }); @@ -237,8 +238,8 @@ describe("test relayed transactions factory", function () { it("should create relayed v2 transaction", async function () { let innerTransaction = new Transaction({ - sender: bob.address.bech32(), - receiver: "erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqzllls8a5w6u", + sender: bob.address, + receiver: Address.newFromBech32("erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqzllls8a5w6u"), gasLimit: 0n, chainID: config.chainID, data: Buffer.from("getContractConfig"), diff --git a/src/relayed/relayedTransactionsFactory.ts b/src/relayed/relayedTransactionsFactory.ts index 52f00853..71f6a440 100644 --- a/src/relayed/relayedTransactionsFactory.ts +++ b/src/relayed/relayedTransactionsFactory.ts @@ -2,7 +2,6 @@ import BigNumber from "bignumber.js"; import { AddressValue, ArgSerializer, BytesValue, U64Value } from "../abi"; import { Address } from "../address"; import { ErrInvalidInnerTransaction } from "../errors"; -import { ITransaction } from "../interface"; import { Transaction } from "../transaction"; const JSONbig = require("json-bigint"); @@ -23,7 +22,7 @@ export class RelayedTransactionsFactory { this.config = options.config; } - createRelayedV1Transaction(relayerAddress: Address, options: { innerTransaction: ITransaction }): Transaction { + createRelayedV1Transaction(relayerAddress: Address, options: { innerTransaction: Transaction }): Transaction { if (!options.innerTransaction.gasLimit) { throw new ErrInvalidInnerTransaction("The gas limit is not set for the inner transaction"); } @@ -40,7 +39,7 @@ export class RelayedTransactionsFactory { return new Transaction({ chainID: this.config.chainID, - sender: relayerAddress.bech32(), + sender: relayerAddress, receiver: options.innerTransaction.sender, gasLimit: gasLimit, data: Buffer.from(data), @@ -50,7 +49,7 @@ export class RelayedTransactionsFactory { createRelayedV2Transaction( relayerAddress: Address, options: { - innerTransaction: ITransaction; + innerTransaction: Transaction; innerTransactionGasLimit: bigint; }, ): Transaction { @@ -63,7 +62,7 @@ export class RelayedTransactionsFactory { } const { argumentsString } = new ArgSerializer().valuesToString([ - new AddressValue(Address.fromBech32(options.innerTransaction.receiver)), + new AddressValue(options.innerTransaction.receiver), new U64Value(new BigNumber(options.innerTransaction.nonce.toString())), new BytesValue(Buffer.from(options.innerTransaction.data)), new BytesValue(Buffer.from(options.innerTransaction.signature)), @@ -75,7 +74,7 @@ export class RelayedTransactionsFactory { const gasLimit = options.innerTransactionGasLimit + this.config.minGasLimit + additionalGasForDataLength; return new Transaction({ - sender: relayerAddress.bech32(), + sender: relayerAddress, receiver: options.innerTransaction.sender, value: 0n, gasLimit: gasLimit, @@ -86,11 +85,11 @@ export class RelayedTransactionsFactory { }); } - private prepareInnerTransactionForRelayedV1(innerTransaction: ITransaction): string { + private prepareInnerTransactionForRelayedV1(innerTransaction: Transaction): string { const txObject = { nonce: innerTransaction.nonce, - sender: Address.newFromBech32(innerTransaction.sender).getPublicKey().toString("base64"), - receiver: Address.newFromBech32(innerTransaction.receiver).getPublicKey().toString("base64"), + sender: innerTransaction.sender.getPublicKey().toString("base64"), + receiver: innerTransaction.receiver.getPublicKey().toString("base64"), value: innerTransaction.value, gasPrice: innerTransaction.gasPrice, gasLimit: innerTransaction.gasLimit, @@ -98,10 +97,10 @@ export class RelayedTransactionsFactory { signature: Buffer.from(innerTransaction.signature).toString("base64"), chainID: Buffer.from(innerTransaction.chainID).toString("base64"), version: innerTransaction.version, - options: innerTransaction.options.valueOf() == 0 ? undefined : innerTransaction.options, - guardian: innerTransaction.guardian - ? Address.newFromBech32(innerTransaction.guardian).getPublicKey().toString("base64") - : undefined, + options: innerTransaction.options == 0 ? undefined : innerTransaction.options, + guardian: innerTransaction.guardian.isEmpty() + ? undefined + : innerTransaction.guardian.getPublicKey().toString("base64"), guardianSignature: innerTransaction.guardianSignature.length ? Buffer.from(innerTransaction.guardianSignature).toString("base64") : undefined, @@ -112,7 +111,6 @@ export class RelayedTransactionsFactory { ? Buffer.from(innerTransaction.receiverUsername).toString("base64") : undefined, }; - return JSONbig.stringify(txObject); } } diff --git a/src/relayed/resources.ts b/src/relayed/resources.ts index cb049465..e4d0589f 100644 --- a/src/relayed/resources.ts +++ b/src/relayed/resources.ts @@ -1,7 +1,7 @@ -import { ITransaction } from "../interface"; +import { Transaction } from "../transaction"; -export type RelayedV1TransactionInput = { innerTransaction: ITransaction }; +export type RelayedV1TransactionInput = { innerTransaction: Transaction }; export type RelayedV2TransactionInput = { - innerTransaction: ITransaction; + innerTransaction: Transaction; innerTransactionGasLimit: bigint; }; diff --git a/src/smartContracts/smartContractTransactionsFactory.spec.ts b/src/smartContracts/smartContractTransactionsFactory.spec.ts index eae01d4c..7e72d63d 100644 --- a/src/smartContracts/smartContractTransactionsFactory.spec.ts +++ b/src/smartContracts/smartContractTransactionsFactory.spec.ts @@ -62,8 +62,14 @@ describe("test smart contract transactions factory", function () { arguments: args, }); - assert.equal(transaction.sender, "erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"); - assert.equal(transaction.receiver, "erd1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq6gq4hu"); + assert.deepEqual( + transaction.sender, + Address.newFromBech32("erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"), + ); + assert.deepEqual( + transaction.receiver, + Address.newFromBech32("erd1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq6gq4hu"), + ); assert.deepEqual(transaction.data, Buffer.from(`${bytecode}@0500@0504@01`)); assert.equal(transaction.gasLimit.valueOf(), gasLimit); assert.equal(transaction.value, 0n); @@ -92,8 +98,14 @@ describe("test smart contract transactions factory", function () { arguments: args, }); - assert.equal(transaction.sender, "erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"); - assert.equal(transaction.receiver, "erd1qqqqqqqqqqqqqpgqhy6nl6zq07rnzry8uyh6rtyq0uzgtk3e69fqgtz9l4"); + assert.deepEqual( + transaction.sender, + Address.newFromBech32("erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"), + ); + assert.deepEqual( + transaction.receiver, + Address.newFromBech32("erd1qqqqqqqqqqqqqpgqhy6nl6zq07rnzry8uyh6rtyq0uzgtk3e69fqgtz9l4"), + ); assert.deepEqual(transaction.data, Buffer.from("add@07")); assert.equal(transaction.gasLimit, gasLimit); assert.equal(transaction.value, 0n); @@ -124,8 +136,14 @@ describe("test smart contract transactions factory", function () { nativeTransferAmount: egldAmount, }); - assert.equal(transaction.sender, "erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"); - assert.equal(transaction.receiver, "erd1qqqqqqqqqqqqqpgqhy6nl6zq07rnzry8uyh6rtyq0uzgtk3e69fqgtz9l4"); + assert.deepEqual( + transaction.sender, + Address.newFromBech32("erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"), + ); + assert.deepEqual( + transaction.receiver, + Address.newFromBech32("erd1qqqqqqqqqqqqqpgqhy6nl6zq07rnzry8uyh6rtyq0uzgtk3e69fqgtz9l4"), + ); assert.deepEqual(transaction.data, Buffer.from("add@07")); assert.equal(transaction.gasLimit, gasLimit); assert.equal(transaction.value, 1000000000000000000n); @@ -158,8 +176,14 @@ describe("test smart contract transactions factory", function () { tokenTransfers: [transfer], }); - assert.equal(transaction.sender, "erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"); - assert.equal(transaction.receiver, "erd1qqqqqqqqqqqqqpgqhy6nl6zq07rnzry8uyh6rtyq0uzgtk3e69fqgtz9l4"); + assert.deepEqual( + transaction.sender, + Address.newFromBech32("erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"), + ); + assert.deepEqual( + transaction.receiver, + Address.newFromBech32("erd1qqqqqqqqqqqqqpgqhy6nl6zq07rnzry8uyh6rtyq0uzgtk3e69fqgtz9l4"), + ); assert.deepEqual(transaction.data, Buffer.from("ESDTTransfer@464f4f2d366365313762@0a@616464@07")); assert.equal(transaction.gasLimit, gasLimit); assert.equal(transaction.value, 0n); @@ -195,8 +219,14 @@ describe("test smart contract transactions factory", function () { tokenTransfers: [fooTransfer, barTransfer], }); - assert.equal(transaction.sender, "erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"); - assert.equal(transaction.receiver, "erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"); + assert.deepEqual( + transaction.sender, + Address.newFromBech32("erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"), + ); + assert.deepEqual( + transaction.receiver, + Address.newFromBech32("erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"), + ); assert.deepEqual( transaction.data, @@ -237,8 +267,14 @@ describe("test smart contract transactions factory", function () { tokenTransfers: [transfer], }); - assert.equal(transaction.sender, "erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"); - assert.equal(transaction.receiver, "erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"); + assert.deepEqual( + transaction.sender, + Address.newFromBech32("erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"), + ); + assert.deepEqual( + transaction.receiver, + Address.newFromBech32("erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"), + ); assert.isDefined(transaction.data); assert.deepEqual( @@ -282,8 +318,14 @@ describe("test smart contract transactions factory", function () { tokenTransfers: [firstTransfer, secondTransfer], }); - assert.equal(transaction.sender, "erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"); - assert.equal(transaction.receiver, "erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"); + assert.deepEqual( + transaction.sender, + Address.newFromBech32("erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"), + ); + assert.deepEqual( + transaction.receiver, + Address.newFromBech32("erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"), + ); assert.isDefined(transaction.data); assert.deepEqual( @@ -329,8 +371,14 @@ describe("test smart contract transactions factory", function () { tokenTransfers: [firstTransfer, secondTransfer], }); - assert.equal(transaction.sender, "erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"); - assert.equal(transaction.receiver, "erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"); + assert.deepEqual( + transaction.sender, + Address.newFromBech32("erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"), + ); + assert.deepEqual( + transaction.receiver, + Address.newFromBech32("erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"), + ); assert.isDefined(transaction.data); assert.deepEqual( @@ -366,8 +414,14 @@ describe("test smart contract transactions factory", function () { arguments: args, }); - assert.equal(transaction.sender, "erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"); - assert.equal(transaction.receiver, "erd1qqqqqqqqqqqqqpgqhy6nl6zq07rnzry8uyh6rtyq0uzgtk3e69fqgtz9l4"); + assert.deepEqual( + transaction.sender, + Address.newFromBech32("erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"), + ); + assert.deepEqual( + transaction.receiver, + Address.newFromBech32("erd1qqqqqqqqqqqqqpgqhy6nl6zq07rnzry8uyh6rtyq0uzgtk3e69fqgtz9l4"), + ); assert.deepEqual(transaction.data!, Buffer.from(`upgradeContract@${bytecode}@0504@07`)); assert.equal(transaction.gasLimit, gasLimit); assert.equal(transaction.value, 0n); @@ -472,16 +526,22 @@ describe("test smart contract transactions factory", function () { }); it("should create 'Transaction' for claiming developer rewards", async function () { - const sender = Address.fromBech32("erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"); - const contract = Address.fromBech32("erd1qqqqqqqqqqqqqpgqhy6nl6zq07rnzry8uyh6rtyq0uzgtk3e69fqgtz9l4"); + const sender = Address.newFromBech32("erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"); + const contract = Address.newFromBech32("erd1qqqqqqqqqqqqqpgqhy6nl6zq07rnzry8uyh6rtyq0uzgtk3e69fqgtz9l4"); const transaction = factory.createTransactionForClaimingDeveloperRewards({ sender: sender, contract: contract, }); - assert.equal(transaction.sender, "erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"); - assert.equal(transaction.receiver, "erd1qqqqqqqqqqqqqpgqhy6nl6zq07rnzry8uyh6rtyq0uzgtk3e69fqgtz9l4"); + assert.deepEqual( + transaction.sender, + Address.newFromBech32("erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"), + ); + assert.deepEqual( + transaction.receiver, + Address.newFromBech32("erd1qqqqqqqqqqqqqpgqhy6nl6zq07rnzry8uyh6rtyq0uzgtk3e69fqgtz9l4"), + ); assert.equal(Buffer.from(transaction.data).toString(), "ClaimDeveloperRewards"); assert.equal(transaction.gasLimit, 6000000n); assert.equal(transaction.value, 0n); @@ -498,8 +558,14 @@ describe("test smart contract transactions factory", function () { newOwner: newOwner, }); - assert.equal(transaction.sender, "erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"); - assert.equal(transaction.receiver, "erd1qqqqqqqqqqqqqpgqhy6nl6zq07rnzry8uyh6rtyq0uzgtk3e69fqgtz9l4"); + assert.deepEqual( + transaction.sender, + Address.newFromBech32("erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"), + ); + assert.deepEqual( + transaction.receiver, + Address.newFromBech32("erd1qqqqqqqqqqqqqpgqhy6nl6zq07rnzry8uyh6rtyq0uzgtk3e69fqgtz9l4"), + ); assert.equal( Buffer.from(transaction.data).toString(), "ChangeOwnerAddress@8049d639e5a6980d1cd2392abcce41029cda74a1563523a202f09641cc2618f8", diff --git a/src/smartContracts/smartContractTransactionsFactory.ts b/src/smartContracts/smartContractTransactionsFactory.ts index 899904d0..dd0cb261 100644 --- a/src/smartContracts/smartContractTransactionsFactory.ts +++ b/src/smartContracts/smartContractTransactionsFactory.ts @@ -185,7 +185,7 @@ export class SmartContractTransactionsFactory { contract: Address; newOwner: Address; }): Transaction { - const dataParts = ["ChangeOwnerAddress", Address.fromBech32(options.newOwner.bech32()).toHex()]; + const dataParts = ["ChangeOwnerAddress", Address.fromBech32(options.newOwner.toBech32()).toHex()]; return new TransactionBuilder({ config: this.config, diff --git a/src/testutils/mockNetworkProvider.ts b/src/testutils/mockNetworkProvider.ts index b73f48d5..053b7754 100644 --- a/src/testutils/mockNetworkProvider.ts +++ b/src/testutils/mockNetworkProvider.ts @@ -2,7 +2,6 @@ import { Address } from "../address"; import { AsyncTimer } from "../asyncTimer"; import * as errors from "../errors"; import { ErrMock } from "../errors"; -import { ITransaction } from "../interface"; import { IAccountOnNetwork } from "../interfaceOfNetwork"; import { AccountOnNetwork, @@ -39,18 +38,18 @@ export class MockNetworkProvider implements INetworkProvider { this.accounts = new Map(); this.accounts.set( - MockNetworkProvider.AddressOfAlice.bech32(), + MockNetworkProvider.AddressOfAlice.toBech32(), new AccountOnNetwork({ nonce: 0, balance: createAccountBalance(1000), }), ); this.accounts.set( - MockNetworkProvider.AddressOfBob.bech32(), + MockNetworkProvider.AddressOfBob.toBech32(), new AccountOnNetwork({ nonce: 5, balance: createAccountBalance(500) }), ); this.accounts.set( - MockNetworkProvider.AddressOfCarol.bech32(), + MockNetworkProvider.AddressOfCarol.toBech32(), new AccountOnNetwork({ nonce: 42, balance: createAccountBalance(300), @@ -88,7 +87,7 @@ export class MockNetworkProvider implements INetworkProvider { ): Promise { throw new Error("Method not implemented."); } - sendTransactions(_txs: ITransaction[]): Promise { + sendTransactions(_txs: Transaction[]): Promise { throw new Error("Method not implemented."); } getDefinitionOfFungibleToken(_tokenIdentifier: string): Promise { @@ -108,7 +107,7 @@ export class MockNetworkProvider implements INetworkProvider { } mockUpdateAccount(address: Address, mutate: (item: IAccountOnNetwork) => void) { - let account = this.accounts.get(address.bech32()); + let account = this.accounts.get(address.toBech32()); if (account) { mutate(account); } @@ -173,8 +172,8 @@ export class MockNetworkProvider implements INetworkProvider { } } - async getAccount(address: IAddress): Promise { - let account = this.accounts.get(address.bech32()); + async getAccount(address: Address): Promise { + let account = this.accounts.get(address.toBech32()); if (account) { return account; } @@ -188,7 +187,7 @@ export class MockNetworkProvider implements INetworkProvider { new TransactionOnNetwork({ sender: transaction.getSender(), receiver: transaction.getReceiver(), - data: transaction.getData().valueOf(), + data: Buffer.from(transaction.data), status: new TransactionStatus("pending"), }), ); diff --git a/src/testutils/utils.ts b/src/testutils/utils.ts index b6d90eac..636c107a 100644 --- a/src/testutils/utils.ts +++ b/src/testutils/utils.ts @@ -2,7 +2,6 @@ import BigNumber from "bignumber.js"; import * as fs from "fs"; import { PathLike } from "fs"; import { AbiRegistry, Code, SmartContract, TypedValue } from "../abi"; -import { IChainID, IGasLimit } from "../interface"; import { Transaction } from "../transaction"; import { TransactionWatcher } from "../transactionWatcher"; import { getAxios } from "../utils"; @@ -13,8 +12,8 @@ export async function prepareDeployment(obj: { contract: SmartContract; codePath: string; initArguments: TypedValue[]; - gasLimit: IGasLimit; - chainID: IChainID; + gasLimit: bigint; + chainID: string; }): Promise { let contract = obj.contract; let deployer = obj.deployer; diff --git a/src/tokenManagement/resources.ts b/src/tokenManagement/resources.ts index 1297652e..90c38eb3 100644 --- a/src/tokenManagement/resources.ts +++ b/src/tokenManagement/resources.ts @@ -1,5 +1,4 @@ import { Address } from "../address"; -import { IAddress } from "../interface"; export type IssueFungibleInput = IssueInput & { initialSupply: bigint; numDecimals: bigint }; @@ -19,7 +18,7 @@ export type IssueInput = { }; export type FungibleSpecialRoleInput = { - user: IAddress; + user: Address; tokenIdentifier: string; addRoleLocalMint: boolean; addRoleLocalBurn: boolean; @@ -28,7 +27,7 @@ export type FungibleSpecialRoleInput = { export type SemiFungibleSpecialRoleInput = SpecialRoleInput & { addRoleNFTAddQuantity: boolean }; export type SpecialRoleInput = { - user: IAddress; + user: Address; tokenIdentifier: string; addRoleNFTCreate: boolean; addRoleNFTBurn: boolean; @@ -50,7 +49,7 @@ export type MintInput = { attributes: Uint8Array; uris: string[]; }; -export type ManagementInput = { user: IAddress; tokenIdentifier: string }; +export type ManagementInput = { user: Address; tokenIdentifier: string }; export type PausingInput = { tokenIdentifier: string }; export type LocalBurnInput = { tokenIdentifier: string; supplyToBurn: bigint }; export type LocalMintInput = { tokenIdentifier: string; supplyToMint: bigint }; diff --git a/src/tokenManagement/tokenManagementTransactionFactory.spec.ts b/src/tokenManagement/tokenManagementTransactionFactory.spec.ts index 9808d41a..904fa4f2 100644 --- a/src/tokenManagement/tokenManagementTransactionFactory.spec.ts +++ b/src/tokenManagement/tokenManagementTransactionFactory.spec.ts @@ -25,8 +25,11 @@ describe("test token management transactions factory", () => { }); assert.deepEqual(transaction.data, Buffer.from("registerAndSetAllRoles@54455354@54455354@464e47@02")); - assert.equal(transaction.sender, frank.address.toString()); - assert.equal(transaction.receiver, "erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqzllls8a5w6u"); + assert.deepEqual(transaction.sender, frank.address); + assert.deepEqual( + transaction.receiver, + Address.newFromBech32("erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqzllls8a5w6u"), + ); assert.deepEqual(transaction.value, config.issueCost); assert.deepEqual(transaction.gasLimit, 60125000n); }); @@ -51,8 +54,8 @@ describe("test token management transactions factory", () => { "issue@4652414e4b@4652414e4b@64@@63616e467265657a65@74727565@63616e57697065@74727565@63616e5061757365@74727565@63616e4368616e67654f776e6572@74727565@63616e55706772616465@66616c7365@63616e4164645370656369616c526f6c6573@66616c7365", ), ); - assert.equal(transaction.sender, frank.address.toString()); - assert.equal(transaction.receiver, Address.newFromHex(ESDT_CONTRACT_ADDRESS_HEX, config.addressHrp).toBech32()); + assert.deepEqual(transaction.sender, frank.address); + assert.deepEqual(transaction.receiver, Address.newFromHex(ESDT_CONTRACT_ADDRESS_HEX, config.addressHrp)); assert.deepEqual(transaction.value, config.issueCost); }); @@ -75,8 +78,8 @@ describe("test token management transactions factory", () => { "issueSemiFungible@4652414e4b@4652414e4b@63616e467265657a65@74727565@63616e57697065@74727565@63616e5061757365@74727565@63616e5472616e736665724e4654437265617465526f6c65@74727565@63616e4368616e67654f776e6572@74727565@63616e55706772616465@66616c7365@63616e4164645370656369616c526f6c6573@66616c7365", ), ); - assert.equal(transaction.sender, frank.address.toString()); - assert.equal(transaction.receiver, Address.newFromHex(ESDT_CONTRACT_ADDRESS_HEX, config.addressHrp).toBech32()); + assert.deepEqual(transaction.sender, frank.address); + assert.deepEqual(transaction.receiver, Address.newFromHex(ESDT_CONTRACT_ADDRESS_HEX, config.addressHrp)); assert.deepEqual(transaction.value, config.issueCost); }); @@ -99,8 +102,8 @@ describe("test token management transactions factory", () => { "issueNonFungible@4652414e4b@4652414e4b@63616e467265657a65@74727565@63616e57697065@74727565@63616e5061757365@74727565@63616e5472616e736665724e4654437265617465526f6c65@74727565@63616e4368616e67654f776e6572@74727565@63616e55706772616465@66616c7365@63616e4164645370656369616c526f6c6573@66616c7365", ), ); - assert.equal(transaction.sender, frank.address.toString()); - assert.equal(transaction.receiver, Address.newFromHex(ESDT_CONTRACT_ADDRESS_HEX, config.addressHrp).toBech32()); + assert.deepEqual(transaction.sender, frank.address); + assert.deepEqual(transaction.receiver, Address.newFromHex(ESDT_CONTRACT_ADDRESS_HEX, config.addressHrp)); assert.deepEqual(transaction.value, config.issueCost); }); @@ -124,8 +127,8 @@ describe("test token management transactions factory", () => { "registerMetaESDT@4652414e4b@4652414e4b@0a@63616e467265657a65@74727565@63616e57697065@74727565@63616e5061757365@74727565@63616e5472616e736665724e4654437265617465526f6c65@74727565@63616e4368616e67654f776e6572@74727565@63616e55706772616465@66616c7365@63616e4164645370656369616c526f6c6573@66616c7365", ), ); - assert.equal(transaction.sender, frank.address.toString()); - assert.equal(transaction.receiver, Address.newFromHex(ESDT_CONTRACT_ADDRESS_HEX, config.addressHrp).toBech32()); + assert.deepEqual(transaction.sender, frank.address); + assert.deepEqual(transaction.receiver, Address.newFromHex(ESDT_CONTRACT_ADDRESS_HEX, config.addressHrp)); assert.deepEqual(transaction.value, config.issueCost); }); @@ -147,8 +150,8 @@ describe("test token management transactions factory", () => { "setSpecialRole@4652414e4b2d313163653365@1e8a8b6b49de5b7be10aaa158a5a6a4abb4b56cc08f524bb5e6cd5f211ad3e13@45534454526f6c654c6f63616c4d696e74", ), ); - assert.equal(transaction.sender, frank.address.toString()); - assert.equal(transaction.receiver, Address.newFromHex(ESDT_CONTRACT_ADDRESS_HEX, config.addressHrp).toBech32()); + assert.deepEqual(transaction.sender, frank.address); + assert.deepEqual(transaction.receiver, Address.newFromHex(ESDT_CONTRACT_ADDRESS_HEX, config.addressHrp)); assert.equal(transaction.value, 0n); }); @@ -170,8 +173,8 @@ describe("test token management transactions factory", () => { "setSpecialRole@4652414e4b2d313163653365@1e8a8b6b49de5b7be10aaa158a5a6a4abb4b56cc08f524bb5e6cd5f211ad3e13@45534454526f6c654c6f63616c4d696e74@45534454526f6c654c6f63616c4275726e@455344545472616e73666572526f6c65", ), ); - assert.equal(transaction.sender, frank.address.toString()); - assert.equal(transaction.receiver, Address.newFromHex(ESDT_CONTRACT_ADDRESS_HEX, config.addressHrp).toBech32()); + assert.deepEqual(transaction.sender, frank.address); + assert.deepEqual(transaction.receiver, Address.newFromHex(ESDT_CONTRACT_ADDRESS_HEX, config.addressHrp)); assert.equal(transaction.value, 0n); }); @@ -197,8 +200,8 @@ describe("test token management transactions factory", () => { "setSpecialRole@4652414e4b2d313163653365@1e8a8b6b49de5b7be10aaa158a5a6a4abb4b56cc08f524bb5e6cd5f211ad3e13@45534454526f6c654e4654437265617465@45534454526f6c654e465455706461746541747472696275746573@45534454526f6c654e4654416464555249@45534454526f6c654d6f6469667943726561746f72@45534454526f6c654e46545265637265617465", ), ); - assert.equal(transaction.sender, frank.address.toString()); - assert.equal(transaction.receiver, Address.newFromHex(ESDT_CONTRACT_ADDRESS_HEX, config.addressHrp).toBech32()); + assert.deepEqual(transaction.sender, frank.address); + assert.deepEqual(transaction.receiver, Address.newFromHex(ESDT_CONTRACT_ADDRESS_HEX, config.addressHrp)); assert.equal(transaction.value, 0n); }); @@ -217,8 +220,8 @@ describe("test token management transactions factory", () => { transaction.data, Buffer.from("ESDTNFTCreate@4652414e4b2d616139653864@01@74657374@03e8@61626261@74657374@61@62"), ); - assert.equal(transaction.sender, grace.address.toString()); - assert.equal(transaction.receiver, grace.address.toString()); + assert.deepEqual(transaction.sender, grace.address); + assert.deepEqual(transaction.receiver, grace.address); assert.equal(transaction.value, 0n); }); @@ -230,8 +233,8 @@ describe("test token management transactions factory", () => { }); assert.deepEqual(transaction.data, Buffer.from("ESDTModifyRoyalties@544553542d313233343536@01@04d2")); - assert.equal(transaction.sender, grace.address.toString()); - assert.equal(transaction.receiver, grace.address.toString()); + assert.deepEqual(transaction.sender, grace.address); + assert.deepEqual(transaction.receiver, grace.address); assert.equal(transaction.value, 0n); assert.equal(transaction.gasLimit, 60125000n); }); @@ -247,8 +250,8 @@ describe("test token management transactions factory", () => { transaction.data, Buffer.from("ESDTSetNewURIs@544553542d313233343536@01@6669727374555249@7365636f6e64555249"), ); - assert.equal(transaction.sender, grace.address.toString()); - assert.equal(transaction.receiver, grace.address.toString()); + assert.deepEqual(transaction.sender, grace.address); + assert.deepEqual(transaction.receiver, grace.address); assert.equal(transaction.value, 0n); assert.equal(transaction.gasLimit, 60164000n); }); @@ -260,8 +263,8 @@ describe("test token management transactions factory", () => { }); assert.deepEqual(transaction.data, Buffer.from("ESDTModifyCreator@544553542d313233343536@01")); - assert.equal(transaction.sender, grace.address.toString()); - assert.equal(transaction.receiver, grace.address.toString()); + assert.deepEqual(transaction.sender, grace.address); + assert.deepEqual(transaction.receiver, grace.address); assert.equal(transaction.value, 0n); assert.equal(transaction.gasLimit, 60114500n); }); @@ -283,8 +286,8 @@ describe("test token management transactions factory", () => { "ESDTMetaDataUpdate@544553542d313233343536@01@54657374@04d2@61626261@74657374@6669727374555249@7365636f6e64555249", ), ); - assert.equal(transaction.sender, grace.address.toString()); - assert.equal(transaction.receiver, grace.address.toString()); + assert.deepEqual(transaction.sender, grace.address); + assert.deepEqual(transaction.receiver, grace.address); assert.equal(transaction.value, 0n); assert.equal(transaction.gasLimit, 60218000n); }); @@ -306,8 +309,8 @@ describe("test token management transactions factory", () => { "ESDTMetaDataRecreate@544553542d313233343536@01@54657374@04d2@61626261@74657374@6669727374555249@7365636f6e64555249", ), ); - assert.equal(transaction.sender, grace.address.toString()); - assert.equal(transaction.receiver, grace.address.toString()); + assert.deepEqual(transaction.sender, grace.address); + assert.deepEqual(transaction.receiver, grace.address); assert.equal(transaction.value, 0n); assert.equal(transaction.gasLimit, 60221000n); }); @@ -318,8 +321,8 @@ describe("test token management transactions factory", () => { }); assert.deepEqual(transaction.data, Buffer.from("changeToDynamic@544553542d313233343536")); - assert.equal(transaction.sender, grace.address.toString()); - assert.equal(transaction.receiver, Address.newFromHex(ESDT_CONTRACT_ADDRESS_HEX, config.addressHrp).toBech32()); + assert.deepEqual(transaction.sender, grace.address); + assert.deepEqual(transaction.receiver, Address.newFromHex(ESDT_CONTRACT_ADDRESS_HEX, config.addressHrp)); assert.equal(transaction.value, 0n); assert.equal(transaction.gasLimit, 60107000n); }); @@ -330,8 +333,8 @@ describe("test token management transactions factory", () => { }); assert.deepEqual(transaction.data, Buffer.from("updateTokenID@544553542d313233343536")); - assert.equal(transaction.sender, grace.address.toString()); - assert.equal(transaction.receiver, Address.newFromHex(ESDT_CONTRACT_ADDRESS_HEX, config.addressHrp).toBech32()); + assert.deepEqual(transaction.sender, grace.address); + assert.deepEqual(transaction.receiver, Address.newFromHex(ESDT_CONTRACT_ADDRESS_HEX, config.addressHrp)); assert.equal(transaction.value, 0n); assert.equal(transaction.gasLimit, 60104000n); }); @@ -344,8 +347,8 @@ describe("test token management transactions factory", () => { }); assert.deepEqual(transaction.data, Buffer.from("registerDynamic@54657374@544553542d313233343536@464e47")); - assert.equal(transaction.sender, grace.address.toString()); - assert.equal(transaction.receiver, Address.newFromHex(ESDT_CONTRACT_ADDRESS_HEX, config.addressHrp).toBech32()); + assert.deepEqual(transaction.sender, grace.address); + assert.deepEqual(transaction.receiver, Address.newFromHex(ESDT_CONTRACT_ADDRESS_HEX, config.addressHrp)); assert.equal(transaction.value, 50000000000000000n); assert.equal(transaction.gasLimit, 60131000n); }); @@ -364,8 +367,8 @@ describe("test token management transactions factory", () => { transaction.data, Buffer.from("registerAndSetAllRolesDynamic@54657374@544553542d313233343536@464e47"), ); - assert.equal(transaction.sender, grace.address.toString()); - assert.equal(transaction.receiver, Address.newFromHex(ESDT_CONTRACT_ADDRESS_HEX, config.addressHrp).toBech32()); + assert.deepEqual(transaction.sender, grace.address); + assert.deepEqual(transaction.receiver, Address.newFromHex(ESDT_CONTRACT_ADDRESS_HEX, config.addressHrp)); assert.equal(transaction.value, 50000000000000000n); assert.equal(transaction.gasLimit, 60152000n); }); diff --git a/src/tokenManagement/tokenManagementTransactionsFactory.ts b/src/tokenManagement/tokenManagementTransactionsFactory.ts index 2b161ee2..b87ab9fb 100644 --- a/src/tokenManagement/tokenManagementTransactionsFactory.ts +++ b/src/tokenManagement/tokenManagementTransactionsFactory.ts @@ -2,7 +2,6 @@ import { AddressValue, ArgSerializer, BigUIntValue, BytesValue, StringValue } fr import { Address } from "../address"; import { ESDT_CONTRACT_ADDRESS_HEX } from "../constants"; import { ErrBadUsage } from "../errors"; -import { IAddress } from "../interface"; import { Logger } from "../logger"; import { Transaction } from "../transaction"; import { TransactionBuilder } from "../transactionBuilder"; @@ -55,7 +54,7 @@ export class TokenManagementTransactionsFactory { this.esdtContractAddress = Address.fromHex(ESDT_CONTRACT_ADDRESS_HEX, this.config.addressHrp); } - createTransactionForIssuingFungible(sender: IAddress, options: resources.IssueFungibleInput): Transaction { + createTransactionForIssuingFungible(sender: Address, options: resources.IssueFungibleInput): Transaction { this.notifyAboutUnsettingBurnRoleGlobally(); const args = [ @@ -90,7 +89,7 @@ export class TokenManagementTransactionsFactory { }).build(); } - createTransactionForIssuingSemiFungible(sender: IAddress, options: resources.IssueSemiFungibleInput): Transaction { + createTransactionForIssuingSemiFungible(sender: Address, options: resources.IssueSemiFungibleInput): Transaction { this.notifyAboutUnsettingBurnRoleGlobally(); const args = [ @@ -125,7 +124,7 @@ export class TokenManagementTransactionsFactory { }).build(); } - createTransactionForIssuingNonFungible(sender: IAddress, options: resources.IssueNonFungibleInput): Transaction { + createTransactionForIssuingNonFungible(sender: Address, options: resources.IssueNonFungibleInput): Transaction { this.notifyAboutUnsettingBurnRoleGlobally(); const args = [ @@ -160,7 +159,7 @@ export class TokenManagementTransactionsFactory { }).build(); } - createTransactionForRegisteringMetaESDT(sender: IAddress, options: resources.RegisterMetaESDTInput): Transaction { + createTransactionForRegisteringMetaESDT(sender: Address, options: resources.RegisterMetaESDTInput): Transaction { this.notifyAboutUnsettingBurnRoleGlobally(); const args = [ @@ -197,7 +196,7 @@ export class TokenManagementTransactionsFactory { } createTransactionForRegisteringAndSettingRoles( - sender: IAddress, + sender: Address, options: resources.RegisterRolesInput, ): Transaction { this.notifyAboutUnsettingBurnRoleGlobally(); @@ -224,7 +223,7 @@ export class TokenManagementTransactionsFactory { } createTransactionForSettingBurnRoleGlobally( - sender: IAddress, + sender: Address, options: resources.BurnRoleGloballyInput, ): Transaction { const dataParts = [ @@ -243,7 +242,7 @@ export class TokenManagementTransactionsFactory { } createTransactionForUnsettingBurnRoleGlobally( - sender: IAddress, + sender: Address, options: resources.BurnRoleGloballyInput, ): Transaction { const dataParts = [ @@ -262,7 +261,7 @@ export class TokenManagementTransactionsFactory { } createTransactionForSettingSpecialRoleOnFungibleToken( - sender: IAddress, + sender: Address, options: resources.FungibleSpecialRoleInput, ): Transaction { const args = [new StringValue(options.tokenIdentifier), new AddressValue(options.user)]; @@ -284,7 +283,7 @@ export class TokenManagementTransactionsFactory { } createTransactionForSettingSpecialRoleOnSemiFungibleToken( - sender: IAddress, + sender: Address, options: resources.SemiFungibleSpecialRoleInput, ): Transaction { const args = [new StringValue(options.tokenIdentifier), new AddressValue(options.user)]; @@ -308,14 +307,14 @@ export class TokenManagementTransactionsFactory { } createTransactionForSettingSpecialRoleOnMetaESDT( - sender: IAddress, + sender: Address, options: resources.SemiFungibleSpecialRoleInput, ): Transaction { return this.createTransactionForSettingSpecialRoleOnSemiFungibleToken(sender, options); } createTransactionForSettingSpecialRoleOnNonFungibleToken( - sender: IAddress, + sender: Address, options: resources.SpecialRoleInput, ): Transaction { const args = [new StringValue(options.tokenIdentifier), new AddressValue(options.user)]; @@ -342,7 +341,7 @@ export class TokenManagementTransactionsFactory { }).build(); } - createTransactionForCreatingNFT(sender: IAddress, options: resources.MintInput): Transaction { + createTransactionForCreatingNFT(sender: Address, options: resources.MintInput): Transaction { const dataParts = [ "ESDTNFTCreate", ...this.argSerializer.valuesToStrings([ @@ -370,7 +369,7 @@ export class TokenManagementTransactionsFactory { }).build(); } - createTransactionForPausing(sender: IAddress, options: resources.PausingInput): Transaction { + createTransactionForPausing(sender: Address, options: resources.PausingInput): Transaction { const dataParts = ["pause", ...this.argSerializer.valuesToStrings([new StringValue(options.tokenIdentifier)])]; return new TransactionBuilder({ @@ -383,7 +382,7 @@ export class TokenManagementTransactionsFactory { }).build(); } - createTransactionForUnpausing(sender: IAddress, options: resources.PausingInput): Transaction { + createTransactionForUnpausing(sender: Address, options: resources.PausingInput): Transaction { const dataParts = [ "unPause", ...this.argSerializer.valuesToStrings([new StringValue(options.tokenIdentifier)]), @@ -399,7 +398,7 @@ export class TokenManagementTransactionsFactory { }).build(); } - createTransactionForFreezing(sender: IAddress, options: resources.ManagementInput): Transaction { + createTransactionForFreezing(sender: Address, options: resources.ManagementInput): Transaction { const dataParts = [ "freeze", ...this.argSerializer.valuesToStrings([ @@ -418,7 +417,7 @@ export class TokenManagementTransactionsFactory { }).build(); } - createTransactionForUnfreezing(sender: IAddress, options: resources.ManagementInput): Transaction { + createTransactionForUnfreezing(sender: Address, options: resources.ManagementInput): Transaction { const dataParts = [ "UnFreeze", ...this.argSerializer.valuesToStrings([ @@ -437,7 +436,7 @@ export class TokenManagementTransactionsFactory { }).build(); } - createTransactionForWiping(sender: IAddress, options: resources.ManagementInput): Transaction { + createTransactionForWiping(sender: Address, options: resources.ManagementInput): Transaction { const dataParts = [ "wipe", ...this.argSerializer.valuesToStrings([ @@ -456,7 +455,7 @@ export class TokenManagementTransactionsFactory { }).build(); } - createTransactionForLocalMint(sender: IAddress, options: resources.LocalMintInput): Transaction { + createTransactionForLocalMint(sender: Address, options: resources.LocalMintInput): Transaction { const dataParts = [ "ESDTLocalMint", ...this.argSerializer.valuesToStrings([ @@ -475,7 +474,7 @@ export class TokenManagementTransactionsFactory { }).build(); } - createTransactionForLocalBurning(sender: IAddress, options: resources.LocalBurnInput): Transaction { + createTransactionForLocalBurning(sender: Address, options: resources.LocalBurnInput): Transaction { const dataParts = [ "ESDTLocalBurn", ...this.argSerializer.valuesToStrings([ @@ -494,7 +493,7 @@ export class TokenManagementTransactionsFactory { }).build(); } - createTransactionForUpdatingAttributes(sender: IAddress, options: resources.UpdateAttributesInput): Transaction { + createTransactionForUpdatingAttributes(sender: Address, options: resources.UpdateAttributesInput): Transaction { const dataParts = [ "ESDTNFTUpdateAttributes", ...this.argSerializer.valuesToStrings([ @@ -514,7 +513,7 @@ export class TokenManagementTransactionsFactory { }).build(); } - createTransactionForAddingQuantity(sender: IAddress, options: resources.UpdateQuantityInput): Transaction { + createTransactionForAddingQuantity(sender: Address, options: resources.UpdateQuantityInput): Transaction { const dataParts = [ "ESDTNFTAddQuantity", ...this.argSerializer.valuesToStrings([ @@ -534,7 +533,7 @@ export class TokenManagementTransactionsFactory { }).build(); } - createTransactionForBurningQuantity(sender: IAddress, options: resources.UpdateQuantityInput): Transaction { + createTransactionForBurningQuantity(sender: Address, options: resources.UpdateQuantityInput): Transaction { const dataParts = [ "ESDTNFTBurn", ...this.argSerializer.valuesToStrings([ @@ -554,7 +553,7 @@ export class TokenManagementTransactionsFactory { }).build(); } - createTransactionForModifyingRoyalties(sender: IAddress, options: resources.ModifyRoyaltiesInput): Transaction { + createTransactionForModifyingRoyalties(sender: Address, options: resources.ModifyRoyaltiesInput): Transaction { const dataParts = [ "ESDTModifyRoyalties", ...this.argSerializer.valuesToStrings([ @@ -574,7 +573,7 @@ export class TokenManagementTransactionsFactory { }).build(); } - createTransactionForSettingNewUris(sender: IAddress, options: resources.SetNewUriInput): Transaction { + createTransactionForSettingNewUris(sender: Address, options: resources.SetNewUriInput): Transaction { if (!options.newUris.length) { throw new ErrBadUsage("No URIs provided"); } @@ -598,7 +597,7 @@ export class TokenManagementTransactionsFactory { }).build(); } - createTransactionForModifyingCreator(sender: IAddress, options: resources.ModifyCreatorInput): Transaction { + createTransactionForModifyingCreator(sender: Address, options: resources.ModifyCreatorInput): Transaction { const dataParts = [ "ESDTModifyCreator", ...this.argSerializer.valuesToStrings([ @@ -617,7 +616,7 @@ export class TokenManagementTransactionsFactory { }).build(); } - createTransactionForUpdatingMetadata(sender: IAddress, options: resources.ManageMetadataInput): Transaction { + createTransactionForUpdatingMetadata(sender: Address, options: resources.ManageMetadataInput): Transaction { const dataParts = [ "ESDTMetaDataUpdate", ...this.argSerializer.valuesToStrings([ @@ -641,7 +640,7 @@ export class TokenManagementTransactionsFactory { }).build(); } - createTransactionForMetadataRecreate(sender: IAddress, options: resources.ManageMetadataInput): Transaction { + createTransactionForMetadataRecreate(sender: Address, options: resources.ManageMetadataInput): Transaction { const dataParts = [ "ESDTMetaDataRecreate", ...this.argSerializer.valuesToStrings([ @@ -666,7 +665,7 @@ export class TokenManagementTransactionsFactory { } createTransactionForChangingTokenToDynamic( - sender: IAddress, + sender: Address, options: resources.ChangeTokenToDynamicInput, ): Transaction { const dataParts = [ @@ -684,7 +683,7 @@ export class TokenManagementTransactionsFactory { }).build(); } - createTransactionForUpdatingTokenId(sender: IAddress, options: resources.UpdateTokenIDInput): Transaction { + createTransactionForUpdatingTokenId(sender: Address, options: resources.UpdateTokenIDInput): Transaction { const dataParts = [ "updateTokenID", ...this.argSerializer.valuesToStrings([new StringValue(options.tokenIdentifier)]), @@ -701,7 +700,7 @@ export class TokenManagementTransactionsFactory { } createTransactionForRegisteringDynamicToken( - sender: IAddress, + sender: Address, options: resources.RegisteringDynamicTokenInput, ): Transaction { const dataParts = [ @@ -725,7 +724,7 @@ export class TokenManagementTransactionsFactory { } createTransactionForRegisteringDynamicAndSettingRoles( - sender: IAddress, + sender: Address, options: resources.RegisteringDynamicTokenInput, ): Transaction { const dataParts = [ diff --git a/src/tokenTransfersDataBuilder.ts b/src/tokenTransfersDataBuilder.ts index 17e416d1..e2a2b2c1 100644 --- a/src/tokenTransfersDataBuilder.ts +++ b/src/tokenTransfersDataBuilder.ts @@ -1,5 +1,5 @@ import { AddressValue, ArgSerializer, BigUIntValue, TokenIdentifierValue, TypedValue, U32Value } from "./abi"; -import { IAddress } from "./interface"; +import { Address } from "./address"; import { TokenComputer, TokenTransfer } from "./tokens"; export class TokenTransfersDataBuilder { @@ -20,7 +20,7 @@ export class TokenTransfersDataBuilder { return ["ESDTTransfer", ...args]; } - buildDataPartsForSingleESDTNFTTransfer(transfer: TokenTransfer, receiver: IAddress) { + buildDataPartsForSingleESDTNFTTransfer(transfer: TokenTransfer, receiver: Address) { const token = transfer.token; const identifier = this.tokenComputer.extractIdentifierFromExtendedIdentifier(token.identifier); @@ -34,7 +34,7 @@ export class TokenTransfersDataBuilder { return ["ESDTNFTTransfer", ...args]; } - buildDataPartsForMultiESDTNFTTransfer(receiver: IAddress, transfers: TokenTransfer[]) { + buildDataPartsForMultiESDTNFTTransfer(receiver: Address, transfers: TokenTransfer[]) { const argsTyped: TypedValue[] = [new AddressValue(receiver), new U32Value(transfers.length)]; for (const transfer of transfers) { diff --git a/src/transaction.local.net.spec.ts b/src/transaction.local.net.spec.ts index 45d3b830..b21667de 100644 --- a/src/transaction.local.net.spec.ts +++ b/src/transaction.local.net.spec.ts @@ -7,7 +7,6 @@ import { createLocalnetProvider } from "./testutils/networkProviders"; 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 { TransferTransactionsFactory } from "./transfers/transferTransactionsFactory"; @@ -40,16 +39,16 @@ describe("test transaction", function () { let transactionOne = new Transaction({ sender: alice.address, receiver: bob.address, - value: TokenTransfer.egldFromAmount(42), - gasLimit: network.MinGasLimit, + value: TokenTransfer.egldFromAmount(42).amount, + gasLimit: BigInt(network.MinGasLimit), chainID: network.ChainID, }); let transactionTwo = new Transaction({ sender: alice.address, receiver: bob.address, - value: TokenTransfer.egldFromAmount(43), - gasLimit: network.MinGasLimit, + value: TokenTransfer.egldFromAmount(43).amount, + gasLimit: BigInt(network.MinGasLimit), chainID: network.ChainID, }); @@ -87,8 +86,8 @@ describe("test transaction", function () { let transactionOne = new Transaction({ sender: alice.address, receiver: bob.address, - value: TokenTransfer.egldFromAmount(42), - gasLimit: network.MinGasLimit, + value: TokenTransfer.egldFromAmount(42).amount, + gasLimit: BigInt(network.MinGasLimit), chainID: network.ChainID, }); @@ -116,19 +115,19 @@ describe("test transaction", function () { let transactionOne = new Transaction({ sender: alice.address, - data: new TransactionPayload("helloWorld"), - gasLimit: 70000, + data: Buffer.from("helloWorld"), + gasLimit: 70000n, receiver: alice.address, - value: TokenTransfer.egldFromAmount(1000), + value: TokenTransfer.egldFromAmount(1000).amount, chainID: network.ChainID, }); let transactionTwo = new Transaction({ sender: alice.address, - data: new TransactionPayload("helloWorld"), - gasLimit: 70000, + data: Buffer.from("helloWorld"), + gasLimit: 70000n, receiver: alice.address, - value: TokenTransfer.egldFromAmount(1000000), + value: TokenTransfer.egldFromAmount(1000000).amount, chainID: network.ChainID, }); diff --git a/src/transaction.spec.ts b/src/transaction.spec.ts index ecf98d5c..b68d12c5 100644 --- a/src/transaction.spec.ts +++ b/src/transaction.spec.ts @@ -1,15 +1,12 @@ -import { UserPublicKey, UserVerifier } from "./wallet"; -import BigNumber from "bignumber.js"; +import { Buffer } from "buffer"; import { assert } from "chai"; import { Address } from "./address"; -import { MIN_TRANSACTION_VERSION_THAT_SUPPORTS_OPTIONS } from "./constants"; -import { TransactionOptions, TransactionVersion } from "./networkParams"; +import { MIN_TRANSACTION_VERSION_THAT_SUPPORTS_OPTIONS, TRANSACTION_OPTIONS_DEFAULT } from "./constants"; import { ProtoSerializer } from "./proto"; import { TestWallet, loadTestWallets } from "./testutils"; -import { TokenTransfer } from "./tokens"; import { Transaction } from "./transaction"; import { TransactionComputer } from "./transactionComputer"; -import { TransactionPayload } from "./transactionPayload"; +import { UserPublicKey, UserVerifier } from "./wallet"; describe("test transaction", async () => { let wallets: Record; @@ -32,8 +29,8 @@ describe("test transaction", async () => { it("should serialize transaction for signing (without data)", async () => { const transaction = new Transaction({ chainID: networkConfig.ChainID, - sender: wallets.alice.address.bech32(), - receiver: wallets.bob.address.bech32(), + sender: wallets.alice.address, + receiver: wallets.bob.address, gasLimit: 50000n, value: 0n, version: 2, @@ -52,8 +49,8 @@ describe("test transaction", async () => { it("should serialize transaction for signing (with data)", async () => { const transaction = new Transaction({ chainID: networkConfig.ChainID, - sender: wallets.alice.address.bech32(), - receiver: wallets.bob.address.bech32(), + sender: wallets.alice.address, + receiver: wallets.bob.address, gasLimit: 70000n, value: 1000000000000000000n, version: 2, @@ -72,12 +69,12 @@ describe("test transaction", async () => { it("should sign transaction (with no data, no value) (legacy)", async () => { const transaction = new Transaction({ - nonce: 89, - value: "0", + nonce: 89n, + value: 0n, sender: wallets.alice.address, receiver: wallets.bob.address, - gasPrice: minGasPrice, - gasLimit: minGasLimit, + gasPrice: BigInt(minGasPrice), + gasLimit: BigInt(minGasLimit), chainID: "local-testnet", }); @@ -95,13 +92,13 @@ describe("test transaction", async () => { it("should sign transaction (with data, no value) (legacy)", async () => { const transaction = new Transaction({ - nonce: 90, - value: "0", + nonce: 90n, + value: 0n, sender: wallets.alice.address, receiver: wallets.bob.address, - gasPrice: minGasPrice, - gasLimit: 80000, - data: new TransactionPayload("hello"), + gasPrice: BigInt(minGasPrice), + gasLimit: 80000n, + data: Buffer.from("hello"), chainID: "local-testnet", }); @@ -120,8 +117,8 @@ describe("test transaction", async () => { it("should sign transaction (with usernames)", async () => { const transaction = new Transaction({ chainID: "T", - sender: wallets.carol.address.bech32(), - receiver: wallets.alice.address.bech32(), + sender: wallets.carol.address, + receiver: wallets.alice.address, gasLimit: 50000n, value: 1000000000000000000n, version: 2, @@ -143,8 +140,8 @@ describe("test transaction", async () => { it("should compute hash", async () => { const transaction = new Transaction({ chainID: networkConfig.ChainID, - sender: wallets.alice.address.bech32(), - receiver: wallets.alice.address.bech32(), + sender: wallets.alice.address, + receiver: wallets.alice.address, gasLimit: 100000n, value: 1000000000000n, version: 2, @@ -168,8 +165,8 @@ describe("test transaction", async () => { it("should compute hash (with usernames)", async () => { const transaction = new Transaction({ chainID: networkConfig.ChainID, - sender: wallets.alice.address.bech32(), - receiver: wallets.alice.address.bech32(), + sender: wallets.alice.address, + receiver: wallets.alice.address, gasLimit: 100000n, value: 1000000000000n, version: 2, @@ -194,16 +191,16 @@ describe("test transaction", async () => { it("should sign & compute hash (with data, with opaque, unused options) (legacy)", async () => { const transaction = new Transaction({ - nonce: 89, - value: "0", + nonce: 89n, + value: 0n, sender: wallets.alice.address, receiver: wallets.bob.address, - gasPrice: minGasPrice, - gasLimit: minGasLimit, + gasPrice: BigInt(minGasPrice), + gasLimit: BigInt(minGasLimit), chainID: "local-testnet", // The protocol ignores the options when version == 1 - version: new TransactionVersion(1), - options: new TransactionOptions(1), + version: 1, + options: 1, }); assert.throws(() => { @@ -213,13 +210,13 @@ describe("test transaction", async () => { it("should sign & compute hash (with data, with value) (legacy)", async () => { const transaction = new Transaction({ - nonce: 91, - value: TokenTransfer.egldFromAmount(10), + nonce: 91n, + value: 10000000000000000000n, sender: wallets.alice.address, receiver: wallets.bob.address, - gasPrice: minGasPrice, - gasLimit: 100000, - data: new TransactionPayload("for the book"), + gasPrice: BigInt(minGasPrice), + gasLimit: 100000n, + data: Buffer.from("for the book"), chainID: "local-testnet", }); @@ -237,13 +234,13 @@ describe("test transaction", async () => { it("should sign & compute hash (with data, with large value) (legacy)", async () => { const transaction = new Transaction({ - nonce: 92, - value: TokenTransfer.egldFromBigInteger("123456789000000000000000000000"), + nonce: 92n, + value: BigInt("123456789000000000000000000000"), sender: wallets.alice.address, receiver: wallets.bob.address, - gasPrice: minGasPrice, - gasLimit: 100000, - data: new TransactionPayload("for the spaceship"), + gasPrice: BigInt(minGasPrice), + gasLimit: 100000n, + data: Buffer.from("for the spaceship"), chainID: "local-testnet", }); @@ -261,15 +258,15 @@ describe("test transaction", async () => { it("should sign & compute hash (with nonce = 0) (legacy)", async () => { const transaction = new Transaction({ - nonce: 0, - value: 0, + nonce: 0n, + value: 0n, sender: wallets.alice.address, receiver: wallets.bob.address, - gasPrice: minGasPrice, - gasLimit: 80000, - data: new TransactionPayload("hello"), + gasPrice: BigInt(minGasPrice), + gasLimit: 80000n, + data: Buffer.from("hello"), chainID: "local-testnet", - version: new TransactionVersion(1), + version: 1, }); transaction.applySignature(await wallets.alice.signer.sign(transaction.serializeForSigning())); @@ -286,12 +283,12 @@ describe("test transaction", async () => { it("should sign & compute hash (without options field, should be omitted) (legacy)", async () => { const transaction = new Transaction({ - nonce: 89, - value: 0, + nonce: 89n, + value: 0n, sender: wallets.alice.address, receiver: wallets.bob.address, - gasPrice: minGasPrice, - gasLimit: minGasLimit, + gasPrice: BigInt(minGasPrice), + gasLimit: BigInt(minGasLimit), chainID: "local-testnet", }); @@ -312,12 +309,12 @@ describe("test transaction", async () => { it("should sign & compute hash (with guardian field, should be omitted) (legacy)", async () => { const transaction = new Transaction({ - nonce: 89, - value: 0, + nonce: 89n, + value: 0n, sender: wallets.alice.address, receiver: wallets.bob.address, - gasPrice: minGasPrice, - gasLimit: minGasLimit, + gasPrice: BigInt(minGasPrice), + gasLimit: BigInt(minGasLimit), chainID: "local-testnet", }); @@ -338,13 +335,13 @@ describe("test transaction", async () => { it("should sign & compute hash (with usernames) (legacy)", async () => { const transaction = new Transaction({ - nonce: 204, - value: "1000000000000000000", + nonce: 204n, + value: 1000000000000000000n, sender: Address.fromBech32("erd1k2s324ww2g0yj38qn2ch2jwctdy8mnfxep94q9arncc6xecg3xaq6mjse8"), receiver: Address.fromBech32("erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"), senderUsername: "carol", receiverUsername: "alice", - gasLimit: 50000, + gasLimit: 50000n, chainID: "T", }); @@ -365,8 +362,8 @@ describe("test transaction", async () => { const transaction = new Transaction({ chainID: "local-testnet", - sender: alice.address.bech32(), - receiver: wallets.bob.address.bech32(), + sender: alice.address, + receiver: wallets.bob.address, gasLimit: 150000n, gasPrice: 1000000000n, data: new Uint8Array(Buffer.from("test data field")), @@ -374,7 +371,7 @@ describe("test transaction", async () => { options: 2, nonce: 92n, value: 123456789000000000000000000000n, - guardian: "erd1x23lzn8483xs2su4fak0r0dqx6w38enpmmqf2yrkylwq7mfnvyhsxqw57y", + guardian: Address.fromBech32("erd1x23lzn8483xs2su4fak0r0dqx6w38enpmmqf2yrkylwq7mfnvyhsxqw57y"), }); transaction.guardianSignature = new Uint8Array(64); transaction.signature = await alice.signer.sign(transactionComputer.computeBytesForSigning(transaction)); @@ -396,12 +393,12 @@ describe("test transaction", async () => { it("computes fee (legacy)", () => { const transaction = new Transaction({ - nonce: 92, - value: TokenTransfer.egldFromBigInteger("123456789000000000000000000000"), + nonce: 92n, + value: BigInt("123456789000000000000000000000"), sender: wallets.alice.address, receiver: wallets.bob.address, - gasPrice: minGasPrice, - gasLimit: minGasLimit, + gasPrice: BigInt(minGasPrice), + gasLimit: BigInt(minGasLimit), chainID: "local-testnet", }); @@ -412,10 +409,10 @@ describe("test transaction", async () => { it("computes fee", async () => { const transaction = new Transaction({ chainID: "D", - sender: wallets.alice.address.bech32(), - receiver: wallets.alice.address.bech32(), + sender: wallets.alice.address, + receiver: wallets.alice.address, gasLimit: 50000n, - gasPrice: minGasPrice, + gasPrice: BigInt(minGasPrice), }); const gasLimit = transactionComputer.computeTransactionFee(transaction, networkConfig); @@ -425,8 +422,8 @@ describe("test transaction", async () => { it("computes fee, but should throw `NotEnoughGas` error", async () => { const transaction = new Transaction({ chainID: networkConfig.ChainID, - sender: wallets.alice.address.bech32(), - receiver: wallets.alice.address.bech32(), + sender: wallets.alice.address, + receiver: wallets.alice.address, gasLimit: 50000n, data: Buffer.from("toolittlegaslimit"), }); @@ -438,13 +435,13 @@ describe("test transaction", async () => { it("computes fee (with data field) (legacy)", () => { let transaction = new Transaction({ - nonce: 92, - value: TokenTransfer.egldFromBigInteger("123456789000000000000000000000"), + nonce: 92n, + value: BigInt("123456789000000000000000000000"), sender: wallets.alice.address, receiver: wallets.bob.address, - data: new TransactionPayload("testdata"), - gasPrice: minGasPrice, - gasLimit: minGasLimit + 12010, + data: Buffer.from("testdata"), + gasPrice: BigInt(minGasPrice), + gasLimit: BigInt(minGasLimit + 12010), chainID: "local-testnet", }); @@ -455,10 +452,10 @@ describe("test transaction", async () => { it("computes fee (with data field)", async () => { const transaction = new Transaction({ chainID: networkConfig.ChainID, - sender: wallets.alice.address.bech32(), - receiver: wallets.alice.address.bech32(), + sender: wallets.alice.address, + receiver: wallets.alice.address, gasLimit: 50000n + 12010n, - gasPrice: minGasPrice, + gasPrice: BigInt(minGasPrice), data: Buffer.from("testdata"), }); @@ -469,15 +466,15 @@ describe("test transaction", async () => { it("should convert transaction to plain object and back", () => { const sender = wallets.alice.address; const transaction = new Transaction({ - nonce: 90, - value: "123456789000000000000000000000", + nonce: 90n, + value: 123456789000000000000000000000n, sender: sender, receiver: wallets.bob.address, senderUsername: "alice", receiverUsername: "bob", - gasPrice: minGasPrice, - gasLimit: 80000, - data: new TransactionPayload("hello"), + gasPrice: BigInt(minGasPrice), + gasLimit: 80000n, + data: Buffer.from("hello"), chainID: "local-testnet", }); @@ -488,19 +485,19 @@ describe("test transaction", async () => { it("should handle large values", () => { const tx1 = new Transaction({ - value: "123456789000000000000000000000", + value: 123456789000000000000000000000n, sender: wallets.alice.address, receiver: wallets.bob.address, - gasLimit: 50000, + gasLimit: 50000n, chainID: "local-testnet", }); assert.equal(tx1.getValue().toString(), "123456789000000000000000000000"); const tx2 = new Transaction({ - value: TokenTransfer.egldFromBigInteger("123456789000000000000000000000"), + value: 123456789000000000000000000000n, sender: wallets.alice.address, receiver: wallets.bob.address, - gasLimit: 50000, + gasLimit: 50000n, chainID: "local-testnet", }); assert.equal(tx2.getValue().toString(), "123456789000000000000000000000"); @@ -509,10 +506,10 @@ describe("test transaction", async () => { // Passing a BigNumber is not recommended. // However, ITransactionValue interface is permissive, and developers may mistakenly pass such objects as values. // TokenTransfer objects or simple strings (see above) are preferred, instead. - value: new BigNumber("123456789000000000000000000000"), + value: BigInt("123456789000000000000000000000"), sender: wallets.alice.address, receiver: wallets.bob.address, - gasLimit: 50000, + gasLimit: 50000n, chainID: "local-testnet", }); assert.equal(tx3.getValue().toString(), "123456789000000000000000000000"); @@ -520,88 +517,88 @@ describe("test transaction", async () => { it("checks correctly the version and options of the transaction", async () => { let transaction = new Transaction({ - nonce: 90, - value: new BigNumber("1000000000000000000"), + nonce: 90n, + value: BigInt("1000000000000000000"), sender: wallets.alice.address, receiver: wallets.bob.address, - gasPrice: minGasPrice, - gasLimit: 80000, - data: new TransactionPayload("hello"), + gasPrice: BigInt(minGasPrice), + gasLimit: 80000n, + data: Buffer.from("hello"), chainID: "local-testnet", - version: new TransactionVersion(1), - options: TransactionOptions.withDefaultOptions(), + version: 1, + options: TRANSACTION_OPTIONS_DEFAULT, }); assert.isFalse(transaction.isGuardedTransaction()); transaction = new Transaction({ - nonce: 90, - value: new BigNumber("1000000000000000000"), + nonce: 90n, + value: BigInt("1000000000000000000"), sender: wallets.alice.address, receiver: wallets.bob.address, - gasPrice: minGasPrice, - gasLimit: 80000, - data: new TransactionPayload("hello"), + gasPrice: BigInt(minGasPrice), + gasLimit: 80000n, + data: Buffer.from("hello"), chainID: "local-testnet", - version: new TransactionVersion(1), - options: TransactionOptions.withOptions({ guarded: true }), + version: 1, + options: 2, }); assert.isFalse(transaction.isGuardedTransaction()); transaction = new Transaction({ - nonce: 90, - value: new BigNumber("1000000000000000000"), + nonce: 90n, + value: BigInt("1000000000000000000"), sender: wallets.alice.address, receiver: wallets.bob.address, - gasPrice: minGasPrice, - gasLimit: 80000, - data: new TransactionPayload("hello"), + gasPrice: BigInt(minGasPrice), + gasLimit: 80000n, + data: Buffer.from("hello"), chainID: "local-testnet", - version: new TransactionVersion(2), - options: TransactionOptions.withOptions({ guarded: true }), + version: 2, + options: 2, }); assert.isFalse(transaction.isGuardedTransaction()); transaction = new Transaction({ - nonce: 90, - value: new BigNumber("1000000000000000000"), + nonce: 90n, + value: BigInt("1000000000000000000"), sender: wallets.alice.address, receiver: wallets.bob.address, - gasPrice: minGasPrice, - gasLimit: 80000, - data: new TransactionPayload("hello"), + gasPrice: BigInt(minGasPrice), + gasLimit: 80000n, + data: Buffer.from("hello"), chainID: "local-testnet", - version: new TransactionVersion(2), - options: TransactionOptions.withOptions({ guarded: true }), + version: 2, + options: 2, }); assert.isFalse(transaction.isGuardedTransaction()); transaction = new Transaction({ - nonce: 90, - value: new BigNumber("1000000000000000000"), + nonce: 90n, + value: BigInt("1000000000000000000"), sender: wallets.alice.address, receiver: wallets.bob.address, - gasPrice: minGasPrice, + gasPrice: BigInt(minGasPrice), guardian: wallets.bob.address, - gasLimit: 80000, - data: new TransactionPayload("hello"), + gasLimit: 80000n, + data: Buffer.from("hello"), chainID: "local-testnet", - version: new TransactionVersion(2), - options: TransactionOptions.withOptions({ guarded: true }), + version: 2, + options: 2, }); assert.isFalse(transaction.isGuardedTransaction()); transaction = new Transaction({ - nonce: 90, - value: new BigNumber("1000000000000000000"), + nonce: 90n, + value: BigInt("1000000000000000000"), sender: wallets.alice.address, receiver: wallets.bob.address, - gasPrice: minGasPrice, + gasPrice: BigInt(minGasPrice), guardian: wallets.bob.address, - gasLimit: 80000, - data: new TransactionPayload("hello"), + gasLimit: 80000n, + data: Buffer.from("hello"), chainID: "local-testnet", - version: new TransactionVersion(2), - options: TransactionOptions.withOptions({ guarded: true }), + version: 2, + options: 2, }); transaction.applySignature(await wallets.alice.signer.sign(transaction.serializeForSigning())); transaction.applyGuardianSignature(transaction.getSignature()); @@ -612,8 +609,8 @@ describe("test transaction", async () => { let transaction = new Transaction({ nonce: 89n, value: 0n, - sender: wallets.alice.address.toBech32(), - receiver: wallets.bob.address.toBech32(), + sender: wallets.alice.address, + receiver: wallets.bob.address, gasLimit: 50000n, gasPrice: 1000000000n, chainID: "integration tests chain ID", @@ -633,25 +630,25 @@ describe("test transaction", async () => { let transaction = new Transaction({ nonce: 89n, value: 0n, - sender: wallets.alice.address.toBech32(), - receiver: wallets.bob.address.toBech32(), + sender: wallets.alice.address, + receiver: wallets.bob.address, gasLimit: 50000n, chainID: "localnet", }); - transactionComputer.applyGuardian(transaction, wallets.carol.address.toBech32()); + transactionComputer.applyGuardian(transaction, wallets.carol.address); assert.equal(transaction.version, 2); assert.equal(transaction.options, 2); - assert.equal(transaction.guardian, wallets.carol.address.toBech32()); + assert.equal(transaction.guardian, wallets.carol.address); }); it("should apply guardian with options set for hash signing", async () => { let transaction = new Transaction({ nonce: 89n, value: 0n, - sender: wallets.alice.address.toBech32(), - receiver: wallets.bob.address.toBech32(), + sender: wallets.alice.address, + receiver: wallets.bob.address, gasLimit: 50000n, chainID: "localnet", version: 1, @@ -661,20 +658,20 @@ describe("test transaction", async () => { assert.equal(transaction.version, 2); assert.equal(transaction.options, 1); - transactionComputer.applyGuardian(transaction, wallets.carol.address.toBech32()); + transactionComputer.applyGuardian(transaction, wallets.carol.address); assert.equal(transaction.version, 2); assert.equal(transaction.options, 3); }); it("should ensure transaction is valid", async () => { let transaction = new Transaction({ - sender: "invalidAddress", - receiver: wallets.bob.address.toBech32(), + sender: Address.empty(), + receiver: wallets.bob.address, gasLimit: 50000n, chainID: "", }); - transaction.sender = wallets.alice.address.toBech32(); + transaction.sender = wallets.alice.address; assert.throws(() => { transactionComputer.computeBytesForSigning(transaction); @@ -696,8 +693,8 @@ describe("test transaction", async () => { it("should compute bytes to verify transaction signature", async () => { let transaction = new Transaction({ - sender: wallets.alice.address.toBech32(), - receiver: wallets.bob.address.toBech32(), + sender: wallets.alice.address, + receiver: wallets.bob.address, gasLimit: 50000n, chainID: "D", nonce: 7n, @@ -725,8 +722,8 @@ describe("test transaction", async () => { it("should compute bytes to verify transaction signature (signed by hash)", async () => { let transaction = new Transaction({ - sender: wallets.alice.address.toBech32(), - receiver: wallets.bob.address.toBech32(), + sender: wallets.alice.address, + receiver: wallets.bob.address, gasLimit: 50000n, chainID: "D", nonce: 7n, diff --git a/src/transaction.ts b/src/transaction.ts index 0921bfcb..60ba73e0 100644 --- a/src/transaction.ts +++ b/src/transaction.ts @@ -3,24 +3,10 @@ import { Address } from "./address"; import { TRANSACTION_MIN_GAS_PRICE, TRANSACTION_OPTIONS_DEFAULT, TRANSACTION_VERSION_DEFAULT } from "./constants"; import { TransactionsConverter } from "./converters/transactionsConverter"; import { Hash } from "./hash"; -import { - IAddress, - IChainID, - IGasLimit, - IGasPrice, - INonce, - IPlainTransactionObject, - ISignature, - ITransactionOptions, - ITransactionPayload, - ITransactionValue, - ITransactionVersion, -} from "./interface"; +import { IGasLimit, IGasPrice, INonce, IPlainTransactionObject, ISignature, ITransactionValue } from "./interface"; import { INetworkConfig } from "./interfaceOfNetwork"; -import { TransactionOptions, TransactionVersion } from "./networkParams"; import { interpretSignatureAsBuffer } from "./signature"; import { TransactionComputer } from "./transactionComputer"; -import { TransactionPayload } from "./transactionPayload"; /** * An abstraction for creating and signing transactions. @@ -37,14 +23,14 @@ export class Transaction { public value: bigint; /** - * The address of the sender, in bech32 format. + * The address of the sender. */ - public sender: string; + public sender: Address; /** - * The address of the receiver, in bech32 format. + * The address of the receiver. */ - public receiver: string; + public receiver: Address; /** * The username of the sender. @@ -89,7 +75,7 @@ export class Transaction { /** * The address of the guardian, in bech32 format. */ - public guardian: string; + public guardian: Address; /** * The signature. @@ -105,19 +91,19 @@ export class Transaction { * Creates a new Transaction object. */ public constructor(options: { - nonce?: INonce | bigint; - value?: ITransactionValue | bigint; - sender: IAddress | string; - receiver: IAddress | string; + nonce?: bigint; + value?: bigint; + sender: Address; + receiver: Address; senderUsername?: string; receiverUsername?: string; - gasPrice?: IGasPrice | bigint; - gasLimit: IGasLimit | bigint; - data?: ITransactionPayload | Uint8Array; - chainID: IChainID | string; - version?: ITransactionVersion | number; - options?: ITransactionOptions | number; - guardian?: IAddress | string; + gasPrice?: bigint; + gasLimit: bigint; + data?: Uint8Array; + chainID: string; + version?: number; + options?: number; + guardian?: Address; signature?: Uint8Array; guardianSignature?: Uint8Array; }) { @@ -125,8 +111,8 @@ export class Transaction { // We still rely on "bigNumber" for value, because client code might be passing a BigNumber object as a legacy "ITransactionValue", // and we want to keep compatibility. this.value = options.value ? BigInt(new BigNumber(options.value.toString()).toFixed(0)) : 0n; - this.sender = this.addressAsBech32(options.sender); - this.receiver = this.addressAsBech32(options.receiver); + this.sender = options.sender; + this.receiver = options.receiver; this.senderUsername = options.senderUsername || ""; this.receiverUsername = options.receiverUsername || ""; this.gasPrice = BigInt(options.gasPrice?.valueOf() || TRANSACTION_MIN_GAS_PRICE); @@ -135,16 +121,12 @@ export class Transaction { this.chainID = options.chainID.valueOf(); this.version = Number(options.version?.valueOf() || TRANSACTION_VERSION_DEFAULT); this.options = Number(options.options?.valueOf() || TRANSACTION_OPTIONS_DEFAULT); - this.guardian = options.guardian ? this.addressAsBech32(options.guardian) : ""; + this.guardian = options.guardian ?? Address.empty(); this.signature = options.signature || Buffer.from([]); this.guardianSignature = options.guardianSignature || Buffer.from([]); } - private addressAsBech32(address: IAddress | string): string { - return typeof address === "string" ? address : address.bech32(); - } - /** * Legacy method, use the "nonce" property instead. */ @@ -178,21 +160,21 @@ export class Transaction { * Legacy method, use the "sender" property instead. */ getSender(): Address { - return Address.fromBech32(this.sender); + return this.sender; } /** * Legacy method, use the "sender" property instead. */ - setSender(sender: Address | string) { - this.sender = typeof sender === "string" ? sender : sender.bech32(); + setSender(sender: Address) { + this.sender = sender; } /** * Legacy method, use the "receiver" property instead. */ getReceiver(): Address { - return Address.fromBech32(this.receiver); + return this.receiver; } /** @@ -227,7 +209,7 @@ export class Transaction { * Legacy method, use the "guardian" property instead. */ getGuardian(): Address { - return new Address(this.guardian); + return this.guardian; } /** @@ -261,43 +243,43 @@ export class Transaction { /** * Legacy method, use the "data" property instead. */ - getData(): ITransactionPayload { - return new TransactionPayload(Buffer.from(this.data)); + getData(): Uint8Array { + return this.data; } /** * Legacy method, use the "chainID" property instead. */ - getChainID(): IChainID { + getChainID(): string { return this.chainID; } /** * Legacy method, use the "chainID" property instead. */ - setChainID(chainID: IChainID | string) { - this.chainID = chainID.valueOf(); + setChainID(chainID: string) { + this.chainID = chainID; } /** * Legacy method, use the "version" property instead. */ - getVersion(): TransactionVersion { - return new TransactionVersion(this.version); + getVersion(): number { + return this.version; } /** * Legacy method, use the "version" property instead. */ - setVersion(version: ITransactionVersion | number) { - this.version = version.valueOf(); + setVersion(version: number) { + this.version = version; } /** * Legacy method, use the "options" property instead. */ - getOptions(): TransactionOptions { - return new TransactionOptions(this.options.valueOf()); + getOptions(): number { + return this.options; } /** @@ -305,8 +287,8 @@ export class Transaction { * * Question for review: check how the options are set by sdk-dapp, wallet, ledger, extension. */ - setOptions(options: ITransactionOptions | number) { - this.options = options.valueOf(); + setOptions(options: number) { + this.options = options; } /** @@ -326,8 +308,8 @@ export class Transaction { /** * Legacy method, use the "guardian" property instead. */ - setGuardian(guardian: IAddress | string) { - this.guardian = typeof guardian === "string" ? guardian : guardian.bech32(); + setGuardian(guardian: Address) { + this.guardian = guardian; } /** @@ -352,9 +334,10 @@ export class Transaction { * Checks the integrity of the guarded transaction */ isGuardedTransaction(): boolean { - const hasGuardian = this.guardian.length > 0; + const computer = new TransactionComputer(); + const hasGuardian = !this.guardian.isEmpty(); const hasGuardianSignature = this.guardianSignature.length > 0; - return this.getOptions().isWithGuardian() && hasGuardian && hasGuardianSignature; + return computer.hasOptionsSetForGuardedTransaction(this) && hasGuardian && hasGuardianSignature; } /** diff --git a/src/transactionBuilder.ts b/src/transactionBuilder.ts index d4b684a6..2ef3b64b 100644 --- a/src/transactionBuilder.ts +++ b/src/transactionBuilder.ts @@ -1,5 +1,6 @@ +import { Address } from "./address"; import { ARGUMENTS_SEPARATOR } from "./constants"; -import { IAddress, ITransactionPayload } from "./interface"; +import { ITransactionPayload } from "./interface"; import { Transaction } from "./transaction"; import { TransactionPayload } from "./transactionPayload"; @@ -14,8 +15,8 @@ interface Config { */ export class TransactionBuilder { private config: Config; - private sender: IAddress; - private receiver: IAddress; + private sender: Address; + private receiver: Address; private dataParts: string[]; private providedGasLimit: bigint; private addDataMovementGas: boolean; @@ -23,8 +24,8 @@ export class TransactionBuilder { constructor(options: { config: Config; - sender: IAddress; - receiver: IAddress; + sender: Address; + receiver: Address; dataParts: string[]; gasLimit: bigint; addDataMovementGas: boolean; @@ -59,8 +60,8 @@ export class TransactionBuilder { const gasLimit = this.computeGasLimit(data); return new Transaction({ - sender: this.sender.bech32(), - receiver: this.receiver.bech32(), + sender: this.sender, + receiver: this.receiver, gasLimit: gasLimit, value: this.amount || 0n, data: data.valueOf(), diff --git a/src/transactionComputer.ts b/src/transactionComputer.ts index da365e7b..52bd568e 100644 --- a/src/transactionComputer.ts +++ b/src/transactionComputer.ts @@ -1,11 +1,11 @@ import BigNumber from "bignumber.js"; +import { Address } from "./address"; import { MIN_TRANSACTION_VERSION_THAT_SUPPORTS_OPTIONS, TRANSACTION_OPTIONS_TX_GUARDED, TRANSACTION_OPTIONS_TX_HASH_SIGN, } from "./constants"; import * as errors from "./errors"; -import { ITransaction } from "./interface"; import { INetworkConfig } from "./interfaceOfNetwork"; import { ProtoSerializer } from "./proto"; import { Transaction } from "./transaction"; @@ -46,7 +46,7 @@ export class TransactionComputer { return feeForMove + processingFee; } - computeBytesForSigning(transaction: ITransaction): Uint8Array { + computeBytesForSigning(transaction: Transaction): Uint8Array { this.ensureValidTransactionFields(transaction); const plainTransaction = this.toPlainObject(transaction); @@ -54,7 +54,7 @@ export class TransactionComputer { return new Uint8Array(Buffer.from(serialized)); } - computeBytesForVerifying(transaction: ITransaction): Uint8Array { + computeBytesForVerifying(transaction: Transaction): Uint8Array { const isTxSignedByHash = this.hasOptionsSetForHashSigning(transaction); if (isTxSignedByHash) { @@ -63,29 +63,29 @@ export class TransactionComputer { return this.computeBytesForSigning(transaction); } - computeHashForSigning(transaction: ITransaction): Uint8Array { + computeHashForSigning(transaction: Transaction): Uint8Array { const plainTransaction = this.toPlainObject(transaction); const signable = Buffer.from(JSON.stringify(plainTransaction)); return createKeccakHash("keccak256").update(signable).digest(); } - computeTransactionHash(transaction: ITransaction): Uint8Array { + computeTransactionHash(transaction: Transaction): Uint8Array { const serializer = new ProtoSerializer(); - const buffer = serializer.serializeTransaction(new Transaction(transaction)); + const buffer = serializer.serializeTransaction(transaction); const hash = createTransactionHasher(TRANSACTION_HASH_LENGTH).update(buffer).digest("hex"); return Buffer.from(hash, "hex"); } - hasOptionsSetForGuardedTransaction(transaction: ITransaction): boolean { + hasOptionsSetForGuardedTransaction(transaction: Transaction): boolean { return (transaction.options & TRANSACTION_OPTIONS_TX_GUARDED) == TRANSACTION_OPTIONS_TX_GUARDED; } - hasOptionsSetForHashSigning(transaction: ITransaction): boolean { + hasOptionsSetForHashSigning(transaction: Transaction): boolean { return (transaction.options & TRANSACTION_OPTIONS_TX_HASH_SIGN) == TRANSACTION_OPTIONS_TX_HASH_SIGN; } - applyGuardian(transaction: ITransaction, guardian: string) { + applyGuardian(transaction: Transaction, guardian: Address) { if (transaction.version < MIN_TRANSACTION_VERSION_THAT_SUPPORTS_OPTIONS) { transaction.version = MIN_TRANSACTION_VERSION_THAT_SUPPORTS_OPTIONS; } @@ -94,19 +94,19 @@ export class TransactionComputer { transaction.guardian = guardian; } - applyOptionsForHashSigning(transaction: ITransaction) { + applyOptionsForHashSigning(transaction: Transaction) { if (transaction.version < MIN_TRANSACTION_VERSION_THAT_SUPPORTS_OPTIONS) { transaction.version = MIN_TRANSACTION_VERSION_THAT_SUPPORTS_OPTIONS; } transaction.options = transaction.options | TRANSACTION_OPTIONS_TX_HASH_SIGN; } - private toPlainObject(transaction: ITransaction, withSignature?: boolean) { + private toPlainObject(transaction: Transaction, withSignature?: boolean) { let obj: any = { nonce: Number(transaction.nonce), value: transaction.value.toString(), - receiver: transaction.receiver, - sender: transaction.sender, + receiver: transaction.receiver.toBech32(), + sender: transaction.sender.toBech32(), senderUsername: this.toBase64OrUndefined(transaction.senderUsername), receiverUsername: this.toBase64OrUndefined(transaction.receiverUsername), gasPrice: Number(transaction.gasPrice), @@ -121,7 +121,7 @@ export class TransactionComputer { obj.chainID = transaction.chainID; obj.version = transaction.version; obj.options = transaction.options ? transaction.options : undefined; - obj.guardian = transaction.guardian ? transaction.guardian : undefined; + obj.guardian = transaction.guardian.isEmpty() ? undefined : transaction.guardian.toBech32(); return obj; } @@ -134,7 +134,7 @@ export class TransactionComputer { return value && value.length ? Buffer.from(value).toString("base64") : undefined; } - private ensureValidTransactionFields(transaction: ITransaction) { + private ensureValidTransactionFields(transaction: Transaction) { if (!transaction.chainID.length) { throw new errors.ErrBadUsage("The `chainID` field is not set"); } diff --git a/src/transactionOnNetwork.ts b/src/transactionOnNetwork.ts index 2a8ccbd5..0e7b4f49 100644 --- a/src/transactionOnNetwork.ts +++ b/src/transactionOnNetwork.ts @@ -1,15 +1,15 @@ import { Address } from "./address"; -import { ITransaction } from "./interface"; +import { Transaction } from "./transaction"; import { TransactionLogs } from "./transactionLogs"; import { SmartContractResult } from "./transactionsOutcomeParsers"; import { TransactionStatus } from "./transactionStatus"; -export function prepareTransactionForBroadcasting(transaction: ITransaction): any { +export function prepareTransactionForBroadcasting(transaction: Transaction): any { return { nonce: Number(transaction.nonce), value: transaction.value.toString(), - receiver: transaction.receiver, - sender: transaction.sender, + receiver: transaction.receiver.toBech32(), + sender: transaction.sender.toBech32(), senderUsername: transaction.senderUsername ? Buffer.from(transaction.senderUsername).toString("base64") : undefined, @@ -22,7 +22,7 @@ export function prepareTransactionForBroadcasting(transaction: ITransaction): an chainID: transaction.chainID, version: transaction.version, options: transaction.options, - guardian: transaction.guardian || undefined, + guardian: transaction.guardian.isEmpty() ? undefined : transaction.guardian.toBech32(), signature: Buffer.from(transaction.signature).toString("hex"), guardianSignature: transaction.guardianSignature.length === 0 diff --git a/src/transfers/transferTransactionsFactory.spec.ts b/src/transfers/transferTransactionsFactory.spec.ts index b4cda39f..e753db24 100644 --- a/src/transfers/transferTransactionsFactory.spec.ts +++ b/src/transfers/transferTransactionsFactory.spec.ts @@ -36,8 +36,8 @@ describe("test transfer transactions factory", function () { nativeAmount: 1000000000000000000n, }); - assert.equal(transaction.sender, alice.toBech32()); - assert.equal(transaction.receiver, bob.toBech32()); + assert.equal(transaction.sender, alice); + assert.equal(transaction.receiver, bob); assert.equal(transaction.value.valueOf(), 1000000000000000000n); assert.equal(transaction.gasLimit.valueOf(), 50000n); assert.deepEqual(transaction.data, new Uint8Array()); @@ -50,8 +50,8 @@ describe("test transfer transactions factory", function () { data: Buffer.from("test data"), }); - assert.equal(transaction.sender, alice.toBech32()); - assert.equal(transaction.receiver, bob.toBech32()); + assert.equal(transaction.sender, alice); + assert.equal(transaction.receiver, bob); assert.equal(transaction.value.valueOf(), 1000000000000000000n); assert.equal(transaction.gasLimit.valueOf(), 63500n); assert.deepEqual(transaction.data, Buffer.from("test data")); @@ -66,8 +66,8 @@ describe("test transfer transactions factory", function () { tokenTransfers: [transfer], }); - assert.equal(transaction.sender, alice.toBech32()); - assert.equal(transaction.receiver, bob.toBech32()); + assert.equal(transaction.sender, alice); + assert.equal(transaction.receiver, bob); assert.equal(transaction.value.valueOf(), 0n); assert.equal(transaction.gasLimit.valueOf(), 410000n); assert.deepEqual(transaction.data.toString(), "ESDTTransfer@464f4f2d313233343536@0f4240"); @@ -82,8 +82,8 @@ describe("test transfer transactions factory", function () { tokenTransfers: [transfer], }); - assert.equal(transaction.sender, alice.toBech32()); - assert.equal(transaction.receiver, alice.toBech32()); + assert.equal(transaction.sender, alice); + assert.equal(transaction.receiver, alice); assert.equal(transaction.value.valueOf(), 0n); assert.equal(transaction.gasLimit.valueOf(), 1210500n); assert.deepEqual( @@ -101,8 +101,8 @@ describe("test transfer transactions factory", function () { tokenTransfers: [transfer], }); - assert.equal(transaction.sender, alice.toBech32()); - assert.equal(transaction.receiver, alice.toBech32()); + assert.equal(transaction.sender, alice); + assert.equal(transaction.receiver, alice); assert.equal(transaction.value.valueOf(), 0n); assert.equal(transaction.gasLimit.valueOf(), 1219500n); assert.deepEqual( @@ -123,8 +123,8 @@ describe("test transfer transactions factory", function () { tokenTransfers: [firstTransfer, secondTransfer], }); - assert.equal(transaction.sender, alice.toBech32()); - assert.equal(transaction.receiver, alice.toBech32()); + assert.equal(transaction.sender, alice); + assert.equal(transaction.receiver, alice); assert.equal(transaction.value.valueOf(), 0n); assert.equal(transaction.gasLimit.valueOf(), 1466000n); assert.deepEqual( @@ -152,8 +152,8 @@ describe("test transfer transactions factory", function () { tokenTransfers: [firstTransfer, secondTransfer], }); - assert.equal(transaction.sender, alice.toBech32()); - assert.equal(transaction.receiver, alice.toBech32()); + assert.equal(transaction.sender, alice); + assert.equal(transaction.receiver, alice); assert.equal(transaction.value.valueOf(), 0n); assert.equal(transaction.gasLimit.valueOf(), 1484000n); assert.deepEqual( @@ -188,8 +188,8 @@ describe("test transfer transactions factory", function () { nativeAmount: 1000000000000000000n, }); - assert.equal(transaction.sender, alice.toBech32()); - assert.equal(transaction.receiver, bob.toBech32()); + assert.equal(transaction.sender, alice); + assert.equal(transaction.receiver, bob); assert.equal(transaction.value.valueOf(), 1000000000000000000n); assert.equal(transaction.gasLimit.valueOf(), 50000n); }); @@ -201,8 +201,8 @@ describe("test transfer transactions factory", function () { data: Buffer.from("hello"), }); - assert.equal(transaction.sender, alice.toBech32()); - assert.equal(transaction.receiver, bob.toBech32()); + assert.equal(transaction.sender, alice); + assert.equal(transaction.receiver, bob); assert.equal(transaction.value.valueOf(), 1000000000000000000n); assert.equal(transaction.gasLimit.valueOf(), 57500n); assert.deepEqual(transaction.data, Buffer.from("hello")); @@ -214,8 +214,8 @@ describe("test transfer transactions factory", function () { data: Buffer.from("hello"), }); - assert.equal(transaction.sender, alice.toBech32()); - assert.equal(transaction.receiver, bob.toBech32()); + assert.equal(transaction.sender, alice); + assert.equal(transaction.receiver, bob); assert.equal(transaction.gasLimit.valueOf(), 57500n); assert.deepEqual(transaction.data, Buffer.from("hello")); }); @@ -233,8 +233,8 @@ describe("test transfer transactions factory", function () { tokenTransfers: [firstTransfer, secondTransfer], }); - assert.equal(transaction.sender, alice.toBech32()); - assert.equal(transaction.receiver, alice.toBech32()); + assert.equal(transaction.sender, alice); + assert.equal(transaction.receiver, alice); assert.equal(transaction.value.valueOf(), 0n); assert.equal(transaction.gasLimit.valueOf(), 1727500n); assert.deepEqual( @@ -256,8 +256,8 @@ describe("test transfer transactions factory", function () { tokenTransfers: [firstTransfer, secondTransfer], }); - assert.equal(transaction.sender, alice.toBech32()); - assert.equal(transaction.receiver, alice.toBech32()); + assert.equal(transaction.sender, alice); + assert.equal(transaction.receiver, alice); assert.equal(transaction.value.valueOf(), 0n); assert.equal(transaction.gasLimit.valueOf(), 1745500n); assert.deepEqual( @@ -275,8 +275,8 @@ describe("test transfer transactions factory", function () { tokenTransfers: [firstTransfer], }); - assert.equal(transaction.sender, alice.toBech32()); - assert.equal(transaction.receiver, alice.toBech32()); + assert.equal(transaction.sender, alice); + assert.equal(transaction.receiver, alice); assert.equal(transaction.value.valueOf(), 0n); assert.equal(transaction.gasLimit.valueOf(), 1_243_500n); assert.deepEqual( diff --git a/src/transfers/transferTransactionsFactory.ts b/src/transfers/transferTransactionsFactory.ts index 8bb64c41..b2b69fd5 100644 --- a/src/transfers/transferTransactionsFactory.ts +++ b/src/transfers/transferTransactionsFactory.ts @@ -1,21 +1,12 @@ import { AddressValue, ArgSerializer, BigUIntValue, BytesValue, TypedValue, U16Value, U64Value } from "../abi"; +import { Address } from "../address"; import { EGLD_IDENTIFIER_FOR_MULTI_ESDTNFT_TRANSFER } from "../constants"; import { Err, ErrBadUsage } from "../errors"; -import { - IAddress, - IChainID, - IGasLimit, - IGasPrice, - INonce, - ITokenTransfer, - ITransactionPayload, - ITransactionValue, -} from "../interface"; +import { ITokenTransfer } from "../interface"; import { TokenComputer, TokenTransfer } from "../tokens"; import { TokenTransfersDataBuilder } from "../tokenTransfersDataBuilder"; import { Transaction } from "../transaction"; import { TransactionBuilder } from "../transactionBuilder"; -import { TransactionPayload } from "../transactionPayload"; import * as resources from "./resources"; const ADDITIONAL_GAS_FOR_ESDT_TRANSFER = 100000; @@ -82,17 +73,14 @@ export class TransferTransactionsFactory { } } - createTransactionForNativeTokenTransfer( - sender: IAddress, - options: resources.NativeTokenTransferInput, - ): Transaction { + createTransactionForNativeTokenTransfer(sender: Address, options: resources.NativeTokenTransferInput): Transaction { this.ensureConfigIsDefined(); const data = options.data || new Uint8Array(); return new Transaction({ - sender: sender.bech32(), - receiver: options.receiver.bech32(), + sender: sender, + receiver: options.receiver, chainID: this.config!.chainID, gasLimit: this.computeGasForMoveBalance(this.config!, data), data: data, @@ -100,7 +88,7 @@ export class TransferTransactionsFactory { }); } - createTransactionForESDTTokenTransfer(sender: IAddress, options: resources.CustomTokenTransferInput): Transaction { + createTransactionForESDTTokenTransfer(sender: Address, options: resources.CustomTokenTransferInput): Transaction { this.ensureConfigIsDefined(); const numberOfTransfers = options.tokenTransfers.length; @@ -128,7 +116,7 @@ export class TransferTransactionsFactory { }).build(); } - createTransactionForTransfer(sender: IAddress, options: resources.CreateTransferTransactionInput): Transaction { + createTransactionForTransfer(sender: Address, options: resources.CreateTransferTransactionInput): Transaction { const nativeAmount = options.nativeAmount ?? 0n; let tokenTransfers = options.tokenTransfers ? [...options.tokenTransfers] : []; const numberOfTokens = tokenTransfers.length; @@ -161,14 +149,14 @@ export class TransferTransactionsFactory { * Use {@link createTransactionForNativeTokenTransfer} instead. */ createEGLDTransfer(args: { - nonce?: INonce; - value: ITransactionValue; - receiver: IAddress; - sender: IAddress; - gasPrice?: IGasPrice; - gasLimit?: IGasLimit; - data?: ITransactionPayload; - chainID: IChainID; + nonce?: bigint; + value: bigint; + receiver: Address; + sender: Address; + gasPrice?: bigint; + gasLimit?: bigint; + data?: Uint8Array; + chainID: string; }) { if (!this.isGasEstimatorDefined()) { throw new Err( @@ -176,7 +164,7 @@ export class TransferTransactionsFactory { ); } - const dataLength = args.data?.length() || 0; + const dataLength = args.data?.length || 0; const estimatedGasLimit = this.gasEstimator!.forEGLDTransfer(dataLength); return new Transaction({ @@ -185,7 +173,7 @@ export class TransferTransactionsFactory { receiver: args.receiver, sender: args.sender, gasPrice: args.gasPrice, - gasLimit: args.gasLimit || estimatedGasLimit, + gasLimit: args.gasLimit || BigInt(estimatedGasLimit), data: args.data, chainID: args.chainID, }); @@ -197,12 +185,12 @@ export class TransferTransactionsFactory { */ createESDTTransfer(args: { tokenTransfer: ITokenTransfer; - nonce?: INonce; - receiver: IAddress; - sender: IAddress; - gasPrice?: IGasPrice; - gasLimit?: IGasLimit; - chainID: IChainID; + nonce?: bigint; + receiver: Address; + sender: Address; + gasPrice?: bigint; + gasLimit?: bigint; + chainID: string; }) { if (!this.isGasEstimatorDefined()) { throw new Err( @@ -218,8 +206,8 @@ export class TransferTransactionsFactory { ]); const data = `ESDTTransfer@${argumentsString}`; - const transactionPayload = new TransactionPayload(data); - const dataLength = transactionPayload.length() || 0; + const transactionPayload = Buffer.from(data); + const dataLength = transactionPayload.length || 0; const estimatedGasLimit = this.gasEstimator!.forESDTTransfer(dataLength); return new Transaction({ @@ -227,7 +215,7 @@ export class TransferTransactionsFactory { receiver: args.receiver, sender: args.sender, gasPrice: args.gasPrice, - gasLimit: args.gasLimit || estimatedGasLimit, + gasLimit: args.gasLimit || BigInt(estimatedGasLimit), data: transactionPayload, chainID: args.chainID, }); @@ -239,12 +227,12 @@ export class TransferTransactionsFactory { */ createESDTNFTTransfer(args: { tokenTransfer: ITokenTransfer; - nonce?: INonce; - destination: IAddress; - sender: IAddress; - gasPrice?: IGasPrice; - gasLimit?: IGasLimit; - chainID: IChainID; + nonce?: bigint; + destination: Address; + sender: Address; + gasPrice?: bigint; + gasLimit?: bigint; + chainID: string; }) { if (!this.isGasEstimatorDefined()) { throw new Err( @@ -264,8 +252,8 @@ export class TransferTransactionsFactory { ]); const data = `ESDTNFTTransfer@${argumentsString}`; - const transactionPayload = new TransactionPayload(data); - const dataLength = transactionPayload.length() || 0; + const transactionPayload = Buffer.from(data); + const dataLength = transactionPayload.length || 0; const estimatedGasLimit = this.gasEstimator!.forESDTNFTTransfer(dataLength); return new Transaction({ @@ -273,7 +261,7 @@ export class TransferTransactionsFactory { receiver: args.sender, sender: args.sender, gasPrice: args.gasPrice, - gasLimit: args.gasLimit || estimatedGasLimit, + gasLimit: args.gasLimit || BigInt(estimatedGasLimit), data: transactionPayload, chainID: args.chainID, }); @@ -285,12 +273,12 @@ export class TransferTransactionsFactory { */ createMultiESDTNFTTransfer(args: { tokenTransfers: ITokenTransfer[]; - nonce?: INonce; - destination: IAddress; - sender: IAddress; - gasPrice?: IGasPrice; - gasLimit?: IGasLimit; - chainID: IChainID; + nonce?: bigint; + destination: Address; + sender: Address; + gasPrice?: bigint; + gasLimit?: bigint; + chainID: string; }) { if (!this.isGasEstimatorDefined()) { throw new Err( @@ -320,8 +308,8 @@ export class TransferTransactionsFactory { const { argumentsString } = new ArgSerializer().valuesToString(parts); const data = `MultiESDTNFTTransfer@${argumentsString}`; - const transactionPayload = new TransactionPayload(data); - const dataLength = transactionPayload.length() || 0; + const transactionPayload = Buffer.from(data); + const dataLength = transactionPayload.length || 0; const estimatedGasLimit = this.gasEstimator!.forMultiESDTNFTTransfer(dataLength, args.tokenTransfers.length); return new Transaction({ @@ -329,16 +317,16 @@ export class TransferTransactionsFactory { receiver: args.sender, sender: args.sender, gasPrice: args.gasPrice, - gasLimit: args.gasLimit || estimatedGasLimit, + gasLimit: args.gasLimit || BigInt(estimatedGasLimit), data: transactionPayload, chainID: args.chainID, }); } private createSingleESDTTransferTransaction( - sender: IAddress, + sender: Address, options: { - receiver: IAddress; + receiver: Address; tokenTransfers: TokenTransfer[]; }, ): Transaction { @@ -360,7 +348,7 @@ export class TransferTransactionsFactory { }).build(); } - private buildTransferData(transfer: TokenTransfer, options: { sender: IAddress; receiver: IAddress }) { + private buildTransferData(transfer: TokenTransfer, options: { sender: Address; receiver: Address }) { let dataParts: string[] = []; let extraGasForTransfer: bigint; let receiver = options.receiver; @@ -379,7 +367,7 @@ export class TransferTransactionsFactory { return { dataParts, extraGasForTransfer, receiver }; } - private buildMultiESDTNFTTransferData(transfer: TokenTransfer[], receiver: IAddress) { + private buildMultiESDTNFTTransferData(transfer: TokenTransfer[], receiver: Address) { return { dataParts: this.tokenTransfersDataBuilder!.buildDataPartsForMultiESDTNFTTransfer(receiver, transfer), extraGasForTransfer: @@ -395,7 +383,7 @@ export class TransferTransactionsFactory { }; } - private buildSingleESDTNFTTransferData(transfer: TokenTransfer, receiver: IAddress) { + private buildSingleESDTNFTTransferData(transfer: TokenTransfer, receiver: Address) { return { dataParts: this.tokenTransfersDataBuilder!.buildDataPartsForSingleESDTNFTTransfer(transfer, receiver), extraGasForTransfer: this.config!.gasLimitESDTNFTTransfer + BigInt(ADDITIONAL_GAS_FOR_ESDT_NFT_TRANSFER), diff --git a/src/wallet/pem.spec.ts b/src/wallet/pem.spec.ts index 73eda6b9..3c4557ae 100644 --- a/src/wallet/pem.spec.ts +++ b/src/wallet/pem.spec.ts @@ -18,7 +18,7 @@ describe("test PEMs", () => { let aliceKey = parseUserKey(alice.pemFileText); assert.equal(aliceKey.hex(), alice.secretKeyHex); - assert.equal(aliceKey.generatePublicKey().toAddress().bech32(), alice.address.bech32()); + assert.equal(aliceKey.generatePublicKey().toAddress().toBech32(), alice.address.toBech32()); }); it("should parseValidatorKey", async () => {