πŸ’Έ
Sending ETH, Tokens and NFTs
Transferring assets using Moralis SDK is very easy. Sending native assets (ETH/BSC/MATIC etc), ERC20 tokens or NFTs is just one line of code.

Why Moralis SDK for transferring

Sending assets using bare-bones low-level libraries like Web3.js is surprisingly difficult. Developers are required to know the ABI of specific smart contracts in order to initiate token and NFT transactions.
Moralis SDK removes all this complexity. Transferring assets with Moralis SDK is just one line of code! πŸ§™
Tutorial explaining how to transfer ETH, tokens and NFTs with 1 line of code with Moralis SDK.

Transferring Native Asset (ETH/BNB/MATIC etc)

In order to transfer native assets of the blockchain (ETH on Ethereum, BNB on Binance Smart Chain, etc) you have to construct anoptionsobject where you settype:nativeto tell Moralis that you are sending the native asset (and not an ERC20 token or NFT).
Next, you have to tell Moralis the amount you want to transfer, to do that you need to use Moralis.Units.ETH (even when sending on BSC or Polygon) and specify the amount as a string denominated in ETH. Moralis.Units.ETHis a helper function that will convert your sting to wei which is required in order to construct the transaction.
Finally, you have to specify thereceiverof the funds. You do so by specifying the address as a string as demonstrated below.
1
// sending 0.5 ETH
2
const options = {type: "native", amount: Moralis.Units.ETH("0.5"), receiver: "0x.."}
3
let result = await Moralis.transfer(options)
Copied!

Transferring ERC20 Tokens

When sending an ERC20 you need to know the contract address of the token and the number of decimals.
1
// sending 0.5 tokens with 18 decimals
2
const options = {type: "erc20",
3
amount: Moralis.Units.Token("0.5", "18"),
4
receiver: "0x..",
5
contractAddress: "0x.."}
6
let result = await Moralis.transfer(options)
Copied!
The contract address and the number of decimals for a token can normally be found on Etherscan or on the website of the project.
By using Moralis.Units.Tokenhelper function you can multiply the amount you want to send by the number of decimals. This is required to construct an ERC20 transaction.
If you need to programmatically get the address and decimals of a token you can use Moralis SDK Moralis.Web3API.token.getTokenMetadataBySymbol().

Transferring ERC721 Tokens (Non-Fungible)

When sending an ERC721 token you have to specify the contactAddress of the NFT and the specific tokenId you are interested in.
As it's only possible to transfer one ERC721 at a time, no amount is needed.
1
// sending a token with token id = 1
2
const options = {type: "erc721",
3
receiver: "0x..",
4
contractAddress: "0x..",
5
tokenId: 1}
6
let result = await Moralis.transfer(options)
Copied!

Transferring ERC1155 Tokens (Semi-Fungible)

When sending an ERC1155 token you have to specify the contactAddress of the NFT and the specifictokenIdyou are interested in. Additionally, you have to specify theamountof the token you want to transfer.
1
// sending 15 tokens with token id = 1
2
const options = {type: "erc1155",
3
receiver: "0x..",
4
contractAddress: "0x..",
5
tokenId: 1,
6
amount: 15}
7
let result = await Moralis.transfer(options)
Copied!

πŸ”₯ Callbacks Promises Events (new)

Moralis.transfer() supports promise events. It allows you to get info on different stages of your interaction with the blockchain.
For example, you can instantly get a transactionHash without awaitng the entire transaction has been processed. And then after the transaction is processed, you can receive a receipt
To receive all data as event callbacks, you need to switch awaitReceipt to false in your transaction options.
1
const txOptions = {
2
type: "erc20",
3
amount: Moralis.Units.Token("10", "18"),
4
receiver: "0xB5...ee035",
5
contractAddress: "0x7b...605e",
6
awaitReceipt: false // should be switched to false
7
}
8
​
9
const tx = await Moralis.transfer(txOptions);
10
​
11
tx.on("transactionHash", (hash) => { ... })
12
.on("receipt", (receipt) => { ... })
13
.on("confirmation", (confirmationNumber, receipt) => { ... })
14
.on("error", (error) => { ... });
15
​
Copied!
Last modified 1mo ago