diff --git a/.github/workflows/package-lock-checks.yml b/.github/workflows/package-lock-checks.yml index a04246a0..7490c457 100644 --- a/.github/workflows/package-lock-checks.yml +++ b/.github/workflows/package-lock-checks.yml @@ -1,8 +1,6 @@ name: Check package-lock.json on: - pull_request: - branches: [ main ] workflow_dispatch: jobs: diff --git a/package-lock.json b/package-lock.json index 11e6f207..7502f59c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@multiversx/sdk-core", - "version": "11.2.0", + "version": "11.3.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@multiversx/sdk-core", - "version": "11.2.0", + "version": "11.3.0", "license": "MIT", "dependencies": { "@multiversx/sdk-transaction-decoder": "1.0.2", diff --git a/package.json b/package.json index ebc49b5c..55246a15 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@multiversx/sdk-core", - "version": "11.2.0", + "version": "11.3.0", "description": "MultiversX SDK for JavaScript and TypeScript", "main": "out/index.js", "types": "out/index.d.js", diff --git a/src/proto/serializer.spec.ts b/src/proto/serializer.spec.ts index 3ba47bbc..6d2a513b 100644 --- a/src/proto/serializer.spec.ts +++ b/src/proto/serializer.spec.ts @@ -1,12 +1,11 @@ import { assert } from "chai"; -import { ProtoSerializer } from "./serializer"; -import { Transaction } from "../transaction"; -import { loadTestWallets, TestWallet } from "../testutils"; -import { Signature } from "../signature"; import { TransactionVersion } from "../networkParams"; -import { TransactionPayload } from "../transactionPayload"; -import BigNumber from "bignumber.js"; +import { Signature } from "../signature"; +import { loadTestWallets, TestWallet } from "../testutils"; import { TokenPayment } from "../tokenPayment"; +import { Transaction } from "../transaction"; +import { TransactionPayload } from "../transactionPayload"; +import { ProtoSerializer } from "./serializer"; describe("serialize transactions", () => { let wallets: Record; @@ -69,7 +68,7 @@ describe("serialize transactions", () => { it("with data, with large value", async () => { let transaction = new Transaction({ nonce: 92, - value: new BigNumber("123456789000000000000000000000"), + value: "123456789000000000000000000000", sender: wallets.alice.address, receiver: wallets.bob.address, gasLimit: 100000, @@ -86,7 +85,7 @@ describe("serialize transactions", () => { it("with nonce = 0", async () => { let transaction = new Transaction({ nonce: 0, - value: new BigNumber("0"), + value: "0", sender: wallets.alice.address, receiver: wallets.bob.address, gasLimit: 80000, diff --git a/src/smartcontracts/typesystem/abiRegistry.spec.ts b/src/smartcontracts/typesystem/abiRegistry.spec.ts index 40588f49..1754ecfc 100644 --- a/src/smartcontracts/typesystem/abiRegistry.spec.ts +++ b/src/smartcontracts/typesystem/abiRegistry.spec.ts @@ -7,7 +7,7 @@ import { BytesType } from "./bytes"; import { EnumType } from "./enum"; import { ListType, OptionType } from "./generic"; import { ArrayVecType } from "./genericArray"; -import { BigUIntType, I64Type, U32Type, U64Type, U8Type } from "./numerical"; +import { BigUIntType, I64Type, U32Type, U64Type } from "./numerical"; import { StructType } from "./struct"; import { TokenIdentifierType } from "./tokenIdentifier"; @@ -98,8 +98,8 @@ describe("test abi registry", () => { assert.equal(dummyType.getFieldDefinition("raw")!.type.getClassName(), ArrayVecType.ClassName); }); - it("should load ABI when custom types are out of order", async () => { - const registry = await loadAbiRegistry("src/testdata/custom-types-out-of-order.abi.json"); + it("should load ABI when custom types are out of order (a)", async () => { + const registry = await loadAbiRegistry("src/testdata/custom-types-out-of-order-a.abi.json"); assert.deepEqual(registry.getStruct("EsdtTokenPayment").getNamesOfDependencies(), ["EsdtTokenType", "TokenIdentifier", "u64", "BigUint"]); assert.deepEqual(registry.getEnum("EsdtTokenType").getNamesOfDependencies(), []); @@ -107,4 +107,29 @@ describe("test abi registry", () => { assert.deepEqual(registry.getStruct("TypeB").getNamesOfDependencies(), ["TypeC", "u64"]); assert.deepEqual(registry.getStruct("TypeC").getNamesOfDependencies(), ["u64"]); }); + + it("should load ABI when custom types are out of order (b)", async () => { + const registry = await loadAbiRegistry("src/testdata/custom-types-out-of-order-b.abi.json"); + + assert.deepEqual(registry.getStruct("EsdtTokenPayment").getNamesOfDependencies(), ["EsdtTokenType", "TokenIdentifier", "u64", "BigUint"]); + assert.deepEqual(registry.getEnum("EsdtTokenType").getNamesOfDependencies(), []); + assert.deepEqual(registry.getStruct("TypeA").getNamesOfDependencies(), ["TypeB", "TypeC", "u64"]); + assert.deepEqual(registry.getStruct("TypeB").getNamesOfDependencies(), ["TypeC", "u64"]); + assert.deepEqual(registry.getStruct("TypeC").getNamesOfDependencies(), ["u64"]); + }); + + it("should load ABI when custom types are out of order (community example: c)", async () => { + const registry = await loadAbiRegistry("src/testdata/custom-types-out-of-order-c.abi.json"); + + assert.lengthOf(registry.customTypes, 5); + assert.deepEqual(registry.getStruct("LoanCreateOptions").getNamesOfDependencies(), ["BigUint", "Address", "TokenIdentifier", "Status", "bytes"]); + + }); + + it("should load ABI when custom types are out of order (community example: d)", async () => { + const registry = await loadAbiRegistry("src/testdata/custom-types-out-of-order-d.abi.json"); + + assert.lengthOf(registry.customTypes, 12); + assert.deepEqual(registry.getStruct("AuctionItem").getNamesOfDependencies(), ["u64", "Address", "BigUint", "Option", "NftData", "bytes", "TokenIdentifier", "List"]); + }); }); diff --git a/src/smartcontracts/typesystem/abiRegistry.ts b/src/smartcontracts/typesystem/abiRegistry.ts index 9a964dc7..2b21afad 100644 --- a/src/smartcontracts/typesystem/abiRegistry.ts +++ b/src/smartcontracts/typesystem/abiRegistry.ts @@ -1,11 +1,11 @@ import * as errors from "../../errors"; import { guardValueIsSetWithMessage } from "../../utils"; -import { StructType } from "./struct"; import { ContractInterface } from "./contractInterface"; -import { CustomType } from "./types"; +import { EndpointDefinition, EndpointParameterDefinition } from "./endpoint"; import { EnumType } from "./enum"; +import { StructType } from "./struct"; import { TypeMapper } from "./typeMapper"; -import { EndpointDefinition, EndpointParameterDefinition } from "./endpoint"; +import { CustomType } from "./types"; export class AbiRegistry { readonly interfaces: ContractInterface[] = []; @@ -33,8 +33,6 @@ export class AbiRegistry { this.customTypes.push(customType); } - this.sortCustomTypesByDependencies(); - return this; } @@ -48,20 +46,6 @@ export class AbiRegistry { throw new errors.ErrTypingSystem(`Unknown type discriminant: ${typeDiscriminant}`); } - private sortCustomTypesByDependencies() { - // TODO: Improve consistency of the sorting function (and make sure the sorting is stable): https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort - this.customTypes.sort((a: CustomType, b: CustomType) => { - const bDependsOnA = b.getNamesOfDependencies().indexOf(a.getName()) > -1; - if (bDependsOnA) { - // Sort "a" before "b". - return -1; - } - - // Sort "b" before "a". - return 1; - }); - } - getInterface(name: string): ContractInterface { let result = this.interfaces.find((e) => e.name == name); guardValueIsSetWithMessage(`interface [${name}] not found`, result); @@ -109,13 +93,15 @@ export class AbiRegistry { // First, remap custom types (actually, under the hood, this will remap types of struct fields) for (const type of this.customTypes) { - const mappedTyped = mapper.mapType(type); - newCustomTypes.push(mappedTyped); + this.mapCustomTypeDepthFirst(type, this.customTypes, mapper, newCustomTypes); + } + + if (this.customTypes.length != newCustomTypes.length) { + throw new errors.ErrTypingSystem("Did not re-map all custom types"); } // Then, remap types of all endpoint parameters. - // But we'll use an enhanced mapper, that takes into account the results from the previous step. - mapper = new TypeMapper(newCustomTypes); + // The mapper learned all necessary types in the previous step. for (const iface of this.interfaces) { let newEndpoints: EndpointDefinition[] = []; for (const endpoint of iface.endpoints) { @@ -132,6 +118,26 @@ export class AbiRegistry { return newRegistry; } + + private mapCustomTypeDepthFirst(typeToMap: CustomType, allTypesToMap: CustomType[], mapper: TypeMapper, mappedTypes: CustomType[]) { + const hasBeenMapped = mappedTypes.findIndex(type => type.getName() == typeToMap.getName()) >= 0; + if (hasBeenMapped) { + return; + } + + for (const typeName of typeToMap.getNamesOfDependencies()) { + const dependencyType = allTypesToMap.find(type => type.getName() == typeName); + if (!dependencyType) { + // It's a type that we don't have to map (e.g. could be a primitive type). + continue; + } + + this.mapCustomTypeDepthFirst(dependencyType, allTypesToMap, mapper, mappedTypes) + } + + const mappedType = mapper.mapType(typeToMap); + mappedTypes.push(mappedType); + } } function mapEndpoint(endpoint: EndpointDefinition, mapper: TypeMapper): EndpointDefinition { diff --git a/src/smartcontracts/typesystem/typeMapper.ts b/src/smartcontracts/typesystem/typeMapper.ts index 5962536c..2f30eafe 100644 --- a/src/smartcontracts/typesystem/typeMapper.ts +++ b/src/smartcontracts/typesystem/typeMapper.ts @@ -103,10 +103,18 @@ export class TypeMapper { } } + /** + * Maps a "raw type" object to a "known (specific) type" object. + * In the process, it also learns the new type. + * Can only map types if their dependencies were previously learned (through mapping). + */ mapType(type: Type): Type { - let mappedType = this.mapRecursiveType(type); + let mappedType = this.mapTypeRecursively(type); if (mappedType) { - // We do not learn generic types (that also have type parameters) + // We do not learn generic types (that also have type parameters), + // we only learn closed, non-generic types. + // Reason: in the ABI, generic types are unnamed. + // E.g.: two occurrences of List aren't recognized as a single type (simplification). if (!mappedType.isGenericType()) { this.learnType(mappedType); } @@ -117,7 +125,7 @@ export class TypeMapper { throw new errors.ErrTypingSystem(`Cannot map the type "${type.getName()}" to a known type`); } - mapRecursiveType(type: Type): Type | null { + private mapTypeRecursively(type: Type): Type | null { let isGeneric = type.isGenericType(); let previouslyLearnedType = this.learnedTypesMap.get(type.getName()); diff --git a/src/testdata/custom-types-out-of-order.abi.json b/src/testdata/custom-types-out-of-order-a.abi.json similarity index 100% rename from src/testdata/custom-types-out-of-order.abi.json rename to src/testdata/custom-types-out-of-order-a.abi.json diff --git a/src/testdata/custom-types-out-of-order-b.abi.json b/src/testdata/custom-types-out-of-order-b.abi.json new file mode 100644 index 00000000..899cc0ae --- /dev/null +++ b/src/testdata/custom-types-out-of-order-b.abi.json @@ -0,0 +1,66 @@ +{ + "name": "Sample", + "types": { + "EsdtTokenPayment": { + "type": "struct", + "fields": [ + { + "name": "token_type", + "type": "EsdtTokenType" + }, + { + "name": "token_identifier", + "type": "TokenIdentifier" + }, + { + "name": "token_nonce", + "type": "u64" + }, + { + "name": "amount", + "type": "BigUint" + } + ] + }, + "TypeC": { + "type": "struct", + "fields": [ + { + "name": "foobar", + "type": "u64" + } + ] + }, + "EsdtTokenType": { + "type": "enum", + "variants": [ + { + "name": "Fungible", + "discriminant": 0 + }, + { + "name": "NonFungible", + "discriminant": 1 + } + ] + }, + "TypeB": { + "type": "struct", + "fields": [ + { + "name": "c", + "type": "TypeC" + } + ] + }, + "TypeA": { + "type": "struct", + "fields": [ + { + "name": "b", + "type": "TypeB" + } + ] + } + } +} diff --git a/src/testdata/custom-types-out-of-order-c.abi.json b/src/testdata/custom-types-out-of-order-c.abi.json new file mode 100644 index 00000000..1a47ec19 --- /dev/null +++ b/src/testdata/custom-types-out-of-order-c.abi.json @@ -0,0 +1,254 @@ +{ + "name": "Issue_237_Part_1", + "constructor": { + "inputs": [], + "outputs": [] + }, + "endpoints": [ + { + "name": "getById", + "mutability": "readonly", + "inputs": [ + { + "name": "id", + "type": "BigUint" + } + ], + "outputs": [ + { + "type": "LoanInfo" + } + ] + }, + { + "name": "getList", + "mutability": "readonly", + "inputs": [ + { + "name": "page", + "type": "u64" + }, + { + "name": "size", + "type": "u16" + } + ], + "outputs": [ + { + "type": "variadic", + "multi_result": true + } + ] + }, + { + "name": "create", + "mutability": "mutable", + "inputs": [ + { + "name": "data", + "type": "LoanCreateOptions" + } + ], + "outputs": [ + { + "type": "LoanInfo" + } + ] + }, + { + "name": "createByArgs", + "mutability": "mutable", + "inputs": [ + { + "name": "amount", + "type": "BigUint" + }, + { + "name": "loaner_address", + "type": "Address" + }, + { + "name": "token", + "type": "EgldOrEsdtTokenIdentifier" + }, + { + "name": "status", + "type": "Status" + }, + { + "name": "note", + "type": "bytes" + } + ], + "outputs": [ + { + "type": "LoanInfo" + } + ] + }, + { + "name": "updateStatus", + "mutability": "mutable", + "inputs": [ + { + "name": "id", + "type": "BigUint" + }, + { + "name": "status", + "type": "Status" + } + ], + "outputs": [ + { + "type": "Status" + } + ] + }, + { + "name": "setArrayOfStruct", + "mutability": "mutable", + "inputs": [ + { + "name": "list", + "type": "variadic", + "multi_arg": true + } + ], + "outputs": [] + }, + { + "name": "setArrayOfAddresses", + "mutability": "mutable", + "inputs": [ + { + "name": "list", + "type": "variadic
", + "multi_arg": true + } + ], + "outputs": [] + }, + { + "name": "setArrayOfBigInt", + "mutability": "mutable", + "inputs": [ + { + "name": "list", + "type": "variadic", + "multi_arg": true + } + ], + "outputs": [] + } + ], + "events": [], + "hasCallback": false, + "types": { + "Item": { + "type": "struct", + "fields": [ + { + "name": "amount", + "type": "BigUint" + }, + { + "name": "address", + "type": "Address" + } + ] + }, + "LoanCreateOptions": { + "type": "struct", + "fields": [ + { + "name": "amount", + "type": "BigUint" + }, + { + "name": "loaner_address", + "type": "Address" + }, + { + "name": "token", + "type": "EgldOrEsdtTokenIdentifier" + }, + { + "name": "status", + "type": "Status" + }, + { + "name": "note", + "type": "bytes" + } + ] + }, + "LoanInfo": { + "type": "struct", + "fields": [ + { + "name": "id", + "type": "BigUint" + }, + { + "name": "amount", + "type": "BigUint" + }, + { + "name": "loaner_address", + "type": "Address" + }, + { + "name": "token", + "type": "EgldOrEsdtTokenIdentifier" + }, + { + "name": "timestamp", + "type": "u64" + }, + { + "name": "status", + "type": "Status" + }, + { + "name": "note", + "type": "bytes" + } + ] + }, + "LoanShortInfo": { + "type": "struct", + "fields": [ + { + "name": "id", + "type": "BigUint" + }, + { + "name": "amount", + "type": "BigUint" + }, + { + "name": "loaner_address", + "type": "Address" + }, + { + "name": "token", + "type": "EgldOrEsdtTokenIdentifier" + } + ] + }, + "Status": { + "type": "enum", + "variants": [ + { + "name": "Redeemed", + "discriminant": 0 + }, + { + "name": "NotRedeemed", + "discriminant": 1 + } + ] + } + } +} diff --git a/src/testdata/custom-types-out-of-order-d.abi.json b/src/testdata/custom-types-out-of-order-d.abi.json new file mode 100644 index 00000000..b93add57 --- /dev/null +++ b/src/testdata/custom-types-out-of-order-d.abi.json @@ -0,0 +1,923 @@ +{ + "name": "Issue_237_Part_2", + "constructor": { + "inputs": [], + "outputs": [] + }, + "endpoints": [ + { + "name": "donate_permanent", + "mutability": "mutable", + "payableInTokens": [ + "EGLD" + ], + "inputs": [], + "outputs": [] + }, + { + "name": "donate_temporary", + "mutability": "mutable", + "payableInTokens": [ + "EGLD" + ], + "inputs": [], + "outputs": [] + }, + { + "name": "get_user_overview", + "mutability": "readonly", + "inputs": [ + { + "name": "wallet_address", + "type": "Address" + } + ], + "outputs": [ + { + "type": "UserOverview" + } + ] + }, + { + "name": "get_donations_overview", + "mutability": "readonly", + "inputs": [], + "outputs": [ + { + "type": "DonationsOverview" + } + ] + }, + { + "name": "set_staking_provider", + "onlyOwner": true, + "mutability": "mutable", + "inputs": [ + { + "name": "staking_provider", + "type": "Address" + }, + { + "name": "unbonding_duration", + "type": "u8" + } + ], + "outputs": [] + }, + { + "name": "unbond", + "mutability": "mutable", + "inputs": [ + { + "name": "amount_to_unbond", + "type": "BigUint" + } + ], + "outputs": [] + }, + { + "name": "initiate_withdraw", + "mutability": "mutable", + "inputs": [], + "outputs": [] + }, + { + "name": "finalize_withdrawal", + "mutability": "mutable", + "inputs": [], + "outputs": [] + }, + { + "name": "claimRewards", + "onlyOwner": true, + "mutability": "mutable", + "inputs": [], + "outputs": [] + }, + { + "name": "reinvestRewards", + "onlyOwner": true, + "mutability": "mutable", + "inputs": [], + "outputs": [] + }, + { + "name": "unbondRewards", + "onlyOwner": true, + "mutability": "mutable", + "inputs": [], + "outputs": [] + }, + { + "name": "moveUnbondedRewardsToSc", + "onlyOwner": true, + "mutability": "mutable", + "inputs": [], + "outputs": [] + }, + { + "name": "moveUnbondedRewardsBackToRewardsPile", + "onlyOwner": true, + "mutability": "mutable", + "inputs": [], + "outputs": [] + }, + { + "name": "withdrawRewards", + "onlyOwner": true, + "mutability": "mutable", + "inputs": [], + "outputs": [] + }, + { + "name": "get_staking_provider_address", + "mutability": "readonly", + "inputs": [], + "outputs": [ + { + "type": "Address" + } + ] + }, + { + "name": "get_staking_unbonding_duration", + "mutability": "readonly", + "inputs": [], + "outputs": [ + { + "type": "u8" + } + ] + }, + { + "name": "get_donators", + "mutability": "readonly", + "inputs": [], + "outputs": [ + { + "type": "variadic
", + "multi_result": true + } + ] + }, + { + "name": "get_total_donated", + "mutability": "readonly", + "inputs": [], + "outputs": [ + { + "type": "BigUint" + } + ] + }, + { + "name": "get_open_donations", + "mutability": "readonly", + "inputs": [], + "outputs": [ + { + "type": "BigUint" + } + ] + }, + { + "name": "get_pending_donations", + "mutability": "readonly", + "inputs": [], + "outputs": [ + { + "type": "BigUint" + } + ] + }, + { + "name": "get_staked_donations", + "mutability": "readonly", + "inputs": [], + "outputs": [ + { + "type": "BigUint" + } + ] + }, + { + "name": "get_unbonding_donations", + "mutability": "readonly", + "inputs": [], + "outputs": [ + { + "type": "BigUint" + } + ] + }, + { + "name": "get_permanent_user_donations", + "mutability": "readonly", + "inputs": [ + { + "name": "wallet_address", + "type": "Address" + } + ], + "outputs": [ + { + "type": "BigUint" + } + ] + }, + { + "name": "get_permanent_user_donations_history", + "mutability": "readonly", + "inputs": [ + { + "name": "wallet_address", + "type": "Address" + } + ], + "outputs": [ + { + "type": "variadic", + "multi_result": true + } + ] + }, + { + "name": "get_lended_user_donations", + "mutability": "readonly", + "inputs": [ + { + "name": "wallet_address", + "type": "Address" + } + ], + "outputs": [ + { + "type": "BigUint" + } + ] + }, + { + "name": "get_lended_user_donations_history", + "mutability": "readonly", + "inputs": [ + { + "name": "wallet_address", + "type": "Address" + } + ], + "outputs": [ + { + "type": "variadic", + "multi_result": true + } + ] + }, + { + "name": "get_unbonding_user_donations", + "mutability": "readonly", + "inputs": [ + { + "name": "wallet_address", + "type": "Address" + } + ], + "outputs": [ + { + "type": "variadic", + "multi_result": true + } + ] + }, + { + "name": "get_waiting_unbonding_requesters", + "mutability": "readonly", + "inputs": [], + "outputs": [ + { + "type": "variadic
", + "multi_result": true + } + ] + }, + { + "name": "get_waiting_unbonding_requests", + "mutability": "readonly", + "inputs": [ + { + "name": "requester", + "type": "Address" + } + ], + "outputs": [ + { + "type": "BigUint" + } + ] + }, + { + "name": "get_initiate_unbonding_pending", + "mutability": "readonly", + "inputs": [ + { + "name": "wallet_address", + "type": "Address" + } + ], + "outputs": [ + { + "type": "BigUint" + } + ] + }, + { + "name": "get_initiate_withdrawal_pending", + "mutability": "readonly", + "inputs": [], + "outputs": [ + { + "type": "bool" + } + ] + }, + { + "name": "get_epoch_last_withdrawal_initiated", + "mutability": "readonly", + "inputs": [], + "outputs": [ + { + "type": "u64" + } + ] + }, + { + "name": "get_claimed_rewards_in_sc", + "mutability": "readonly", + "inputs": [], + "outputs": [ + { + "type": "BigUint" + } + ] + }, + { + "name": "get_claimed_rewards_period", + "mutability": "readonly", + "inputs": [], + "outputs": [ + { + "type": "BigUint" + } + ] + }, + { + "name": "get_claimed_rewards_alltime", + "mutability": "readonly", + "inputs": [], + "outputs": [ + { + "type": "BigUint" + } + ] + }, + { + "name": "get_withdrawn_rewards_history", + "mutability": "readonly", + "inputs": [], + "outputs": [ + { + "type": "variadic", + "multi_result": true + } + ] + }, + { + "name": "get_total_donation_points_for", + "mutability": "readonly", + "inputs": [ + { + "name": "donator", + "type": "Address" + } + ], + "outputs": [ + { + "type": "u64" + } + ] + }, + { + "name": "get_unclaimed_donation_points_for", + "mutability": "readonly", + "inputs": [ + { + "name": "donator", + "type": "Address" + } + ], + "outputs": [ + { + "type": "u64" + } + ] + }, + { + "name": "get_perm_donation_tier_list", + "mutability": "readonly", + "inputs": [], + "outputs": [ + { + "type": "variadic", + "multi_result": true + } + ] + }, + { + "name": "get_lended_donation_tier_list", + "mutability": "readonly", + "inputs": [], + "outputs": [ + { + "type": "variadic", + "multi_result": true + } + ] + }, + { + "name": "get_epoch_donation_points_last_updated", + "mutability": "readonly", + "inputs": [ + { + "name": "donator", + "type": "Address" + } + ], + "outputs": [ + { + "type": "u64" + } + ] + }, + { + "name": "get_donation_points", + "mutability": "readonly", + "inputs": [ + { + "name": "donator", + "type": "Address" + } + ], + "outputs": [ + { + "type": "u64" + } + ] + }, + { + "name": "createAuction", + "mutability": "mutable", + "payableInTokens": [ + "*" + ], + "inputs": [ + { + "name": "auction_start", + "type": "u64" + }, + { + "name": "auction_end", + "type": "u64" + }, + { + "name": "minimum_bid", + "type": "BigUint" + } + ], + "outputs": [] + }, + { + "name": "cancelAuction", + "mutability": "mutable", + "inputs": [ + { + "name": "auction_id", + "type": "u64" + } + ], + "outputs": [] + }, + { + "name": "auctionBid", + "mutability": "mutable", + "payableInTokens": [ + "EGLD" + ], + "inputs": [ + { + "name": "auction_id", + "type": "u64" + } + ], + "outputs": [] + }, + { + "name": "auctionClaim", + "mutability": "mutable", + "inputs": [ + { + "name": "auction_id", + "type": "u64" + } + ], + "outputs": [] + }, + { + "name": "get_all_active_auctions", + "mutability": "readonly", + "inputs": [], + "outputs": [ + { + "type": "List" + } + ] + }, + { + "name": "get_highest_auction_id", + "mutability": "readonly", + "inputs": [], + "outputs": [ + { + "type": "u64" + } + ] + }, + { + "name": "get_auctions", + "mutability": "readonly", + "inputs": [ + { + "name": "auction_id", + "type": "u64" + } + ], + "outputs": [ + { + "type": "Auction" + } + ] + }, + { + "name": "get_all_auctions", + "mutability": "readonly", + "inputs": [], + "outputs": [ + { + "type": "variadic", + "multi_result": true + } + ] + }, + { + "name": "get_active_auctions", + "mutability": "readonly", + "inputs": [], + "outputs": [ + { + "type": "variadic", + "multi_result": true + } + ] + }, + { + "name": "get_my_active_auctions", + "mutability": "readonly", + "inputs": [ + { + "name": "creator", + "type": "Address" + } + ], + "outputs": [ + { + "type": "variadic", + "multi_result": true + } + ] + }, + { + "name": "get_highest_bid_auction", + "mutability": "readonly", + "inputs": [ + { + "name": "auction_id", + "type": "u64" + } + ], + "outputs": [ + { + "type": "BigUint" + } + ] + }, + { + "name": "get_highest_bidder_auction", + "mutability": "readonly", + "inputs": [ + { + "name": "auction_id", + "type": "u64" + } + ], + "outputs": [ + { + "type": "Address" + } + ] + }, + { + "name": "get_fee_per_thousand_auction", + "mutability": "readonly", + "inputs": [], + "outputs": [ + { + "type": "u32" + } + ] + } + ], + "events": [ + { + "identifier": "skipped_because_already_withdrawn", + "inputs": [ + { + "name": "from", + "type": "Address", + "indexed": true + }, + { + "name": "epoch", + "type": "u64", + "indexed": true + } + ] + } + ], + "hasCallback": true, + "types": { + "Auction": { + "type": "struct", + "fields": [ + { + "name": "auction_id", + "type": "u64" + }, + { + "name": "creator", + "type": "Address" + }, + { + "name": "token_identifier", + "type": "EgldOrEsdtTokenIdentifier" + }, + { + "name": "nonce", + "type": "u64" + }, + { + "name": "auction_start", + "type": "u64" + }, + { + "name": "auction_end", + "type": "u64" + }, + { + "name": "minimum_bid", + "type": "BigUint" + } + ] + }, + "AuctionItem": { + "type": "struct", + "fields": [ + { + "name": "auction_id", + "type": "u64" + }, + { + "name": "creator", + "type": "Address" + }, + { + "name": "auction_start", + "type": "u64" + }, + { + "name": "auction_end", + "type": "u64" + }, + { + "name": "minimum_bid", + "type": "BigUint" + }, + { + "name": "highest_bid", + "type": "BigUint" + }, + { + "name": "highest_bidder", + "type": "Option
" + }, + { + "name": "nft_data", + "type": "NftData" + } + ] + }, + "DonationData": { + "type": "struct", + "fields": [ + { + "name": "donation", + "type": "BigUint" + }, + { + "name": "timestamp", + "type": "u64" + } + ] + }, + "DonationHistory": { + "type": "struct", + "fields": [ + { + "name": "donation_data", + "type": "DonationData" + }, + { + "name": "donation_type", + "type": "DonationType" + } + ] + }, + "DonationTier": { + "type": "struct", + "fields": [ + { + "name": "minimum_required", + "type": "BigUint" + }, + { + "name": "donatiom_point_multiplier", + "type": "u8" + } + ] + }, + "DonationType": { + "type": "enum", + "variants": [ + { + "name": "Deposit", + "discriminant": 0 + }, + { + "name": "Withdraw", + "discriminant": 1 + }, + { + "name": "ReinvestRewards", + "discriminant": 2 + }, + { + "name": "WithdrawRewards", + "discriminant": 3 + }, + { + "name": "AuctionFee", + "discriminant": 4 + } + ] + }, + "DonationsOverview": { + "type": "struct", + "fields": [ + { + "name": "donators", + "type": "u32" + }, + { + "name": "total_donated", + "type": "BigUint" + }, + { + "name": "open_donations", + "type": "BigUint" + }, + { + "name": "staked_donations", + "type": "BigUint" + }, + { + "name": "rewards_alltime", + "type": "BigUint" + }, + { + "name": "rewards_period", + "type": "BigUint" + }, + { + "name": "rewards_pending", + "type": "BigUint" + }, + { + "name": "rewards_withdraw_history", + "type": "List" + } + ] + }, + "NftData": { + "type": "struct", + "fields": [ + { + "name": "name", + "type": "bytes" + }, + { + "name": "collection", + "type": "TokenIdentifier" + }, + { + "name": "nonce", + "type": "u64" + }, + { + "name": "royalties", + "type": "BigUint" + }, + { + "name": "uris", + "type": "List" + } + ] + }, + "RewardsHistory": { + "type": "struct", + "fields": [ + { + "name": "amount", + "type": "BigUint" + }, + { + "name": "withdrawn_timestamp", + "type": "u64" + } + ] + }, + "UnbondingData": { + "type": "struct", + "fields": [ + { + "name": "amount", + "type": "BigUint" + }, + { + "name": "epoch", + "type": "u64" + } + ] + }, + "UserDonation": { + "type": "struct", + "fields": [ + { + "name": "donator", + "type": "Address" + }, + { + "name": "donations_permanent", + "type": "BigUint" + }, + { + "name": "donations_lended", + "type": "BigUint" + }, + { + "name": "withdrawal_requests", + "type": "List" + } + ] + }, + "UserOverview": { + "type": "struct", + "fields": [ + { + "name": "my_donations", + "type": "UserDonation" + }, + { + "name": "my_donation_points", + "type": "u64" + } + ] + } + } +} diff --git a/src/transaction.spec.ts b/src/transaction.spec.ts index 4b1fad9b..f6f2f9be 100644 --- a/src/transaction.spec.ts +++ b/src/transaction.spec.ts @@ -1,10 +1,10 @@ -import { assert } from "chai"; import BigNumber from "bignumber.js"; -import { Transaction } from "./transaction"; +import { assert } from "chai"; import { TransactionOptions, TransactionVersion } from "./networkParams"; -import { TransactionPayload } from "./transactionPayload"; import { loadTestWallets, TestWallet } from "./testutils"; import { TokenPayment } from "./tokenPayment"; +import { Transaction } from "./transaction"; +import { TransactionPayload } from "./transactionPayload"; describe("test transaction construction", async () => { @@ -187,8 +187,8 @@ describe("test transaction construction", async () => { const sender = wallets.alice.address; const transaction = new Transaction({ nonce: 90, - value: new BigNumber("1000000000000000000"), - sender: wallets.alice.address, + value: "123456789000000000000000000000", + sender: sender, receiver: wallets.bob.address, gasPrice: minGasPrice, gasLimit: 80000, @@ -200,4 +200,36 @@ describe("test transaction construction", async () => { const restoredTransaction = Transaction.fromPlainObject(plainObject); assert.deepEqual(restoredTransaction, transaction); }); + + it("should handle large values", () => { + const tx1 = new Transaction({ + value: "123456789000000000000000000000", + sender: wallets.alice.address, + receiver: wallets.bob.address, + gasLimit: 50000, + chainID: "local-testnet" + }); + assert.equal(tx1.getValue().toString(), "123456789000000000000000000000"); + + const tx2 = new Transaction({ + value: TokenPayment.egldFromBigInteger("123456789000000000000000000000"), + sender: wallets.alice.address, + receiver: wallets.bob.address, + gasLimit: 50000, + chainID: "local-testnet" + }); + assert.equal(tx2.getValue().toString(), "123456789000000000000000000000"); + + const tx3 = new Transaction({ + // Passing a BigNumber is not recommended. + // However, ITransactionValue interface is permissive, and developers may mistakenly pass such objects as values. + // TokenPayment objects or simple strings (see above) are preferred, instead. + value: new BigNumber("123456789000000000000000000000"), + sender: wallets.alice.address, + receiver: wallets.bob.address, + gasLimit: 50000, + chainID: "local-testnet" + }); + assert.equal(tx3.getValue().toString(), "123456789000000000000000000000"); + }); }); diff --git a/src/transaction.ts b/src/transaction.ts index 9aded117..af639a1e 100644 --- a/src/transaction.ts +++ b/src/transaction.ts @@ -108,7 +108,7 @@ export class Transaction { options?: TransactionOptions; }) { this.nonce = nonce || 0; - this.value = value || 0; + this.value = value ? new BigNumber(value.toString()).toFixed(0) : 0; this.sender = sender; this.receiver = receiver; this.gasPrice = gasPrice || TRANSACTION_MIN_GAS_PRICE; @@ -242,7 +242,7 @@ export class Transaction { static fromPlainObject(plainObjectTransaction: IPlainTransactionObject): Transaction { const tx = new Transaction({ nonce: Number(plainObjectTransaction.nonce), - value: new BigNumber(plainObjectTransaction.value), + value: new BigNumber(plainObjectTransaction.value).toFixed(0), receiver: Address.fromString(plainObjectTransaction.receiver), sender: Address.fromString(plainObjectTransaction.sender), gasPrice: Number(plainObjectTransaction.gasPrice),