Skip to content

Commit

Permalink
test in prod rate provider; arbitrum
Browse files Browse the repository at this point in the history
  • Loading branch information
bout3fiddy committed Jul 12, 2024
1 parent 6a52a66 commit 095d062
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 37 deletions.
88 changes: 51 additions & 37 deletions contracts/RateProvider.vy
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,25 @@
# pragma evm-version paris
"""
@title CurveRateProvider
@custom:version 1.1.0
@custom:version 1.0.0
@author Curve.Fi
@license Copyright (c) Curve.Fi, 2020-2024 - all rights reserved
@notice Provides quotes for coin pairs, iff coin pair is in a Curve AMM
that the Metaregistry recognises.
@dev Rate contract calls metaregistry to fetch a list of coins for coin_a and coin_b via: metaregistry.find_pools_for_coins
Rate contract gets coin indices from metaregistry.get_coin_indices
If pool is stableswap (check if pool has gamma parameter], then step 2 returns is_underlying as True in the Tuple output.
The rate contract calls get_dy or get_dy_underlying for each pool and coin indices list.
The rate contract compiles this into a list of quotes.
@notice Provides quotes for coin pairs, iff coin pair is in a Curve AMM that the Metaregistry recognises.
"""

version: public(constant(String[8])) = "1.1.0"
version: public(constant(String[8])) = "1.0.0"

MAX_COINS: constant(uint256) = 8
MAX_QUOTES: constant(uint256) = 100

struct Quote:

source_token_index: uint256
dest_token_index: uint256
is_underlying: bool

amount_out: address

amount_out: uint256
pool: address

pool_balances: DynArray[uint256, MAX_COINS]

# 0 for stableswap, 1 for cryptoswap, 2 for LLAMMA.
pool_type: uint8
pool_type: uint8 # 0 for stableswap, 1 for cryptoswap, 2 for LLAMMA.


# Interfaces
Expand All @@ -50,7 +38,9 @@ interface Metaregistry:

ADDRESS_PROVIDER: public(immutable(AddressProvider))
METAREGISTRY_ID: constant(uint256) = 7

STABLESWAP_META_ABI: constant(String[64]) = "get_dy_underlying(int128,int128,uint256)"
STABLESWA_ABI: constant(String[64]) = "get_dy(int128,int128,uint256)"
CRYPTOSWAP_ABI: constant(String[64]) = "get_dy(uint256,uint256,uint256)"

@external
def __init__(address_provider: address):
Expand All @@ -76,34 +66,58 @@ def get_quotes(source_token: address, destination_token: address, amount_in: uin
pool_type: uint8 = self._get_pool_type(pool, metaregistry)

# get coin indices
i: uint256 = 0
j: uint256 = 0
is_underlying: uint256 = False
i: int128 = 0
j: int128 = 0
is_underlying: bool = False
(i, j, is_underlying) = metaregistry.get_coin_indices(pool, source_token, destination_token)

# get balances
balances: uint256[MAX_COINS] = metaregistry.get_underlying_balances(pool)

# if pool is too small, dont post call and skip pool:
if balances[source_token_index] <= amount_in:
if balances[i] <= amount_in:
continue

# convert to Dynamic Arrays:
dyn_balances: DynArray[uint256, MAX_COINS] = []
for bal in balances:
if bal > 0:
dyn_balances.append(bal)

# do a get_dy call and only save quote if call does not bork; use correct abi (in128 vs uint256)
success: bool = False
response: Bytes[32] = b""

method_abi: String[50] = ""
if pool_type == 0 and is_stableswap_metapool:
method_abi = "get_dy_underlying(int128,int128,uint256)"
if pool_type == 0 and is_underlying:
success, response = raw_call(
pool,
concat(
method_id(STABLESWAP_META_ABI),
convert(i, bytes32),
convert(j, bytes32),
convert(amount_in, bytes32),
),
max_outsize=32,
revert_on_failure=False,
is_static_call=True
)
elif pool_type == 0 and not is_underlying:
method_abi = "get_dy(int128,int128,uint256)"
success, response = raw_call(
pool,
concat(
method_id(STABLESWA_ABI),
convert(i, bytes32),
convert(j, bytes32),
convert(amount_in, bytes32),
),
max_outsize=32,
revert_on_failure=False,
is_static_call=True
)
else:
method_abi = "get_dy(uint256,uint256,uint256)"

success, response = raw_call(
success, response = raw_call(
pool,
concat(
method_id(method_abi),
method_id(CRYPTOSWAP_ABI),
convert(i, bytes32),
convert(j, bytes32),
convert(amount_in, bytes32),
Expand All @@ -118,12 +132,12 @@ def get_quotes(source_token: address, destination_token: address, amount_in: uin
quotes.append(
Quote(
{
source_token_index: i,
dest_token_index: j,
source_token_index: convert(i, uint256),
dest_token_index: convert(j, uint256),
is_underlying: is_underlying,
amount_out: convert(response, uint256),
pool: pool,
pool_balances: balances,
pool_balances: dyn_balances,
pool_type: pool_type
}
)
Expand All @@ -135,7 +149,7 @@ def get_quotes(source_token: address, destination_token: address, amount_in: uin

@internal
@view
def _get_pool_type(pool: address, metaregistry: Metaregistry) -> uint8
def _get_pool_type(pool: address, metaregistry: Metaregistry) -> uint8:
# 0 for stableswap, 1 for cryptoswap, 2 for LLAMMA.

success: bool = False
Expand All @@ -144,7 +158,7 @@ def _get_pool_type(pool: address, metaregistry: Metaregistry) -> uint8
# check if cryptoswap
success, response = raw_call(
pool,
method_id("allowed_extra_profit"),
method_id("allowed_extra_profit()"),
max_outsize=32,
revert_on_failure=False,
is_static_call=True
Expand All @@ -155,7 +169,7 @@ def _get_pool_type(pool: address, metaregistry: Metaregistry) -> uint8
# check if llamma
success, response = raw_call(
pool,
method_id("get_rate_mul"),
method_id("get_rate_mul()"),
max_outsize=32,
revert_on_failure=False,
is_static_call=True
Expand Down
77 changes: 77 additions & 0 deletions scripts/deploy_rate_provider.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# flake8: noqa

import os
import sys

import boa
import yaml
from boa.network import NetworkEnv
from eth.constants import ZERO_ADDRESS
from eth_account import Account
from rich import console as rich_console

sys.path.append("./")
from scripts.deploy_addressprovider_and_setup import fetch_url
from scripts.legacy_base_pools import base_pools as BASE_POOLS
from scripts.utils.constants import FIDDY_DEPLOYER

console = rich_console.Console()

ZERO_ADDRESS = "0x0000000000000000000000000000000000000000"
ADDRESS_PROVIDER = (
"0x5ffe7FB82894076ECB99A30D6A32e969e6e35E98" # gets replaced for zksync
)


def main(network, fork, url):
if network == "zksync":
if not fork:
boa_zksync.set_zksync_env(url)
console.log("Prodmode on zksync Era ...")
else:
boa_zksync.set_zksync_fork(url)
console.log("Forkmode on zksync Era ...")

boa.env.set_eoa(Account.from_key(os.environ["FIDDYDEPLOYER"]))

else:
if fork:
boa.env.fork(url)
console.log("Forkmode ...")
boa.env.eoa = FIDDY_DEPLOYER # set eoa address here
else:
console.log("Prodmode ...")
boa.set_env(NetworkEnv(url))
boa.env.add_account(Account.from_key(os.environ["FIDDYDEPLOYER"]))

address_provider = boa.load_partial("contracts/AddressProviderNG.vy").at(
ADDRESS_PROVIDER
)
console.log("Deploying rate provider ...")
rate_provider = boa.load(
"contracts/RateProvider.vy", address_provider.address
)
console.log("Adding rate provider to address provider")
address_provider.add_new_id(
18, rate_provider.address, "Spot Rate Provider"
)


if __name__ == "__main__":
network = "arbitrum"
url = ""
fork = False

if network == "zksync":
import boa_zksync

url = "https://mainnet.era.zksync.io"
ADDRESS_PROVIDER = "0x54A5a69e17Aa6eB89d77aa3828E38C9Eb4fF263D"
elif network == "fraxtal":
network_url = "https://rpc.frax.com"
elif network == "kava":
network_url = "https://rpc.ankr.com/kava_evm"
else:
network_url = fetch_url(network)

main(network, fork, network_url)

0 comments on commit 095d062

Please sign in to comment.