Skip to content

Instantly share code, notes, and snippets.

@nCally
Created February 23, 2022 18:09
Show Gist options
  • Select an option

  • Save nCally/78135b88d3dbdf293a527ecdb810f6dc to your computer and use it in GitHub Desktop.

Select an option

Save nCally/78135b88d3dbdf293a527ecdb810f6dc to your computer and use it in GitHub Desktop.
export const sendSignedTransaction = async (tx, contractAddress) => {
const privatekey = ""; // your wallets secret key (very private, don't keep online)
const client = "";
const networkId = await web3.eth.net.getId();
const nonce = await web3.eth.getTransactionCount(client);
const gas = await tx.estimateGas({ from: client, nonce });
const gasPrice = await web3.eth.getGasPrice();
const data = tx.encodeABI();
const signedTx = await web3.eth.accounts.signTransaction(
{
to: contractAddress,
data,
gas,
gasPrice,
nonce,
chainId: networkId,
},
key
);
const receipt = await web3.eth.sendSignedTransaction(signedTx.rawTransaction);
return receipt;
};
const client = ""; // the public address of your wallet
const protocolAddress = ""; // xvUSDT, xBUSD etc
const amount = ""; // amount to deposit. 100 BUSD, 10 USDT etc
try {
const token = await new web3.eth.Contract(tokenAbi, tokenAddress); // USDT, BUSD etc
const contract = await new web3.eth.Contract(protocolAbi, protocolAddress);
let allowance = await token.methods["allowance"](client, protocolAddress).call();
allowance = web3.utils.fromWei(allowance, "ether");
if (Number(allowance) <= Number(amount)) {
const approveAmount = web3.utils.toWei("100000", "ether");
const approvedTX = await token.methods["approve"](protocolAddress, approveAmount);
await sendSignedTransaction(approvedTX, tokenAddress);
}
// deposit
const receiptTX = await contract.methods["deposit"](amount);
const receipt = await sendSignedTransaction(receiptTX, protocolAddress);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment