How to set contract transfer fee? #3295
Answered
by
Unintendedz
Unintendedz
asked this question in
Q&A
-
Hi, everyone. For now, I'm doing this. import { ethers } from 'ethers'
import { BscscanProvider } from "@ethers-ancillary/bsc"
const abi: ethers.ContractInterface = [{
"inputs": [
{
"internalType": "address",
"name": "recipient",
"type": "address"
},
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
}
],
"name": "transfer",
"outputs": [
{
"internalType": "bool",
"name": "",
"type": "bool"
}
],
"stateMutability": "nonpayable",
"type": "function"
},]
const myWallet = new ethers.Wallet(
SENDERS_PRIVATE_KEY,
new BscscanProvider("bsc-mainnet")
)
console.log(myWallet);
const contract = new ethers.Contract(CONTRACT_ADDRESS, abi, myWallet)
const amount = ethers.utils.parseUnits("1")
const transferResult = await contract.transfer(RECIPIENTS_ADDRESS, amount)
console.log("transferResult:", transferResult); It's working perfectly, but I have no idea how to set the transfer fee like Metamask. Any help is appreciated! |
Beta Was this translation helpful? Give feedback.
Answered by
Unintendedz
Aug 20, 2022
Replies: 1 comment
-
After some research I found solution const abi= new ethers.utils.Interface([
'function transfer(address recipient, uint256 amount)',
'function balanceOf(address owner) view returns (uint)'
]);
const contractAddress = 'CONTRACT_ADDRESS';
const provider = new BscscanProvider("bsc-mainnet")
const myWallet = new ethers.Wallet(
'PRIVATE_KEY',
provider
)
const contract = new ethers.Contract(contractAddress , abi, provider);
const balance = await contract.balanceOf(myWallet.address)
const tx = await contract.populateTransaction.transfer(
RECIPIENTS_ADDRESS,
balance
)
tx.gasPrice = ethers.utils.parseUnits("5", "gwei")
tx.gasLimit = BigNumber.from("21000")
tx.chainId = 56
tx.nonce = await provider.getTransactionCount(myWallet.address);
console.log(tx);
const signedTx = await myWallet.signTransaction(tx)
const result = await provider.sendTransaction(signedTx)
console.log("result:", result); |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
Unintendedz
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
After some research I found solution