반응형
ERC-20 토큰을 전송하기 위해선 이더리움을 전송하는 코드와 비슷하지만, ERC-20 토큰 전송에는 추가적임 'data'필드가 필요하다.
이 'data'필드에는 'transfer'함수의 호출 정보가 담긴다.
1. 필요 패키지 설치
npm install web3 dotenv
2. '.env' 환경 변수 설정
INFURA_API_KEY=your_infura_api_key
SENDER_PRIVATE_KEY=your_private_key
RECIPIENT_ADDRESS=recipient_ethereum_address
TOKEN_CONTRACT_ADDRESS=your_token_contract_address
3. 예제코드
require('dotenv').config();
const Web3 = require('web3');
const INFURA_API_KEY = process.env.INFURA_API_KEY;
const SENDER_PRIVATE_KEY = process.env.SENDER_PRIVATE_KEY;
const RECIPIENT_ADDRESS = process.env.RECIPIENT_ADDRESS;
const TOKEN_CONTRACT_ADDRESS = process.env.TOKEN_CONTRACT_ADDRESS;
const web3 = new Web3(`https://mainnet.infura.io/v3/${INFURA_API_KEY}`);
const account = web3.eth.accounts.privateKeyToAccount(`0x${SENDER_PRIVATE_KEY}`);
web3.eth.accounts.wallet.add(account);
const tokenABI = [
{
"constant": true,
"inputs": [],
"name": "name",
"outputs": [
{
"name": "",
"type": "string"
}
],
"payable": false,
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "_spender",
"type": "address"
},
{
"name": "_value",
"type": "uint256"
}
],
"name": "approve",
"outputs": [
{
"name": "",
"type": "bool"
}
],
"payable": false,
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "totalSupply",
"outputs": [
{
"name": "",
"type": "uint256"
}
],
"payable": false,
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "_from",
"type": "address"
},
{
"name": "_to",
"type": "address"
},
{
"name": "_value",
"type": "uint256"
}
],
"name": "transferFrom",
"outputs": [
{
"name": "",
"type": "bool"
}
],
"payable": false,
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "decimals",
"outputs": [
{
"name": "",
"type": "uint8"
}
],
"payable": false,
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "balanceOf",
"outputs": [
{
"name": "balance",
"type": "uint256"
}
],
"payable": false,
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "symbol",
"outputs": [
{
"name": "",
"type": "string"
}
],
"payable": false,
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "_to",
"type": "address"
},
{
"name": "_value",
"type": "uint256"
}
],
"name": "transfer",
"outputs": [
{
"name": "",
"type": "bool"
}
],
"payable": false,
"type": "function"
},
{
"constant": true,
"inputs": [
{
"name": "_owner",
"type": "address"
},
{
"name": "_spender",
"type": "address"
}
],
"name": "allowance",
"outputs": [
{
"name": "remaining",
"type": "uint256"
}
],
"payable": false,
"type": "function"
},
{
"inputs": [
{
"name": "_initialAmount",
"type": "uint256"
},
{
"name": "_tokenName",
"type": "string"
},
{
"name": "_decimalUnits",
"type": "uint8"
},
{
"name": "_tokenSymbol",
"type": "string"
}
],
"type": "constructor"
},
{
"payable": false,
"type": "fallback"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"name": "_from",
"type": "address"
},
{
"indexed": true,
"name": "_to",
"type": "address"
},
{
"indexed": false,
"name": "_value",
"type": "uint256"
}
],
"name": "Transfer",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"name": "_owner",
"type": "address"
},
{
"indexed": true,
"name": "_spender",
"type": "address"
},
{
"indexed": false,
"name": "_value",
"type": "uint256"
}
],
"name": "Approval",
"type": "event"
}
];
const tokenContract = new web3.eth.Contract(tokenABI, TOKEN_CONTRACT_ADDRESS);
async function sendToken() {
const tx = {
from: account.address,
to: TOKEN_CONTRACT_ADDRESS,
gas: 100000,
data: tokenContract.methods.transfer(RECIPIENT_ADDRESS, web3.utils.toWei('10', 'ether')).encodeABI(), // 토큰의 양
};
const signedTx = await web3.eth.accounts.signTransaction(tx, `0x${SENDER_PRIVATE_KEY}`);
const receipt = await web3.eth.sendSignedTransaction(signedTx.rawTransaction);
console.log(`Transaction hash: ${receipt.transactionHash}`);
}
sendToken().catch(console.error);
이 코드는 해당 ERC-20 토큰의 'transfer' 함수를 호출하여 토큰을 RECIPIENT_ADDRESS로 전송한다.
계정의 'private key'로 서명한 후 'sendSignedTransaction'을 사용하여 서명된 거래를 네트워크로 보낸다.
반응형
'DEVEL > NODE.JS' 카테고리의 다른 글
node.js 이더리움 전송 (0) | 2023.07.22 |
---|---|
node.js caver-js로 KLAY 잔액 확인 (0) | 2023.03.21 |
node.js caver-js로 klaytn token 전송 (0) | 2023.03.21 |
node.js caver-js로 klay 전송 (0) | 2023.03.21 |