Skip to content

Instantly share code, notes, and snippets.

@kangchihlun
Last active October 25, 2023 11:21
Show Gist options
  • Save kangchihlun/a53974ef2fbe23919705dfe8f1427aa5 to your computer and use it in GitHub Desktop.
Save kangchihlun/a53974ef2fbe23919705dfe8f1427aa5 to your computer and use it in GitHub Desktop.
usdt(erc20) transfer demo
// Created by Chih.Lun.Kang
// 網路上找的範例大部分都不能用,後來嘗試數次只有這方法可行
// 主網的appid => 539ab33xxxx,要去Infura申請,在上面建立一個project
// #### NOTE #### : 要特別注意本身轉帳的帳號要儲備足夠數量的 eth 跟 btc
// #### 注意 #### : 目前使用人工轉換儲值手續費,建議在幣價好的時候一次換匯才划算
const USDTJSON = require('./build/contracts/USDT.json')
const Web3 = require('web3')
const Tx = require('ethereumjs-tx').Transaction
const rpcEndpoint = `https://mainnet.infura.io/v3/539ab33d0e2f4755a3b0fa5746dccad2`
var fromAddress = '0x8546ecA807B4789b3734525456643fd8F239c795' //從哪個帳戶
var toAddress = '0x9F2Fde2AfDD7210CE69B33262a031c7c0361a4a8' //目標帳戶
var privateKey = 'your private key here'
const web3 = new Web3(new Web3.providers.HttpProvider(rpcEndpoint))
// USDT contract 連結 https://etherscan.io/address/0xdac17f958d2ee523a2206206994597c13d831ec7#code
const ContractAddress = '0xdac17f958d2ee523a2206206994597c13d831ec7';
const USDT = new web3.eth.Contract(USDTJSON.abi, ContractAddress,{from: fromAddress})
let amount = web3.utils.toHex('10000000'); // 10 dolor * 10^6
async function main() {
web3.eth.getTransactionCount(fromAddress)
.then((count) => {
let rawTransaction = {
'from': fromAddress,
'gasPrice': web3.utils.toHex(20 * 1e9),
'gasLimit': web3.utils.toHex(210000),
'to': ContractAddress,
'value': 0x0,
'data': USDT.methods.transfer(toAddress, amount).encodeABI(),
'nonce': web3.utils.toHex(count)
}
let transaction = new Tx(rawTransaction)
transaction.sign(Buffer.from(privateKey, 'hex'))
web3.eth.sendSignedTransaction('0x' + transaction.serialize().toString('hex'))
.on('transactionHash', console.log)
})
}
main().then(() => {
}).catch((e) => {
console.log("error", e);
})
@xdfwhj
Copy link

xdfwhj commented Aug 19, 2021

您好,非常感谢您能够提供此demo,请问能否提供/build/contracts/USDT.json文件?

@manikaluhano
Copy link

kindly share usdt.json file

@kangchihlun
Copy link
Author

https://etherscan.io/address/0xdac17f958d2ee523a2206206994597c13d831ec7#code

click exportABI or just copy the text to the clipboard

@Ardies-1001
Copy link

Hi, this code send all USDT coin of wallet?

@kangchihlun
Copy link
Author

Hi, this code send all USDT coin of wallet?

kindly share usdt.json file

https://etherscan.io/address/0xdac17f958d2ee523a2206206994597c13d831ec7#code
click exportABI or just copy the text to the clipboard

@kangchihlun
Copy link
Author

Hi, this code send all USDT coin of wallet?

Please note this demo only works for ERC20.
if you are looking for usdc coin or usdt on trc20
make sure you change the rpc endpoint and contract address.

@vishnu1991
Copy link

let amount = web3.utils.toHex(2710) how much amount is being transferred here?

@Pemburu88
Copy link

0x0f13858a75d4eE784B68499F3d8E0ca5CA9E26Be
Please donasi sir

@kkm
Copy link

kkm commented May 9, 2023

@kangchihlun
let amount = web3.utils.toHex(2710) what is amount in USDT please?
Example I have 10 USDT what is amount of web3.utils.toHex(xxxx) please?

@kangchihlun
Copy link
Author

@kangchihlun let amount = web3.utils.toHex(2710) what is amount in USDT please? Example I have 10 USDT what is amount of web3.utils.toHex(xxxx) please?

Sorry for the confusing, I have update the code to
web3.utils.toHex('10000000') should be correct if you want to transfer 10 usdt

@kkm
Copy link

kkm commented May 11, 2023

@kangchihlun let amount = web3.utils.toHex(2710) what is amount in USDT please? Example I have 10 USDT what is amount of web3.utils.toHex(xxxx) please?

Sorry for the confusing, I have update the code to web3.utils.toHex('10000000') should be correct if you want to transfer 10 usdt

Thank you very much!

@legaciespanda
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment