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 3 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
31 changes: 26 additions & 5 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";
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 @@ -249,19 +249,31 @@ export class TokenComputer {

extractIdentifierFromExtendedIdentifier(identifier: string): string {
const parts = identifier.split("-");
const { prefix, ticker, randomnes } = this.splitIdentifierIntoComponent(parts);
Copy link
Contributor

Choose a reason for hiding this comment

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

typo in randomness. can also be renamed to randomSequence.

Copy link
Contributor

Choose a reason for hiding this comment

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

Typo 🙃

randomness and components.


this.checkIfExtendedIdentifierWasProvided(parts);
this.ensureTokenTickerValidity(parts[0]);
this.checkLengthOfRandomSequence(parts[1]);
this.ensureTokenTickerValidity(ticker);
this.checkLengthOfRandomSequence(randomnes);
if (prefix) {
this.checkLengthOfPrefix(prefix);
return prefix + "-" + ticker + "-" + randomnes;
}
return ticker + "-" + randomnes;
}

splitIdentifierIntoComponent(parts: string[]): { prefix: any; ticker: any; randomnes: any } {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we have this as private?

if (parts.length >= 3 && parts[2].length === 6) {
Copy link
Contributor

Choose a reason for hiding this comment

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

In other functions, we have constants defined (for 3 and 6).

Copy link
Contributor

Choose a reason for hiding this comment

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

How would it be to contain the const parts = identifier.split("-"); itself within this function, too? 🌨️

Copy link
Contributor Author

@danielailie danielailie Nov 7, 2024

Choose a reason for hiding this comment

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

I don't know what name should I use for 3, any suggestions
?

return { prefix: parts[0], ticker: parts[1], randomnes: parts[2] };
}

return parts[0] + "-" + parts[1];
return { prefix: null, ticker: parts[0], randomnes: 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 @@ -281,6 +293,15 @@ export class TokenComputer {
}
}

private checkLengthOfPrefix(prefix: string): void {
const TOKEN_PREFIX_LENGTH = 4;
if (prefix.length > TOKEN_PREFIX_LENGTH) {
Copy link
Contributor

Choose a reason for hiding this comment

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

maybe we should also check that the length of the prefix is at least 1.

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