DEV Community

Lloyd A. Ramos
Lloyd A. Ramos

Posted on

How to call a function when transfer is occur?

0

I have a token contract, every tranfer a function is triggered Assumming the function I want to call is addFund

So I have this interface

`interface IContract {
struct Funds {
IERC20 token;
uint256 amount;
}

function addFunds(Funds[] calldata funds) external;
Enter fullscreen mode Exit fullscreen mode

}`

The use of addFunds is to receive a fund. The logic of the token is in every transfer an amount will goes to addFunds as the amount and the token is the contract itself.

The problem is The owner of the addFunds should be the token contract in order to trigger the addFunds. How can I trigger the addFunds by every transfer of token?

This is the code for addfunds

`/// @notice transfers {_tokens, amounts} for each token, amount pair provided from the tx sender to the campaign address
/// while taking commission for postmint.
/// @param funds defines the tokens and amounts to transfer, they MUST be part of the tokens supported by the campaign.
/// @dev this function transfers amounts[i] for tokens[i] to the campaign.
/// The number of tokens MUST match the number of amounts.
function addFunds(Funds[] calldata funds) public onlyOwner {
uint256 fundsLen = funds.length;
if (fundsLen == 0) {
revert NoFundsProvided();
}
for (uint256 i = 0; i < fundsLen; ) {
bool supported = supportedToken(funds[i].token);
if (!supported) {
revert UnsupportedToken(address(funds[i].token));
}
if (funds[i].amount == 0) {
revert NoAmountProvided();
}
_handleAddFunds(funds[i].token, funds[i].amount);

  unchecked {
    i++;
  }
}
emit AddedFunds(msg.sender, funds);
Enter fullscreen mode Exit fullscreen mode

}`

Top comments (0)