Skip to content

Commit e1bbfb5

Browse files
committed
add token transfers
1 parent 548ad5d commit e1bbfb5

File tree

5 files changed

+299
-0
lines changed

5 files changed

+299
-0
lines changed

multi-transfer.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import {
2+
TransactionComputer,
3+
Address,
4+
TransactionsFactoryConfig,
5+
TransferTransactionsFactory,
6+
TokenTransfer,
7+
Token,
8+
} from "@multiversx/sdk-core";
9+
import {
10+
receiverAddress,
11+
syncAndGetAccount,
12+
senderAddress,
13+
getSigner,
14+
apiNetworkProvider,
15+
} from "./setup.js";
16+
17+
const makeTransfer = async () => {
18+
const user = await syncAndGetAccount();
19+
const computer = new TransactionComputer();
20+
const signer = await getSigner();
21+
22+
// Prepare transfer transactions factory
23+
const factoryConfig = new TransactionsFactoryConfig({ chainID: "D" });
24+
const factory = new TransferTransactionsFactory({ config: factoryConfig });
25+
26+
// Transfer native EGLD token (value transfer, the same as with the simple transaction)
27+
const multiTransferTransaction =
28+
factory.createTransactionForESDTTokenTransfer({
29+
sender: new Address(senderAddress),
30+
receiver: new Address(receiverAddress),
31+
tokenTransfers: [
32+
new TokenTransfer({
33+
token: new Token({
34+
identifier: "ELVNFACE-762e9d",
35+
nonce: BigInt("90"),
36+
}),
37+
// Send 1, it is always 1 for NFTs
38+
amount: BigInt("1"), // or 1n
39+
}),
40+
new TokenTransfer({
41+
token: new Token({ identifier: "DEMSFT-00eac9", nonce: BigInt("1") }),
42+
// Send 10
43+
amount: BigInt("10"), // or 10n
44+
}),
45+
new TokenTransfer({
46+
token: new Token({ identifier: "DEMFUNGI-3ec13b" }),
47+
// Send 10, remember about 18 decimal places
48+
amount: BigInt("10000000000000000000"), // or 10000000000000000000n
49+
}),
50+
],
51+
});
52+
53+
multiTransferTransaction.nonce = user.getNonceThenIncrement();
54+
55+
const serializedmultiTransferTransaction = computer.computeBytesForSigning(
56+
multiTransferTransaction
57+
);
58+
59+
multiTransferTransaction.signature = await signer.sign(
60+
serializedmultiTransferTransaction
61+
);
62+
63+
const txHash = await apiNetworkProvider.sendTransaction(
64+
multiTransferTransaction
65+
);
66+
67+
console.log(
68+
"Multiple ESDTs sent. Check in the Explorer: ",
69+
`https://devnet-explorer.multiversx.com/transactions/${txHash}`
70+
);
71+
};
72+
73+
makeTransfer();

transfer-egld.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import {
2+
TransactionComputer,
3+
Address,
4+
TransactionsFactoryConfig,
5+
TransferTransactionsFactory,
6+
} from "@multiversx/sdk-core";
7+
import {
8+
receiverAddress,
9+
syncAndGetAccount,
10+
senderAddress,
11+
getSigner,
12+
apiNetworkProvider,
13+
} from "./setup.js";
14+
15+
const makeTransfer = async () => {
16+
const user = await syncAndGetAccount();
17+
const computer = new TransactionComputer();
18+
const signer = await getSigner();
19+
20+
// Prepare transfer transactions factory
21+
const factoryConfig = new TransactionsFactoryConfig({ chainID: "D" });
22+
const factory = new TransferTransactionsFactory({ config: factoryConfig });
23+
24+
// Transfer native EGLD token (value transfer, the same as with the simple transaction)
25+
const egldTransaction = factory.createTransactionForNativeTokenTransfer({
26+
sender: new Address(senderAddress),
27+
receiver: new Address(receiverAddress),
28+
// 0.01 EGLD (EGLD has 18 decimal places)
29+
nativeAmount: BigInt("10000000000000000"),
30+
});
31+
32+
egldTransaction.nonce = user.getNonceThenIncrement();
33+
34+
const serializedEgldTransaction =
35+
computer.computeBytesForSigning(egldTransaction);
36+
37+
egldTransaction.signature = await signer.sign(serializedEgldTransaction);
38+
39+
const txHash = await apiNetworkProvider.sendTransaction(egldTransaction);
40+
41+
console.log(
42+
"EGLD sent. Check in the Explorer: ",
43+
`https://devnet-explorer.multiversx.com/transactions/${txHash}`
44+
);
45+
};
46+
47+
makeTransfer();

transfer-fungible.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import {
2+
TransactionComputer,
3+
Address,
4+
TransactionsFactoryConfig,
5+
TransferTransactionsFactory,
6+
TokenTransfer,
7+
Token,
8+
} from "@multiversx/sdk-core";
9+
import {
10+
receiverAddress,
11+
syncAndGetAccount,
12+
senderAddress,
13+
getSigner,
14+
apiNetworkProvider,
15+
} from "./setup.js";
16+
17+
const makeTransfer = async () => {
18+
const user = await syncAndGetAccount();
19+
const computer = new TransactionComputer();
20+
const signer = await getSigner();
21+
22+
// Prepare transfer transactions factory
23+
const factoryConfig = new TransactionsFactoryConfig({ chainID: "D" });
24+
const factory = new TransferTransactionsFactory({ config: factoryConfig });
25+
26+
// Transfer native EGLD token (value transfer, the same as with the simple transaction)
27+
const fungibleTransaction = factory.createTransactionForESDTTokenTransfer({
28+
sender: new Address(senderAddress),
29+
receiver: new Address(receiverAddress),
30+
tokenTransfers: [
31+
new TokenTransfer({
32+
token: new Token({ identifier: "DEMFUNGI-3ec13b" }),
33+
// Send 10, remember about 18 decimal places
34+
amount: BigInt("10000000000000000000"), // or 10000000000000000000n
35+
}),
36+
],
37+
});
38+
39+
fungibleTransaction.nonce = user.getNonceThenIncrement();
40+
41+
const serializedfungibleTransaction =
42+
computer.computeBytesForSigning(fungibleTransaction);
43+
44+
fungibleTransaction.signature = await signer.sign(
45+
serializedfungibleTransaction
46+
);
47+
48+
const txHash = await apiNetworkProvider.sendTransaction(fungibleTransaction);
49+
50+
console.log(
51+
"Fungible ESDT sent. Check in the Explorer: ",
52+
`https://devnet-explorer.multiversx.com/transactions/${txHash}`
53+
);
54+
};
55+
56+
makeTransfer();

transfer-nft.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import {
2+
TransactionComputer,
3+
Address,
4+
TransactionsFactoryConfig,
5+
TransferTransactionsFactory,
6+
TokenTransfer,
7+
Token,
8+
} from "@multiversx/sdk-core";
9+
import {
10+
receiverAddress,
11+
syncAndGetAccount,
12+
senderAddress,
13+
getSigner,
14+
apiNetworkProvider,
15+
} from "./setup.js";
16+
17+
const makeTransfer = async () => {
18+
const user = await syncAndGetAccount();
19+
const computer = new TransactionComputer();
20+
const signer = await getSigner();
21+
22+
// Prepare transfer transactions factory
23+
const factoryConfig = new TransactionsFactoryConfig({ chainID: "D" });
24+
const factory = new TransferTransactionsFactory({ config: factoryConfig });
25+
26+
// Transfer native EGLD token (value transfer, the same as with the simple transaction)
27+
const nonFungibleTransaction = factory.createTransactionForESDTTokenTransfer({
28+
sender: new Address(senderAddress),
29+
receiver: new Address(receiverAddress),
30+
tokenTransfers: [
31+
new TokenTransfer({
32+
token: new Token({
33+
identifier: "ELVNFACE-762e9d",
34+
nonce: BigInt("86"),
35+
}),
36+
// Send 1, it is always 1 for NFTs
37+
amount: BigInt("1"), // or 1n
38+
}),
39+
],
40+
});
41+
42+
nonFungibleTransaction.nonce = user.getNonceThenIncrement();
43+
44+
const serializednonFungibleTransaction = computer.computeBytesForSigning(
45+
nonFungibleTransaction
46+
);
47+
48+
nonFungibleTransaction.signature = await signer.sign(
49+
serializednonFungibleTransaction
50+
);
51+
52+
const txHash = await apiNetworkProvider.sendTransaction(
53+
nonFungibleTransaction
54+
);
55+
56+
console.log(
57+
"Semi-fungible ESDT sent. Check in the Explorer: ",
58+
`https://devnet-explorer.multiversx.com/transactions/${txHash}`
59+
);
60+
};
61+
62+
makeTransfer();

transfer-sft.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import {
2+
TransactionComputer,
3+
Address,
4+
TransactionsFactoryConfig,
5+
TransferTransactionsFactory,
6+
TokenTransfer,
7+
Token,
8+
} from "@multiversx/sdk-core";
9+
import {
10+
receiverAddress,
11+
syncAndGetAccount,
12+
senderAddress,
13+
getSigner,
14+
apiNetworkProvider,
15+
} from "./setup.js";
16+
17+
const makeTransfer = async () => {
18+
const user = await syncAndGetAccount();
19+
const computer = new TransactionComputer();
20+
const signer = await getSigner();
21+
22+
// Prepare transfer transactions factory
23+
const factoryConfig = new TransactionsFactoryConfig({ chainID: "D" });
24+
const factory = new TransferTransactionsFactory({ config: factoryConfig });
25+
26+
// Transfer native EGLD token (value transfer, the same as with the simple transaction)
27+
const semiFungibleTransaction = factory.createTransactionForESDTTokenTransfer(
28+
{
29+
sender: new Address(senderAddress),
30+
receiver: new Address(receiverAddress),
31+
tokenTransfers: [
32+
new TokenTransfer({
33+
token: new Token({ identifier: "DEMSFT-00eac9", nonce: BigInt("1") }),
34+
// Send 10
35+
amount: BigInt("10"), // or 10n
36+
}),
37+
],
38+
}
39+
);
40+
41+
semiFungibleTransaction.nonce = user.getNonceThenIncrement();
42+
43+
const serializedsemiFungibleTransaction = computer.computeBytesForSigning(
44+
semiFungibleTransaction
45+
);
46+
47+
semiFungibleTransaction.signature = await signer.sign(
48+
serializedsemiFungibleTransaction
49+
);
50+
51+
const txHash = await apiNetworkProvider.sendTransaction(
52+
semiFungibleTransaction
53+
);
54+
55+
console.log(
56+
"Semi-fungible ESDT sent. Check in the Explorer: ",
57+
`https://devnet-explorer.multiversx.com/transactions/${txHash}`
58+
);
59+
};
60+
61+
makeTransfer();

0 commit comments

Comments
 (0)