1

Hey guys so I'm looking to build a donation button of sorts that requires a set amount of BUSD to a smart contract address I've already deployed! My only issue is that I can't seem to find a way to send BUSD instead of BNB which is set automatically as the accepted token! how would you go about this? I want to send 0.40 BUSD instead of 0.40 BNB and I want to lock the button so that it only accepts BUSD as payment!

const contractHandler = async({setTxn}) => {
try {
  const {wallet} = window;
  
  if (wallet) {
    const provider = new ethers.providers.Web3Provider(wallet);
    const signer = provider.getSigner();
    
    console.log("Initialize Payment");
    const txn = await signer.sendTransaction({to: contractAddress, value: ethers.utils.parseEther("0.40")});
    setTxn([txn])
  } else {
    console.log("Ethereum object does not exist");
  }
} catch(err) {
  console.log(err);
}

}

6
  • before looking at that does øu contract support transferring BUSD?
    – Majd TL
    Commented Feb 6, 2022 at 19:24
  • I've tested it with BNB and it worked well! I've also tested it with sending custom tokens as well (not from react). I haven't been able to check if it accepts BUSD just yet Commented Feb 6, 2022 at 19:27
  • any address can take the tokens. the problem is can your contract transfer them out to you later :D ?
    – Majd TL
    Commented Feb 6, 2022 at 19:28
  • your contract need explicitly a function to transfer the token by its owner to another address otherwise the token will stuck there. but that is not your question
    – Majd TL
    Commented Feb 6, 2022 at 19:29
  • Yes I know that, I've actually added a function which, given the address of a token will withdraw the funds back to the owner (me). I'm also using testnets right now so my funds are not in danger whatsoever I am mostly running tests :) Commented Feb 6, 2022 at 19:31

1 Answer 1

1

try this

const signer = <the signer>;
const addressOfYourContract = '0x...';
const amountInTheSmallestUnit = BigNumber.from("10000...");
const busdContractAddress = '0x...';
const busdContractAbi = `....`;
const busdContract = new ethers.Contract(busdContractAddress, busdContractAbi, signer);
const tx = await busdContract.transfer(addressOfYourContract, amountInTheSmallestUnit);
const txResponse = tx.wait();

You can get the busdContractAbi from etherscan

3 things to take care of:

  • your contract support and can transfer the tokens if it has them (this should be implemented in the smart contract code)
  • the given amount in should be the smallest unit. If the token has decimal 18 then to send 1 token you need BigNumber.from("1000000000000000000"); and use BigNumbers because js cannot handle those big numbers
  • Test that code it is only an example :) no warranty
5
  • I seem to be getting the grasp of it and I'm getting closer. right now this one has a bit of a problem as this line -> const busdContract = new ethers.Contract(busdContractAddress, busdContractAbi, signer); has a typerror of abi is undefined for some reason. I've found the abi and copy pasted it in a json file so the variable itself doesn't seem to be an issue so I'm a bit confused xD Commented Feb 6, 2022 at 20:12
  • can u try to past it directly in code using ‘ …. ‘ and not json file ?
    – Majd TL
    Commented Feb 6, 2022 at 20:26
  • I will try to check that tomorrow
    – Majd TL
    Commented Feb 6, 2022 at 20:27
  • I DID IT!!! turns out my ABI variable really was undefined so I fixed it by adding the abi directly like you suggested! thank you so much!! Commented Feb 6, 2022 at 20:34
  • welcome , accept it as answer if it helped you
    – Majd TL
    Commented Feb 7, 2022 at 9:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.