Skip to content

Commit

Permalink
Make user agent to pay for a portion of the fees
Browse files Browse the repository at this point in the history
  • Loading branch information
yrong committed May 2, 2024
1 parent 539c180 commit 3dcd8ed
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions contracts/src/Gateway.sol
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,10 @@ contract Gateway is IGateway, IInitializable, IUpgradable {

bool success = true;

uint256 gasUsed;
uint256 gasUsedForTransact;
address agentForTransact;

// Dispatch message to a handler
if (message.command == Command.AgentExecute) {
try Gateway(this).agentExecute{gas: maxDispatchGas}(message.params) {}
Expand Down Expand Up @@ -215,15 +219,27 @@ contract Gateway is IGateway, IInitializable, IUpgradable {
success = false;
}
} else if (message.command == Command.Transact) {
try Gateway(this).transact{gas: maxDispatchGas}(message.params) {}
catch {
try Gateway(this).transact{gas: maxDispatchGas}(message.params) returns (address _agent, uint256 _gasUsed) {
gasUsedForTransact = _gasUsed;
agentForTransact = _agent;
} catch {
success = false;
}
}

// Calculate a gas refund, capped to protect against huge spikes in `tx.gasprice`
// that could drain funds unnecessarily. During these spikes, relayers should back off.
uint256 gasUsed = _transactionBaseGas() + (startGas - gasleft());
gasUsed = _transactionBaseGas() + (startGas - gasleft());
if (message.command == Command.Transact) {
// User agent pays for the transact dispatch
uint256 transact_fee = Math.min(gasUsed * tx.gasprice, address(agentForTransact).balance);
if (transact_fee > _dustThreshold()) {
_transferNativeFromAgent(agentForTransact, payable(msg.sender), transact_fee);
}
// gas used for the channel agent should not include the cost for transact
gasUsed = gasUsed - gasUsedForTransact;
}

uint256 refund = gasUsed * Math.min(tx.gasprice, message.maxFeePerGas);

// Add the reward to the refund amount. If the sum is more than the funds available
Expand Down Expand Up @@ -390,7 +406,9 @@ contract Gateway is IGateway, IInitializable, IUpgradable {
}

// @dev Transact
function transact(bytes calldata data) external onlySelf {
function transact(bytes calldata data) external onlySelf returns (address, uint256) {
uint256 gasLeftBefore = gasleft();

TransactCallParams memory params = abi.decode(data, (TransactCallParams));
address agent = _ensureAgent(params.agentID);
bytes memory call =
Expand All @@ -401,6 +419,9 @@ contract Gateway is IGateway, IInitializable, IUpgradable {
revert AgentTransactCallFailed();
}
emit TransactExecuted(params.agentID, params.target, params.payload);

uint256 gasUsed = gasLeftBefore - gasleft();
return (agent, gasUsed);
}

/**
Expand Down

0 comments on commit 3dcd8ed

Please sign in to comment.