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

Use a DataNFT to determine and set the start block #807

Open
wants to merge 85 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
85 commits
Select commit Hold shift + click to select a range
7ad52fd
datanft blocktime module and set function
trizin Feb 26, 2024
80c06a1
Add read_data function
trizin Feb 26, 2024
f67e400
Add set_blocknumber_data function
trizin Feb 26, 2024
9340e7f
Add _read_blocknumber_data function
trizin Feb 26, 2024
e549c3f
Add read_blocknumber_data function
trizin Feb 26, 2024
9ffb0b4
Add get_block_number_from_datanft function
trizin Feb 26, 2024
b446293
Chainid as argument
trizin Feb 26, 2024
2b5b19b
Add get_w3_object function
trizin Feb 26, 2024
e1b86e3
Implement use_data_nft into get_st_block
trizin Feb 26, 2024
31c76b7
Add dataNFT blocktime utility functions
trizin Feb 27, 2024
cff6411
Add test
trizin Feb 27, 2024
c4a759e
Add test
trizin Feb 27, 2024
960ee8e
Add test
trizin Feb 27, 2024
09c537a
Add test
trizin Feb 27, 2024
1c061a7
Add test
trizin Feb 27, 2024
2f4571d
Add test
trizin Feb 27, 2024
868d754
Add test
trizin Feb 27, 2024
99194ec
Add test
trizin Feb 27, 2024
3406f8a
Add test
trizin Feb 27, 2024
348ba9e
Add test
trizin Feb 27, 2024
13f6af3
Add fixture
trizin Feb 27, 2024
60e84e8
Formatting
trizin Feb 27, 2024
cdc8b44
Fixes to blocktime
trizin Feb 27, 2024
0ebea09
Refactor block number handling to incorporate week number
trizin Feb 27, 2024
938938f
Update tests to use week number
trizin Feb 27, 2024
c109b15
Formatting
trizin Feb 27, 2024
d3ed3c1
Add support for calculating DF week number in get_st_block function
trizin Feb 27, 2024
1a225ea
Add test_get_st_block test using get_df_week_number
trizin Feb 27, 2024
3b8176a
Add function to get block number from date
trizin Feb 27, 2024
a0ecad4
Use get_blocknumber_from_date function
trizin Feb 27, 2024
d0e1532
Formatting
trizin Feb 27, 2024
03f691d
Add get_block_number_from_datanft function
trizin Feb 27, 2024
8829cfd
Add test for get_fin_block
trizin Feb 27, 2024
f70417a
Optional use_data_nft
trizin Mar 6, 2024
3a8509e
Add use_data_nft
trizin Mar 6, 2024
b46c720
Add do_set_datanft_block_numbers command and update other commands
trizin Mar 6, 2024
82888cc
Formatting
trizin Mar 6, 2024
8e44cef
Rename to get_block_number_from_weeknumber
trizin Mar 6, 2024
f5a976d
Fix linter errors
trizin Mar 6, 2024
586a20e
Move deploy nft to conftest
trizin Mar 8, 2024
e63a2a2
Update tests
trizin Mar 8, 2024
5c6cdbd
Fixes to do_set_datanft_block_numbers
trizin Mar 8, 2024
a209595
Add a test for do_set_datanft_block_numbers
trizin Mar 8, 2024
83963be
Formatting
trizin Mar 8, 2024
c3219db
Enable session start and fix linter
trizin Mar 8, 2024
6640d1f
Linter fixes
trizin Mar 8, 2024
45d1a6c
Update CI
trizin Mar 8, 2024
17aefb2
Set --USE_DATANFT True
trizin Mar 8, 2024
ac4ea4e
Convert to int
trizin Mar 8, 2024
c56a1fd
Add DataNFT address as env var
trizin Mar 8, 2024
9d07038
Add block start and end numbers for test network
trizin Mar 8, 2024
a46f303
Add environment variables for test network
trizin Mar 8, 2024
73db233
Add DEVELOPMENT_RPC_URL
trizin Mar 8, 2024
02ff57b
Use mainnet
trizin Mar 8, 2024
84796ca
Set DEVELOPMENT_RPC_URL
trizin Mar 8, 2024
f72153e
Fix chainid arg
trizin Mar 11, 2024
10aec56
Use get_web3
trizin Mar 11, 2024
f989dd5
Formatting
trizin Mar 11, 2024
db81841
Update rpc url and datanft addr
trizin Mar 11, 2024
7f00d6e
Fix rpc url
trizin Mar 11, 2024
1fa7667
Print statement and fixes
trizin Mar 11, 2024
92e62bb
Use "get_rpc_url"
trizin Mar 11, 2024
0e63c09
Use infura rpc
trizin Mar 11, 2024
19b15f3
Convert to int
trizin Mar 11, 2024
682447a
Correct end week number calc
trizin Mar 11, 2024
02a705e
Remove unused var
trizin Mar 11, 2024
d680a11
Disable Goerli vesting contract check
trizin Mar 11, 2024
2dee898
Add a note
trizin Mar 11, 2024
0c1d0a6
set DFTOOL_KEY in pytest.ini
trizin Mar 11, 2024
fd0b7da
Fixes
trizin Mar 11, 2024
098b11c
Remove subrepo
trizin Mar 11, 2024
e3e703a
Set as default acc
trizin Mar 11, 2024
59c8a24
Convert week number to str
trizin Mar 11, 2024
57c5d98
Enforce types
trizin Mar 11, 2024
9c7f800
Add a test for get_blocknumber_from_date
trizin Mar 11, 2024
1943c28
Add a test for test_get_block_number_from_datanft
trizin Mar 11, 2024
a6c883b
Formatting
trizin Mar 11, 2024
bc207c3
Fix linter error
trizin Mar 11, 2024
57bd2a7
set env vars and convert to int
trizin Mar 11, 2024
7953fe2
Debug
trizin Mar 11, 2024
519087f
Formatting
trizin Mar 11, 2024
4192f3f
Use now ts
trizin Mar 11, 2024
4aff108
Block number 1
trizin Mar 11, 2024
5eaba47
Remove debug print
trizin Mar 11, 2024
026242f
Merge branch 'main' into issue363-bisection-search-causes-inconsisten…
trizin Mar 29, 2024
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
28 changes: 23 additions & 5 deletions .github/workflows/dispense_volume.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ jobs:
echo "GOERLI_RPC_URL=https://rpc.ankr.com/eth_goerli" >> $GITHUB_ENV
echo "INFURA_NETWORKS=polygon,mumbai,mainnet" >> $GITHUB_ENV
echo "SEPOLIA_RPC_URL=https://rpc.sepolia.org/" >> $GITHUB_ENV
echo "DATANFT_ADDR=0xc5eba5234B69dAeC98747c906658AEa31d74b3d2" >> $GITHUB_ENV

- name: Make dftool executable
run: chmod +x dftool
Expand All @@ -90,19 +91,36 @@ jobs:
./dftool get_rate MATIC $date $now $CSV_DIR --RETRIES $RETRY_TIMES
./dftool get_rate ROSE $date $now $CSV_DIR --RETRIES $RETRY_TIMES

- name: Set block start and end numbers
if: github.event_name == 'schedule'
run: |
./dftool set_datanft_block_numbers $date $now 1
./dftool set_datanft_block_numbers $date $now 137
./dftool set_datanft_block_numbers $date $now 23294


- name: Set block start and end numbers for test network
if: github.event_name != 'schedule'
env:
DATANFT_ADDR: "0x23D41CBdceF94132edB21b7f2ae58c959B0C9bD9"
POLYGON_RPC_URL: "https://polygon-mumbai.infura.io/v3/"
run: |
./dftool set_datanft_block_numbers $date $now 1


- name: Run dftool volsym
run: |
./dftool volsym $date $now 200 $CSV_DIR 1 --RETRIES $RETRY_TIMES
./dftool volsym $date $now 200 $CSV_DIR 137 --RETRIES $RETRY_TIMES
./dftool volsym $date $now 200 $CSV_DIR 23294 --RETRIES $RETRY_TIMES
./dftool volsym $date $now 200 $CSV_DIR 1 --RETRIES $RETRY_TIMES --USE_DATANFT True
./dftool volsym $date $now 200 $CSV_DIR 137 --RETRIES $RETRY_TIMES --USE_DATANFT True
./dftool volsym $date $now 200 $CSV_DIR 23294 --RETRIES $RETRY_TIMES --USE_DATANFT True

- name: Run dftool vebals
run: |
./dftool vebals $date $now 200 $CSV_DIR 1 --RETRIES $RETRY_TIMES
./dftool vebals $date $now 200 $CSV_DIR 1 --RETRIES $RETRY_TIMES --USE_DATANFT True

- name: Run dftool allocations
run: |
./dftool allocations $date $now 200 $CSV_DIR 1 --RETRIES $RETRY_TIMES
./dftool allocations $date $now 200 $CSV_DIR 1 --RETRIES $RETRY_TIMES --USE_DATANFT True

- name: Run dftool predictoor_data
run: |
Expand Down
14 changes: 13 additions & 1 deletion df_py/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

from df_py.util import networkutil, oceantestutil
from df_py.util.base18 import to_wei
from df_py.util.oceanutil import OCEAN_token, record_dev_deployed_contracts
from df_py.util.oceanutil import (
OCEAN_token,
create_data_nft,
record_dev_deployed_contracts,
)


def pytest_sessionstart():
Expand Down Expand Up @@ -30,3 +34,11 @@ def account0():
@pytest.fixture
def all_accounts():
return oceantestutil.get_all_accounts()


@pytest.fixture
# pylint: disable=redefined-outer-name
def nft_addr(w3, account0, monkeypatch):
data_NFT = create_data_nft(w3, "1", "1", account0)
monkeypatch.setenv("DATANFT_ADDR", data_NFT.contract.address)
return data_NFT.contract.address
6 changes: 4 additions & 2 deletions df_py/util/blockrange.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,10 @@ def __str__(self):
)


def create_range(web3, st, fin, samples, rndseed) -> BlockRange:
st_block, fin_block = get_st_fin_blocks(web3, st, fin)
def create_range(
web3, st, fin, samples, rndseed, use_data_nft: bool = False
) -> BlockRange:
st_block, fin_block = get_st_fin_blocks(web3, st, fin, use_data_nft)
rng = BlockRange(st_block, fin_block, samples, rndseed, web3=web3)
rng.filter_by_max_block(web3.eth.get_block("latest").number - 4)

Expand Down
46 changes: 37 additions & 9 deletions df_py/util/blocktime.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
from scipy import optimize
from web3.main import Web3

from df_py.util.datanft_blocktime import (
get_blocknumber_from_date,
)


@enforce_types
def get_block_number_thursday(web3) -> int:
Expand Down Expand Up @@ -248,22 +252,46 @@ def eth_find_closest_block(


@enforce_types
def get_fin_block(web3, FIN):
def get_block_number_from_datanft(web3, timestamp: int) -> int:
date = datetime.fromtimestamp(timestamp)
block_number = get_blocknumber_from_date(web3, date)
block_found = web3.eth.get_block(block_number)
block_timestamp = block_found.timestamp
if abs(block_timestamp - timestamp) > 60 * 15:
print("The recorded block number is too far from the target")
print("Canceling use_data_nft")
return 0
return block_number


@enforce_types
def get_fin_block(web3, FIN, use_data_nft: bool = False):
fin_block = 0
if FIN == "latest":
fin_block = web3.eth.get_block("latest").number - 4
elif FIN == "thu":
fin_block = get_block_number_thursday(web3)
elif "-" in str(FIN):
return web3.eth.get_block("latest").number - 4
if FIN == "thu":
return get_block_number_thursday(web3)
if use_data_nft:
timestamp = int(timestr_to_timestamp(FIN) if "-" in str(FIN) else int(FIN))
block_number = get_block_number_from_datanft(web3, timestamp)
if block_number != 0:
return block_number
if "-" in str(FIN):
fin_block = timestr_to_block(web3, FIN)
else:
fin_block = int(FIN)
return fin_block


@enforce_types
def get_st_block(web3, ST):
def get_st_block(web3, ST, use_data_nft: bool = False):
st_block = 0

if use_data_nft:
timestamp = timestr_to_timestamp(ST) if "-" in str(ST) else int(ST)
block_number = get_block_number_from_datanft(web3, int(timestamp))
if block_number != 0:
return block_number
if "-" in str(ST):
st_block = timestr_to_block(web3, ST)
else:
Expand All @@ -272,7 +300,7 @@ def get_st_block(web3, ST):


@enforce_types
def get_st_fin_blocks(web3, ST, FIN):
st_block = get_st_block(web3, ST)
fin_block = get_fin_block(web3, FIN)
def get_st_fin_blocks(web3, ST, FIN, use_data_nft: bool = False):
st_block = get_st_block(web3, ST, use_data_nft)
fin_block = get_fin_block(web3, FIN, use_data_nft)
return (st_block, fin_block)
96 changes: 96 additions & 0 deletions df_py/util/datanft_blocktime.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import os
import json
from datetime import datetime
from typing import Dict, Union

from enforce_typing import enforce_types
from web3 import Web3

from df_py.volume.reward_calculator import get_df_week_number
from df_py.util.contract_base import ContractBase
from df_py.util.web3 import get_web3, get_rpc_url


@enforce_types
def _set_data(w3, nft_addr: str, field_label: str, data: str) -> bool:
field_label_hash = Web3.keccak(text=field_label)
field_value_bytes = data.encode()
contract_instance = ContractBase(w3, "ERC721Template", nft_addr)
tx = contract_instance.contract.functions.setNewData(
field_label_hash, field_value_bytes
).transact()
receipt = w3.eth.wait_for_transaction_receipt(tx)
return receipt is not None and receipt["status"] == 1


@enforce_types
def _read_data(w3, nft_addr: str, field_label: str) -> str:
field_label_hash = Web3.keccak(text=field_label)
contract_instance = ContractBase(w3, "ERC721Template", nft_addr)
value = contract_instance.contract.functions.getData(field_label_hash).call()
value_str = value.decode("utf-8")
return value_str


@enforce_types
def _get_w3_object():
# DataNFT that holds the block numbers is deployed on Polygon
w3 = get_web3(get_rpc_url("polygon"))
return w3


@enforce_types
def _set_blocknumber_data(
nft_addr: str,
from_account,
blocknumbers: Dict[str, Dict[str, int]],
week_number: str,
w3=None,
) -> bool:
w3 = _get_w3_object() if w3 is None else w3
w3.eth.default_account = from_account
# w3.middleware_onion.add(construct_sign_and_send_raw_middleware(from_account))
data = json.dumps(blocknumbers)
return _set_data(w3, nft_addr, week_number, data)


@enforce_types
def _read_blocknumber_data(
nft_addr: str, week_number: str, w3
) -> Dict[str, Dict[str, int]]:
w3 = _get_w3_object() if w3 is None else w3
data = _read_data(w3, nft_addr, week_number)
if data == "":
return {}
return json.loads(data)


@enforce_types
def get_block_number_from_weeknumber(
chainid: Union[str, int], week_number: Union[str, int], w3=None
) -> int:
data = _read_blocknumber_data(os.getenv("DATANFT_ADDR"), str(week_number), w3)
week_number = str(week_number)
if week_number not in data:
return 0
return data[week_number].get(str(chainid), 0)


@enforce_types
def set_blocknumber_to_datanft(
chainid: int, from_account, blocknumber: int, week_number: Union[str, int], w3=None
) -> bool:
nft_addr = os.getenv("DATANFT_ADDR")
week_number = str(week_number)
data = _read_blocknumber_data(nft_addr, week_number, w3)
if week_number not in data:
data[week_number] = {}
data[week_number][chainid] = blocknumber
return _set_blocknumber_data(nft_addr, from_account, data, str(week_number), w3)


@enforce_types
def get_blocknumber_from_date(w3, date: datetime) -> int:
df_week = get_df_week_number(date)
chainid = w3.eth.chain_id
return get_block_number_from_weeknumber(chainid, df_week)
Loading
Loading