From e4c65af9ede4a75d79029c909f02fcfeb7f4950d Mon Sep 17 00:00:00 2001 From: andreibancioiu Date: Mon, 20 Feb 2023 12:23:15 +0200 Subject: [PATCH 1/2] Add unit tests (fix after review). --- src/tokenOperations/codec.spec.ts | 19 +++++++++++++++++++ src/tokenOperations/codec.ts | 8 ++++++++ 2 files changed, 27 insertions(+) create mode 100644 src/tokenOperations/codec.spec.ts diff --git a/src/tokenOperations/codec.spec.ts b/src/tokenOperations/codec.spec.ts new file mode 100644 index 00000000..f220ea58 --- /dev/null +++ b/src/tokenOperations/codec.spec.ts @@ -0,0 +1,19 @@ +import BigNumber from "bignumber.js"; +import { assert } from "chai"; +import { bigIntToBuffer, bigIntToHex, bufferToBigInt, bufferToHex, stringToBuffer, utf8ToHex } from "./codec"; + +describe("test codec", () => { + it.only("should properly encode and decode values", () => { + assert.deepEqual(stringToBuffer("hello"), Buffer.from("hello")); + assert.deepEqual(bufferToBigInt(Buffer.from("075bcd15", "hex")), new BigNumber("123456789")); + assert.deepEqual(bufferToBigInt(Buffer.from([])), new BigNumber("0")); + assert.deepEqual(bigIntToBuffer("123456789"), Buffer.from("075bcd15", "hex")); + assert.deepEqual(bigIntToBuffer(0), Buffer.from([])); + assert.equal(bigIntToHex("123456789"), "075bcd15"); + assert.equal(bigIntToHex(0), ""); + assert.equal(utf8ToHex("hello"), "68656c6c6f"); + assert.equal(utf8ToHex(""), ""); + assert.equal(bufferToHex(Buffer.from("hello")), "68656c6c6f"); + assert.equal(bufferToHex(Buffer.from([])), ""); + }); +}); diff --git a/src/tokenOperations/codec.ts b/src/tokenOperations/codec.ts index e0787f9d..3b33abfd 100644 --- a/src/tokenOperations/codec.ts +++ b/src/tokenOperations/codec.ts @@ -17,10 +17,18 @@ export function bufferToBigInt(buffer: Buffer): BigNumber { } export function bigIntToBuffer(value: BigNumber.Value): Buffer { + if (value == 0) { + return Buffer.from([]); + } + return contractsCodecUtils.bigIntToBuffer(value); } export function bigIntToHex(value: BigNumber.Value): string { + if (value == 0) { + return ""; + } + return contractsCodecUtils.getHexMagnitudeOfBigInt(value); } From fbabc3fb29b30af5b5697b500df79d5fce26b42c Mon Sep 17 00:00:00 2001 From: andreibancioiu Date: Mon, 20 Feb 2023 12:35:39 +0200 Subject: [PATCH 2/2] Fix after review. --- package-lock.json | 4 ++-- package.json | 2 +- src/tokenOperations/codec.spec.ts | 2 +- src/tokenOperations/tokenOperationsFactory.spec.ts | 2 +- src/tokenOperations/tokenOperationsFactory.ts | 4 ++-- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/package-lock.json b/package-lock.json index 82f1d47e..660b968c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@multiversx/sdk-core", - "version": "11.4.0-alpha.1", + "version": "11.4.0-alpha.2", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@multiversx/sdk-core", - "version": "11.4.0-alpha.1", + "version": "11.4.0-alpha.2", "license": "MIT", "dependencies": { "@multiversx/sdk-transaction-decoder": "1.0.2", diff --git a/package.json b/package.json index 04950487..8cdf353f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@multiversx/sdk-core", - "version": "11.4.0-alpha.1", + "version": "11.4.0-alpha.2", "description": "MultiversX SDK for JavaScript and TypeScript", "main": "out/index.js", "types": "out/index.d.js", diff --git a/src/tokenOperations/codec.spec.ts b/src/tokenOperations/codec.spec.ts index f220ea58..1c2a92e1 100644 --- a/src/tokenOperations/codec.spec.ts +++ b/src/tokenOperations/codec.spec.ts @@ -3,7 +3,7 @@ import { assert } from "chai"; import { bigIntToBuffer, bigIntToHex, bufferToBigInt, bufferToHex, stringToBuffer, utf8ToHex } from "./codec"; describe("test codec", () => { - it.only("should properly encode and decode values", () => { + it("should properly encode and decode values", () => { assert.deepEqual(stringToBuffer("hello"), Buffer.from("hello")); assert.deepEqual(bufferToBigInt(Buffer.from("075bcd15", "hex")), new BigNumber("123456789")); assert.deepEqual(bufferToBigInt(Buffer.from([])), new BigNumber("0")); diff --git a/src/tokenOperations/tokenOperationsFactory.spec.ts b/src/tokenOperations/tokenOperationsFactory.spec.ts index f33c9031..6010019a 100644 --- a/src/tokenOperations/tokenOperationsFactory.spec.ts +++ b/src/tokenOperations/tokenOperationsFactory.spec.ts @@ -30,7 +30,7 @@ describe("test factory", () => { transactionNonce: 42 }); - assert.equal(transaction.getData().toString(), "issue@4652414e4b@4652414e4b@64@00@63616e467265657a65@74727565@63616e57697065@74727565@63616e5061757365@74727565@63616e4d696e74@74727565@63616e4275726e@74727565@63616e4368616e67654f776e6572@74727565@63616e55706772616465@74727565@63616e4164645370656369616c526f6c6573@74727565") + assert.equal(transaction.getData().toString(), "issue@4652414e4b@4652414e4b@64@@63616e467265657a65@74727565@63616e57697065@74727565@63616e5061757365@74727565@63616e4d696e74@74727565@63616e4275726e@74727565@63616e4368616e67654f776e6572@74727565@63616e55706772616465@74727565@63616e4164645370656369616c526f6c6573@74727565") 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.ts b/src/tokenOperations/tokenOperationsFactory.ts index a6a7d20c..3e06a5b8 100644 --- a/src/tokenOperations/tokenOperationsFactory.ts +++ b/src/tokenOperations/tokenOperationsFactory.ts @@ -358,8 +358,8 @@ export class TokenOperationsFactory { ...args.uris.map(utf8ToHex), ]; - // Question for review: is this the one to be considered as "NFT data"? - const nftData = args.attributes + args.uris.join(""); + // Note that the following is an approximation (a reasonable one): + const nftData = args.name + args.hash + args.attributes + args.uris.join(""); const storageGasLimit = nftData.length * this.config.gasLimitStorePerByte.valueOf(); return this.createTransaction({