-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
evm-contracts/deployment-audit-xhack #472
Open
swimivan
wants to merge
14
commits into
master
Choose a base branch
from
evm-contracts/deployment-audit-xhack-fixed
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,693
−804
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
07a1f54
fix: Use updated balances when calculating govMintAmount
swimivan fdce66a
feat: SwimFactory now also catches Panic during proxy construction
swimivan 498bcd2
feat: Impl audit feedback, refactor Propeller gas fees, cleanup, rede…
swimivan 7634617
feat: Use Swim packages to further improve code
swimivan bd33c78
feat: add .env example
swimivan b263a7f
test: implement, deploy, and fund faucet contract for test tokens on …
swimivan 66d6015
chore: revisit Propeller deterministic sandwich attack issue
swimivan 4157f6d
fix: add missing yarn.lock
swimivan 9774e6a
fix: Fix solidity lint issues
swimivan ab78615
fix: Fix TypeScript lint issue
swimivan f18e5d3
refactor(evm-contracts): build workspace deps
nicomiicro 9610647
fix(evm-contracts): build command for CI
nicomiicro 4c910bd
fix: Incorporating PR comments
swimivan 9843e55
fix: fix typo in comment
swimivan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
#!/usr/bin/env python3 | ||
|
||
from swim_invariant import SwimPool, Decimal | ||
|
||
token_count = 2 | ||
amp_factor = Decimal(10) | ||
lp_fee = Decimal("0.0003") | ||
gov_fee = Decimal("0.0001") | ||
tolerance = Decimal("0.000001") | ||
base = Decimal(1000000) | ||
abort_search_factor = Decimal("0.00000001") | ||
search_step = Decimal("0.01") | ||
initial_guess = base * Decimal("0.001") | ||
|
||
def setup(): | ||
balances = [Decimal(base) for _ in range(token_count)] | ||
pool = SwimPool(token_count, amp_factor, lp_fee, gov_fee, tolerance) | ||
pool.add(balances) | ||
return pool | ||
|
||
def swap(pool, amount, index): | ||
input = [Decimal(0) for _ in range(token_count)] | ||
input[index] = amount | ||
return pool.swap_exact_input(input, 0 if index == 1 else 1)[0] | ||
|
||
def frontrun(swap_amount, fr_amount, debug=False): | ||
pool = setup() | ||
fr_pre = swap(pool, fr_amount, 0) | ||
user_output = swap(pool, swap_amount, 0) | ||
fr_post = swap(pool, fr_pre, 1) | ||
fr_profit = fr_post - fr_amount | ||
if debug: | ||
print(f" user input: {swap_amount:>10.5f}") | ||
print(f" user output: {user_output:>10.5f}") | ||
print(f"frontrun amount: {fr_amount:>10.5f}") | ||
print(f"frontrun pre: {fr_pre:>10.5f}") | ||
print(f"frontrun post: {fr_post:>10.5f}") | ||
print(f"frontrun profit: {fr_profit:>10.5f}") | ||
print("---------------------") | ||
return [fr_profit, user_output] | ||
|
||
def profitability_threshold(swap_amount, debug=False): | ||
fr_amount = swap_amount | ||
while True: | ||
[fr_profit, user_output] = frontrun(swap_amount, fr_amount, debug) | ||
if fr_profit > 0: | ||
return fr_amount | ||
if user_output < swap_amount * abort_search_factor: | ||
return -1 | ||
fr_amount *= Decimal(1) + search_step | ||
|
||
swap_amount = initial_guess | ||
search_direction = -1 if profitability_threshold(swap_amount) > 0 else 1 | ||
while True: | ||
fr_amount = profitability_threshold(swap_amount) | ||
if search_direction == -1 and fr_amount == -1: | ||
break | ||
if search_direction == 1 and fr_amount > 0: | ||
swap_amount *= 1 / (Decimal(1) + search_direction * search_step) | ||
break | ||
if fr_amount == -1: | ||
print(f"{100*swap_amount/base:.4f} % is not exploitable") | ||
else: | ||
print(f"{100*swap_amount/base:.4f} % exploitable with {100*fr_amount/base:.2f} %") | ||
swap_amount *= Decimal(1) + search_direction * search_step | ||
|
||
fr_amount = profitability_threshold(swap_amount / (Decimal(1) + search_direction * search_step), True) | ||
print() | ||
print("given:") | ||
print(f" token count: {token_count}") | ||
print(f"pool balances: {base} each") | ||
print(f" amp factor: {amp_factor}") | ||
print(f" total fee: {int((lp_fee+gov_fee)*10000)} bips") | ||
print(f"then a swap of {swap_amount:.2f} ({100*swap_amount/base:.3f} % of a pool balance) " + | ||
"is unexploitable\n") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
MNEMONIC=test test test test test test test test test test test junk | ||
FACTORY_MNEMONIC=try exercise column boring supreme corn fabric idea federal today hood equip | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "try exercise" :D |
||
ETHERSCAN_API_KEY= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Love this whole setup