use contract/version.json for init contract addrs

This commit is contained in:
Tim
2024-07-12 17:39:37 -04:00
parent 620ef62cb0
commit 875d843536

View File

@@ -4,41 +4,29 @@ import logging
from eth_abi.packed import encode_packed
from eth_utils import keccak, to_bytes, to_checksum_address
from dexorder import config
from dexorder.base.chain import current_chain
from dexorder.contract import ContractProxy
from dexorder.util import hexstr
log = logging.getLogger(__name__)
_factory = {}
_dexorder = {}
_vault_init_code_hash = {}
def _load_chain(chain_id: int):
try:
with open(f'../contract/broadcast/Deploy.sol/{chain_id}/run-latest.json', 'rt') as file:
deployment = json.load(file)
for tx in deployment['transactions']:
if tx['contractName'] == 'VaultFactory':
addr = to_checksum_address(tx['contractAddress'])
_factory[chain_id] = ContractProxy(addr, 'VaultFactory')
log.info(f'VaultFactory {addr}')
elif tx['contractName'] == 'Dexorder':
addr = to_checksum_address(tx['contractAddress'])
_dexorder[chain_id] = DexorderContract(addr)
log.info(f'Dexorder {addr}')
except FileNotFoundError:
log.warning(f'Could not find deployment for chain {chain_id}')
with open('../contract/version.json') as version_file:
version = json.load(version_file)
chain_info = version['chainInfo']
for chain_id, info in chain_info.items():
chain_id = int(chain_id)
_factory[chain_id] = info['factory']
_dexorder[chain_id] = info['dexorder']
_vault_init_code_hash[chain_id] = to_bytes(hexstr=info['vaultInitCodeHash'])
def get_by_chain(d):
chain_id = current_chain.get().id
try:
return d[chain_id]
except KeyError:
_load_chain(chain_id)
return d.get(chain_id)
return d[current_chain.get().id]
def get_factory_contract() -> ContractProxy:
return get_by_chain(_factory)
@@ -46,23 +34,16 @@ def get_factory_contract() -> ContractProxy:
def get_dexorder_contract() -> ContractProxy:
return get_by_chain(_dexorder)
VAULT_INIT_CODE_HASH = None
def get_vault_init_code_hash() -> bytes:
return get_by_chain(_vault_init_code_hash)
def vault_address(owner, num):
global VAULT_INIT_CODE_HASH
if VAULT_INIT_CODE_HASH is None:
with open('../contract/out/Vault.sol/Vault.json', 'rt') as _file:
vault_info = json.load(_file)
VAULT_INIT_CODE_HASH = keccak(to_bytes(hexstr=vault_info['bytecode']['object']))
log.info(f'VAULT_INIT_CODE_HASH {hexstr(VAULT_INIT_CODE_HASH)}')
salt = keccak(encode_packed(['address','uint8'],[owner,num]))
contract_address = keccak(
b"\xff"
+ to_bytes(hexstr=get_factory_contract().address)
+ salt
+ VAULT_INIT_CODE_HASH
+ get_vault_init_code_hash()
).hex()[-40:]
addr = to_checksum_address(contract_address)
# log.debug(f'vault addr {owner} #{num} => {salt.hex()} {VAULT_INIT_CODE_HASH.hex()} = {addr}')
@@ -75,3 +56,5 @@ def VaultContract(addr):
def DexorderContract(addr):
return ContractProxy(addr, 'Dexorder')
print('chain_info', chain_info)