Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add prefix handling for esdt prefix #526

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@multiversx/sdk-core",
"version": "13.13.1",
"version": "13.14.0",
"description": "MultiversX SDK for JavaScript and TypeScript",
"author": "MultiversX",
"homepage": "https://multiversx.com",
Expand Down
16 changes: 16 additions & 0 deletions src/tokens.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,26 @@ describe("test tokens and token computer", async () => {
let identifier = tokenComputer.extractIdentifierFromExtendedIdentifier(extendedIdentifier);
assert.equal(identifier, "TEST-123456");

const extendedIdentifierWithPrefix = "t0-TEST-123456-0a";
identifier = tokenComputer.extractIdentifierFromExtendedIdentifier(extendedIdentifierWithPrefix);
assert.equal(identifier, "t0-TEST-123456");

const extendedIdentifierWithPrefixWithoutNonce = "t0-TEST-123456";
identifier = tokenComputer.extractIdentifierFromExtendedIdentifier(extendedIdentifierWithPrefixWithoutNonce);
assert.equal(identifier, "t0-TEST-123456");

const fungibleTokenIdentifier = "FNG-123456";
identifier = tokenComputer.extractIdentifierFromExtendedIdentifier(fungibleTokenIdentifier);
assert.equal(identifier, "FNG-123456");
});

it("should fail if prefix longer than expected", async () => {
const nftIdentifier = "prefix-TEST-123456";
assert.throw(
() => tokenComputer.extractIdentifierFromExtendedIdentifier(nftIdentifier),
"The identifier is not valid. The prefix does not have the right length",
);
});
});

describe("test token transfer (legacy)", () => {
Expand Down
38 changes: 30 additions & 8 deletions src/tokens.ts
axenteoctavian marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import BigNumber from "bignumber.js";

Check failure on line 1 in src/tokens.ts

View workflow job for this annotation

GitHub Actions / build (16.x)

Argument of type 'string[]' is not assignable to parameter of type 'string'.
import { ErrInvalidArgument, ErrInvalidTokenIdentifier } from "./errors";
import { EGLD_IDENTIFIER_FOR_MULTI_ESDTNFT_TRANSFER } from "./constants";
import { ErrInvalidArgument, ErrInvalidTokenIdentifier } from "./errors";

// Legacy constants:
const EGLDTokenIdentifier = "EGLD";
Expand Down Expand Up @@ -226,6 +226,7 @@
}

export class TokenComputer {
TOKEN_RANDOM_SEQUENCE_LENGTH = 6;
constructor() {}

isFungible(token: Token): boolean {
Expand All @@ -249,19 +250,32 @@

extractIdentifierFromExtendedIdentifier(identifier: string): string {
const parts = identifier.split("-");
const { prefix, ticker, randomSequence } = this.splitIdentifierIntoComponents(parts);

Check failure on line 253 in src/tokens.ts

View workflow job for this annotation

GitHub Actions / integration_tests

Argument of type 'string[]' is not assignable to parameter of type 'string'.

this.checkIfExtendedIdentifierWasProvided(parts);
this.ensureTokenTickerValidity(parts[0]);
this.checkLengthOfRandomSequence(parts[1]);
this.ensureTokenTickerValidity(ticker);
this.checkLengthOfRandomSequence(randomSequence);
if (prefix) {
this.checkLengthOfPrefix(prefix);
return prefix + "-" + ticker + "-" + randomSequence;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could have had string interpolation / optional 🙃

}
return ticker + "-" + randomSequence;
}

private splitIdentifierIntoComponents(identifier: string): { prefix: any; ticker: any; randomSequence: any } {
const parts = identifier.split("-");
if (parts.length >= 3 && parts[2].length === this.TOKEN_RANDOM_SEQUENCE_LENGTH) {
axenteoctavian marked this conversation as resolved.
Show resolved Hide resolved
return { prefix: parts[0], ticker: parts[1], randomSequence: parts[2] };
}

return parts[0] + "-" + parts[1];
return { prefix: null, ticker: parts[0], randomSequence: parts[1] };
}

private checkIfExtendedIdentifierWasProvided(tokenParts: string[]): void {
axenteoctavian marked this conversation as resolved.
Show resolved Hide resolved
// this is for the identifiers of fungible tokens
const MIN_EXTENDED_IDENTIFIER_LENGTH_IF_SPLITTED = 2;
// this is for the identifiers of nft, sft and meta-esdt
const MAX_EXTENDED_IDENTIFIER_LENGTH_IF_SPLITTED = 3;
const MAX_EXTENDED_IDENTIFIER_LENGTH_IF_SPLITTED = 4;

if (
tokenParts.length < MIN_EXTENDED_IDENTIFIER_LENGTH_IF_SPLITTED ||
Expand All @@ -272,15 +286,23 @@
}

private checkLengthOfRandomSequence(randomSequence: string): void {
const TOKEN_RANDOM_SEQUENCE_LENGTH = 6;

if (randomSequence.length !== TOKEN_RANDOM_SEQUENCE_LENGTH) {
if (randomSequence.length !== this.TOKEN_RANDOM_SEQUENCE_LENGTH) {
throw new ErrInvalidTokenIdentifier(
"The identifier is not valid. The random sequence does not have the right length",
);
}
}

private checkLengthOfPrefix(prefix: string): void {
const MAX_TOKEN_PREFIX_LENGTH = 4;
const MIN_TOKEN_PREFIX_LENGTH = 1;
if (prefix.length < MIN_TOKEN_PREFIX_LENGTH || prefix.length > MAX_TOKEN_PREFIX_LENGTH) {
throw new ErrInvalidTokenIdentifier(
"The identifier is not valid. The prefix does not have the right length",
);
}
}

private ensureTokenTickerValidity(ticker: string) {
const MIN_TICKER_LENGTH = 3;
const MAX_TICKER_LENGTH = 10;
Expand Down
75 changes: 75 additions & 0 deletions src/transactionsFactories/transferTransactionsFactory.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,26 @@ describe("test transfer transactions factory", function () {
);
});

it("should create 'Transaction' for nft transfer with prefix", async () => {
const nft = new Token({ identifier: "t0-NFT-123456", nonce: 10n });
const transfer = new TokenTransfer({ token: nft, amount: 1n });

const transaction = transferFactory.createTransactionForESDTTokenTransfer({
sender: alice,
receiver: bob,
tokenTransfers: [transfer],
});

assert.equal(transaction.sender, alice.toBech32());
assert.equal(transaction.receiver, alice.toBech32());
assert.equal(transaction.value.valueOf(), 0n);
assert.equal(transaction.gasLimit.valueOf(), 1219500n);
assert.deepEqual(
transaction.data.toString(),
"ESDTNFTTransfer@74302d4e46542d313233343536@0a@01@8049d639e5a6980d1cd2392abcce41029cda74a1563523a202f09641cc2618f8",
);
});

it("should create 'Transaction' for multiple nft transfers", async () => {
const firstNft = new Token({ identifier: "NFT-123456", nonce: 10n });
const firstTransfer = new TokenTransfer({ token: firstNft, amount: 1n });
Expand Down Expand Up @@ -128,6 +148,37 @@ describe("test transfer transactions factory", function () {
assert.deepEqual(transaction, secondTransaction);
});

it("should create 'Transaction' for multiple nft transfers with prefix", async () => {
const firstNft = new Token({ identifier: "t0-NFT-123456", nonce: 10n });
const firstTransfer = new TokenTransfer({ token: firstNft, amount: 1n });

const secondNft = new Token({ identifier: "t0-TEST-987654", nonce: 1n });
const secondTransfer = new TokenTransfer({ token: secondNft, amount: 1n });

const transaction = transferFactory.createTransactionForESDTTokenTransfer({
sender: alice,
receiver: bob,
tokenTransfers: [firstTransfer, secondTransfer],
});

assert.equal(transaction.sender, alice.toBech32());
assert.equal(transaction.receiver, alice.toBech32());
assert.equal(transaction.value.valueOf(), 0n);
assert.equal(transaction.gasLimit.valueOf(), 1484000n);
assert.deepEqual(
transaction.data.toString(),
"MultiESDTNFTTransfer@8049d639e5a6980d1cd2392abcce41029cda74a1563523a202f09641cc2618f8@02@74302d4e46542d313233343536@0a@01@74302d544553542d393837363534@01@01",
);

const secondTransaction = transferFactory.createTransactionForTransfer({
sender: alice,
receiver: bob,
tokenTransfers: [firstTransfer, secondTransfer],
});

assert.deepEqual(transaction, secondTransaction);
});

it("should fail to create transaction for token transfers", async () => {
assert.throws(() => {
const nft = new Token({ identifier: "NFT-123456", nonce: 10n });
Expand Down Expand Up @@ -207,6 +258,30 @@ describe("test transfer transactions factory", function () {
);
});

it("should create transaction for token transfers with prefix", async () => {
const firstNft = new Token({ identifier: "t0-NFT-123456", nonce: 10n });
const firstTransfer = new TokenTransfer({ token: firstNft, amount: 1n });

const secondNft = new Token({ identifier: "t0-TEST-987654", nonce: 1n });
const secondTransfer = new TokenTransfer({ token: secondNft, amount: 1n });

const transaction = transferFactory.createTransactionForTransfer({
sender: alice,
receiver: bob,
nativeAmount: 1000000000000000000n,
tokenTransfers: [firstTransfer, secondTransfer],
});

assert.equal(transaction.sender, alice.toBech32());
assert.equal(transaction.receiver, alice.toBech32());
assert.equal(transaction.value.valueOf(), 0n);
assert.equal(transaction.gasLimit.valueOf(), 1745500n);
assert.deepEqual(
transaction.data.toString(),
"MultiESDTNFTTransfer@8049d639e5a6980d1cd2392abcce41029cda74a1563523a202f09641cc2618f8@03@74302d4e46542d313233343536@0a@01@74302d544553542d393837363534@01@01@45474c442d303030303030@@0de0b6b3a7640000",
);
});

it("should create multi transfer for egld", async () => {
const firstNft = new Token({ identifier: EGLD_IDENTIFIER_FOR_MULTI_ESDTNFT_TRANSFER });
const firstTransfer = new TokenTransfer({ token: firstNft, amount: 1000000000000000000n });
Expand Down
Loading