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

web3: initialize hyperdrive #604

Merged
merged 21 commits into from
Jul 12, 2023
Merged

Conversation

dpaiton
Copy link
Contributor

@dpaiton dpaiton commented Jun 29, 2023

This adds necessary functions for initializing the Hyperdrive contract. A followup PR will reorganize the code according to #598 and break up contract_interfaces.py. I will also add additional documentation at that time.

Here is an example script for creating & funding a test account, and then using this account to initialize hyperdrive:

from fixedpointmath import FixedPoint
from web3.contract.contract import Contract

from elfpy.data import contract_interface as ci

# define constants
ETHEREUM_NODE = "http://localhost:8545"
CONTRACTS_URL = "http://localhost:80/addresses.json"
BUILD_FOLDER = "./hyperdrive_solidity/.build"
HYPERDRIVE_ABI = "IHyperdrive"
BASE_ABI = "ERC20Mintable"

initial_supply = int(10e18)  # wei
initial_apr = int(0.1e18)
extra_entropy = "BEEP BOOP"

# generate the contract object
web3 = ci.initialize_web3_with_http_provider(ETHEREUM_NODE)
tx_receipt = web3.provider.make_request(method="anvil_reset", params=[])
print(f"\n{web3.eth.gas_price=}")
print(f"{web3.eth.accounts=}")

# get abis & addresses from the Hyperdrive ABI
hyperdrive_abis = ci.load_all_abis(BUILD_FOLDER)
addresses = ci.fetch_address_from_url(CONTRACTS_URL)
print(f"\nhyperdrive contract {addresses=}")

# set up the ERC20 contract for minting base tokens
base_token_contract: Contract = web3.eth.contract(abi=hyperdrive_abis[BASE_ABI], address=addresses.base_token)
print(f"{base_token_contract.functions.totalSupply().call()=}")

# set up a test account for initializing the hyperdrive market
test_account = ci.TestAccount(extra_entropy)

# check test account ether balance
test_ether_balance = ci.get_account_balance_from_provider(web3, test_account.checksum_address)
print(f"\nether balance for acccount {test_account.checksum_address=}:\n\t{test_ether_balance=}")
# fund test account with ether
rpc_response = ci.set_account_balance(web3, test_account.checksum_address, int(web3.to_wei(1000, "ether")))
# check ether balance again
test_ether_balance = ci.get_account_balance_from_provider(web3, test_account.checksum_address)
print(f"ether balance for acccount {test_account.checksum_address=}:\n\t{test_ether_balance=}")

# check token balance
test_token_balance = ci.smart_contract_read(base_token_contract, "balanceOf", test_account.checksum_address)
print(f"token balance for acccount {test_account.checksum_address=}:\n\t{test_token_balance=}")
# fund test account by minting with the ERC20 base account
tx_receipt = ci.mint_tokens(base_token_contract, test_account.checksum_address, initial_supply)
# check token balance again
test_token_balance = ci.smart_contract_read(base_token_contract, "balanceOf", test_account.checksum_address)
print(f"token balance AFTER funding for account {test_account.checksum_address=}:\n\t{test_token_balance=}")

# set up hyperdrive contract
hyperdrive_contract: Contract = web3.eth.contract(
    abi=hyperdrive_abis[HYPERDRIVE_ABI],
    address=addresses.mock_hyperdrive,
)

# approve hyperdrive_contract to withdraw test_account's tokens from base_contract
tx_receipt = ci.smart_contract_transact(
    web3, base_token_contract, "approve", test_account, hyperdrive_contract.address, initial_supply
)

# initialize hyperdrive
# NOTE: this will not work if using the infra deployment because hyperdrive is already innitialized
tx_receipt = ci.smart_contract_transact(
   web3,
   hyperdrive_contract,
   "initialize",
   test_account,
   initial_supply,
   initial_apr,
   test_account.checksum_address,
   True,
)

# test_account should have lost some money
test_token_balance = ci.smart_contract_read(base_token_contract, "balanceOf", test_account.checksum_address)
print(f"token balance initializing hyperdrive for account {test_account.checksum_address=}:\n\t{test_token_balance=}")

# amount of share_reserves in the pool should equal the initial_supply * share_price
pool_info_data_dict = ci.smart_contract_read(hyperdrive_contract, "getPoolInfo")
share_reserves = FixedPoint(scaled_value=pool_info_data_dict["shareReserves"])
share_price = FixedPoint(scaled_value=pool_info_data_dict["sharePrice"])
initial_supply = FixedPoint(scaled_value=initial_supply)
print(f"\n{pool_info_data_dict=}")
print(f"\n{share_reserves=}")
print(f"{initial_supply * share_price=}")
print(f"{(share_reserves - (initial_supply * share_price))=}")

Additionally (just for record), one can access the provided Anvil accounts this way:

# make meta account for paying for approval gas
meta_account = web3.eth.accounts[0]

# check token blance
meta_token_balance = ci.get_account_balance_from_contract(base_token_contract, meta_account)
print(f"\nbase token balance for {meta_account=}:\n\t{meta_token_balance=}")
# check meta account before setting
meta_ether_balance = ci.get_account_balance_from_provider(web3, meta_account)
print(f"ether balance for {meta_account=}:\n\t{meta_ether_balance=}")

# set and check again
meta_funding_amount = int(web3.to_wei(148, "ether"))
rpc_response = ci.set_account_balance(web3, meta_account, meta_funding_amount)
meta_ether_balance = ci.get_account_balance_from_provider(web3, meta_account)
print(f"ether balance after setting for {meta_account=}:\n\t{meta_ether_balance=}")

@vercel
Copy link

vercel bot commented Jun 29, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
elf-simulations ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jul 12, 2023 6:18pm

@codecov-commenter
Copy link

codecov-commenter commented Jun 29, 2023

Codecov Report

Patch coverage has no change and project coverage change: +3.25 🎉

Comparison is base (92235e8) 71.85% compared to head (2f4dc59) 75.11%.

❗ Current head 2f4dc59 differs from pull request most recent head a451b6e. Consider uploading reports for the commit a451b6e to get more accurate results

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #604      +/-   ##
==========================================
+ Coverage   71.85%   75.11%   +3.25%     
==========================================
  Files          54       55       +1     
  Lines        2903     3359     +456     
==========================================
+ Hits         2086     2523     +437     
- Misses        817      836      +19     
Flag Coverage Δ
unittests 75.11% <ø> (+3.25%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

see 41 files with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

@dpaiton dpaiton force-pushed the dpaiton/web3-initialize-hyperdrive branch from 2f4dc59 to f116353 Compare June 30, 2023 18:33
@dpaiton dpaiton force-pushed the dpaiton/web3-initialize-hyperdrive branch from f116353 to a451b6e Compare June 30, 2023 18:57
@dpaiton dpaiton linked an issue Jun 30, 2023 that may be closed by this pull request
@dpaiton dpaiton force-pushed the dpaiton/web3-initialize-hyperdrive branch from a451b6e to b432f1e Compare July 1, 2023 00:23
@dpaiton dpaiton force-pushed the dpaiton/web3-initialize-hyperdrive branch from cfcd078 to f92a70c Compare July 5, 2023 21:47
@dpaiton dpaiton force-pushed the dpaiton/web3-initialize-hyperdrive branch from f92a70c to 6beae41 Compare July 5, 2023 22:17
@dpaiton dpaiton force-pushed the dpaiton/web3-initialize-hyperdrive branch from 6beae41 to 100ecf4 Compare July 5, 2023 22:53
@dpaiton dpaiton force-pushed the dpaiton/web3-initialize-hyperdrive branch from 34e0009 to cf800e5 Compare July 10, 2023 19:59
@github-actions
Copy link

Remaining rate limit: 4980 🚀

@github-actions
Copy link

Remaining rate limit: 4988 🚀

@github-actions
Copy link

Remaining rate limit: 4998 🚀

@github-actions
Copy link

Remaining rate limit: 4967 🚀

Copy link
Contributor

@sentilesdal sentilesdal left a comment

Choose a reason for hiding this comment

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

Looks good, request for some TODOs to add tests. Happy to add these myself today.

Copy link
Contributor

@sentilesdal sentilesdal left a comment

Choose a reason for hiding this comment

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

Looks good, request for some TODOs to add tests. Happy to add these myself today.

@dpaiton dpaiton merged commit ef0b759 into main Jul 12, 2023
@dpaiton dpaiton deleted the dpaiton/web3-initialize-hyperdrive branch July 12, 2023 19:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

web3py: initialize hyperdrive
4 participants