diff --git a/package-lock.json b/package-lock.json index 8d649008..0e2e9cad 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@multiversx/sdk-core", - "version": "v13.0.0-alpha.2", + "version": "12.18.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@multiversx/sdk-core", - "version": "v13.0.0-alpha.2", + "version": "12.18.0", "license": "MIT", "dependencies": { "@multiversx/sdk-transaction-decoder": "1.0.2", @@ -27,7 +27,7 @@ "@types/mocha": "9.1.0", "@types/node": "13.13.2", "assert": "2.0.0", - "axios": "1.6.2", + "axios": "1.6.5", "browserify": "17.0.0", "chai": "4.2.0", "esmify": "2.1.1", @@ -808,12 +808,12 @@ } }, "node_modules/axios": { - "version": "1.6.2", - "resolved": "https://registry.npmjs.org/axios/-/axios-1.6.2.tgz", - "integrity": "sha512-7i24Ri4pmDRfJTR7LDBhsOTtcm+9kjX5WiY1X3wIisx6G9So3pfMkEiU7emUBe46oceVImccTEM3k6C5dbVW8A==", + "version": "1.6.5", + "resolved": "https://registry.npmjs.org/axios/-/axios-1.6.5.tgz", + "integrity": "sha512-Ii012v05KEVuUoFWmMW/UQv9aRIc3ZwkWDcM+h5Il8izZCtRVpDUfwpoFf7eOtajT3QiGR4yDUx7lPqHJULgbg==", "dev": true, "dependencies": { - "follow-redirects": "^1.15.0", + "follow-redirects": "^1.15.4", "form-data": "^4.0.0", "proxy-from-env": "^1.1.0" } @@ -2072,9 +2072,9 @@ } }, "node_modules/follow-redirects": { - "version": "1.15.3", - "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.3.tgz", - "integrity": "sha512-1VzOtuEM8pC9SFU1E+8KfTjZyMztRsgEfwQl44z8A25uy13jSzTj6dyK2Df52iV0vgHCfBwLhDWevLn95w5v6Q==", + "version": "1.15.4", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.4.tgz", + "integrity": "sha512-Cr4D/5wlrb0z9dgERpUL3LrmPKVDsETIJhaCMeDfuFYcqa5bldGV6wBsAN6X/vxlXQtFBMrXdXxdL8CbDTGniw==", "dev": true, "funding": [ { @@ -5060,12 +5060,12 @@ "dev": true }, "axios": { - "version": "1.6.2", - "resolved": "https://registry.npmjs.org/axios/-/axios-1.6.2.tgz", - "integrity": "sha512-7i24Ri4pmDRfJTR7LDBhsOTtcm+9kjX5WiY1X3wIisx6G9So3pfMkEiU7emUBe46oceVImccTEM3k6C5dbVW8A==", + "version": "1.6.5", + "resolved": "https://registry.npmjs.org/axios/-/axios-1.6.5.tgz", + "integrity": "sha512-Ii012v05KEVuUoFWmMW/UQv9aRIc3ZwkWDcM+h5Il8izZCtRVpDUfwpoFf7eOtajT3QiGR4yDUx7lPqHJULgbg==", "dev": true, "requires": { - "follow-redirects": "^1.15.0", + "follow-redirects": "^1.15.4", "form-data": "^4.0.0", "proxy-from-env": "^1.1.0" } @@ -6123,9 +6123,9 @@ "dev": true }, "follow-redirects": { - "version": "1.15.3", - "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.3.tgz", - "integrity": "sha512-1VzOtuEM8pC9SFU1E+8KfTjZyMztRsgEfwQl44z8A25uy13jSzTj6dyK2Df52iV0vgHCfBwLhDWevLn95w5v6Q==", + "version": "1.15.4", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.4.tgz", + "integrity": "sha512-Cr4D/5wlrb0z9dgERpUL3LrmPKVDsETIJhaCMeDfuFYcqa5bldGV6wBsAN6X/vxlXQtFBMrXdXxdL8CbDTGniw==", "dev": true }, "for-each": { diff --git a/package.json b/package.json index 5512cd1a..15109992 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@multiversx/sdk-core", - "version": "v13.0.0-alpha.2", + "version": "12.18.0", "description": "MultiversX SDK for JavaScript and TypeScript", "main": "out/index.js", "types": "out/index.d.js", @@ -43,7 +43,7 @@ "@types/mocha": "9.1.0", "@types/node": "13.13.2", "assert": "2.0.0", - "axios": "1.6.2", + "axios": "1.6.5", "browserify": "17.0.0", "chai": "4.2.0", "esmify": "2.1.1", diff --git a/src/address.spec.ts b/src/address.spec.ts index 26590d28..ca731f8e 100644 --- a/src/address.spec.ts +++ b/src/address.spec.ts @@ -44,4 +44,10 @@ describe("test address", () => { assert.throw(() => new Address("erd1l453hd0gt5gzdp7czpuall8ggt2dcv5zwmfdf3sd3lguxseux2"), errors.ErrAddressCannotCreate); assert.throw(() => new Address("xerd1l453hd0gt5gzdp7czpuall8ggt2dcv5zwmfdf3sd3lguxseux2fsmsgldz"), errors.ErrAddressCannotCreate); }); + + it("should validate the address without throwing the error", () => { + assert.isTrue(Address.isValid(aliceBech32)); + assert.isFalse(Address.isValid('xerd1l453hd0gt5gzdp7czpuall8ggt2dcv5zwmfdf3sd3lguxseux2fsmsgldz')); + assert.isFalse(Address.isValid('erd1l453hd0gt5gzdp7czpuall8ggt2dcv5zwmfdf3sd3lguxseux2')) + }) }); diff --git a/src/address.ts b/src/address.ts index 1931d624..48bfdbaa 100644 --- a/src/address.ts +++ b/src/address.ts @@ -122,6 +122,23 @@ export class Address { return Address.fromValidHex(pubkey.toString("hex")); } + /** + * Performs address validation without throwing errors + */ + static isValid(value: string): boolean { + const decoded = bech32.decodeUnsafe(value); + const prefix = decoded?.prefix; + const pubkey = decoded + ? Buffer.from(bech32.fromWords(decoded.words)) + : undefined; + + if (prefix !== HRP || pubkey?.length !== PUBKEY_LENGTH) { + return false; + } + + return true; + } + /** * Returns the hex representation of the address (pubkey) */ diff --git a/src/smartcontracts/typesystem/abiRegistry.spec.ts b/src/smartcontracts/typesystem/abiRegistry.spec.ts index 55910ace..3d151e1d 100644 --- a/src/smartcontracts/typesystem/abiRegistry.spec.ts +++ b/src/smartcontracts/typesystem/abiRegistry.spec.ts @@ -156,4 +156,16 @@ describe("test abi registry", () => { assert.deepEqual(setStatusEvent.inputs[1].type, new U64Type()); assert.deepEqual(setStatusEvent.inputs[2].type, registry.getCustomType("TransactionStatus")); }); + + it("should load ABI explicit-enum", async () => { + const registry = await loadAbiRegistry("src/testdata/explicit-enum.abi.json"); + + const enumType = registry.getEnum("OperationCompletionStatus"); + + assert.deepEqual(enumType.variants[0].name, "completed"); + assert.deepEqual(enumType.variants[0].discriminant, 0); + + assert.deepEqual(enumType.variants[1].name, "interrupted"); + assert.deepEqual(enumType.variants[1].discriminant, 1); + }); }); diff --git a/src/smartcontracts/typesystem/abiRegistry.ts b/src/smartcontracts/typesystem/abiRegistry.ts index 41976255..27c300ee 100644 --- a/src/smartcontracts/typesystem/abiRegistry.ts +++ b/src/smartcontracts/typesystem/abiRegistry.ts @@ -53,7 +53,7 @@ export class AbiRegistry { if (typeDefinition.type == "struct") { customTypes.push(StructType.fromJSON({ name: customTypeName, fields: typeDefinition.fields })); - } else if (typeDefinition.type == "enum") { + } else if (typeDefinition.type == "enum" || typeDefinition.type == "explicit-enum") { customTypes.push(EnumType.fromJSON({ name: customTypeName, variants: typeDefinition.variants })); } else { throw new errors.ErrTypingSystem(`Cannot handle custom type: ${customTypeName}`); diff --git a/src/smartcontracts/typesystem/enum.ts b/src/smartcontracts/typesystem/enum.ts index 7ae08a1f..1320a4b4 100644 --- a/src/smartcontracts/typesystem/enum.ts +++ b/src/smartcontracts/typesystem/enum.ts @@ -19,10 +19,27 @@ export class EnumType extends CustomType { } static fromJSON(json: { name: string; variants: any[] }): EnumType { - let variants = (json.variants || []).map((variant) => EnumVariantDefinition.fromJSON(variant)); + const rawVariants = EnumType.assignMissingDiscriminants(json.variants || []); + const variants = rawVariants.map((variant) => EnumVariantDefinition.fromJSON(variant)); return new EnumType(json.name, variants); } + // For some enums (e.g. some "explicit-enum" types), the discriminants are missing. + private static assignMissingDiscriminants(variants: any[]): any[] { + const allDiscriminantsAreMissing = variants.every((variant) => variant.discriminant == undefined); + if (!allDiscriminantsAreMissing) { + // We only assign discriminants if all of them are missing. + return variants; + } + + return variants.map((variant, index) => { + return { + ...variant, + discriminant: index + } + }); + } + getVariantByDiscriminant(discriminant: number): EnumVariantDefinition { let result = this.variants.find((e) => e.discriminant == discriminant); guardValueIsSet(`variant by discriminant (${discriminant})`, result); diff --git a/src/testdata/explicit-enum.abi.json b/src/testdata/explicit-enum.abi.json new file mode 100644 index 00000000..7aab9202 --- /dev/null +++ b/src/testdata/explicit-enum.abi.json @@ -0,0 +1,32 @@ +{ + "endpoints": [ + { + "name": "foobar", + "inputs": [], + "outputs": [ + { + "type": "OperationCompletionStatus" + } + ] + } + ], + "types": { + "OperationCompletionStatus": { + "type": "explicit-enum", + "variants": [ + { + "docs": [ + "indicates that operation was completed" + ], + "name": "completed" + }, + { + "docs": [ + "indicates that operation was interrupted prematurely, due to low gas" + ], + "name": "interrupted" + } + ] + } + } +} diff --git a/src/tokenOperations/tokenOperationsFactory.spec.ts b/src/tokenOperations/tokenOperationsFactory.spec.ts index f7257b6d..44c39761 100644 --- a/src/tokenOperations/tokenOperationsFactory.spec.ts +++ b/src/tokenOperations/tokenOperationsFactory.spec.ts @@ -39,12 +39,12 @@ describe("test factory", () => { canWipe: true, canPause: true, canChangeOwner: true, - canUpgrade: true, - canAddSpecialRoles: true, + canUpgrade: false, + canAddSpecialRoles: false, transactionNonce: 42 }); - assert.equal(transaction.getData().toString(), "issue@4652414e4b@4652414e4b@64@@63616e467265657a65@74727565@63616e57697065@74727565@63616e5061757365@74727565@63616e4368616e67654f776e6572@74727565@63616e55706772616465@74727565@63616e4164645370656369616c526f6c6573@74727565") + assert.equal(transaction.getData().toString(), "issue@4652414e4b@4652414e4b@64@@63616e467265657a65@74727565@63616e57697065@74727565@63616e5061757365@74727565@63616e4368616e67654f776e6572@74727565@63616e55706772616465@66616c7365@63616e4164645370656369616c526f6c6573@66616c7365") assert.equal(transaction.getNonce(), 42); assert.equal(transaction.getSender().toString(), frank.address.toString()); assert.equal(transaction.getReceiver().toString(), "erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqzllls8a5w6u"); @@ -60,12 +60,12 @@ describe("test factory", () => { canPause: true, canTransferNFTCreateRole: true, canChangeOwner: true, - canUpgrade: true, - canAddSpecialRoles: true, + canUpgrade: false, + canAddSpecialRoles: false, transactionNonce: 42 }); - assert.equal(transaction.getData().toString(), "issueSemiFungible@4652414e4b@4652414e4b@63616e467265657a65@74727565@63616e57697065@74727565@63616e5061757365@74727565@63616e5472616e736665724e4654437265617465526f6c65@74727565@63616e4368616e67654f776e6572@74727565@63616e55706772616465@74727565@63616e4164645370656369616c526f6c6573@74727565") + assert.equal(transaction.getData().toString(), "issueSemiFungible@4652414e4b@4652414e4b@63616e467265657a65@74727565@63616e57697065@74727565@63616e5061757365@74727565@63616e5472616e736665724e4654437265617465526f6c65@74727565@63616e4368616e67654f776e6572@74727565@63616e55706772616465@66616c7365@63616e4164645370656369616c526f6c6573@66616c7365") assert.equal(transaction.getNonce(), 42); assert.equal(transaction.getSender().toString(), frank.address.toString()); assert.equal(transaction.getReceiver().toString(), "erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqzllls8a5w6u"); @@ -81,12 +81,12 @@ describe("test factory", () => { canPause: true, canTransferNFTCreateRole: true, canChangeOwner: true, - canUpgrade: true, - canAddSpecialRoles: true, + canUpgrade: false, + canAddSpecialRoles: false, transactionNonce: 42 }); - assert.equal(transaction.getData().toString(), "issueNonFungible@4652414e4b@4652414e4b@63616e467265657a65@74727565@63616e57697065@74727565@63616e5061757365@74727565@63616e5472616e736665724e4654437265617465526f6c65@74727565@63616e4368616e67654f776e6572@74727565@63616e55706772616465@74727565@63616e4164645370656369616c526f6c6573@74727565") + assert.equal(transaction.getData().toString(), "issueNonFungible@4652414e4b@4652414e4b@63616e467265657a65@74727565@63616e57697065@74727565@63616e5061757365@74727565@63616e5472616e736665724e4654437265617465526f6c65@74727565@63616e4368616e67654f776e6572@74727565@63616e55706772616465@66616c7365@63616e4164645370656369616c526f6c6573@66616c7365") assert.equal(transaction.getNonce(), 42); assert.equal(transaction.getSender().toString(), frank.address.toString()); assert.equal(transaction.getReceiver().toString(), "erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqzllls8a5w6u"); @@ -103,12 +103,12 @@ describe("test factory", () => { canPause: true, canTransferNFTCreateRole: true, canChangeOwner: true, - canUpgrade: true, - canAddSpecialRoles: true, + canUpgrade: false, + canAddSpecialRoles: false, transactionNonce: 42 }); - assert.equal(transaction.getData().toString(), "registerMetaESDT@4652414e4b@4652414e4b@0a@63616e467265657a65@74727565@63616e57697065@74727565@63616e5061757365@74727565@63616e5472616e736665724e4654437265617465526f6c65@74727565@63616e4368616e67654f776e6572@74727565@63616e55706772616465@74727565@63616e4164645370656369616c526f6c6573@74727565") + assert.equal(transaction.getData().toString(), "registerMetaESDT@4652414e4b@4652414e4b@0a@63616e467265657a65@74727565@63616e57697065@74727565@63616e5061757365@74727565@63616e5472616e736665724e4654437265617465526f6c65@74727565@63616e4368616e67654f776e6572@74727565@63616e55706772616465@66616c7365@63616e4164645370656369616c526f6c6573@66616c7365") assert.equal(transaction.getNonce(), 42); assert.equal(transaction.getSender().toString(), frank.address.toString()); assert.equal(transaction.getReceiver().toString(), "erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqzllls8a5w6u"); diff --git a/src/tokenOperations/tokenOperationsFactory.test.net.spec.ts b/src/tokenOperations/tokenOperationsFactory.test.net.spec.ts index 3e582344..b93095b8 100644 --- a/src/tokenOperations/tokenOperationsFactory.test.net.spec.ts +++ b/src/tokenOperations/tokenOperationsFactory.test.net.spec.ts @@ -43,7 +43,7 @@ describe("test factory on testnet", function () { tokenTicker: "TEST", tokenType: "FNG", numDecimals: 2, - transactionNonce: frank.account.nonce + transactionNonce: frank.account.nonce, }); const tx1OnNetwork = await processTransaction(frank, tx1, "tx1"); @@ -64,7 +64,7 @@ describe("test factory on testnet", function () { tokenTicker: "TEST", tokenType: "NFT", numDecimals: 0, - transactionNonce: frank.account.nonce + transactionNonce: frank.account.nonce, }); const tx1OnNetwork = await processTransaction(frank, tx1, "tx1"); @@ -87,7 +87,7 @@ describe("test factory on testnet", function () { tokenTicker: "TEST", tokenType: "SFT", numDecimals: 0, - transactionNonce: frank.account.nonce + transactionNonce: frank.account.nonce, }); const tx1OnNetwork = await processTransaction(frank, tx1, "tx1"); @@ -109,7 +109,7 @@ describe("test factory on testnet", function () { tokenTicker: "TEST", tokenType: "META", numDecimals: 2, - transactionNonce: frank.account.nonce + transactionNonce: frank.account.nonce, }); const tx1OnNetwork = await processTransaction(frank, tx1, "tx1"); @@ -138,7 +138,7 @@ describe("test factory on testnet", function () { canChangeOwner: true, canUpgrade: true, canAddSpecialRoles: true, - transactionNonce: frank.account.nonce + transactionNonce: frank.account.nonce, }); const tx1OnNetwork = await processTransaction(frank, tx1, "tx1"); @@ -150,7 +150,7 @@ describe("test factory on testnet", function () { const tx2 = factory.unsetBurnRoleGlobally({ manager: frank.address, tokenIdentifier: tx1Outcome.tokenIdentifier, - transactionNonce: frank.account.nonce + transactionNonce: frank.account.nonce, }); const tx2OnNetwork = await processTransaction(frank, tx2, "tx2"); @@ -160,7 +160,7 @@ describe("test factory on testnet", function () { const tx3 = factory.setBurnRoleGlobally({ manager: frank.address, tokenIdentifier: tx1Outcome.tokenIdentifier, - transactionNonce: frank.account.nonce + transactionNonce: frank.account.nonce, }); const tx3OnNetwork = await processTransaction(frank, tx3, "tx3"); @@ -185,7 +185,7 @@ describe("test factory on testnet", function () { canChangeOwner: true, canUpgrade: true, canAddSpecialRoles: true, - transactionNonce: frank.account.nonce + transactionNonce: frank.account.nonce, }); const tx1OnNetwork = await processTransaction(frank, tx1, "tx1"); @@ -200,7 +200,7 @@ describe("test factory on testnet", function () { tokenIdentifier: tx1Outcome.tokenIdentifier, addRoleLocalMint: true, addRoleLocalBurn: true, - transactionNonce: frank.account.nonce + transactionNonce: frank.account.nonce, }); const tx2OnNetwork = await processTransaction(frank, tx2, "tx2"); @@ -214,7 +214,7 @@ describe("test factory on testnet", function () { user: grace.address, tokenIdentifier: tokenIdentifier, supplyToMint: 200, - transactionNonce: grace.account.nonce + transactionNonce: grace.account.nonce, }); const tx3OnNetwork = await processTransaction(grace, tx3, "tx3"); @@ -227,7 +227,7 @@ describe("test factory on testnet", function () { user: grace.address, tokenIdentifier: tokenIdentifier, supplyToBurn: 50, - transactionNonce: grace.account.nonce + transactionNonce: grace.account.nonce, }); const tx4OnNetwork = await processTransaction(grace, tx4, "tx4"); @@ -252,7 +252,7 @@ describe("test factory on testnet", function () { canChangeOwner: true, canUpgrade: true, canAddSpecialRoles: true, - transactionNonce: frank.account.nonce + transactionNonce: frank.account.nonce, }); const tx1OnNetwork = await processTransaction(frank, tx1, "tx1"); @@ -264,7 +264,7 @@ describe("test factory on testnet", function () { const tx2 = factory.pause({ manager: frank.address, tokenIdentifier: tokenIdentifier, - transactionNonce: frank.account.nonce + transactionNonce: frank.account.nonce, }); const tx2OnNetwork = await processTransaction(frank, tx2, "tx2"); @@ -274,7 +274,7 @@ describe("test factory on testnet", function () { const tx3 = factory.unpause({ manager: frank.address, tokenIdentifier: tokenIdentifier, - transactionNonce: frank.account.nonce + transactionNonce: frank.account.nonce, }); const tx3OnNetwork = await processTransaction(frank, tx3, "tx3"); @@ -286,7 +286,7 @@ describe("test factory on testnet", function () { sender: frank.account.address, receiver: grace.account.address, chainID: network.ChainID, - nonce: frank.account.nonce + nonce: frank.account.nonce, }); const _tx4OnNetwork = await processTransaction(frank, tx4, "tx4"); @@ -309,7 +309,7 @@ describe("test factory on testnet", function () { canChangeOwner: true, canUpgrade: true, canAddSpecialRoles: true, - transactionNonce: frank.account.nonce + transactionNonce: frank.account.nonce, }); const tx1OnNetwork = await processTransaction(frank, tx1, "tx1"); @@ -323,7 +323,7 @@ describe("test factory on testnet", function () { sender: frank.account.address, receiver: grace.account.address, chainID: network.ChainID, - nonce: frank.account.nonce + nonce: frank.account.nonce, }); const _tx2OnNetwork = await processTransaction(frank, tx2, "tx2"); @@ -333,7 +333,7 @@ describe("test factory on testnet", function () { manager: frank.address, user: grace.address, tokenIdentifier: tokenIdentifier, - transactionNonce: frank.account.nonce + transactionNonce: frank.account.nonce, }); const tx3OnNetwork = await processTransaction(frank, tx3, "tx3"); @@ -348,7 +348,7 @@ describe("test factory on testnet", function () { manager: frank.address, user: grace.address, tokenIdentifier: tokenIdentifier, - transactionNonce: frank.account.nonce + transactionNonce: frank.account.nonce, }); const tx4OnNetwork = await processTransaction(frank, tx4, "tx4"); @@ -376,7 +376,7 @@ describe("test factory on testnet", function () { canChangeOwner: true, canUpgrade: true, canAddSpecialRoles: true, - transactionNonce: frank.account.nonce + transactionNonce: frank.account.nonce, }); const tx1OnNetwork = await processTransaction(frank, tx1, "tx1"); @@ -390,7 +390,7 @@ describe("test factory on testnet", function () { sender: frank.account.address, receiver: grace.account.address, chainID: network.ChainID, - nonce: frank.account.nonce + nonce: frank.account.nonce, }); const _tx2OnNetwork = await processTransaction(frank, tx2, "tx2"); @@ -400,7 +400,7 @@ describe("test factory on testnet", function () { manager: frank.address, user: grace.address, tokenIdentifier: tokenIdentifier, - transactionNonce: frank.account.nonce + transactionNonce: frank.account.nonce, }); const tx3OnNetwork = await processTransaction(frank, tx3, "tx3"); @@ -415,7 +415,7 @@ describe("test factory on testnet", function () { manager: frank.address, user: grace.address, tokenIdentifier: tokenIdentifier, - transactionNonce: frank.account.nonce + transactionNonce: frank.account.nonce, }); const tx4OnNetwork = await processTransaction(frank, tx4, "tx4"); @@ -443,7 +443,7 @@ describe("test factory on testnet", function () { canChangeOwner: true, canUpgrade: true, canAddSpecialRoles: true, - transactionNonce: frank.account.nonce + transactionNonce: frank.account.nonce, }); const tx1OnNetwork = await processTransaction(frank, tx1, "tx1"); @@ -461,7 +461,7 @@ describe("test factory on testnet", function () { addRoleNFTUpdateAttributes: true, addRoleNFTAddURI: true, addRoleESDTTransferRole: false, - transactionNonce: frank.account.nonce + transactionNonce: frank.account.nonce, }); const tx2OnNetwork = await processTransaction(frank, tx2, "tx2"); @@ -481,7 +481,7 @@ describe("test factory on testnet", function () { hash: "abba", attributes: Buffer.from("test"), uris: ["a", "b"], - transactionNonce: grace.account.nonce + transactionNonce: grace.account.nonce, }); const txCreateOnNetwork = await processTransaction(grace, txCreate, "txCreate"); @@ -526,7 +526,7 @@ describe("test factory on testnet", function () { canChangeOwner: true, canUpgrade: true, canAddSpecialRoles: true, - transactionNonce: frank.account.nonce + transactionNonce: frank.account.nonce, }); const tx1OnNetwork = await processTransaction(frank, tx1, "tx1"); @@ -543,7 +543,7 @@ describe("test factory on testnet", function () { addRoleNFTBurn: false, addRoleNFTAddQuantity: true, addRoleESDTTransferRole: false, - transactionNonce: frank.account.nonce + transactionNonce: frank.account.nonce, }); const tx2OnNetwork = await processTransaction(frank, tx2, "tx2"); @@ -562,7 +562,7 @@ describe("test factory on testnet", function () { hash: "abba", attributes: Buffer.from("test"), uris: ["a", "b"], - transactionNonce: grace.account.nonce + transactionNonce: grace.account.nonce, }); const txCreateOnNetwork = await processTransaction(grace, txCreate, "txCreate"); @@ -578,7 +578,7 @@ describe("test factory on testnet", function () { tokenIdentifier: txCreateOutcome.tokenIdentifier, tokenNonce: txCreateOutcome.nonce, quantityToAdd: "3", - transactionNonce: grace.account.nonce + transactionNonce: grace.account.nonce, }); const txAddQuantityOnNetwork = await processTransaction(grace, txAddQuantity, "txAddQuantity"); @@ -594,7 +594,7 @@ describe("test factory on testnet", function () { tokenIdentifier: txCreateOutcome.tokenIdentifier, tokenNonce: txCreateOutcome.nonce, quantityToBurn: "2", - transactionNonce: grace.account.nonce + transactionNonce: grace.account.nonce, }); const txBurnQuantityOnNetwork = await processTransaction(grace, txBurnQuantity, "txBurnQuantity"); @@ -624,7 +624,7 @@ describe("test factory on testnet", function () { canChangeOwner: true, canUpgrade: true, canAddSpecialRoles: true, - transactionNonce: frank.account.nonce + transactionNonce: frank.account.nonce, }); const tx1OnNetwork = await processTransaction(frank, tx1, "tx1"); @@ -641,7 +641,7 @@ describe("test factory on testnet", function () { addRoleNFTBurn: false, addRoleNFTAddQuantity: true, addRoleESDTTransferRole: false, - transactionNonce: frank.account.nonce + transactionNonce: frank.account.nonce, }); const tx2OnNetwork = await processTransaction(frank, tx2, "tx2"); @@ -660,7 +660,7 @@ describe("test factory on testnet", function () { hash: "abba", attributes: Buffer.from("test"), uris: ["a", "b"], - transactionNonce: grace.account.nonce + transactionNonce: grace.account.nonce, }); const txOnNetwork = await processTransaction(grace, tx, "tx"); @@ -672,7 +672,11 @@ describe("test factory on testnet", function () { } }); - async function processTransaction(wallet: TestWallet, transaction: Transaction, tag: string): Promise { + async function processTransaction( + wallet: TestWallet, + transaction: Transaction, + tag: string + ): Promise { wallet.account.incrementNonce(); transaction.applySignature(await wallet.signer.sign(transaction.serializeForSigning())); await provider.sendTransaction(transaction); diff --git a/src/tokenOperations/tokenOperationsFactory.ts b/src/tokenOperations/tokenOperationsFactory.ts index 3d93592d..a55371b9 100644 --- a/src/tokenOperations/tokenOperationsFactory.ts +++ b/src/tokenOperations/tokenOperationsFactory.ts @@ -63,7 +63,7 @@ interface IIssueSemiFungibleArgs extends IBaseArgs { canAddSpecialRoles: boolean; } -interface IIssueNonFungibleArgs extends IIssueSemiFungibleArgs {} +interface IIssueNonFungibleArgs extends IIssueSemiFungibleArgs { } interface IRegisterMetaESDT extends IIssueSemiFungibleArgs { numDecimals: number; @@ -182,10 +182,12 @@ interface IBurnQuantityArgs extends IBaseArgs { export class TokenOperationsFactory { private readonly config: IConfig; private readonly trueAsHex; + private readonly falseAsHex; constructor(config: IConfig) { this.config = config; this.trueAsHex = utf8ToHex("true"); + this.falseAsHex = utf8ToHex("false"); } issueFungible(args: IIssueFungibleArgs): Transaction { @@ -197,12 +199,18 @@ export class TokenOperationsFactory { utf8ToHex(args.tokenTicker), bigIntToHex(args.initialSupply), bigIntToHex(args.numDecimals), - ...(args.canFreeze ? [utf8ToHex("canFreeze"), this.trueAsHex] : []), - ...(args.canWipe ? [utf8ToHex("canWipe"), this.trueAsHex] : []), - ...(args.canPause ? [utf8ToHex("canPause"), this.trueAsHex] : []), - ...(args.canChangeOwner ? [utf8ToHex("canChangeOwner"), this.trueAsHex] : []), - ...(args.canUpgrade ? [utf8ToHex("canUpgrade"), this.trueAsHex] : []), - ...(args.canAddSpecialRoles ? [utf8ToHex("canAddSpecialRoles"), this.trueAsHex] : []), + utf8ToHex("canFreeze"), + args.canFreeze ? this.trueAsHex : this.falseAsHex, + utf8ToHex("canWipe"), + args.canWipe ? this.trueAsHex : this.falseAsHex, + utf8ToHex("canPause"), + args.canPause ? this.trueAsHex : this.falseAsHex, + utf8ToHex("canChangeOwner"), + args.canChangeOwner ? this.trueAsHex : this.falseAsHex, + utf8ToHex("canUpgrade"), + args.canUpgrade ? this.trueAsHex : this.falseAsHex, + utf8ToHex("canAddSpecialRoles"), + args.canAddSpecialRoles ? this.trueAsHex : this.falseAsHex ]; return this.createTransaction({ @@ -233,13 +241,20 @@ Once the token is registered, you can unset this role by calling "unsetBurnRoleG "issueSemiFungible", utf8ToHex(args.tokenName), utf8ToHex(args.tokenTicker), - ...(args.canFreeze ? [utf8ToHex("canFreeze"), this.trueAsHex] : []), - ...(args.canWipe ? [utf8ToHex("canWipe"), this.trueAsHex] : []), - ...(args.canPause ? [utf8ToHex("canPause"), this.trueAsHex] : []), - ...(args.canTransferNFTCreateRole ? [utf8ToHex("canTransferNFTCreateRole"), this.trueAsHex] : []), - ...(args.canChangeOwner ? [utf8ToHex("canChangeOwner"), this.trueAsHex] : []), - ...(args.canUpgrade ? [utf8ToHex("canUpgrade"), this.trueAsHex] : []), - ...(args.canAddSpecialRoles ? [utf8ToHex("canAddSpecialRoles"), this.trueAsHex] : []), + utf8ToHex("canFreeze"), + args.canFreeze ? this.trueAsHex : this.falseAsHex, + utf8ToHex("canWipe"), + args.canWipe ? this.trueAsHex : this.falseAsHex, + utf8ToHex("canPause"), + args.canPause ? this.trueAsHex : this.falseAsHex, + utf8ToHex("canTransferNFTCreateRole"), + args.canTransferNFTCreateRole ? this.trueAsHex : this.falseAsHex, + utf8ToHex("canChangeOwner"), + args.canChangeOwner ? this.trueAsHex : this.falseAsHex, + utf8ToHex("canUpgrade"), + args.canUpgrade ? this.trueAsHex : this.falseAsHex, + utf8ToHex("canAddSpecialRoles"), + args.canAddSpecialRoles ? this.trueAsHex : this.falseAsHex ]; return this.createTransaction({ @@ -261,13 +276,20 @@ Once the token is registered, you can unset this role by calling "unsetBurnRoleG "issueNonFungible", utf8ToHex(args.tokenName), utf8ToHex(args.tokenTicker), - ...(args.canFreeze ? [utf8ToHex("canFreeze"), this.trueAsHex] : []), - ...(args.canWipe ? [utf8ToHex("canWipe"), this.trueAsHex] : []), - ...(args.canPause ? [utf8ToHex("canPause"), this.trueAsHex] : []), - ...(args.canTransferNFTCreateRole ? [utf8ToHex("canTransferNFTCreateRole"), this.trueAsHex] : []), - ...(args.canChangeOwner ? [utf8ToHex("canChangeOwner"), this.trueAsHex] : []), - ...(args.canUpgrade ? [utf8ToHex("canUpgrade"), this.trueAsHex] : []), - ...(args.canAddSpecialRoles ? [utf8ToHex("canAddSpecialRoles"), this.trueAsHex] : []), + utf8ToHex("canFreeze"), + args.canFreeze ? this.trueAsHex : this.falseAsHex, + utf8ToHex("canWipe"), + args.canWipe ? this.trueAsHex : this.falseAsHex, + utf8ToHex("canPause"), + args.canPause ? this.trueAsHex : this.falseAsHex, + utf8ToHex("canTransferNFTCreateRole"), + args.canTransferNFTCreateRole ? this.trueAsHex : this.falseAsHex, + utf8ToHex("canChangeOwner"), + args.canChangeOwner ? this.trueAsHex : this.falseAsHex, + utf8ToHex("canUpgrade"), + args.canUpgrade ? this.trueAsHex : this.falseAsHex, + utf8ToHex("canAddSpecialRoles"), + args.canAddSpecialRoles ? this.trueAsHex : this.falseAsHex ]; return this.createTransaction({ @@ -290,13 +312,20 @@ Once the token is registered, you can unset this role by calling "unsetBurnRoleG utf8ToHex(args.tokenName), utf8ToHex(args.tokenTicker), bigIntToHex(args.numDecimals), - ...(args.canFreeze ? [utf8ToHex("canFreeze"), this.trueAsHex] : []), - ...(args.canWipe ? [utf8ToHex("canWipe"), this.trueAsHex] : []), - ...(args.canPause ? [utf8ToHex("canPause"), this.trueAsHex] : []), - ...(args.canTransferNFTCreateRole ? [utf8ToHex("canTransferNFTCreateRole"), this.trueAsHex] : []), - ...(args.canChangeOwner ? [utf8ToHex("canChangeOwner"), this.trueAsHex] : []), - ...(args.canUpgrade ? [utf8ToHex("canUpgrade"), this.trueAsHex] : []), - ...(args.canAddSpecialRoles ? [utf8ToHex("canAddSpecialRoles"), this.trueAsHex] : []), + utf8ToHex("canFreeze"), + args.canFreeze ? this.trueAsHex : this.falseAsHex, + utf8ToHex("canWipe"), + args.canWipe ? this.trueAsHex : this.falseAsHex, + utf8ToHex("canPause"), + args.canPause ? this.trueAsHex : this.falseAsHex, + utf8ToHex("canTransferNFTCreateRole"), + args.canTransferNFTCreateRole ? this.trueAsHex : this.falseAsHex, + utf8ToHex("canChangeOwner"), + args.canChangeOwner ? this.trueAsHex : this.falseAsHex, + utf8ToHex("canUpgrade"), + args.canUpgrade ? this.trueAsHex : this.falseAsHex, + utf8ToHex("canAddSpecialRoles"), + args.canAddSpecialRoles ? this.trueAsHex : this.falseAsHex ]; return this.createTransaction({ @@ -501,11 +530,7 @@ Once the token is registered, you can unset this role by calling "unsetBurnRoleG } unfreeze(args: IFreezingArgs): Transaction { - const parts = [ - "unFreeze", - utf8ToHex(args.tokenIdentifier), - addressToHex(args.user) - ]; + const parts = ["unFreeze", utf8ToHex(args.tokenIdentifier), addressToHex(args.user)]; return this.createTransaction({ sender: args.manager, @@ -519,11 +544,7 @@ Once the token is registered, you can unset this role by calling "unsetBurnRoleG } wipe(args: IWipingArgs): Transaction { - const parts = [ - "wipe", - utf8ToHex(args.tokenIdentifier), - addressToHex(args.user) - ]; + const parts = ["wipe", utf8ToHex(args.tokenIdentifier), addressToHex(args.user)]; return this.createTransaction({ sender: args.manager, @@ -537,11 +558,7 @@ Once the token is registered, you can unset this role by calling "unsetBurnRoleG } localMint(args: ILocalMintArgs): Transaction { - const parts = [ - "ESDTLocalMint", - utf8ToHex(args.tokenIdentifier), - bigIntToHex(args.supplyToMint), - ]; + const parts = ["ESDTLocalMint", utf8ToHex(args.tokenIdentifier), bigIntToHex(args.supplyToMint)]; return this.createTransaction({ sender: args.manager, @@ -669,7 +686,8 @@ Once the token is registered, you can unset this role by calling "unsetBurnRoleG } private computeGasLimit(payload: TransactionPayload, executionGas: IGasLimit): IGasLimit { - const dataMovementGas = this.config.minGasLimit.valueOf() + this.config.gasLimitPerByte.valueOf() * payload.length(); + const dataMovementGas = + this.config.minGasLimit.valueOf() + this.config.gasLimitPerByte.valueOf() * payload.length(); return dataMovementGas + executionGas.valueOf(); } }