'how to add multiple wallets on Payable?
the code part was originally like this:
function swapAndSendToFee (uint256 tokens) private {
uint256 ethToSend = swapTokensForEth(tokens);
if (ethToSend > 0)
payable(_projectWallet).transfer(ethToSend);
}
but I wanted to add an extra wallet and I defined it earlier all good, but now it doesn't sell:
function swapAndSendToFee (uint256 tokens) private {
uint256 ethToSend = swapTokensForEth(tokens);
if (ethToSend > 0)
payable(_projectWallet).transfer(ethToSend);
payable(_marketingWallet).transfer(ethToSend);
}
What am I doing wrong?
Thanks!
Solution 1:[1]
There are two issues in the second snippet:
- A condition without a block only applies to the next line. So your current code could be also written as this:
if (ethToSend > 0) {
payable(_projectWallet).transfer(ethToSend);
}
payable(_marketingWallet).transfer(ethToSend);
I'm assuming you also wanted to wrap the transfer() functions in a block, so that the condition looks like this:
if (ethToSend > 0) {
payable(_projectWallet).transfer(ethToSend);
payable(_marketingWallet).transfer(ethToSend);
}
- In the second snippet, you're trying to transfer twice as much ETH from the contract, compared to the first snippet. It's possible that the contract doesn't have sufficient funds to perform both transfers.
First you transfer ethToSend amount to the _projectWallet, and then you also transfer the same ethToSend amount to the _marketingWallet
If you want to split the amount between the two wallets, you can perform a basic arithmetic operation:
payable(_projectWallet).transfer(ethToSend / 2);
payable(_marketingWallet).transfer(ethToSend / 2);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Petr Hejda |
