use contract/version.json for init contract addrs
This commit is contained in:
@@ -4,41 +4,29 @@ import logging
|
|||||||
from eth_abi.packed import encode_packed
|
from eth_abi.packed import encode_packed
|
||||||
from eth_utils import keccak, to_bytes, to_checksum_address
|
from eth_utils import keccak, to_bytes, to_checksum_address
|
||||||
|
|
||||||
from dexorder import config
|
|
||||||
from dexorder.base.chain import current_chain
|
from dexorder.base.chain import current_chain
|
||||||
from dexorder.contract import ContractProxy
|
from dexorder.contract import ContractProxy
|
||||||
from dexorder.util import hexstr
|
|
||||||
|
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
_factory = {}
|
_factory = {}
|
||||||
_dexorder = {}
|
_dexorder = {}
|
||||||
|
_vault_init_code_hash = {}
|
||||||
|
|
||||||
|
|
||||||
def _load_chain(chain_id: int):
|
with open('../contract/version.json') as version_file:
|
||||||
try:
|
version = json.load(version_file)
|
||||||
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}')
|
|
||||||
|
|
||||||
|
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):
|
def get_by_chain(d):
|
||||||
chain_id = current_chain.get().id
|
return d[current_chain.get().id]
|
||||||
try:
|
|
||||||
return d[chain_id]
|
|
||||||
except KeyError:
|
|
||||||
_load_chain(chain_id)
|
|
||||||
return d.get(chain_id)
|
|
||||||
|
|
||||||
def get_factory_contract() -> ContractProxy:
|
def get_factory_contract() -> ContractProxy:
|
||||||
return get_by_chain(_factory)
|
return get_by_chain(_factory)
|
||||||
@@ -46,23 +34,16 @@ def get_factory_contract() -> ContractProxy:
|
|||||||
def get_dexorder_contract() -> ContractProxy:
|
def get_dexorder_contract() -> ContractProxy:
|
||||||
return get_by_chain(_dexorder)
|
return get_by_chain(_dexorder)
|
||||||
|
|
||||||
|
def get_vault_init_code_hash() -> bytes:
|
||||||
VAULT_INIT_CODE_HASH = None
|
return get_by_chain(_vault_init_code_hash)
|
||||||
|
|
||||||
|
|
||||||
def vault_address(owner, num):
|
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]))
|
salt = keccak(encode_packed(['address','uint8'],[owner,num]))
|
||||||
contract_address = keccak(
|
contract_address = keccak(
|
||||||
b"\xff"
|
b"\xff"
|
||||||
+ to_bytes(hexstr=get_factory_contract().address)
|
+ to_bytes(hexstr=get_factory_contract().address)
|
||||||
+ salt
|
+ salt
|
||||||
+ VAULT_INIT_CODE_HASH
|
+ get_vault_init_code_hash()
|
||||||
).hex()[-40:]
|
).hex()[-40:]
|
||||||
addr = to_checksum_address(contract_address)
|
addr = to_checksum_address(contract_address)
|
||||||
# log.debug(f'vault addr {owner} #{num} => {salt.hex()} {VAULT_INIT_CODE_HASH.hex()} = {addr}')
|
# 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):
|
def DexorderContract(addr):
|
||||||
return ContractProxy(addr, 'Dexorder')
|
return ContractProxy(addr, 'Dexorder')
|
||||||
|
|
||||||
|
print('chain_info', chain_info)
|
||||||
Reference in New Issue
Block a user