Skip to content

Commit

Permalink
add token transfers
Browse files Browse the repository at this point in the history
  • Loading branch information
juliancwirko committed May 2, 2024
1 parent 548ad5d commit e1bbfb5
Show file tree
Hide file tree
Showing 5 changed files with 299 additions and 0 deletions.
73 changes: 73 additions & 0 deletions multi-transfer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import {
TransactionComputer,
Address,
TransactionsFactoryConfig,
TransferTransactionsFactory,
TokenTransfer,
Token,
} from "@multiversx/sdk-core";
import {
receiverAddress,
syncAndGetAccount,
senderAddress,
getSigner,
apiNetworkProvider,
} from "./setup.js";

const makeTransfer = async () => {
const user = await syncAndGetAccount();
const computer = new TransactionComputer();
const signer = await getSigner();

// Prepare transfer transactions factory
const factoryConfig = new TransactionsFactoryConfig({ chainID: "D" });
const factory = new TransferTransactionsFactory({ config: factoryConfig });

// Transfer native EGLD token (value transfer, the same as with the simple transaction)
const multiTransferTransaction =
factory.createTransactionForESDTTokenTransfer({
sender: new Address(senderAddress),
receiver: new Address(receiverAddress),
tokenTransfers: [
new TokenTransfer({
token: new Token({
identifier: "ELVNFACE-762e9d",
nonce: BigInt("90"),
}),
// Send 1, it is always 1 for NFTs
amount: BigInt("1"), // or 1n
}),
new TokenTransfer({
token: new Token({ identifier: "DEMSFT-00eac9", nonce: BigInt("1") }),
// Send 10
amount: BigInt("10"), // or 10n
}),
new TokenTransfer({
token: new Token({ identifier: "DEMFUNGI-3ec13b" }),
// Send 10, remember about 18 decimal places
amount: BigInt("10000000000000000000"), // or 10000000000000000000n
}),
],
});

multiTransferTransaction.nonce = user.getNonceThenIncrement();

const serializedmultiTransferTransaction = computer.computeBytesForSigning(
multiTransferTransaction
);

multiTransferTransaction.signature = await signer.sign(
serializedmultiTransferTransaction
);

const txHash = await apiNetworkProvider.sendTransaction(
multiTransferTransaction
);

console.log(
"Multiple ESDTs sent. Check in the Explorer: ",
`https://devnet-explorer.multiversx.com/transactions/${txHash}`
);
};

makeTransfer();
47 changes: 47 additions & 0 deletions transfer-egld.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import {
TransactionComputer,
Address,
TransactionsFactoryConfig,
TransferTransactionsFactory,
} from "@multiversx/sdk-core";
import {
receiverAddress,
syncAndGetAccount,
senderAddress,
getSigner,
apiNetworkProvider,
} from "./setup.js";

const makeTransfer = async () => {
const user = await syncAndGetAccount();
const computer = new TransactionComputer();
const signer = await getSigner();

// Prepare transfer transactions factory
const factoryConfig = new TransactionsFactoryConfig({ chainID: "D" });
const factory = new TransferTransactionsFactory({ config: factoryConfig });

// Transfer native EGLD token (value transfer, the same as with the simple transaction)
const egldTransaction = factory.createTransactionForNativeTokenTransfer({
sender: new Address(senderAddress),
receiver: new Address(receiverAddress),
// 0.01 EGLD (EGLD has 18 decimal places)
nativeAmount: BigInt("10000000000000000"),
});

egldTransaction.nonce = user.getNonceThenIncrement();

const serializedEgldTransaction =
computer.computeBytesForSigning(egldTransaction);

egldTransaction.signature = await signer.sign(serializedEgldTransaction);

const txHash = await apiNetworkProvider.sendTransaction(egldTransaction);

console.log(
"EGLD sent. Check in the Explorer: ",
`https://devnet-explorer.multiversx.com/transactions/${txHash}`
);
};

makeTransfer();
56 changes: 56 additions & 0 deletions transfer-fungible.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import {
TransactionComputer,
Address,
TransactionsFactoryConfig,
TransferTransactionsFactory,
TokenTransfer,
Token,
} from "@multiversx/sdk-core";
import {
receiverAddress,
syncAndGetAccount,
senderAddress,
getSigner,
apiNetworkProvider,
} from "./setup.js";

const makeTransfer = async () => {
const user = await syncAndGetAccount();
const computer = new TransactionComputer();
const signer = await getSigner();

// Prepare transfer transactions factory
const factoryConfig = new TransactionsFactoryConfig({ chainID: "D" });
const factory = new TransferTransactionsFactory({ config: factoryConfig });

// Transfer native EGLD token (value transfer, the same as with the simple transaction)
const fungibleTransaction = factory.createTransactionForESDTTokenTransfer({
sender: new Address(senderAddress),
receiver: new Address(receiverAddress),
tokenTransfers: [
new TokenTransfer({
token: new Token({ identifier: "DEMFUNGI-3ec13b" }),
// Send 10, remember about 18 decimal places
amount: BigInt("10000000000000000000"), // or 10000000000000000000n
}),
],
});

fungibleTransaction.nonce = user.getNonceThenIncrement();

const serializedfungibleTransaction =
computer.computeBytesForSigning(fungibleTransaction);

fungibleTransaction.signature = await signer.sign(
serializedfungibleTransaction
);

const txHash = await apiNetworkProvider.sendTransaction(fungibleTransaction);

console.log(
"Fungible ESDT sent. Check in the Explorer: ",
`https://devnet-explorer.multiversx.com/transactions/${txHash}`
);
};

makeTransfer();
62 changes: 62 additions & 0 deletions transfer-nft.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import {
TransactionComputer,
Address,
TransactionsFactoryConfig,
TransferTransactionsFactory,
TokenTransfer,
Token,
} from "@multiversx/sdk-core";
import {
receiverAddress,
syncAndGetAccount,
senderAddress,
getSigner,
apiNetworkProvider,
} from "./setup.js";

const makeTransfer = async () => {
const user = await syncAndGetAccount();
const computer = new TransactionComputer();
const signer = await getSigner();

// Prepare transfer transactions factory
const factoryConfig = new TransactionsFactoryConfig({ chainID: "D" });
const factory = new TransferTransactionsFactory({ config: factoryConfig });

// Transfer native EGLD token (value transfer, the same as with the simple transaction)
const nonFungibleTransaction = factory.createTransactionForESDTTokenTransfer({
sender: new Address(senderAddress),
receiver: new Address(receiverAddress),
tokenTransfers: [
new TokenTransfer({
token: new Token({
identifier: "ELVNFACE-762e9d",
nonce: BigInt("86"),
}),
// Send 1, it is always 1 for NFTs
amount: BigInt("1"), // or 1n
}),
],
});

nonFungibleTransaction.nonce = user.getNonceThenIncrement();

const serializednonFungibleTransaction = computer.computeBytesForSigning(
nonFungibleTransaction
);

nonFungibleTransaction.signature = await signer.sign(
serializednonFungibleTransaction
);

const txHash = await apiNetworkProvider.sendTransaction(
nonFungibleTransaction
);

console.log(
"Semi-fungible ESDT sent. Check in the Explorer: ",
`https://devnet-explorer.multiversx.com/transactions/${txHash}`
);
};

makeTransfer();
61 changes: 61 additions & 0 deletions transfer-sft.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import {
TransactionComputer,
Address,
TransactionsFactoryConfig,
TransferTransactionsFactory,
TokenTransfer,
Token,
} from "@multiversx/sdk-core";
import {
receiverAddress,
syncAndGetAccount,
senderAddress,
getSigner,
apiNetworkProvider,
} from "./setup.js";

const makeTransfer = async () => {
const user = await syncAndGetAccount();
const computer = new TransactionComputer();
const signer = await getSigner();

// Prepare transfer transactions factory
const factoryConfig = new TransactionsFactoryConfig({ chainID: "D" });
const factory = new TransferTransactionsFactory({ config: factoryConfig });

// Transfer native EGLD token (value transfer, the same as with the simple transaction)
const semiFungibleTransaction = factory.createTransactionForESDTTokenTransfer(
{
sender: new Address(senderAddress),
receiver: new Address(receiverAddress),
tokenTransfers: [
new TokenTransfer({
token: new Token({ identifier: "DEMSFT-00eac9", nonce: BigInt("1") }),
// Send 10
amount: BigInt("10"), // or 10n
}),
],
}
);

semiFungibleTransaction.nonce = user.getNonceThenIncrement();

const serializedsemiFungibleTransaction = computer.computeBytesForSigning(
semiFungibleTransaction
);

semiFungibleTransaction.signature = await signer.sign(
serializedsemiFungibleTransaction
);

const txHash = await apiNetworkProvider.sendTransaction(
semiFungibleTransaction
);

console.log(
"Semi-fungible ESDT sent. Check in the Explorer: ",
`https://devnet-explorer.multiversx.com/transactions/${txHash}`
);
};

makeTransfer();

0 comments on commit e1bbfb5

Please sign in to comment.