Skip to content
This repository has been archived by the owner on Jul 9, 2021. It is now read-only.

Gas Tokens in Bridges #2523

Merged
merged 3 commits into from
Mar 26, 2020
Merged

Gas Tokens in Bridges #2523

merged 3 commits into from
Mar 26, 2020

Conversation

dekz
Copy link
Member

@dekz dekz commented Mar 13, 2020

Description

When a user makes a trade against a Bridge like Curve (10e5 gas). They spend approximately (10e5 * 8e9) * 1e-18 * $150 $1.20 in gas. We can reduce this by approximately 40% by freeing gas tokens if we have an available balance. So the user only pays $0.72 to fill on Curve.

Calculations are from gastoken.io

// GST2
var FREE_BASE = 14154;
var FREE_TOKEN = 6870;
var REIMBURSE = 24000;
Math.floor((gasUsed + FREE_BASE) / (2 * REIMBURSE - FREE_TOKEN));
bridge gas consumed GST2 freed
Uniswap 3e5 0.03
Oasis 5e5 0.12
Curve 10e5 0.24

freeFrom

Rather than have each bridge use its own balance, we chose to use freeFromUpTo to allow a EOA (GST Collector) to hold the GST balance, preventing a drain if GST2 was traded via a Bridge. This keeps the balances of tokens traded distinct from the gst balance. The collector can whitelist certain contracts, and remove contracts if required. If there is no allowance set it does not revert.

We also don't loose any unspent (and sent) tokens when we re-deploy bridges.

Process

  • GST Collector holds the balance of GST2.
  • User deploys new Bridge Contract specifying the GST Collector as the address to free from
  • Until Collector approves the Bridge Contract, 0 GST will be freed
  • Collector approves Bridge Contract

Notes

  • This does not refund on a revert
  • This does not cover 0x overhead (only bridge overhead)

@dekz dekz requested a review from dorothy-zbornak March 13, 2020 04:47
@dekz dekz force-pushed the feature/gas-token-bridge branch 4 times, most recently from cc10454 to 3c0e590 Compare March 13, 2020 06:04
Copy link
Contributor

@hysz hysz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! (~‾▿‾)~

dekz added 3 commits March 25, 2020 15:18
To handle the case where GST2 is being traded and the balanceOf checks become confusing
@dekz dekz marked this pull request as ready for review March 25, 2020 05:23
@dekz dekz force-pushed the feature/gas-token-bridge branch from 3c0e590 to 1e17841 Compare March 25, 2020 05:24
@coveralls
Copy link

Coverage Status

Coverage increased (+0.05%) to 79.638% when pulling 1e17841 on feature/gas-token-bridge into 99b0a95 on development.

if (address(gst) != address(0)) {
// (gasUsed + FREE_BASE) / (2 * REIMBURSE - FREE_TOKEN)
// 14154 24000 6870
uint256 value = (gasBefore - gasleft() + 14154) / 41130;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This tries to refund the entire amount of gas consumed, right? Maybe we should just try to refund half. I believe a quirk of the EVM is that refunds are capped at half the gas consumed, so any excess might be wasted.

Copy link
Member Author

@dekz dekz Mar 26, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes this calculation gets us to maximum efficiency. You can also play with the calculator (https://gastoken.io/) to confirm.

gasUsed = 2,000,000
value = 48 =  (gasUsed + 14154) / 41130 

SELFDESTRUCT = -24000 (refund)
SELFDESTRUCT_COST = 700 (CALL) + 5000 (SUICIDE)
refund = -878,400 = SELFDESTRUCT * 48 + SELFDESTRUCT_COST * 48


newGasUsed = 1,121,600 > gasUsed * 0.5

note: also a bit of book keeping w.r.t balance updates is excluded

@dekz dekz merged commit 568e9be into development Mar 26, 2020
@dekz dekz deleted the feature/gas-token-bridge branch March 26, 2020 01:58
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants