From 418d023351750abb70b9c5ed02897e967ace6489 Mon Sep 17 00:00:00 2001 From: danielailie Date: Thu, 12 Dec 2024 16:18:03 +0200 Subject: [PATCH] Remove unused interfaces (INonce, IGasLimit, IGasPrice, ITransactionOptions, ITransactionVersion, ITransactionValue, ISignature, IAccountBalance --- src/abi/interaction.local.net.spec.ts | 20 +++++----- src/abi/interaction.spec.ts | 52 +++++++++++++------------- src/abi/interaction.ts | 34 ++++++++--------- src/abi/interface.ts | 3 +- src/abi/nativeSerializer.ts | 3 +- src/abi/query.ts | 7 ++-- src/abi/smartContract.spec.ts | 36 +++++++++--------- src/abi/smartContract.ts | 15 ++++---- src/accountManagement/resources.ts | 4 +- src/accounts/account.ts | 9 ++--- src/compatibility.ts | 3 +- src/entrypoints/entrypoints.spec.ts | 6 +-- src/entrypoints/entrypoints.ts | 2 +- src/interface.ts | 40 -------------------- src/interfaceOfNetwork.ts | 13 +++---- src/networkProviders/accounts.ts | 9 ++--- src/proto/serializer.spec.ts | 4 +- src/testutils/mockNetworkProvider.ts | 6 +-- src/testutils/utils.ts | 5 +-- src/testutils/wallets.ts | 3 +- src/transaction.spec.ts | 3 -- src/transaction.ts | 54 +++++++++++++-------------- src/utils.codec.ts | 3 +- 23 files changed, 139 insertions(+), 195 deletions(-) diff --git a/src/abi/interaction.local.net.spec.ts b/src/abi/interaction.local.net.spec.ts index f306f2c8..42b76d82 100644 --- a/src/abi/interaction.local.net.spec.ts +++ b/src/abi/interaction.local.net.spec.ts @@ -150,10 +150,10 @@ describe("test smart contract interactor", function () { let returnEgldInteraction = ( contract.methods .returns_egld_decimal([]) - .withGasLimit(10000000) + .withGasLimit(10000000n) .withChainID(network.ChainID) .withSender(alice.address) - .withValue(1) + .withValue(1n) ); // returnEgld() @@ -164,10 +164,10 @@ describe("test smart contract interactor", function () { let additionInteraction = contract.methods .managed_decimal_addition([new ManagedDecimalValue("2.5", 2), new ManagedDecimalValue("2.7", 2)]) - .withGasLimit(10000000) + .withGasLimit(10000000n) .withChainID(network.ChainID) .withSender(alice.address) - .withValue(0); + .withValue(0n); // addition() let additionTransaction = additionInteraction @@ -178,10 +178,10 @@ describe("test smart contract interactor", function () { // log let mdLnInteraction = contract.methods .managed_decimal_ln([new ManagedDecimalValue("23", 9)]) - .withGasLimit(10000000) + .withGasLimit(10000000n) .withChainID(network.ChainID) .withSender(alice.address) - .withValue(0); + .withValue(0n); // mdLn() let mdLnTransaction = mdLnInteraction @@ -194,10 +194,10 @@ describe("test smart contract interactor", function () { new ManagedDecimalValue("4", 2, true), new ManagedDecimalValue("5", 2, true), ]) - .withGasLimit(50000000) + .withGasLimit(50000000n) .withChainID(network.ChainID) .withSender(alice.address) - .withValue(0); + .withValue(0n); // addition() let additionVarTransaction = additionVarInteraction @@ -207,10 +207,10 @@ describe("test smart contract interactor", function () { let lnVarInteraction = contract.methods .managed_decimal_ln_var([new ManagedDecimalValue("23", 9, true)]) - .withGasLimit(50000000) + .withGasLimit(50000000n) .withChainID(network.ChainID) .withSender(alice.address) - .withValue(0); + .withValue(0n); // managed_decimal_ln_var() let lnVarTransaction = lnVarInteraction diff --git a/src/abi/interaction.spec.ts b/src/abi/interaction.spec.ts index db210faf..d5edc503 100644 --- a/src/abi/interaction.spec.ts +++ b/src/abi/interaction.spec.ts @@ -33,15 +33,15 @@ describe("test smart contract interactor", function () { let transaction = interaction .withSender(alice.address) - .withNonce(7) - .withValue(TokenTransfer.egldFromAmount(1)) - .withGasLimit(20000000) + .withNonce(7n) + .withValue(TokenTransfer.egldFromAmount(1).amount) + .withGasLimit(20000000n) .buildTransaction(); assert.deepEqual(transaction.getReceiver(), dummyAddress); assert.equal(transaction.getValue().toString(), "1000000000000000000"); - assert.equal(transaction.getNonce(), 7); - assert.equal(transaction.getGasLimit().valueOf(), 20000000); + assert.equal(transaction.getNonce(), 7n); + assert.equal(transaction.getGasLimit().valueOf(), 20000000n); }); it("should set transfers (payments) on contract calls (transfer and execute)", async function () { @@ -181,9 +181,9 @@ describe("test smart contract interactor", function () { .getUltimateAnswer() .withChainID("T") .withSender(alice) - .withGasLimit(543210) + .withGasLimit(543210n) .withSingleESDTTransfer(new TokenTransfer({ token, amount: 100n })) - .withNonce(42) + .withNonce(42n) .buildTransaction(); assert.deepEqual( @@ -208,12 +208,12 @@ describe("test smart contract interactor", function () { let contract = new SmartContract({ address: dummyAddress, abi: abiRegistry }); let controller = new SmartContractController({ chainID: "D", networkProvider: provider, abi: abiRegistry }); - let interaction = contract.methods.getUltimateAnswer().withGasLimit(543210).withChainID("T"); + let interaction = contract.methods.getUltimateAnswer().withGasLimit(543210n).withChainID("T"); assert.equal(contract.getAddress(), dummyAddress); assert.deepEqual(interaction.getFunction(), new ContractFunction("getUltimateAnswer")); assert.lengthOf(interaction.getArguments(), 0); - assert.deepEqual(interaction.getGasLimit(), 543210); + assert.deepEqual(interaction.getGasLimit(), 543210n); provider.mockQueryContractOnFunction( "getUltimateAnswer", @@ -239,29 +239,29 @@ describe("test smart contract interactor", function () { assert.deepEqual(response[0], new BigNumber(42)); // Execute, do not wait for execution - let transaction = interaction.withSender(alice.address).withNonce(0).buildTransaction(); + let transaction = interaction.withSender(alice.address).withNonce(0n).buildTransaction(); transaction.setSender(alice.address); transaction.applySignature(await alice.signer.sign(transaction.serializeForSigning())); await provider.sendTransaction(transaction); - assert.equal(transaction.getNonce().valueOf(), 0); + assert.equal(transaction.getNonce().valueOf(), 0n); assert.equal(transaction.getData().toString(), "getUltimateAnswer"); assert.equal( transaction.getHash().toString(), "3579ad09099feb9755c860ddd225251170806d833342e912fccdfe2ed5c3a364", ); - transaction = interaction.withNonce(1).buildTransaction(); + transaction = interaction.withNonce(1n).buildTransaction(); transaction.setSender(alice.address); transaction.applySignature(await alice.signer.sign(transaction.serializeForSigning())); await provider.sendTransaction(transaction); - assert.equal(transaction.getNonce().valueOf(), 1); + assert.equal(transaction.getNonce(), 1n); assert.equal( transaction.getHash().toString(), "ad513ce7c5d371d30e48f073326899766736eac1ac231d847d45bc3facbcb496", ); // Execute, and wait for execution - transaction = interaction.withNonce(2).buildTransaction(); + transaction = interaction.withNonce(2n).buildTransaction(); transaction.setSender(alice.address); transaction.applySignature(await alice.signer.sign(transaction.serializeForSigning())); provider.mockGetTransactionWithAnyHashAsNotarizedWithOneResult("@6f6b@2bs", "getUltimateAnswer"); @@ -281,8 +281,8 @@ describe("test smart contract interactor", function () { let controller = new SmartContractController({ chainID: "D", networkProvider: provider, abi: abi }); let getInteraction = contract.methodsExplicit.get(); - let incrementInteraction = (contract.methods.increment()).withGasLimit(543210); - let decrementInteraction = (contract.methods.decrement()).withGasLimit(987654); + let incrementInteraction = (contract.methods.increment()).withGasLimit(543210n); + let decrementInteraction = (contract.methods.decrement()).withGasLimit(987654n); // For "get()", return fake 7 provider.mockQueryContractOnFunction( @@ -308,7 +308,7 @@ describe("test smart contract interactor", function () { let incrementTransaction = incrementInteraction .withSender(alice.address) - .withNonce(14) + .withNonce(14n) .withChainID("mock") .buildTransaction(); @@ -322,19 +322,19 @@ describe("test smart contract interactor", function () { // Decrement #1 let decrementTransaction = decrementInteraction .withSender(alice.address) - .withNonce(15) + .withNonce(15n) .withChainID("mock") .buildTransaction(); decrementTransaction.applySignature(await alice.signer.sign(decrementTransaction.serializeForSigning())); await provider.sendTransaction(decrementTransaction); // Decrement #2 - decrementTransaction = decrementInteraction.withNonce(16).buildTransaction(); + decrementTransaction = decrementInteraction.withNonce(16n).buildTransaction(); decrementTransaction.applySignature(await alice.signer.sign(decrementTransaction.serializeForSigning())); await provider.sendTransaction(decrementTransaction); // Decrement #3 - decrementTransaction = decrementInteraction.withNonce(17).buildTransaction(); + decrementTransaction = decrementInteraction.withNonce(17n).buildTransaction(); decrementTransaction.applySignature(await alice.signer.sign(decrementTransaction.serializeForSigning())); provider.mockGetTransactionWithAnyHashAsNotarizedWithOneResult("@6f6b@05", "decrement"); hash = await provider.sendTransaction(decrementTransaction); @@ -362,17 +362,17 @@ describe("test smart contract interactor", function () { OptionValue.newMissing(), OptionalValue.newMissing(), ]) - .withGasLimit(5000000) + .withGasLimit(5000000n) ); - let statusInteraction = contract.methods.status(["lucky"]).withGasLimit(5000000); + let statusInteraction = contract.methods.status(["lucky"]).withGasLimit(5000000n); - let getLotteryInfoInteraction = contract.methods.getLotteryInfo(["lucky"]).withGasLimit(5000000); + let getLotteryInfoInteraction = contract.methods.getLotteryInfo(["lucky"]).withGasLimit(5000000n); // start() let startTransaction = startInteraction .withSender(alice.address) - .withNonce(14) + .withNonce(14n) .withChainID("mock") .buildTransaction(); @@ -392,7 +392,7 @@ describe("test smart contract interactor", function () { // status() (this is a view function, but for the sake of the test, we'll execute it) let statusTransaction = statusInteraction .withSender(alice.address) - .withNonce(15) + .withNonce(15n) .withChainID("mock") .buildTransaction(); @@ -410,7 +410,7 @@ describe("test smart contract interactor", function () { // lotteryInfo() (this is a view function, but for the sake of the test, we'll execute it) let getLotteryInfoTransaction = getLotteryInfoInteraction .withSender(alice.address) - .withNonce(15) + .withNonce(15n) .withChainID("mock") .buildTransaction(); diff --git a/src/abi/interaction.ts b/src/abi/interaction.ts index 2726cb3c..fa63b2a8 100644 --- a/src/abi/interaction.ts +++ b/src/abi/interaction.ts @@ -2,7 +2,7 @@ import { Account } from "../accounts"; import { Address } from "../address"; import { Compatibility } from "../compatibility"; import { TRANSACTION_VERSION_DEFAULT } from "../constants"; -import { IAddress, IChainID, IGasLimit, IGasPrice, INonce, ITokenTransfer, ITransactionValue } from "../interface"; +import { ITokenTransfer } from "../interface"; import { SmartContractTransactionsFactory } from "../smartContracts"; import { TokenTransfer } from "../tokens"; import { Transaction } from "../transaction"; @@ -34,13 +34,13 @@ export class Interaction { private readonly function: ContractFunction; private readonly args: TypedValue[]; - private nonce: INonce = 0; - private value: ITransactionValue = "0"; - private gasLimit: IGasLimit = 0; - private gasPrice: IGasPrice | undefined = undefined; - private chainID: IChainID = ""; + private nonce: bigint = 0n; + private value: bigint = 0n; + private gasLimit: bigint = 0n; + private gasPrice: bigint | undefined = undefined; + private chainID: string = ""; private querent: Address = Address.empty(); - private explicitReceiver?: IAddress; + private explicitReceiver?: Address; private sender: Address = Address.empty(); private version: number = TRANSACTION_VERSION_DEFAULT; @@ -53,7 +53,7 @@ export class Interaction { this.tokenTransfers = []; } - getContractAddress(): IAddress { + getContractAddress(): Address { return this.contract.getAddress(); } @@ -69,7 +69,7 @@ export class Interaction { return this.args; } - getValue(): ITransactionValue { + getValue(): bigint { return this.value; } @@ -77,11 +77,11 @@ export class Interaction { return this.tokenTransfers; } - getGasLimit(): IGasLimit { + getGasLimit(): bigint { return this.gasLimit; } - getExplicitReceiver(): IAddress | undefined { + getExplicitReceiver(): Address | undefined { return this.explicitReceiver; } @@ -128,7 +128,7 @@ export class Interaction { }); } - withValue(value: ITransactionValue): Interaction { + withValue(value: bigint): Interaction { this.value = value; return this; } @@ -148,17 +148,17 @@ export class Interaction { return this; } - withGasLimit(gasLimit: IGasLimit): Interaction { + withGasLimit(gasLimit: bigint): Interaction { this.gasLimit = gasLimit; return this; } - withGasPrice(gasPrice: IGasPrice): Interaction { + withGasPrice(gasPrice: bigint): Interaction { this.gasPrice = gasPrice; return this; } - withNonce(nonce: INonce): Interaction { + withNonce(nonce: bigint): Interaction { this.nonce = nonce; return this; } @@ -167,7 +167,7 @@ export class Interaction { return this.withNonce(account.getNonceThenIncrement()); } - withChainID(chainID: IChainID): Interaction { + withChainID(chainID: string): Interaction { this.chainID = chainID; return this; } @@ -190,7 +190,7 @@ export class Interaction { return this; } - withExplicitReceiver(receiver: IAddress): Interaction { + withExplicitReceiver(receiver: Address): Interaction { this.explicitReceiver = receiver; return this; } diff --git a/src/abi/interface.ts b/src/abi/interface.ts index 9fc39656..0f2b54e5 100644 --- a/src/abi/interface.ts +++ b/src/abi/interface.ts @@ -1,5 +1,4 @@ import { Address } from "../address"; -import { ITransactionValue } from "../interface"; import { Transaction } from "../transaction"; import { ReturnCode } from "./returnCode"; import { TypedValue } from "./typesystem"; @@ -65,7 +64,7 @@ export interface CallArguments { export interface QueryArguments { func: IContractFunction; args?: TypedValue[]; - value?: ITransactionValue; + value?: bigint; caller?: Address; } diff --git a/src/abi/nativeSerializer.ts b/src/abi/nativeSerializer.ts index 85ba5b7c..0632d97a 100644 --- a/src/abi/nativeSerializer.ts +++ b/src/abi/nativeSerializer.ts @@ -2,7 +2,6 @@ import BigNumber from "bignumber.js"; import { Address } from "../address"; import { ErrInvalidArgument } from "../errors"; -import { IAddress } from "../interface"; import { numberToPaddedHex } from "../utils.codec"; import { ArgumentErrorContext } from "./argumentErrorContext"; import { @@ -67,7 +66,7 @@ import { export namespace NativeTypes { export type NativeBuffer = Buffer | string; export type NativeBytes = Buffer | { valueOf(): Buffer } | string; - export type NativeAddress = string | Buffer | Address | { getAddress(): IAddress }; + export type NativeAddress = string | Buffer | Address | { getAddress(): Address }; export type NativeBigNumber = BigNumber.Value | bigint; } diff --git a/src/abi/query.ts b/src/abi/query.ts index f773f9f2..93e3e609 100644 --- a/src/abi/query.ts +++ b/src/abi/query.ts @@ -1,5 +1,4 @@ import { Address } from "../address"; -import { ITransactionValue } from "../interface"; import { ArgSerializer } from "./argSerializer"; import { IContractFunction } from "./interface"; import { TypedValue } from "./typesystem"; @@ -9,20 +8,20 @@ export class Query { address: Address; func: IContractFunction; args: TypedValue[]; - value: ITransactionValue; + value: bigint; constructor(obj: { caller?: Address; address: Address; func: IContractFunction; args?: TypedValue[]; - value?: ITransactionValue; + value?: bigint; }) { this.caller = obj.caller || Address.empty(); this.address = obj.address; this.func = obj.func; this.args = obj.args || []; - this.value = obj.value || 0; + this.value = obj.value || 0n; } getEncodedArguments(): string[] { diff --git a/src/abi/smartContract.spec.ts b/src/abi/smartContract.spec.ts index cc494451..4dc5dd0e 100644 --- a/src/abi/smartContract.spec.ts +++ b/src/abi/smartContract.spec.ts @@ -28,10 +28,10 @@ describe("test contract", () => { it("should compute contract address", async () => { let owner = new Address("93ee6143cdc10ce79f15b2a6c2ad38e9b6021c72a1779051f47154fd54cfbd5e"); - let firstContractAddress = SmartContract.computeAddress(owner, 0); + let firstContractAddress = SmartContract.computeAddress(owner, 0n); assert.equal(firstContractAddress.toBech32(), "erd1qqqqqqqqqqqqqpgqhdjjyq8dr7v5yq9tv6v5vt9tfvd00vg7h40q6779zn"); - let secondContractAddress = SmartContract.computeAddress(owner, 1); + let secondContractAddress = SmartContract.computeAddress(owner, 1n); assert.equal( secondContractAddress.toBech32(), "erd1qqqqqqqqqqqqqpgqde8eqjywyu6zlxjxuxqfg5kgtmn3setxh40qen8egy", @@ -51,18 +51,18 @@ describe("test contract", () => { }); provider.mockUpdateAccount(alice.address, (account) => { - account.nonce = 42; + account.nonce = 42n; }); await alice.sync(provider); - deployTransaction.setNonce(alice.account.nonce); + deployTransaction.nonce = alice.account.nonce; assert.equal(deployTransaction.getData().valueOf().toString(), "01020304@0500@0100"); - assert.equal(deployTransaction.getGasLimit().valueOf(), 1000000); - assert.equal(deployTransaction.getNonce().valueOf(), 42); + assert.equal(deployTransaction.gasLimit, 1000000n); + assert.equal(deployTransaction.nonce, 42n); // Compute & set the contract address - contract.setAddress(SmartContract.computeAddress(alice.address, 42)); + contract.setAddress(SmartContract.computeAddress(alice.address, 42n)); assert.equal( contract.getAddress().toBech32(), "erd1qqqqqqqqqqqqqpgq3ytm9m8dpeud35v3us20vsafp77smqghd8ss4jtm0q", @@ -97,7 +97,7 @@ describe("test contract", () => { }); provider.mockUpdateAccount(alice.address, (account) => { - account.nonce = 42; + account.nonce = 42n; }); let callTransactionOne = contract.call({ @@ -117,16 +117,16 @@ describe("test contract", () => { }); await alice.sync(provider); - callTransactionOne.setNonce(alice.account.nonce); + callTransactionOne.nonce = alice.account.nonce; alice.account.incrementNonce(); - callTransactionTwo.setNonce(alice.account.nonce); + callTransactionTwo.nonce = alice.account.nonce; - assert.equal(callTransactionOne.getNonce().valueOf(), 42); + assert.equal(callTransactionOne.nonce, 42n); assert.equal(callTransactionOne.getData().valueOf().toString(), "helloEarth@05@0123"); - assert.equal(callTransactionOne.getGasLimit().valueOf(), 150000); - assert.equal(callTransactionTwo.getNonce().valueOf(), 43); + assert.equal(callTransactionOne.gasLimit, 150000n); + assert.equal(callTransactionTwo.nonce, 43n); assert.equal(callTransactionTwo.getData().valueOf().toString(), "helloMars@05@0123"); - assert.equal(callTransactionTwo.getGasLimit().valueOf(), 1500000); + assert.equal(callTransactionTwo.gasLimit, 1500000n); // Sign transactions, broadcast them callTransactionOne.applySignature(await alice.signer.sign(callTransactionOne.serializeForSigning())); @@ -173,15 +173,15 @@ describe("test contract", () => { }); provider.mockUpdateAccount(alice.address, (account) => { - account.nonce = 42; + account.nonce = 42n; }); await alice.sync(provider); - deployTransaction.setNonce(alice.account.nonce); + deployTransaction.nonce = alice.account.nonce; assert.equal(deployTransaction.getData().valueOf().toString(), "upgradeContract@01020304@0100"); - assert.equal(deployTransaction.getGasLimit().valueOf(), 1000000); - assert.equal(deployTransaction.getNonce().valueOf(), 42); + assert.equal(deployTransaction.gasLimit, 1000000n); + assert.equal(deployTransaction.nonce, 42n); // Sign the transaction deployTransaction.applySignature(await alice.signer.sign(deployTransaction.serializeForSigning())); diff --git a/src/abi/smartContract.ts b/src/abi/smartContract.ts index b8fb5fe7..2814336c 100644 --- a/src/abi/smartContract.ts +++ b/src/abi/smartContract.ts @@ -2,7 +2,6 @@ import { Address, AddressComputer } from "../address"; import { Compatibility } from "../compatibility"; import { TRANSACTION_MIN_GAS_PRICE } from "../constants"; import { ErrContractHasNoAddress } from "../errors"; -import { INonce } from "../interface"; import { Transaction } from "../transaction"; import { TransactionsFactoryConfig } from "../transactionsFactoryConfig"; import { guardValueIsSet } from "../utils"; @@ -151,9 +150,9 @@ export class SmartContract implements ISmartContract { isPayableBySmartContract: metadataAsJson.payableBySc, }); - transaction.setChainID(chainID); - transaction.setValue(value ?? 0); - transaction.setGasPrice(gasPrice ?? TRANSACTION_MIN_GAS_PRICE); + transaction.chainID = chainID; + transaction.value = value ?? 0n; + transaction.gasPrice = gasPrice ?? BigInt(TRANSACTION_MIN_GAS_PRICE); return transaction; } @@ -222,8 +221,8 @@ export class SmartContract implements ISmartContract { }); transaction.setChainID(chainID); - transaction.setValue(value ?? 0); - transaction.setGasPrice(gasPrice ?? TRANSACTION_MIN_GAS_PRICE); + transaction.value = value ?? 0n; + transaction.gasPrice = gasPrice ?? BigInt(TRANSACTION_MIN_GAS_PRICE); return transaction; } @@ -258,7 +257,7 @@ export class SmartContract implements ISmartContract { transaction.setChainID(chainID); transaction.setValue(value); - transaction.setGasPrice(gasPrice ?? TRANSACTION_MIN_GAS_PRICE); + transaction.gasPrice = gasPrice ?? BigInt(TRANSACTION_MIN_GAS_PRICE); return transaction; } @@ -288,7 +287,7 @@ export class SmartContract implements ISmartContract { * @param owner The owner of the Smart Contract * @param nonce The owner nonce used for the deployment transaction */ - static computeAddress(owner: Address, nonce: INonce): Address { + static computeAddress(owner: Address, nonce: bigint): Address { const deployer = Address.fromBech32(owner.toBech32()); const addressComputer = new AddressComputer(); return addressComputer.computeContractAddress(deployer, BigInt(nonce.valueOf())); diff --git a/src/accountManagement/resources.ts b/src/accountManagement/resources.ts index aae540e7..26d10786 100644 --- a/src/accountManagement/resources.ts +++ b/src/accountManagement/resources.ts @@ -1,4 +1,4 @@ -import { IAddress } from "../interface"; +import { Address } from "../address"; -export type SetGuardianInput = { guardianAddress: IAddress; serviceID: string }; +export type SetGuardianInput = { guardianAddress: Address; serviceID: string }; export type SaveKeyValueInput = { keyValuePairs: Map }; diff --git a/src/accounts/account.ts b/src/accounts/account.ts index 4f6060a3..5eea3379 100644 --- a/src/accounts/account.ts +++ b/src/accounts/account.ts @@ -1,6 +1,5 @@ import { Address } from "../address"; import { LibraryConfig } from "../config"; -import { INonce } from "../interface"; import { Mnemonic, UserSigner, UserWallet } from "../wallet"; import { IAccount } from "./interfaces"; @@ -16,7 +15,7 @@ export class Account implements IAccount { /** * The nonce of the account (the account sequence number). */ - nonce: INonce = 0; + nonce: bigint = 0n; /** * The signer of the account. @@ -35,15 +34,15 @@ export class Account implements IAccount { * Increments (locally) the nonce (the account sequence number). */ incrementNonce() { - this.nonce = this.nonce.valueOf() + 1; + this.nonce = this.nonce + 1n; } /** * Gets then increments (locally) the nonce (the account sequence number). */ - getNonceThenIncrement(): INonce { + getNonceThenIncrement(): bigint { let nonce = this.nonce; - this.nonce = this.nonce.valueOf() + 1; + this.nonce = this.nonce + 1n; return nonce; } diff --git a/src/compatibility.ts b/src/compatibility.ts index 7fb18b5a..357b724a 100644 --- a/src/compatibility.ts +++ b/src/compatibility.ts @@ -1,5 +1,4 @@ import { Address } from "./address"; -import { IAddress } from "./interface"; /** * For internal use only. @@ -8,7 +7,7 @@ export class Compatibility { /** * For internal use only. */ - static guardAddressIsSetAndNonZero(address: IAddress | undefined, context: string, resolution: string) { + static guardAddressIsSetAndNonZero(address: Address | undefined, context: string, resolution: string) { if (!address || address.bech32() == "") { console.warn( `${context}: address should be set; ${resolution}. In the future, this will throw an exception instead of emitting a WARN.`, diff --git a/src/entrypoints/entrypoints.spec.ts b/src/entrypoints/entrypoints.spec.ts index e2173e05..f26e423e 100644 --- a/src/entrypoints/entrypoints.spec.ts +++ b/src/entrypoints/entrypoints.spec.ts @@ -21,7 +21,7 @@ describe("TestEntrypoint", () => { it("native transfer", async () => { const controller = entrypoint.createTransfersController(); const sender = Account.newFromPem(alicePem.pemFileText); - sender.nonce = 77777; + sender.nonce = 77777n; const transaction = await controller.createTransactionForTransfer( sender, @@ -87,10 +87,10 @@ describe("TestEntrypoint", () => { it("create relayed transaction", async function () { const transferController = entrypoint.createTransfersController(); const sender = Account.newFromPem(alicePem.pemFileText); - sender.nonce = 77777; + sender.nonce = 77777n; const relayer = Account.newFromPem(bobPem.pemFileText); - relayer.nonce = 7; + relayer.nonce = 7n; const transaction = await transferController.createTransactionForTransfer( sender, diff --git a/src/entrypoints/entrypoints.ts b/src/entrypoints/entrypoints.ts index 7ee11e04..45b4a2cb 100644 --- a/src/entrypoints/entrypoints.ts +++ b/src/entrypoints/entrypoints.ts @@ -64,7 +64,7 @@ class NetworkEntrypoint { return verifier.verify(messageComputer.computeBytesForVerifying(message), message.signature); } - async recallAccountNonce(address: Address): Promise { + async recallAccountNonce(address: Address): Promise { return (await this.networkProvider.getAccount(address)).nonce; } diff --git a/src/interface.ts b/src/interface.ts index 545160b5..95252daa 100644 --- a/src/interface.ts +++ b/src/interface.ts @@ -28,46 +28,6 @@ export interface IPlainTransactionObject { relayerSignature?: string; } -export interface ISignature { - hex(): string; -} - -export interface IAddress { - bech32(): string; -} - -export interface ITransactionValue { - toString(): string; -} - -export interface IAccountBalance { - toString(): string; -} - -export interface INonce { - valueOf(): number; -} - -export interface IChainID { - valueOf(): string; -} - -export interface IGasLimit { - valueOf(): number; -} - -export interface IGasPrice { - valueOf(): number; -} - -export interface ITransactionVersion { - valueOf(): number; -} - -export interface ITransactionOptions { - valueOf(): number; -} - export interface ITransactionPayload { length(): number; encoded(): string; diff --git a/src/interfaceOfNetwork.ts b/src/interfaceOfNetwork.ts index 8d530377..e3a4e7e5 100644 --- a/src/interfaceOfNetwork.ts +++ b/src/interfaceOfNetwork.ts @@ -1,9 +1,8 @@ import { Address } from "./address"; -import { IAccountBalance, IAddress } from "./interface"; export interface IAccountOnNetwork { - nonce: number; - balance: IAccountBalance; + nonce: bigint; + balance: bigint; } export interface INetworkConfig { @@ -19,8 +18,8 @@ export interface ITransactionOnNetwork { hash: string; type: string; value: string; - receiver: IAddress; - sender: IAddress; + receiver: Address; + sender: Address; function?: string; data: Buffer; status: ITransactionStatus; @@ -49,8 +48,8 @@ export interface IContractResults { export interface IContractResultItem { hash: string; nonce: number; - receiver: IAddress; - sender: IAddress; + receiver: Address; + sender: Address; data: string; returnMessage: string; logs: ITransactionLogs; diff --git a/src/networkProviders/accounts.ts b/src/networkProviders/accounts.ts index 673487e0..086885d8 100644 --- a/src/networkProviders/accounts.ts +++ b/src/networkProviders/accounts.ts @@ -1,4 +1,3 @@ -import BigNumber from "bignumber.js"; import { Address } from "../address"; /** @@ -6,8 +5,8 @@ import { Address } from "../address"; */ export class AccountOnNetwork { address: Address = Address.empty(); - nonce: number = 0; - balance: BigNumber = new BigNumber(0); + nonce: bigint = 0n; + balance: bigint = 0n; code: string = ""; userName: string = ""; @@ -19,8 +18,8 @@ export class AccountOnNetwork { let result = new AccountOnNetwork(); result.address = new Address(payload["address"] || ""); - result.nonce = Number(payload["nonce"] || 0); - result.balance = new BigNumber(payload["balance"] || 0); + result.nonce = BigInt(payload["nonce"] || 0); + result.balance = BigInt(payload["balance"] || 0); result.code = payload["code"] || ""; result.userName = payload["username"] || ""; diff --git a/src/proto/serializer.spec.ts b/src/proto/serializer.spec.ts index 9a6b693e..dd7e7068 100644 --- a/src/proto/serializer.spec.ts +++ b/src/proto/serializer.spec.ts @@ -1,6 +1,5 @@ import { assert } from "chai"; import { Address } from "../address"; -import { Signature } from "../signature"; import { loadTestWallets, TestWallet } from "../testutils"; import { TokenTransfer } from "../tokens"; import { Transaction } from "../transaction"; @@ -110,8 +109,9 @@ describe("serialize transactions", () => { }); transaction.applySignature( - new Signature( + Buffer.from( "dfa3e9f2fdec60dcb353bac3b3435b4a2ff251e7e98eaf8620f46c731fc70c8ba5615fd4e208b05e75fe0f7dc44b7a99567e29f94fcd91efac7e67b182cd2a04", + "hex", ), ); diff --git a/src/testutils/mockNetworkProvider.ts b/src/testutils/mockNetworkProvider.ts index 6d61df27..ea560da9 100644 --- a/src/testutils/mockNetworkProvider.ts +++ b/src/testutils/mockNetworkProvider.ts @@ -40,18 +40,18 @@ export class MockNetworkProvider implements INetworkProvider { this.accounts.set( MockNetworkProvider.AddressOfAlice.toBech32(), new AccountOnNetwork({ - nonce: 0, + nonce: 0n, balance: createAccountBalance(1000), }), ); this.accounts.set( MockNetworkProvider.AddressOfBob.toBech32(), - new AccountOnNetwork({ nonce: 5, balance: createAccountBalance(500) }), + new AccountOnNetwork({ nonce: 5n, balance: createAccountBalance(500) }), ); this.accounts.set( MockNetworkProvider.AddressOfCarol.toBech32(), new AccountOnNetwork({ - nonce: 42, + nonce: 42n, balance: createAccountBalance(300), }), ); diff --git a/src/testutils/utils.ts b/src/testutils/utils.ts index 636c107a..1ba118c9 100644 --- a/src/testutils/utils.ts +++ b/src/testutils/utils.ts @@ -1,4 +1,3 @@ -import BigNumber from "bignumber.js"; import * as fs from "fs"; import { PathLike } from "fs"; import { AbiRegistry, Code, SmartContract, TypedValue } from "../abi"; @@ -86,8 +85,8 @@ export function setupUnitTestWatcherTimeouts() { TransactionWatcher.DefaultTimeout = 42 * 42; } -export function createAccountBalance(egld: number): BigNumber { - return new BigNumber(egld.toString() + "0".repeat(18)); +export function createAccountBalance(egld: number): bigint { + return BigInt(egld.toString() + "0".repeat(18)); } export function b64TopicsToBytes(topics: string[]): Uint8Array[] { diff --git a/src/testutils/wallets.ts b/src/testutils/wallets.ts index b6631444..628e53d1 100644 --- a/src/testutils/wallets.ts +++ b/src/testutils/wallets.ts @@ -2,7 +2,6 @@ import * as fs from "fs"; import * as path from "path"; import { Account } from "../accounts"; import { Address } from "../address"; -import { IAddress } from "../interface"; import { IAccountOnNetwork } from "../interfaceOfNetwork"; import { getAxios } from "../utils"; import { UserSecretKey, UserSigner } from "./../wallet"; @@ -12,7 +11,7 @@ import { isOnBrowserTests } from "./utils"; export const DummyMnemonicOf12Words = "matter trumpet twenty parade fame north lift sail valve salon foster cinnamon"; interface IAccountFetcher { - getAccount(address: IAddress): Promise; + getAccount(address: Address): Promise; } export async function loadAndSyncTestWallets(provider: IAccountFetcher): Promise> { diff --git a/src/transaction.spec.ts b/src/transaction.spec.ts index f128b4b1..745ed11a 100644 --- a/src/transaction.spec.ts +++ b/src/transaction.spec.ts @@ -503,9 +503,6 @@ describe("test transaction", async () => { assert.equal(tx2.getValue().toString(), "123456789000000000000000000000"); const tx3 = new Transaction({ - // Passing a BigNumber is not recommended. - // However, ITransactionValue interface is permissive, and developers may mistakenly pass such objects as values. - // TokenTransfer objects or simple strings (see above) are preferred, instead. value: BigInt("123456789000000000000000000000"), sender: wallets.alice.address, receiver: wallets.bob.address, diff --git a/src/transaction.ts b/src/transaction.ts index 3f61a40e..53ca342f 100644 --- a/src/transaction.ts +++ b/src/transaction.ts @@ -2,7 +2,7 @@ import { BigNumber } from "bignumber.js"; import { Address } from "./address"; import { TRANSACTION_MIN_GAS_PRICE, TRANSACTION_OPTIONS_DEFAULT, TRANSACTION_VERSION_DEFAULT } from "./constants"; import { Hash } from "./hash"; -import { IGasLimit, IGasPrice, INonce, IPlainTransactionObject, ISignature, ITransactionValue } from "./interface"; +import { IPlainTransactionObject } from "./interface"; import { INetworkConfig } from "./interfaceOfNetwork"; import { interpretSignatureAsBuffer } from "./signature"; import { TransactionComputer } from "./transactionComputer"; @@ -119,20 +119,18 @@ export class Transaction { guardianSignature?: Uint8Array; relayerSignature?: Uint8Array; }) { - this.nonce = BigInt(options.nonce?.valueOf() || 0n); - // 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.nonce = options.nonce ?? 0n; + this.value = options.value ?? 0n; 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); - this.gasLimit = BigInt(options.gasLimit.valueOf()); - this.data = options.data?.valueOf() || new Uint8Array(); + this.gasPrice = options.gasPrice ?? BigInt(TRANSACTION_MIN_GAS_PRICE); + this.gasLimit = options.gasLimit; + this.data = options.data ?? new Uint8Array(); this.chainID = options.chainID.valueOf(); - this.version = Number(options.version?.valueOf() || TRANSACTION_VERSION_DEFAULT); - this.options = Number(options.options?.valueOf() || TRANSACTION_OPTIONS_DEFAULT); + this.version = options.version ?? TRANSACTION_VERSION_DEFAULT; + this.options = options.options ?? TRANSACTION_OPTIONS_DEFAULT; this.guardian = options.guardian ?? Address.empty(); this.relayer = options.relayer ? options.relayer : Address.empty(); @@ -144,30 +142,30 @@ export class Transaction { /** * Legacy method, use the "nonce" property instead. */ - getNonce(): INonce { - return Number(this.nonce); + getNonce(): bigint { + return this.nonce; } /** * Legacy method, use the "nonce" property instead. * Sets the account sequence number of the sender. Must be done prior signing. */ - setNonce(nonce: INonce | bigint) { - this.nonce = BigInt(nonce.valueOf()); + setNonce(nonce: bigint) { + this.nonce = nonce; } /** * Legacy method, use the "value" property instead. */ - getValue(): ITransactionValue { + getValue(): bigint { return this.value; } /** * Legacy method, use the "value" property instead. */ - setValue(value: ITransactionValue | bigint) { - this.value = BigInt(value.toString()); + setValue(value: bigint) { + this.value = value; } /** @@ -229,29 +227,29 @@ export class Transaction { /** * Legacy method, use the "gasPrice" property instead. */ - getGasPrice(): IGasPrice { - return Number(this.gasPrice); + getGasPrice(): bigint { + return this.gasPrice; } /** * Legacy method, use the "gasPrice" property instead. */ - setGasPrice(gasPrice: IGasPrice | bigint) { - this.gasPrice = BigInt(gasPrice.valueOf()); + setGasPrice(gasPrice: bigint) { + this.gasPrice = gasPrice; } /** * Legacy method, use the "gasLimit" property instead. */ - getGasLimit(): IGasLimit { - return Number(this.gasLimit); + getGasLimit(): bigint { + return this.gasLimit; } /** * Legacy method, use the "gasLimit" property instead. */ - setGasLimit(gasLimit: IGasLimit | bigint) { - this.gasLimit = BigInt(gasLimit.valueOf()); + setGasLimit(gasLimit: bigint) { + this.gasLimit = gasLimit; } /** @@ -412,7 +410,7 @@ export class Transaction { data: Buffer.from(object.data || "", "base64"), chainID: String(object.chainID), version: Number(object.version), - options: Number(object.options), + options: object.options ? Number(object.options) : undefined, signature: Buffer.from(object.signature || "", "hex"), guardianSignature: Buffer.from(object.guardianSignature || "", "hex"), relayerSignature: Buffer.from(object.relayerSignature || "", "hex"), @@ -427,7 +425,7 @@ export class Transaction { * * @param signature The signature, as computed by a signer. */ - applySignature(signature: ISignature | Uint8Array) { + applySignature(signature: Uint8Array) { this.signature = interpretSignatureAsBuffer(signature); } @@ -437,7 +435,7 @@ export class Transaction { * * @param guardianSignature The signature, as computed by a signer. */ - applyGuardianSignature(guardianSignature: ISignature | Uint8Array) { + applyGuardianSignature(guardianSignature: Uint8Array) { this.guardianSignature = interpretSignatureAsBuffer(guardianSignature); } diff --git a/src/utils.codec.ts b/src/utils.codec.ts index 4f70e93c..ab64eed8 100644 --- a/src/utils.codec.ts +++ b/src/utils.codec.ts @@ -1,7 +1,6 @@ import BigNumber from "bignumber.js"; import * as contractsCodecUtils from "./abi/codec/utils"; import { Address } from "./address"; -import { IAddress } from "./interface"; export function numberToPaddedHex(value: bigint | number | BigNumber.Value) { let hexableNumber: { toString(radix?: number): string }; @@ -54,7 +53,7 @@ export function bigIntToHex(value: BigNumber.Value): string { return contractsCodecUtils.getHexMagnitudeOfBigInt(value); } -export function addressToHex(address: IAddress): string { +export function addressToHex(address: Address): string { const buffer = Address.fromBech32(address.toString()).pubkey(); return buffer.toString("hex"); }