Skip to content

Commit

Permalink
Merge pull request #551 from multiversx/TOOL-405-update-to-use-new-me…
Browse files Browse the repository at this point in the history
…thods-on-token-transfer

Update token transfer to follow specs
  • Loading branch information
danielailie authored Dec 17, 2024
2 parents df2420f + 0f0102c commit b8f4eae
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 96 deletions.
24 changes: 15 additions & 9 deletions src/abi/interaction.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe("test smart contract interactor", function () {
let transaction = interaction
.withSender(alice.address)
.withNonce(7n)
.withValue(TokenTransfer.egldFromAmount(1).amount)
.withValue(TokenTransfer.newFromNativeAmount(1000000000000000000n).amount)
.withGasLimit(20000000n)
.buildTransaction();

Expand All @@ -49,11 +49,17 @@ describe("test smart contract interactor", function () {
let dummyFunction = new ContractFunction("dummy");
let alice = new Address("erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th");

const TokenFoo = (amount: BigNumber.Value) => TokenTransfer.fungibleFromAmount("FOO-6ce17b", amount, 0);
const TokenBar = (amount: BigNumber.Value) => TokenTransfer.fungibleFromAmount("BAR-5bc08f", amount, 3);
const TokenFoo = (amount: BigNumber.Value) =>
new TokenTransfer({ token: new Token({ identifier: "FOO-6ce17b" }), amount: BigInt(amount.toString()) });
const TokenBar = (amount: BigNumber.Value) =>
new TokenTransfer({ token: new Token({ identifier: "BAR-5bc08f" }), amount: BigInt(amount.toString()) });
const LKMEX = (nonce: number, amount: BigNumber.Value) =>
TokenTransfer.metaEsdtFromAmount("LKMEX-aab910", nonce, amount, 18);
const nonFungibleToken = (nonce: number) => TokenTransfer.nonFungible("MOS-b9b4b2", nonce);
new TokenTransfer({
token: new Token({ identifier: "LKMEX-aab910", nonce: BigInt(nonce) }),
amount: BigInt(amount.toString()),
});
const nonFungibleToken = (nonce: number) =>
new TokenTransfer({ token: new Token({ identifier: "MOS-b9b4b2", nonce: BigInt(nonce) }), amount: 1n });

const hexFoo = "464f4f2d366365313762";
const hexBar = "4241522d356263303866";
Expand All @@ -73,7 +79,7 @@ describe("test smart contract interactor", function () {
// Meta ESDT (special SFT), single
transaction = new Interaction(contract, dummyFunction, [])
.withSender(alice)
.withSingleESDTNFTTransfer(LKMEX(123456, 123.456))
.withSingleESDTNFTTransfer(LKMEX(123456, "123456000000000000000"))
.buildTransaction();

assert.equal(transaction.getSender().toBech32(), alice.toBech32());
Expand All @@ -85,7 +91,7 @@ describe("test smart contract interactor", function () {

// Meta ESDT (special SFT), single, but using "withSender()" (recommended)
transaction = new Interaction(contract, dummyFunction, [])
.withSingleESDTNFTTransfer(LKMEX(123456, 123.456))
.withSingleESDTNFTTransfer(LKMEX(123456, 123456000000000000000))
.withSender(alice)
.buildTransaction();

Expand Down Expand Up @@ -125,7 +131,7 @@ describe("test smart contract interactor", function () {
// ESDT, multiple
transaction = new Interaction(contract, dummyFunction, [])
.withSender(alice)
.withMultiESDTNFTTransfer([TokenFoo(3), TokenBar(3.14)])
.withMultiESDTNFTTransfer([TokenFoo(3), TokenBar(3140)])
.buildTransaction();

assert.equal(transaction.getSender().toBech32(), alice.toBech32());
Expand All @@ -137,7 +143,7 @@ describe("test smart contract interactor", function () {

// ESDT, multiple, but using "withSender()" (recommended)
transaction = new Interaction(contract, dummyFunction, [])
.withMultiESDTNFTTransfer([TokenFoo(3), TokenBar(3.14)])
.withMultiESDTNFTTransfer([TokenFoo(3), TokenBar(3140)])
.withSender(alice)
.buildTransaction();

Expand Down
2 changes: 1 addition & 1 deletion src/proto/serializer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ describe("serialize transactions", () => {
it("with data, with value", async () => {
let transaction = new Transaction({
nonce: 91n,
value: TokenTransfer.egldFromAmount(10).amount,
value: TokenTransfer.newFromNativeAmount(10000000000000000000n).amount,
sender: wallets.alice.address,
receiver: wallets.bob.address,
gasLimit: 100000n,
Expand Down
2 changes: 1 addition & 1 deletion src/smartContracts/smartContractTransactionsFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export class SmartContractTransactionsFactory {
let numberOfTokens = tokenTransfers.length;

if (nativeTransferAmount && numberOfTokens) {
tokenTransfers.push(TokenTransfer.newFromEgldAmount(nativeTransferAmount));
tokenTransfers.push(TokenTransfer.newFromNativeAmount(nativeTransferAmount));
nativeTransferAmount = 0n;
numberOfTokens++;
}
Expand Down
71 changes: 16 additions & 55 deletions src/tokens.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,71 +57,32 @@ describe("test tokens and token computer", async () => {
});
});

describe("test token transfer (legacy)", () => {
it("should work with EGLD", () => {
assert.equal(TokenTransfer.egldFromAmount("1").toString(), "1000000000000000000");
assert.equal(TokenTransfer.egldFromAmount("10").toString(), "10000000000000000000");
assert.equal(TokenTransfer.egldFromAmount("100").toString(), "100000000000000000000");
assert.equal(TokenTransfer.egldFromAmount("1000").toString(), "1000000000000000000000");
assert.equal(TokenTransfer.egldFromAmount("0.1").toString(), "100000000000000000");
assert.equal(TokenTransfer.egldFromAmount("0.123456789").toString(), "123456789000000000");
assert.equal(TokenTransfer.egldFromAmount("0.123456789123456789").toString(), "123456789123456789");
assert.equal(TokenTransfer.egldFromAmount("0.123456789123456789777").toString(), "123456789123456789");
assert.equal(TokenTransfer.egldFromAmount("0.123456789123456789777777888888").toString(), "123456789123456789");

assert.equal(TokenTransfer.egldFromAmount(0.1).toPrettyString(), "0.100000000000000000 EGLD");
assert.equal(TokenTransfer.egldFromAmount(1).toPrettyString(), "1.000000000000000000 EGLD");
assert.equal(TokenTransfer.egldFromAmount(10).toPrettyString(), "10.000000000000000000 EGLD");
assert.equal(TokenTransfer.egldFromAmount(100).toPrettyString(), "100.000000000000000000 EGLD");
assert.equal(TokenTransfer.egldFromAmount(1000).toPrettyString(), "1000.000000000000000000 EGLD");
assert.equal(TokenTransfer.egldFromAmount("0.123456789").toPrettyString(), "0.123456789000000000 EGLD");
assert.equal(
TokenTransfer.egldFromAmount("0.123456789123456789777777888888").toPrettyString(),
"0.123456789123456789 EGLD",
);

assert.equal(TokenTransfer.egldFromBigInteger("1").toString(), "1");
assert.equal(TokenTransfer.egldFromBigInteger("1").toPrettyString(), "0.000000000000000001 EGLD");
assert.isTrue(TokenTransfer.egldFromAmount("1").isEgld());
});

it("should work with USDC (legacy)", () => {
const identifier = "USDC-c76f1f";
const numDecimals = 6;

assert.equal(TokenTransfer.fungibleFromAmount(identifier, "1", numDecimals).toString(), "1000000");
assert.equal(TokenTransfer.fungibleFromAmount(identifier, "0.1", numDecimals).toString(), "100000");
assert.equal(TokenTransfer.fungibleFromAmount(identifier, "0.123456789", numDecimals).toString(), "123456");
assert.equal(TokenTransfer.fungibleFromBigInteger(identifier, "1000000", numDecimals).toString(), "1000000");
assert.equal(
TokenTransfer.fungibleFromBigInteger(identifier, "1000000", numDecimals).toPrettyString(),
"1.000000 USDC-c76f1f",
);
});

it("should work with MetaESDT (legacy)", () => {
describe("test token transfer", () => {
it("should work with custom token type", () => {
const identifier = "MEXFARML-28d646";
const numDecimals = 18;
const nonce = 12345678;
const transfer = TokenTransfer.metaEsdtFromAmount(identifier, nonce, "0.1", numDecimals);

assert.equal(transfer.tokenIdentifier, identifier);
assert.equal(transfer.nonce, nonce);
const nonce = 12345678n;
const transfer = new TokenTransfer({
token: new Token({ identifier, nonce }),
amount: BigInt(100000000000000000),
});

assert.equal(transfer.token.identifier, identifier);
assert.equal(transfer.token.nonce, nonce);
assert.equal(transfer.toString(), "100000000000000000");
});

it("should work with NFTs (legacy)", () => {
it("should work with NFTs", () => {
const identifier = "TEST-38f249";
const nonce = 1;
const transfer = TokenTransfer.nonFungible(identifier, nonce);
const nonce = 1n;
const transfer = new TokenTransfer({ token: new Token({ identifier, nonce }), amount: 1n });

assert.equal(transfer.tokenIdentifier, identifier);
assert.equal(transfer.nonce, nonce);
assert.equal(transfer.toPrettyString(), "1 TEST-38f249");
assert.equal(transfer.token.nonce, nonce);
assert.equal(transfer.amount, 1n);
});

it("should create TokenTransfer from native token amount", () => {
const transfer = TokenTransfer.newFromEgldAmount(1000000000000000000n);
const transfer = TokenTransfer.newFromNativeAmount(1000000000000000000n);

assert.equal(transfer.token.identifier, "EGLD-000000");
assert.equal(transfer.token.nonce, 0n);
Expand Down
26 changes: 13 additions & 13 deletions src/tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export class TokenTransfer {
}
}

static newFromEgldAmount(amount: bigint): TokenTransfer {
static newFromNativeAmount(amount: bigint): TokenTransfer {
const token = new Token({ identifier: EGLD_IDENTIFIER_FOR_MULTI_ESDTNFT_TRANSFER });
return new TokenTransfer({ token, amount });
}
Expand All @@ -95,15 +95,15 @@ export class TokenTransfer {
}

/**
* Legacy function. Use the constructor instead: new TokenTransfer({ token, amount });
* @deprecated Use the constructor instead: new TokenTransfer({ token, amount });
*/
static egldFromAmount(amount: BigNumber.Value) {
const amountAsBigInteger = new BigNumber(amount).shiftedBy(EGLDNumDecimals).decimalPlaces(0);
return this.egldFromBigInteger(amountAsBigInteger);
}

/**
* Legacy function. Use the constructor instead: new TokenTransfer({ token, amount });
* @deprecated Use the constructor instead: new TokenTransfer({ token, amount });
*/
static egldFromBigInteger(amountAsBigInteger: BigNumber.Value) {
return new TokenTransfer({
Expand All @@ -115,15 +115,15 @@ export class TokenTransfer {
}

/**
* Legacy function. Use the constructor instead: new TokenTransfer({ token, amount });
* @deprecated Use the constructor instead: new TokenTransfer({ token, amount });
*/
static fungibleFromAmount(tokenIdentifier: string, amount: BigNumber.Value, numDecimals: number) {
const amountAsBigInteger = new BigNumber(amount).shiftedBy(numDecimals).decimalPlaces(0);
return this.fungibleFromBigInteger(tokenIdentifier, amountAsBigInteger, numDecimals);
}

/**
* Legacy function. Use the constructor instead: new TokenTransfer({ token, amount });
* @deprecated Use the constructor instead: new TokenTransfer({ token, amount });
*/
static fungibleFromBigInteger(
tokenIdentifier: string,
Expand All @@ -139,7 +139,7 @@ export class TokenTransfer {
}

/**
* Legacy function. Use the constructor instead: new TokenTransfer({ token, amount });
* @deprecated Use the constructor instead: new TokenTransfer({ token, amount });
*/
static nonFungible(tokenIdentifier: string, nonce: number) {
return new TokenTransfer({
Expand All @@ -151,7 +151,7 @@ export class TokenTransfer {
}

/**
* Legacy function. Use the constructor instead: new TokenTransfer({ token, amount });
* @deprecated Use the constructor instead: new TokenTransfer({ token, amount });
*/
static semiFungible(tokenIdentifier: string, nonce: number, quantity: number) {
return new TokenTransfer({
Expand All @@ -163,15 +163,15 @@ export class TokenTransfer {
}

/**
* Legacy function. Use the constructor instead: new TokenTransfer({ token, amount });
* @deprecated Use the constructor instead: new TokenTransfer({ token, amount });
*/
static metaEsdtFromAmount(tokenIdentifier: string, nonce: number, amount: BigNumber.Value, numDecimals: number) {
const amountAsBigInteger = new BigNumber(amount).shiftedBy(numDecimals).decimalPlaces(0);
return this.metaEsdtFromBigInteger(tokenIdentifier, nonce, amountAsBigInteger, numDecimals);
}

/**
* Legacy function. Use the constructor instead: new TokenTransfer({ token, amount });
* @deprecated Use the constructor instead: new TokenTransfer({ token, amount });
*/
static metaEsdtFromBigInteger(
tokenIdentifier: string,
Expand All @@ -192,14 +192,14 @@ export class TokenTransfer {
}

/**
* Legacy function. Use the "amount" field instead.
* @deprecated Use the "amount" field instead.
*/
valueOf(): BigNumber {
return new BigNumber(this.amount.toString());
}

/**
* Legacy function. For formatting and parsing amounts, use "sdk-dapp" or "bignumber.js" directly.
* @deprecated For formatting and parsing amounts, use "sdk-dapp" or "bignumber.js" directly.
*/
toPrettyString(): string {
return `${this.toAmount()} ${this.tokenIdentifier}`;
Expand All @@ -210,15 +210,15 @@ export class TokenTransfer {
}

/**
* Legacy function. Within your code, don't mix native values (EGLD) and custom (ESDT) tokens.
* @deprecated Within your code, don't mix native values (EGLD) and custom (ESDT) tokens.
* See "TransferTransactionsFactory.createTransactionForNativeTokenTransfer()" vs. "TransferTransactionsFactory.createTransactionForESDTTokenTransfer()".
*/
isEgld(): boolean {
return this.token.identifier == EGLDTokenIdentifier;
}

/**
* Legacy function. Use "TokenComputer.isFungible(token)" instead.
* @deprecated Use "TokenComputer.isFungible(token)" instead.
*/
isFungible(): boolean {
return this.token.nonce == 0n;
Expand Down
25 changes: 17 additions & 8 deletions src/transaction.local.net.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ describe("test transaction", function () {
let transactionOne = new Transaction({
sender: alice.address,
receiver: bob.address,
value: TokenTransfer.egldFromAmount(42).amount,
value: TokenTransfer.newFromNativeAmount(42000000000000000000n).amount,
gasLimit: BigInt(network.MinGasLimit),
chainID: network.ChainID,
});

let transactionTwo = new Transaction({
sender: alice.address,
receiver: bob.address,
value: TokenTransfer.egldFromAmount(43).amount,
value: TokenTransfer.newFromNativeAmount(43000000000000000000n).amount,
gasLimit: BigInt(network.MinGasLimit),
chainID: network.ChainID,
});
Expand All @@ -72,7 +72,10 @@ describe("test transaction", function () {
await bob.sync(provider);
let newBalanceOfBob = new BigNumber((await bob.getBalance(provider)).toString());

assert.deepEqual(TokenTransfer.egldFromAmount(85).valueOf(), newBalanceOfBob.minus(initialBalanceOfBob));
assert.deepEqual(
TokenTransfer.newFromNativeAmount(85000000000000000000n).amount,
BigInt(newBalanceOfBob.minus(initialBalanceOfBob).toString()),
);
});

it("should send transaction and wait for completion using the new proxy provider", async function () {
Expand All @@ -86,7 +89,7 @@ describe("test transaction", function () {
let transactionOne = new Transaction({
sender: alice.address,
receiver: bob.address,
value: TokenTransfer.egldFromAmount(42).amount,
value: TokenTransfer.newFromNativeAmount(42n).amount,
gasLimit: BigInt(network.MinGasLimit),
chainID: network.ChainID,
});
Expand All @@ -103,7 +106,10 @@ describe("test transaction", function () {
await bob.sync(provider);
let newBalanceOfBob = new BigNumber((await bob.getBalance(provider)).toString());

assert.deepEqual(TokenTransfer.egldFromAmount(42).valueOf(), newBalanceOfBob.minus(initialBalanceOfBob));
assert.deepEqual(
TokenTransfer.newFromNativeAmount(42n).amount,
BigInt(newBalanceOfBob.minus(initialBalanceOfBob).toString()),
);
});

it("should simulate transactions", async function () {
Expand All @@ -118,7 +124,7 @@ describe("test transaction", function () {
data: Buffer.from("helloWorld"),
gasLimit: 70000n,
receiver: alice.address,
value: TokenTransfer.egldFromAmount(1000).amount,
value: TokenTransfer.newFromNativeAmount(1000n).amount,
chainID: network.ChainID,
});

Expand All @@ -127,7 +133,7 @@ describe("test transaction", function () {
data: Buffer.from("helloWorld"),
gasLimit: 70000n,
receiver: alice.address,
value: TokenTransfer.egldFromAmount(1000000).amount,
value: TokenTransfer.newFromNativeAmount(1000000n).amount,
chainID: network.ChainID,
});

Expand Down Expand Up @@ -171,7 +177,10 @@ describe("test transaction", function () {
await bob.sync(provider);
const newBalanceOfBob = new BigNumber((await bob.getBalance(provider)).toString());

assert.deepEqual(TokenTransfer.egldFromAmount(42).valueOf(), newBalanceOfBob.minus(initialBalanceOfBob));
assert.deepEqual(
TokenTransfer.newFromNativeAmount(42000000000000000000n).amount,
BigInt(newBalanceOfBob.minus(initialBalanceOfBob).toString()),
);
});

async function signTransaction(options: { transaction: Transaction; wallet: TestWallet }) {
Expand Down
Loading

0 comments on commit b8f4eae

Please sign in to comment.