Skip to main content

Add Custom Methods

You can easily add new methods that do additional computation and emit events.

function logTax(address token, uint expected, uint recv) private {
uint shortfall = expected - recv;
uint feeBips = (shortfall * 10000) / expected;
emit TokenTax(token, feeBips);
}

And then we can use the function like this in the swap method

function swap(uint amount0Out, uint amount1Out, address to, bytes calldata data) external lock {
uint b0 = IERC20(token0).balanceOf(to);
uint b1 = IERC20(token1).balanceOf(to);
swapInternal(amount0Out, amount1Out, to, data);
if (amount0Out > 0) {
uint delta = IERC20(token0).balanceOf(to) - b0;
if (amount0Out > delta) {
logTax(token0, amount0Out, delta);
}
}
if (amount1Out > 0) {
uint delta = IERC20(token1).balanceOf(to) - b1;
if (amount1Out > delta) {
logTax(token1, amount1Out, delta);
}
}
}