示例#1
0
def setup(make_project, db, configuration: Config, erc20_token):
    configuration.token_contract_addr = erc20_token.address

    eth_token, eth_token_hash = init_token_contract(configuration)
    swap_contract, swap_contract_hash = init_swap_contract(
        configuration, eth_token, eth_token_hash)
    add_minter(eth_token, swap_contract)
    erc_token, erc_token_hash = init_token_contract(configuration,
                                                    swap_contract)

    add_to_whitelist(swap_contract, erc_token, erc_token_hash)

    change_owner(swap_contract, configuration.multisig_acc_addr)

    # add token pairings to db
    TokenPairing(src_network="Ethereum",
                 src_coin="ETH",
                 src_address="native",
                 dst_network="Secret",
                 dst_coin="secret-ETH",
                 dst_address=eth_token,
                 decimals=18,
                 name="ETH").save()
    TokenPairing(src_network="Ethereum",
                 src_coin="ERC",
                 src_address=erc20_token.address,
                 dst_network="Secret",
                 dst_coin="secret-ERC",
                 dst_address=erc_token,
                 decimals=18,
                 name="ERC").save()

    configuration.swap_code_hash = swap_contract_hash
    configuration.scrt_swap_address = swap_contract
示例#2
0
def run_bridge():  # pylint: disable=too-many-statements
    runners = []
    logger = get_logger(logger_name='runner')
    required_configs = ['MODE', 'secret_node', 'multisig_acc_addr', 'chain_id']
    cfg = Config(required=required_configs)
    try:
        configure_secretcli(cfg)
    except RuntimeError:
        logger = get_logger(logger_name='runner')
        logger.error('Failed to set up secretcli')
        sys.exit(1)

    if cfg.get('token', ''):
        signer = Pkcs11CryptoStore(store=cfg["PKCS11_MODULE"],
                                   token=cfg["token"],
                                   user_pin=cfg["user_pin"],
                                   label=cfg.get('label'))
    else:
        signer = LocalCryptoStore(private_key=bytes_from_hex(
            cfg['eth_private_key']),
                                  account=cfg['eth_address'])

    logger.info(f'Starting with ETH address {signer.address}')

    with database(db=cfg['db_name'],
                  host=cfg['db_host'],
                  password=cfg['db_password'],
                  username=cfg['db_username']):

        eth_wallet = MultisigWallet(w3, cfg['multisig_wallet_address'])

        secret_account = SecretAccount(cfg['multisig_acc_addr'],
                                       cfg['secret_key_name'])

        eth_signer = EtherSigner(eth_wallet,
                                 signer,
                                 dst_network="Secret",
                                 config=cfg)
        s20_signer = Secret20Signer(secret_account, eth_wallet, cfg)

        runners.append(eth_signer)
        runners.append(s20_signer)

        if cfg['MODE'].lower() == 'leader':
            eth_leader = EtherLeader(eth_wallet,
                                     signer,
                                     dst_network="Secret",
                                     config=cfg)

            secret_leader = SecretAccount(cfg['multisig_acc_addr'],
                                          cfg['multisig_key_name'])
            s20_leader = Secret20Leader(secret_leader,
                                        eth_wallet,
                                        src_network="Ethereum",
                                        config=cfg)

            runners.append(eth_leader)
            runners.append(s20_leader)

        run_all(runners)
示例#3
0
    def __init__(self, secret_multisig: SecretAccount,
                 contract: MultisigWallet, src_network: str, config: Config,
                 *args, **kwargs):
        super().__init__(*args, **kwargs)

        token_map = {}
        pairs = TokenPairing.objects(dst_network=self.network,
                                     src_network=src_network)
        for pair in pairs:
            token_map.update(
                {pair.src_address: Token(pair.dst_address, pair.dst_coin)})

        self.multisig_name = secret_multisig.name
        self.config = config
        self.manager = SecretManager(contract, token_map, secret_multisig,
                                     config)
        self.logger = get_logger(
            db_name=self.config['db_name'],
            logger_name=config.get(
                'logger_name',
                f"{self.__class__.__name__}-{self.multisig_name}"))
        self.stop_event = Event()

        super().__init__(group=None,
                         name="SecretLeader",
                         target=self.run,
                         **kwargs)
示例#4
0
def swap_eth():

    cfg = Config()

    private_key = "b84db86a570359ca8a16ad840f7495d3d8a1b799b29ae60a2032451d918f3826"  # your private key here
    account = "0xA48e330838A6167a68d7991bf76F2E522566Da33"  # your account here

    with open('./src/contracts/ethereum/compiled/MultiSigSwapWallet.json',
              'r') as f:
        contract_source_code = json.loads(f.read())

    w3 = web3_provider(cfg['eth_node'])
    # multisig_wallet = MultisigWallet(web3_provider, config.multisig_wallet_address)
    multisig_wallet = MultisigWallet(w3, cfg['multisig_wallet_address'])

    class SwapMessage(Message):
        def args(self):
            return "secret13l72vhjngmg55ykajxdnlalktwglyqjqv9pkq4".encode(),

    m = SwapMessage()
    tx_hash = send_contract_tx(multisig_wallet.contract,
                               'swap',
                               account,
                               bytes.fromhex(private_key),
                               value=200,
                               args=m.args())
    print(repr(tx_hash))
示例#5
0
    def __init__(self, contract: MultisigWallet,
                 token_to_secret_map: Dict[str, Token],
                 s20_multisig_account: SecretAccount, config: Config,
                 **kwargs):
        self.contract = contract
        self.s20_map = token_to_secret_map
        self.config = config
        self.multisig = s20_multisig_account
        self.event_listener = EthEventListener(contract, config)

        self.logger = get_logger(
            db_name=self.config['db_name'],
            logger_name=config.get(
                'logger_name',
                f"{self.__class__.__name__}-{self.multisig.name}"))
        self.stop_signal = Event()
        self.account_num = 0
        self.sequence_lock = Lock()
        self.sequence = 0
        self.update_sequence()
        self.event_listener.register(
            self._handle,
            contract.tracked_event(),
        )
        super().__init__(group=None,
                         name="SecretManager",
                         target=self.run,
                         **kwargs)
示例#6
0
def test_3_confirm_and_finalize_eth_tx(web3_provider, ethr_signers,
                                       configuration: Config):
    # To allow the new EthrSigner to "catch up", we start it after the event submission event in Ethereum
    secret_token_addr = TokenPairing.objects().get(src_network="Ethereum",
                                                   src_coin="ETH").dst_address
    prev_bal = web3_provider.eth.getBalance(zero_address, "latest")
    prev_bal_fee = web3_provider.eth.getBalance(PAYABLE_ADDRESS, "latest")
    ethr_signers[-1].start()
    sleep(1)
    assert increase_block_number(web3_provider,
                                 configuration.eth_confirmations)

    sleep(configuration.sleep_interval * 5)
    # Validate the tx is confirmed in the smart contract
    last_nonce = SwapTrackerObject.last_processed(secret_token_addr)
    # ethr_signers[-1].signer.multisig_contract.contract.functions.confirmations(last_nonce,
    #                                                                            ethr_signers[-1].account).call()

    assert last_nonce >= 0

    bal_fee = web3_provider.eth.getBalance(PAYABLE_ADDRESS, "latest")
    assert bal_fee > prev_bal_fee

    bal = web3_provider.eth.getBalance(zero_address, "latest")
    assert bal > prev_bal

    last_nonce = SwapTrackerObject.last_processed(secret_token_addr)

    swap = Swap.objects().get(src_tx_hash=f'{last_nonce}|{secret_token_addr}')
    assert swap.status == Status.SWAP_CONFIRMED
    configuration.eth_start_block = web3_provider.eth.blockNumber
示例#7
0
def add_token(token: str):

    cfg = Config()

    with open('./src/contracts/ethereum/compiled/MultiSigSwapWallet.json',
              'r') as f:
        contract_source_code = json.loads(f.read())

    w3 = web3_provider(cfg['eth_node'])
    account = w3.eth.account.from_key(
        "0xb84db86a570359ca8a16ad840f7495d3d8a1b799b29ae60a2032451d918f3826")

    contract = w3.eth.contract(
        address=cfg['multisig_wallet_address'],
        abi=contract_source_code['abi'],
        bytecode=contract_source_code['data']['bytecode']['object'])

    nonce = w3.eth.getTransactionCount(account.address, "pending")
    tx = contract.functions.addToken(token)
    raw_tx = tx.buildTransaction(transaction={
        'from': account.address,
        'gas': 3000000,
        'nonce': nonce
    })
    signed_tx = account.sign_transaction(raw_tx)
    tx_hash = w3.eth.sendRawTransaction(signed_tx.rawTransaction)
    tx_receipt = w3.eth.waitForTransactionReceipt(tx_hash)
    print(f"Done adding token: {tx_receipt=}")
示例#8
0
    def __init__(self, multisig_wallet: MultisigWallet,
                 signer: CryptoManagerBase, dst_network: str, config: Config,
                 **kwargs):
        self.config = config
        self.multisig_wallet = multisig_wallet
        self.erc20 = erc20_contract()

        token_map = {}
        pairs = TokenPairing.objects(dst_network=dst_network,
                                     src_network=self.network)
        for pair in pairs:
            token_map.update(
                {pair.dst_address: Token(pair.src_address, pair.src_coin)})
        self.signer = signer
        # self.private_key = private_key
        # self.default_account = account
        self.token_map = token_map
        self.logger = get_logger(db_name=self.config['db_name'],
                                 logger_name=config.get(
                                     'logger_name', self.__class__.__name__))
        self.stop_event = Event()
        super().__init__(group=None,
                         name="EtherLeader",
                         target=self.run,
                         **kwargs)
示例#9
0
 def __init__(self, multisig_wallet: MultisigWallet, private_key: bytes, account: str, config: Config, **kwargs):
     self.config = config
     self.multisig_wallet = multisig_wallet
     self.private_key = private_key
     self.default_account = account
     self.logger = get_logger(db_name=self.config['db_name'],
                              logger_name=config.get('logger_name', self.__class__.__name__))
     self.stop_event = Event()
     super().__init__(group=None, name="EtherLeader", target=self.run, **kwargs)
示例#10
0
def ethr_leader(multisig_account, configuration: Config, web3_provider,
                erc20_token, multisig_wallet, ether_accounts):
    configuration.leader_key = ether_accounts[0].key
    configuration.leader_acc_addr = normalize_address(
        ether_accounts[0].address)
    configuration.eth_start_block = web3_provider.eth.blockNumber

    # token_map = configuration["token_map_scrt"]
    signer = LocalCryptoStore(private_key=configuration.leader_key,
                              account=configuration.leader_acc_addr)
    leader = EtherLeader(multisig_wallet,
                         signer,
                         dst_network="Secret",
                         config=configuration)

    leader.start()
    yield leader
    leader.stop()
示例#11
0
def run_bridge():  # pylint: disable=too-many-statements
    runners = []
    required_configs = [
        'SRC_COIN', 'DST_COIN', 'MODE', 'private_key', 'account',
        'secret_node', 'multisig_acc_addr', 'chain_id'
    ]
    cfg = Config(required=required_configs)
    try:
        configure_secretcli(cfg)
    except RuntimeError:
        logger = get_logger(logger_name='runner')
        logger.error('Failed to set up secretcli')
        sys.exit(1)

    with database(db=cfg['db_name'],
                  host=cfg['db_host'],
                  password=cfg['db_password'],
                  username=cfg['db_username']):

        eth_wallet = MultisigWallet(w3, cfg['multisig_wallet_address'])

        private_key = bytes_from_hex(cfg['private_key'])
        account = cfg['account']

        erc20_contract = ''
        secret_account = SecretAccount(cfg['multisig_acc_addr'],
                                       cfg['secret_key_name'])

        if NETWORK_PARAMS[cfg['SRC_COIN']]['type'] == 'erc20':
            token = get_token(cfg['SRC_COIN'], cfg['network'])
            erc20_contract = Erc20(w3, token, eth_wallet.address)
            src_signer = ERC20Signer(eth_wallet, token, private_key, account,
                                     cfg)
            dst_signer = Secret20Signer(erc20_contract, secret_account, cfg)
        else:
            src_signer = EtherSigner(eth_wallet, private_key, account, cfg)
            dst_signer = Secret20Signer(eth_wallet, secret_account, cfg)

        runners.append(src_signer)
        runners.append(dst_signer)

        if cfg['MODE'].lower() == 'leader':
            src_leader = get_leader(cfg['SRC_COIN'], eth_wallet, private_key,
                                    account, cfg)
            if erc20_contract:
                dst_leader = get_leader(cfg['DST_COIN'], erc20_contract,
                                        private_key, account, cfg)
            else:
                dst_leader = get_leader(cfg['DST_COIN'], eth_wallet,
                                        private_key, account, cfg)
            runners.append(src_leader)
            runners.append(dst_leader)

        run_all(runners)
示例#12
0
def configuration():
    # get address of account 'a' on docker
    config = Config(config_file='config/test_config.json')
    a_address = run("docker exec secretdev secretcli keys show a | jq '.address'", shell=True, stdout=PIPE)
    config['a_address'] = a_address.stdout.decode().strip()[1:-1].encode()
    config['multisig_acc_addr'] = get_key_multisig_addr(f"ms{config['signatures_threshold']}")
    config['enclave_key'] = path.join(tests_folder, config["path_to_certs"], 'io-master-cert.der')

    res = run("secretcli query compute list-contract-by-code 1 | jq '.[-1].address'", shell=True, stdout=PIPE)
    config['secret_contract_address'] = res.stdout.decode().strip()[1:-1]

    return config
示例#13
0
    def __init__(self, multisig_wallet: MultisigWallet, private_key: bytes, account: str, config: Config):
        # todo: simplify this, pylint is right
        self.multisig_wallet = multisig_wallet
        self.private_key = private_key
        self.account = account
        self.config = config
        self.logger = get_logger(db_name=config['db_name'],
                                 logger_name=config.get('logger_name', f"{self.__class__.__name__}-{self.account[0:5]}"))

        self.submissions_lock = Lock()
        self.catch_up_complete = False
        self.cache = self._create_cache()

        self.thread_pool = ThreadPoolExecutor()
示例#14
0
def deploy_scrt():

    configuration = Config()

    # docker exec -it secretdev secretcli tx compute store "/token.wasm.gz" --from a --gas 2000000 -b block -y
    #
    # docker exec -it secretdev secretcli tx compute store "/swap.wasm.gz" --from a --gas 2000000 -b block -y

    multisig_account = configuration["multisig_acc_addr"]
    deployer = "secret1qcz0405jctqvar3e5wmlsj2q5vrehgudtv5nqd"

    tx_data = {
        "admin": multisig_account,
        "name": "Coin Name",
        "symbol": "ETHR",
        "decimals": 6,
        "initial_balances": [],
        "config": {},
        "prng_seed": "YWE"
    }

    cmd = f"secretcli tx compute instantiate 1 --label {rand_str(10)} '{json.dumps(tx_data)}'" \
          f" --from t1 -b block -y"
    res = subprocess.run(cmd, shell=True, stdout=subprocess.PIPE)

    res = subprocess.run(
        "secretcli query compute list-contract-by-code 1 | jq '.[-1].address'",
        shell=True,
        stdout=subprocess.PIPE)
    configuration['secret_token_address'] = res.stdout.decode().strip()[1:-1]

    tx_data = {"owner": multisig_account}

    cmd = f"secretcli tx compute instantiate 2 --label {rand_str(10)} '{json.dumps(tx_data)}'" \
          f" --from t1 -b block -y"
    res = subprocess.run(cmd, shell=True, stdout=subprocess.PIPE)

    res = subprocess.run(
        "secretcli query compute list-contract-by-code 2 | jq '.[-1].address'",
        shell=True,
        stdout=subprocess.PIPE)
    configuration['secret_swap_contract_address'] = res.stdout.decode().strip(
    )[1:-1]

    res = subprocess.run(
        f"secretcli q compute contract-hash {configuration['secret_swap_contract_address']}",
        shell=True,
        stdout=subprocess.PIPE).stdout.decode().strip()[2:]
    configuration['code_hash'] = res
示例#15
0
 def __init__(self, contract: EthereumContract, config: Config, **kwargs):
     # Note: each event listener can listen to one contract at a time
     self.id = next(self._ids)
     self.contract = contract
     self.config = config
     self.callbacks = Callbacks()
     self.logger = get_logger(db_name=config['db_name'],
                              logger_name=config.get(
                                  'logger_name',
                                  f"{self.__class__.__name__}-{self.id}"))
     self.events = []
     self.stop_event = Event()
     self.confirmations = config['eth_confirmations']
     super().__init__(group=None,
                      name=f"EventListener-{config.get('logger_name', '')}",
                      target=self.run,
                      **kwargs)
示例#16
0
    def __init__(self, contract: MultisigWallet, private_key: bytes,
                 account: str, config: Config, **kwargs):
        self.account = account
        self.private_key = private_key
        self.event_listener = EthEventListener(contract, config)
        self.stop_event = Event()
        self.logger = get_logger(
            db_name=config['db_name'],
            logger_name=config.get(
                'logger_name',
                f"{self.__class__.__name__}-{self.account[0:5]}"))
        self.signer = EthSignerImpl(contract, self.private_key, self.account,
                                    config)

        super().__init__(group=None,
                         name=f"{self.__class__.__name__}-{self.account[0:5]}",
                         target=self.run,
                         **kwargs)
示例#17
0
    def __init__(self, secret_multisig: SecretAccount, s20_contract: Token,
                 contract: EthereumContract, config: Config, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.multisig_name = secret_multisig.name
        self.config = config
        self.manager = SecretManager(contract, s20_contract, secret_multisig,
                                     config)
        self.logger = get_logger(
            db_name=self.config['db_name'],
            logger_name=config.get(
                'logger_name',
                f"{self.__class__.__name__}-{self.multisig_name}"))
        self.stop_event = Event()

        super().__init__(group=None,
                         name="SecretLeader",
                         target=self.run,
                         **kwargs)
示例#18
0
def deploy_eth():

    cfg = Config()

    with open('./src/contracts/ethereum/compiled/MultiSigSwapWallet.json',
              'r') as f:
        contract_source_code = json.loads(f.read())

    w3 = web3_provider(cfg['eth_node_address'])
    account = w3.eth.account.from_key(
        "0xb84db86a570359ca8a16ad840f7495d3d8a1b799b29ae60a2032451d918f3826")
    print(f"Deploying on {cfg['network']} from address {account.address}")
    balance = w3.eth.getBalance(account.address, "latest")
    if balance < 1000000000000:
        print("You gotta have some cash dawg")
        return

    # Instantiate and deploy contract
    contract = w3.eth.contract(
        abi=contract_source_code['abi'],
        bytecode=contract_source_code['data']['bytecode']['object'])
    tx = contract.constructor(
        signer_accounts,
        cfg['signatures_threshold'],
    )

    nonce = w3.eth.getTransactionCount(account.address, "pending")

    raw_tx = tx.buildTransaction(transaction={
        'from': account.address,
        'gas': 3000000,
        'nonce': nonce
    })

    signed_tx = account.sign_transaction(raw_tx)

    tx_hash = w3.eth.sendRawTransaction(signed_tx.rawTransaction)
    # .transact()
    # Get transaction hash from deployed contract
    tx_receipt = w3.eth.waitForTransactionReceipt(tx_hash)
    print(f"Deployed at: {tx_receipt.contractAddress}")
    multisig_wallet = w3.eth.contract(address=tx_receipt.contractAddress,
                                      abi=contract_source_code['abi'])
    print("All done")
示例#19
0
    def __init__(self, contract: MultisigWallet, signer: CryptoManagerBase,
                 dst_network: str, config: Config, **kwargs):
        self.account = signer.address
        # self.private_key = private_key
        self.event_listener = EthEventListener(contract, config)
        self.stop_event = Event()
        self.logger = get_logger(
            db_name=config['db_name'],
            logger_name=config.get(
                'logger_name',
                f"{self.__class__.__name__}-{self.account[0:5]}"))
        self.config = config
        self.signer = EthSignerImpl(contract, signer, dst_network, config)

        super().__init__(group=None,
                         name=f"{self.__class__.__name__}-{self.account[0:5]}",
                         target=self.run,
                         **kwargs)
        self.setDaemon(True)  # so tests don't hang
示例#20
0
    def __init__(self, multisig_contract: MultisigWallet,
                 signer: CryptoManagerBase, dst_network: str, config: Config):
        # todo: simplify this, pylint is right
        self.multisig_contract = multisig_contract
        self.account = signer.address
        self.signer = signer
        self.config = config
        self.logger = get_logger(
            db_name=config['db_name'],
            logger_name=config.get(
                'logger_name',
                f"{self.__class__.__name__}-{self.account[0:5]}"))

        self.erc20 = erc20_contract()
        self.catch_up_complete = False

        self.token_map = {}
        pairs = TokenPairing.objects(dst_network=dst_network,
                                     src_network=self.network)
        for pair in pairs:
            self.token_map.update(
                {pair.src_address: Token(pair.dst_address, pair.dst_coin)})

        self.tracked_tokens = self.token_map.keys()
示例#21
0
def swap_erc():
    cfg = Config()
    TRANSFER_AMOUNT = 100
    private_key = "b84db86a570359ca8a16ad840f7495d3d8a1b799b29ae60a2032451d918f3826"  # your private key here
    address = "0xA48e330838A6167a68d7991bf76F2E522566Da33"  # your account here

    with open('./src/contracts/ethereum/abi/IERC20.json', 'r') as f:
        contract_source_code = json.loads(f.read())
    #
    w3 = web3_provider(cfg['eth_node'])
    #
    account = w3.eth.account.from_key(private_key)
    nonce = w3.eth.getTransactionCount(account.address, "pending")
    # # multisig_wallet = MultisigWallet(web3_provider, config.multisig_wallet_address)
    multisig_wallet = MultisigWallet(
        w3, "0x03A95ab8A5de93e47b3802cbA6295ebf85f4aA6f")
    #
    try:
        erc20_contract = w3.eth.contract(
            address="0xF6fF95D53E08c9660dC7820fD5A775484f77183A",
            abi=contract_source_code['abi'])
        tx = erc20_contract.functions.approve(multisig_wallet.address,
                                              TRANSFER_AMOUNT)

        raw_tx = tx.buildTransaction(transaction={
            'from': account.address,
            'gas': 3000000,
            'nonce': nonce
        })
        signed_tx = account.sign_transaction(raw_tx)
        tx_hash = w3.eth.sendRawTransaction(signed_tx.rawTransaction)

        # Get transaction hash from deployed contract
        # tx_receipt = w3.eth.waitForTransactionReceipt(tx_hash)
    except Exception:
        pass

    class SwapMessage(Message):
        def args(self):
            return "secret13l72vhjngmg55ykajxdnlalktwglyqjqv9pkq4".encode(
            ), TRANSFER_AMOUNT, "0xF6fF95D53E08c9660dC7820fD5A775484f77183A"

    #         return '0xA48e330838A6167a68d7991bf76F2E522566Da33', 0, 0, '0xA48e330838A6167a68d7991bf76F2E522566Da33', '0xa9059cbb00000000000000000000000055810874c137605b96e9d2b76c3089fcc325ed5d0000000000000000000000000000000000000000000000000000000000000001'

    owners = multisig_wallet.contract.functions.getOwners().call()
    print(owners)

    tokens = multisig_wallet.contract.functions.SupportedTokens().call()
    print(tokens)

    # account = w3.eth.account.from_key("0xb84db86a570359ca8a16ad840f7495d3d8a1b799b29ae60a2032451d918f3826")
    # print(f"Deploying on {cfg['network']} from address {account.address}")
    # balance = w3.eth.getBalance(account.address, "latest")
    # if balance < 1000000000000:
    #     print("You gotta have some cash dawg")
    #     return
    #
    # # Instantiate and deploy contract
    # # contract = w3.eth.contract(abi=contract_source_code['abi'], bytecode=contract_source_code['data']['bytecode']['object'])
    # # tx = contract.constructor(signer_accounts, cfg['signatures_threshold'],)
    #

    ### add token
    # tx = multisig_wallet.contract.functions.addToken('0xF6fF95D53E08c9660dC7820fD5A775484f77183A')
    # nonce = w3.eth.getTransactionCount(account.address, "pending")
    # raw_tx = tx.buildTransaction(transaction={'from': account.address, 'gas': 3000000, 'nonce': nonce})
    # #
    # signed_tx = account.sign_transaction(raw_tx)
    # #
    # tx_hash = w3.eth.sendRawTransaction(signed_tx.rawTransaction)
    # #

    #tokens = multisig_wallet.contract.functions.SupportedTokens().call()
    #print(tokens)
    m = SwapMessage()
    tx_hash = send_contract_tx(multisig_wallet.contract,
                               'swapToken',
                               address,
                               bytes.fromhex(private_key),
                               1000000,
                               value=0,
                               args=m.args())
    print(repr(tx_hash))
示例#22
0
def main():
    args = parse_args()
    gpu_id = args.gpu_id
    train = args.train
    use_cuda = torch.cuda.is_available() and gpu_id > -1
    max_sent_length = 64
    if use_cuda:
        device = torch.device('cuda:{}'.format(gpu_id))
        torch.cuda.set_device(gpu_id)
    else:
        device = torch.device('cpu')

    config = Config(args.config_file)

    if train:
        batch_size = config.batch_size
        n_epochs = config.n_epochs
        word_embed_size = config.word_embed_size
        hidden_size = config.hidden_size
        learning_rate = config.learning_rate
        if not os.path.isfile(args.input_file):
            raise FileNotFoundError

        print('Loading input file')
        counter = 0
        with open(args.input_file) as f:
            sentences = []
            for line in f:
                sentence = line.strip().lower().split()
                if 0 < len(sentence) < max_sent_length:
                    counter += 1

        sentences = np.empty(counter, dtype=object)
        counter = 0
        with open(args.input_file) as f:
            for line in f:
                sentence = line.strip().lower().split()
                if 0 < len(sentence) < max_sent_length:
                    sentences[counter] = np.array(sentence)
                    counter += 1

        print('Creating dataset')
        dataset = Dataset(sentences, batch_size, config.min_freq, device)
        counter = np.array([
            dataset.vocab.freqs[word] if word in dataset.vocab.freqs else 0
            for word in dataset.vocab.itos
        ])
        model = Context2vec(vocab_size=len(dataset.vocab),
                            counter=counter,
                            word_embed_size=word_embed_size,
                            hidden_size=hidden_size,
                            n_layers=config.n_layers,
                            bidirectional=True,
                            use_mlp=config.use_mlp,
                            dropout=config.dropout,
                            pad_index=dataset.pad_index,
                            device=device,
                            inference=False).to(device)
        optimizer = optim.Adam(model.parameters(), lr=learning_rate)

        print(batch_size, n_epochs, word_embed_size, hidden_size, device)
        print(model)

        interval = 1e6
        for epoch in range(n_epochs):
            begin_time = time.time()
            cur_at = begin_time
            total_loss = 0.0
            word_count = 0
            next_count = interval
            last_accum_loss = 0.0
            last_word_count = 0
            for iterator in dataset.get_batch_iter(batch_size):
                for batch in iterator:
                    sentence = getattr(batch, 'sentence')
                    target = sentence[:, 1:-1]
                    if target.size(0) == 0:
                        continue
                    optimizer.zero_grad()
                    loss = model(sentence, target)
                    loss.backward()
                    optimizer.step()
                    total_loss += loss.data.mean()

                    minibatch_size, sentence_length = target.size()
                    word_count += minibatch_size * sentence_length
                    accum_mean_loss = float(
                        total_loss) / word_count if total_loss > 0.0 else 0.0
                    if word_count >= next_count:
                        now = time.time()
                        duration = now - cur_at
                        throuput = float(
                            (word_count - last_word_count)) / (now - cur_at)
                        cur_mean_loss = (float(total_loss) - last_accum_loss
                                         ) / (word_count - last_word_count)
                        print(
                            '{} words, {:.2f} sec, {:.2f} words/sec, {:.4f} accum_loss/word, {:.4f} cur_loss/word'
                            .format(word_count, duration, throuput,
                                    accum_mean_loss, cur_mean_loss))
                        next_count += interval
                        cur_at = now
                        last_accum_loss = float(total_loss)
                        last_word_count = word_count

            print(total_loss.item())

        output_dir = os.path.dirname(args.wordsfile)
        if output_dir and not os.path.exists(output_dir):
            os.makedirs(output_dir)
        write_embedding(dataset.vocab.itos, model.criterion.W, use_cuda,
                        args.wordsfile)
        torch.save(model.state_dict(), args.modelfile)
        torch.save(optimizer.state_dict(), args.modelfile + '.optim')
        output_config_file = args.modelfile + '.config.json'
        write_config(output_config_file,
                     vocab_size=len(dataset.vocab),
                     word_embed_size=word_embed_size,
                     hidden_size=hidden_size,
                     n_layers=config.n_layers,
                     bidirectional=True,
                     use_mlp=config.use_mlp,
                     dropout=config.dropout,
                     pad_index=dataset.pad_index,
                     unk_token=dataset.unk_token,
                     bos_token=dataset.bos_token,
                     eos_token=dataset.eos_token,
                     learning_rate=learning_rate)
    else:
        config_file = args.modelfile + '.config.json'
        config_dict = read_config(config_file)
        model = Context2vec(vocab_size=config_dict['vocab_size'],
                            counter=[1] * config_dict['vocab_size'],
                            word_embed_size=config_dict['word_embed_size'],
                            hidden_size=config_dict['hidden_size'],
                            n_layers=config_dict['n_layers'],
                            bidirectional=config_dict['bidirectional'],
                            use_mlp=config_dict['use_mlp'],
                            dropout=config_dict['dropout'],
                            pad_index=config_dict['pad_index'],
                            device=device,
                            inference=True).to(device)
        model.load_state_dict(torch.load(args.modelfile))
        optimizer = optim.Adam(model.parameters(),
                               lr=config_dict['learning_rate'])
        optimizer.load_state_dict(torch.load(args.modelfile + '.optim'))
        itos, stoi = load_vocab(args.wordsfile)
        unk_token = config_dict['unk_token']
        bos_token = config_dict['bos_token']
        eos_token = config_dict['eos_token']
        model.eval()

        if args.task == 'mscc':
            if not os.path.isfile(config.question_file) or not os.path.isfile(
                    config.answer_file):
                raise FileNotFoundError

            mscc_evaluation(config.question_file,
                            config.answer_file,
                            'mscc.result',
                            model,
                            stoi,
                            unk_token=unk_token,
                            bos_token=bos_token,
                            eos_token=eos_token,
                            device=device)

        else:
            run_inference_by_user_input(model,
                                        itos,
                                        stoi,
                                        unk_token=unk_token,
                                        bos_token=bos_token,
                                        eos_token=eos_token,
                                        device=device)
示例#23
0
    if address_.startswith('http'):  # HTTP
        return Web3(Web3.HTTPProvider(address_))
    if address_.startswith('ws'):  # WebSocket
        return Web3(Web3.WebsocketProvider(address_))
    return Web3(Web3.IPCProvider(address_))


w3: Web3 = None


def init_provider(config: Config):
    global w3  # pylint: disable=global-statement
    w3 = web3_provider(config["eth_node_address"])


cfg = Config()
init_provider(cfg)

w3_lock = Lock()
event_lock = Lock()


def get_block(block_identifier, full_transactions: bool = False):
    with w3_lock:
        res = w3.eth.getBlock(block_identifier, full_transactions)
    return res


def extract_tx_by_address(address, block: BlockData) -> list:
    # Note: block attribute dict has to be generated with full_transactions=True flag
    return [
示例#24
0
 def _api_key():
     return {'api-key': Config()['ethgastation_api_key']}
示例#25
0
from src.api.app.app_factory import ApplicationFactory
from src.api.service.recommendation_service import RecommendationService
from src.repository import DatabaseClient
from src.util.config import Config

cfg = Config(f'./config.yaml')

app = ApplicationFactory.create(cfg['api.uri'])

client = DatabaseClient(cfg['database.url'], cfg['database.username'],
                        cfg['database.password'])

recommendation_service = RecommendationService(client, cfg)