Skip to content
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

WIP: add tests #7

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion ape-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,15 @@ hardhat:
fork:
ethereum:
mainnet:
upstream_provider: infura
upstream_provider: infura

hardhat:
fork:
ethereum:
mainnet:
upstream_provider: infura

ethereum:
default_network: mainnet-fork
mainnet_fork:
default_provider: foundry
1 change: 1 addition & 0 deletions contracts/wbtc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"constant":true,"inputs":[],"name":"mintingFinished","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"name","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_spender","type":"address"},{"name":"_value","type":"uint256"}],"name":"approve","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"_token","type":"address"}],"name":"reclaimToken","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_from","type":"address"},{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transferFrom","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"decimals","outputs":[{"name":"","type":"uint8"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[],"name":"unpause","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_amount","type":"uint256"}],"name":"mint","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"value","type":"uint256"}],"name":"burn","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[],"name":"claimOwnership","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"paused","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_spender","type":"address"},{"name":"_subtractedValue","type":"uint256"}],"name":"decreaseApproval","outputs":[{"name":"success","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"_owner","type":"address"}],"name":"balanceOf","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[],"name":"renounceOwnership","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[],"name":"finishMinting","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[],"name":"pause","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"owner","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"symbol","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transfer","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"_spender","type":"address"},{"name":"_addedValue","type":"uint256"}],"name":"increaseApproval","outputs":[{"name":"success","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"_owner","type":"address"},{"name":"_spender","type":"address"}],"name":"allowance","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"pendingOwner","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"newOwner","type":"address"}],"name":"transferOwnership","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"anonymous":false,"inputs":[],"name":"Pause","type":"event"},{"anonymous":false,"inputs":[],"name":"Unpause","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"burner","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Burn","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"to","type":"address"},{"indexed":false,"name":"amount","type":"uint256"}],"name":"Mint","type":"event"},{"anonymous":false,"inputs":[],"name":"MintFinished","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"previousOwner","type":"address"}],"name":"OwnershipRenounced","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"previousOwner","type":"address"},{"indexed":true,"name":"newOwner","type":"address"}],"name":"OwnershipTransferred","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"owner","type":"address"},{"indexed":true,"name":"spender","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Approval","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"from","type":"address"},{"indexed":true,"name":"to","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Transfer","type":"event"}]
42 changes: 19 additions & 23 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 1 addition & 13 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,5 @@
{
"name": "curve-leverage-bot-vyper",
"version": "1.0.0",
"description": "## factory.vy",
"main": "index.js",
"directories": {
"test": "tests"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"hardhat": "^2.19.1"
"hardhat": "^2.19.4"
}
}
26 changes: 24 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#!/usr/bin/python3

import pytest
from web3 import Web3
from eth_abi import encode
from typing import Union


Expand Down Expand Up @@ -50,6 +52,11 @@ def USDC(project):
return project.usdc.at("0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48")


@pytest.fixture(scope="session")
def WBTC(project):
return project.wbtc.at("0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599")


@pytest.fixture(scope="session")
def UniswapV3Router(project):
return project.uniswap_v3_router.at(
Expand All @@ -73,12 +80,13 @@ def LeverageBotFactory(
BotBlueprint, Compass, CurveRouter, RefundWallet,
ServiceFeeCollector, Deployer, project):
controller_factory = "0xC9332fdCB1C491Dcc683bAe86Fe3cb70360738BC"
gas_fee = 15000000000000000 # 0.015ETH
gas_fee = 25000000000000000 # 0.015ETH
service_fee = 2000000000000000 # 0.2%
return Deployer.deploy(
factory_contract = Deployer.deploy(
project.factory, BotBlueprint.contract_address, Compass,
controller_factory, CurveRouter, RefundWallet, gas_fee,
ServiceFeeCollector, service_fee)
return factory_contract


def get_blueprint_initcode(initcode: Union[str, bytes]):
Expand All @@ -90,3 +98,17 @@ def get_blueprint_initcode(initcode: Union[str, bytes]):
b"\x3d\x81\x60\x0a\x3d\x39\xf3" + initcode
)
return initcode


def set_paloma(project, factory_contract, Compass):
data = function_signature("set_paloma()") + bstring2bytes32(b"paloma")
tx = project.provider.network.ecosystem.create_transaction(chain_id=project.provider.chain_id, to=factory_contract.address, data=data)
Compass.call(tx)


def bstring2bytes32(str):
return encode(["bytes32"], [str])


def function_signature(str):
return Web3.keccak(text=str)[:4]
163 changes: 163 additions & 0 deletions tests/test_leverage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
#!/usr/bin/python3

from conftest import function_signature, encode, bstring2bytes32, set_paloma
import ape


def test_set_paloma(project, LeverageBotFactory, Alice, Compass):
with ape.reverts():
LeverageBotFactory.set_paloma(sender=Alice)
with ape.reverts():
LeverageBotFactory.set_paloma(sender=Compass)
set_paloma(project, LeverageBotFactory, Compass)


def test_deposit(project, LeverageBotFactory, UniswapV3Router, WBTC, WETH, Alice, Compass):
set_paloma(project, LeverageBotFactory, Compass)
UniswapV3Router.exactInputSingle(
[WETH, WBTC, 3000, Alice, 2 ** 32, 10 ** 18, 2 * 10 ** 4, 0],
sender=Alice,
value=10 ** 18)
WBTC.approve(LeverageBotFactory, 2 * 10 ** 4, sender=Alice)
swap_infos = [[
[
'0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599',
'0x0000000000000000000000000000000000000000',
'0x0000000000000000000000000000000000000000',
'0x0000000000000000000000000000000000000000',
'0x0000000000000000000000000000000000000000',
'0x0000000000000000000000000000000000000000',
'0x0000000000000000000000000000000000000000',
'0x0000000000000000000000000000000000000000',
'0x0000000000000000000000000000000000000000',
'0x0000000000000000000000000000000000000000',
'0x0000000000000000000000000000000000000000',
],
[
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
],
20000,
20000,
[
'0x0000000000000000000000000000000000000000',
'0x0000000000000000000000000000000000000000',
'0x0000000000000000000000000000000000000000',
'0x0000000000000000000000000000000000000000',
'0x0000000000000000000000000000000000000000',
]]]
collateral = '0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599'
debt = 30000000000000000000
N = 10
callbacker = '0xa2518b71ee64e910741f5cf480b19e8e402de4d7'
callback_args = [1, 10000]
leverage = 624
deleverage_percentage = 100
health_threshold = 206
expire = 33241352537
number_trades = 2
interval = 30
LeverageBotFactory.create_bot(swap_infos, collateral, debt, N, callbacker, callback_args, leverage, deleverage_percentage, health_threshold, expire, number_trades, interval, sender=Alice, value=5 * 10 ** 16)


def test_create_next_bot(project, LeverageBotFactory, UniswapV3Router, WBTC, WETH, Alice, Compass):
set_paloma(project, LeverageBotFactory, Compass)
UniswapV3Router.exactInputSingle(
[WETH, WBTC, 3000, Alice, 2 ** 32, 10 ** 18, 2 * 10 ** 4, 0],
sender=Alice,
value=10 ** 18)
WBTC.approve(LeverageBotFactory, 2 * 10 ** 4, sender=Alice)
swap_infos = [[
[
'0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599',
'0x0000000000000000000000000000000000000000',
'0x0000000000000000000000000000000000000000',
'0x0000000000000000000000000000000000000000',
'0x0000000000000000000000000000000000000000',
'0x0000000000000000000000000000000000000000',
'0x0000000000000000000000000000000000000000',
'0x0000000000000000000000000000000000000000',
'0x0000000000000000000000000000000000000000',
'0x0000000000000000000000000000000000000000',
'0x0000000000000000000000000000000000000000',
],
[
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
],
20000,
20000,
[
'0x0000000000000000000000000000000000000000',
'0x0000000000000000000000000000000000000000',
'0x0000000000000000000000000000000000000000',
'0x0000000000000000000000000000000000000000',
'0x0000000000000000000000000000000000000000',
]]]
collateral = '0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599'
debt = 30000000000000000000
N = 10
callbacker = '0xa2518b71ee64e910741f5cf480b19e8e402de4d7'
callback_args = [1, 10000]
leverage = 624
deleverage_percentage = 100
health_threshold = 206
expire = 33241352537
number_trades = 2
interval = 30
LeverageBotFactory.create_bot(swap_infos, collateral, debt, N, callbacker, callback_args, leverage, deleverage_percentage, health_threshold, expire, number_trades, interval, sender=Alice, value=5 * 10 ** 16)
deposit_id = 0
callbacker = '0xa2518b71ee64e910741f5cf480b19e8e402de4d7'
callback_args = [1, 10000]
remaining_count = 1
data = function_signature("create_next_bot(uint256,address,uint256[],uint256)") + encode(["uint256", "address", "uint256[]", "uint256"], [deposit_id, callbacker, callback_args, remaining_count]) + bstring2bytes32(b"paloma")
tx = project.provider.network.ecosystem.create_transaction(chain_id=project.provider.chain_id, to=LeverageBotFactory.address, data=data)
Compass.call(tx)


def test_update_compass(project, LeverageBotFactory, Bob, Compass):
set_paloma(project, LeverageBotFactory, Compass)
data = LeverageBotFactory.update_compass.encode_input(Bob) + bstring2bytes32(b"paloma")
tx = project.provider.network.ecosystem.create_transaction(chain_id=project.provider.chain_id, to=LeverageBotFactory.address, data=data)
Compass.call(tx)


def test_update_blueprint(project, LeverageBotFactory, Bob, Compass):
set_paloma(project, LeverageBotFactory, Compass)
data = LeverageBotFactory.update_blueprint.encode_input(Bob) + bstring2bytes32(b"paloma")
tx = project.provider.network.ecosystem.create_transaction(chain_id=project.provider.chain_id, to=LeverageBotFactory.address, data=data)
Compass.call(tx)


def test_update_refund_wallet(project, LeverageBotFactory, Bob, Compass):
set_paloma(project, LeverageBotFactory, Compass)
data = LeverageBotFactory.update_refund_wallet.encode_input(Bob) + bstring2bytes32(b"paloma")
tx = project.provider.network.ecosystem.create_transaction(chain_id=project.provider.chain_id, to=LeverageBotFactory.address, data=data)
Compass.call(tx)


def test_update_gas_fee(project, LeverageBotFactory, Compass):
set_paloma(project, LeverageBotFactory, Compass)
data = LeverageBotFactory.update_gas_fee.encode_input(10 ** 16) + bstring2bytes32(b"paloma")
tx = project.provider.network.ecosystem.create_transaction(chain_id=project.provider.chain_id, to=LeverageBotFactory.address, data=data)
Compass.call(tx)


def test_update_service_fee_collector(project, LeverageBotFactory, Bob, Compass):
set_paloma(project, LeverageBotFactory, Compass)
data = LeverageBotFactory.update_service_fee_collector.encode_input(Bob) + bstring2bytes32(b"paloma")
tx = project.provider.network.ecosystem.create_transaction(chain_id=project.provider.chain_id, to=LeverageBotFactory.address, data=data)
Compass.call(tx)


def update_service_fee(project, LeverageBotFactory, Compass):
set_paloma(project, LeverageBotFactory, Compass)
data = LeverageBotFactory.update_service_fee.encode_input(10 ** 16) + bstring2bytes32(b"paloma")
tx = project.provider.network.ecosystem.create_transaction(chain_id=project.provider.chain_id, to=LeverageBotFactory.address, data=data)
Compass.call(tx)