0

I am trying to work on a contract that buys and sells within the same contract and self destructs the contract. This happens all in 1 transaction. However, when it sells - it doesnt sell all, it sells almost 99%.

left over tokens

How can I make sure that all tokens are sold or sent back to my contract address before it self destructs?

1 Answer 1

1

You can call balanceOf(address(this)) within your contract to get how many tokens you have left, and then transfer this to your own address

uint256 remaining = IERC20(token).balanceOf(address(this))
transfer(your_address, remaining)

Optionally, if you don't care about instantly receiving the tokens you can utilize the create2 opcode to deploy your contract, selfdestruct and then redeploy at the same address. When you finally want to withdraw these tokens, you can write a function which does the following above for all tokens under the contract address.

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.