I had a chance to read the pull over push pattern for transferring/withdrawing ethers, but i'm not sure if i need to apply this for erc20 transfers.
i.e.
function buy(uint256 _someId, uint256 _price) external {
require(weth.allowance(msgSender(), address(this)) >= _price, 'insuffient weth');
// more codes ...
weth.safeTransferFrom(msgSender(), test[_someId].beneficiary, _price);
_mint(msgSender(), tokenId);
}
This is my current implementation where weth is transfered to beneficiary EOA right away, but if I apply pull and push pattern here, it would look like
function buy(uint256 _someId, uint256 _price) external {
require(weth.allowance(msgSender(), address(this)) >= _price, 'insuffient weth');
// more codes ...
balance[msgSender()] += _price;
weth.safeTransferFrom(msgSender(), **address(this)**, _price);
_mint(msgSender(), tokenId);
}
function withdraw()... {
uint amount = balance[msgSender()];
require(amount != 0);
require(weth.balanceOf(address(this)) >= amount);
balance[msgSender()] = 0;
weth.transfer(msgSender(), amount);
}
contract receives weth first and records the balance of the erc20 deposit.
Then user would withdraw it later by contract sending directly to beneficiary EOA.
To me it seems not right to apply erc20 transfers with pull over push? What's the suggestion?