Skip to content

Instantly share code, notes, and snippets.

@korrio
Last active December 19, 2022 11:25
Show Gist options
  • Save korrio/5b8b496a71e403aca8952673230fe9b4 to your computer and use it in GitHub Desktop.
Save korrio/5b8b496a71e403aca8952673230fe9b4 to your computer and use it in GitHub Desktop.
withdrawBNB
// EscrowBNBContract
// https://testnet.bscscan.com/address/0x3e8ae7bf5a73b23bfd48bb8362ed55df079d11db#code
// BSC
const BSCMainnetUrl = process.env.BSC_MAINNET_URL
const BSCTestnetUrl = process.env.BSC_TESTNET_URL
const BSCPrivateKey = process.env.PRIVATE_KEY_BSC;
const BSCProvider = new ethers.providers.JsonRpcProvider(BSCTestnetUrl)
const BSCWallet = new ethers.Wallet(BSCPrivateKey);
const BSCAccount = BSCWallet.connect(BSCProvider);
const EscrowBNBContract = new ethers.Contract(
process.env.ESCROW_BNB_V2_ADDRESS,
[
// 'function depositBNB(uint userId, address _receiver, uint _amount) payable',
'function withdrawBNB(uint256 userId, address userAddress) payable',
//'event Deposit(uint, address, uint);',
// 'event Withdraw(address, uint);'
],
BSCAccount
);
const withdrawBNB = async (userId, address, amountSwordz) => {
console.log(chalk.yellow("withdrawBNB"));
console.log(chalk.yellow(`address : ${address}`));
console.log(chalk.yellow(`amount: ${amountSwordz}`));
let amountBNB = amountSwordz / bnbToSwordzRate;
let amountOut = ethers.utils.parseUnits(amountBNB.toString(),"ether");
console.log("amountBNB:",amountBNB);
console.log("amountOut:",amountOut);
try {
const estimateGas = await EscrowBNBContract.estimateGas.withdrawBNB(
userId,
address,
{
value: amountOut
});
console.log("estimateGas",estimateGas);
const txWithdraw = await EscrowBNBContract.withdrawBNB(
userId,
address,
{
value: amountOut
});
txWithdraw.wait();
console.log(chalk.yellow(`tx : ${txWithdraw.hash}`))
return txWithdraw
} catch (error) {
return error
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment