Last active
February 7, 2022 21:43
-
-
Save cristian495/a4ad9fec3c53656f74ece9eb3dde5909 to your computer and use it in GitHub Desktop.
Example send ETH token using Web3.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Web3 = require("web3"); | |
import Tx from "ethereumjs-tx"; | |
import { usdtABI, usdtAddress } from "../../helpers/currencies/usdt"; | |
import { ethHost, net, chainId } from "../../ethhost"; | |
import fs from "fs"; | |
const web3 = new Web3(new Web3.providers.HttpProvider(ethHost)); | |
const USDTContract = new web3.eth.Contract(usdtABI, usdtAddress, { | |
from: sendFrom, | |
}); | |
/* obtiene simbolo del contrato del token */ | |
// const balanceUSDT = await USDTContract.methods.symbol().call() | |
/* obtiene balance de ethers */ | |
const etherBalance = await web3.eth.getBalance(sendFrom); | |
/* obtiene balance de tokens */ | |
const sendFromBalanceUSDT = await USDTContract.methods | |
.balanceOf(sendFrom) | |
.call(); | |
console.log("receiver: " + toAddress); | |
console.log("sender: " + sendFrom); | |
console.log("amount: " + amount); | |
console.log( | |
"amount (wei): " + web3.utils.toWei(amount.toString(), "ether") | |
); | |
console.log("sender balance: " + sendFromBalanceUSDT, "tokens wei"); | |
console.log( | |
"sender balance: " + | |
web3.utils.fromWei(sendFromBalanceUSDT, "ether") + | |
"tokens" | |
); | |
console.log( | |
"sender balance: " + web3.utils.fromWei(etherBalance, "ether"), | |
"ethers" | |
); | |
if (etherBalance <= 0) { | |
return res.status(409).send({ | |
type: "insufficient-funds", | |
msg: `No cuenta con ningun ether en su Wallet`, | |
}); | |
} | |
if (sendFromBalanceUSDT <= 0) { | |
return res.status(409).send({ | |
type: "insufficient-funds", | |
msg: `No cuenta con ningun Tether en su Wallet`, | |
}); | |
} | |
// if (web3.utils.fromWei(sendFromBalanceUSDT, "ether") <= amount) { | |
// return res.status(409).send({ | |
// type: "insufficient-funds", | |
// msg: `El monto a enviar supera su saldo`, | |
// }); | |
// } | |
/* Obtiene el nonce para la transaccion */ | |
const txCount = await web3.eth.getTransactionCount(sendFrom); | |
/* Encondifica la transaccion */ | |
// const data = USDTContract.methods | |
// .transfer( | |
// toAddress, | |
// web3.utils.toHex(web3.utils.toWei(amount.toString(), "ether")) // en wei | |
// ) | |
// .encodeABI(); | |
/* Estima cuanto gas se necesita para la transaccion */ | |
// const estimatedGas = await USDTContract.methods | |
// .transfer(toAddress, amount) | |
// .estimateGas({ | |
// from: sendFrom, | |
// data: data, | |
// to: toAddress, | |
// }); | |
/* obtiene el precio actual del gas */ | |
// const gasPrice = await web3.eth.getGasPrice(); | |
/* obtiene GAS limit del ultimo block minado */ | |
// const latestBlock = await web3.eth.getBlock("latest"); | |
// const gasLimit = latestBlock.gasLimit / latestBlock.transactions.length; | |
await web3.eth.personal.unlockAccount(sendFrom, "123456789", 15000); | |
const data = await USDTContract.methods | |
.transfer( | |
toAddress, | |
web3.utils.toHex(web3.utils.toWei(amount.toString(), "ether")) // en wei | |
) | |
.send({ from: sendFrom }) | |
.on("transactionHash", async (hash) => { | |
console.log("transactionHash", hash); | |
/* GUARDADO EN DB */ | |
const sendTransactionProps = { | |
userId: userId, | |
// tx: resulTx.transactionHash, | |
tx: hash, | |
category: "send", | |
fromAddress: sendFrom, | |
toAddress: toAddress, | |
amount: amount, | |
fee: 0, | |
inner: 0, | |
agenteBTCFee: basicFee, | |
createdAt: Date.now(), | |
modifiedAt: Date.now(), | |
currency: "USDT", | |
history: [{ date: Date.now(), state: "DELIVERED" }], | |
state: "DELIVERED", | |
}; | |
const sendTransaction = new Transaction(sendTransactionProps); | |
const st = await sendTransaction.save(); | |
const feeProps = { | |
transactionId: st._id, | |
category: "SEND", | |
currency: "USDT", | |
amount: st.agenteBTCFee, | |
}; | |
const sendFeeObj = new Fee(feeProps); | |
await sendFeeObj.save(); | |
wallet.balance = | |
wallet.balance - parseFloat(basicFee) - parseFloat(amount); | |
await wallet.save(); | |
console.log("enviar resultado") | |
return res.status(200).send({ | |
type: "success", | |
msg: "Transaccion en proceso", | |
response: sendTransaction, | |
}); | |
console.log("no envia") | |
}) | |
.on("receipt", function (receipt) { | |
console.log("receipt"); | |
}) | |
// .on("confirmation", function (confirmationNumber, receipt) { | |
// console.log("confirmation"); | |
// }) | |
.on("error", function (error, receipt) { | |
console.log(error); | |
}); | |
//https://stackoverflow.com/questions/46902093/sending-erc20-tokens?noredirect=1&lq=1 | |
// https://piyopiyo.medium.com/how-to-send-erc20-token-with-web3-js-1-0-0-95164b32e9a6 | |
console.log("data",data); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment