I am developing my own contract and I am having the following issue:
module.exports = {
networks: {
development: {
provider: () => new HDWalletProvider(mnemonic, `http://127.0.0.1:8545`),
host: "127.0.0.1", // Localhost (default: none)
port: 8545, // Standard BSC port (default: none)
network_id: "*", // Any network (default: none)
skipDryRun: true // Skip dry run before migrations? (default: false for public nets )
},
and my code contains the following logic:
receive() external payable {}
function _transfer(
address sender,
address recipient,
uint256 amount
) internal virtual override{
if (ready_to_market && !inSwapAndLiquify) {
amount = swapAndLiquify(amount, buy);
}
if (!ready_to_market) {
// First transaction is the creation of the liquidity
// So I assume that the recipient is going to be the liquidity wallet
liquidity_wallet = address(recipient);
ready_to_market = true;
}
super._transfer(sender, recipient, amount);
}
function swapAndLiquify(uint256 amount, bool buy) private lockTheSwap returns (uint256) {
uint256 prize_rate = getTaxRates(buy);
uint256 prize = _calculatePercentage(amount, prize_rate);
// Take those tokens I want to keep
amount = amount.sub(prize);
SentToPrizePool(prize);
return amount;
}
function SendToPrizePool(uint256 tokens) private {
uint256 initialBalance = address(this).balance;
// Swap tokens and keep the result in the contract
_make_swap(tokens);
// how much BNB did we just swap into?
uint256 newBalance = address(this).balance.sub(initialBalance);
payable(prize_pool_wallet).transfer(newBalance);
}
function _make_swap(uint256 amount) private{
address[] memory path = new address[](2);
path[0] = address(this);
path[1] = pancakeswap_router.WETH();
_approve(address(this), address(pancakeswap_router), amount);
pancakeswap_router.swapExactTokensForETHSupportingFeeOnTransferTokens(
amount,
0, // accept any amount of ETH
path,
address(this),
block.timestamp
);
}
Stack trace:
{
"code": -32603,
"message": "Internal JSON-RPC error.",
"data": {
"message": "VM Exception while processing transaction: revert UniswapV2: TRANSFER_FAILED",
"code": -32000,
"data": {
"stack": "c: VM Exception while processing transaction: revert UniswapV2: TRANSFER_FAILED\n at Function.c.fromResults (/usr/local/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:4:192416)\n at e.exports (/usr/local/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:55:2089395)",
"name": "c"
}
}
}
So the logic behind this is:
- Uniswap swaps the tokens
- I calculate the tax I want to retain
- I want to exchange back those tokens I retained in step 2 to BNB
- I want to get those bnb and send them to the prize wallet
I can't even test this because uniswap and pancakeswap are not even allowing me to invoke the method, I am getting an error -3200.
If I comment the code with the logic to swap back it works.
Any clue why I am not able to swap again my tokens?
Also, I noticed that address(this).balance is always 0, am I missing something here?
Are there better ways to do this logic I want to do?
Update: I tried in Rinkeby, binance testnet and with ganache with the following config:
ganache-cli --fork https://rinkeby.infura.io/v3/id --chain-id 4
Error persists
Thank you