Пример #1
0
def test_fail_swap_token_not_whitelisted(setup, scrt_leader, scrt_signers, web3_provider, configuration: Config,
                                         erc20_contract, multisig_wallet):

    t1_address = get_key_signer("t1", Path.joinpath(project_base_path(), configuration['path_to_keys']))['address']

    tx_hash = erc20_contract.contract.functions.approve(multisig_wallet.address,
                                                                TRANSFER_AMOUNT_ERC). \
        transact({'from': web3_provider.eth.coinbase}).hex().lower()

    try:
        tx_hash = multisig_wallet.contract.functions.swapToken(t1_address.encode(),
                                                                       TRANSFER_AMOUNT_ERC,
                                                                       erc20_contract.address). \
                transact({'from': web3_provider.eth.coinbase}).hex().lower()
    except ValueError:
        pass
Пример #2
0
 def __init__(self, provider: Web3, contract_address: str):
     abi_path = os.path.join(project_base_path(), 'src', 'contracts', 'ethereum', 'abi', 'MultiSigSwapWallet.json')
     super().__init__(provider, contract_address, abi_path)
Пример #3
0
def erc20_contract():
    abi_path = os.path.join(project_base_path(), 'src', 'contracts',
                            'ethereum', 'abi', 'IERC20.json')
    with open(abi_path, "r") as f:
        abi = json.load(f)['abi']
    return w3.eth.contract(abi=abi)
Пример #4
0
 def __init__(self, provider: Web3, token: Token, dest_address: str):
     abi_path = os.path.join(project_base_path(), 'src', 'contracts',
                             'ethereum', 'abi', 'EIP20.json')
     self.token = token
     super().__init__(provider, token.address, abi_path)
     self.dest_address = dest_address
Пример #5
0
def test_1_swap_erc_to_s20(setup, scrt_leader, scrt_signers, web3_provider,
                           configuration: Config, erc20_contract,
                           multisig_wallet):

    scrt_leader.start()
    for signer in scrt_signers:
        signer.start()

    t1_address = get_key_signer(
        "t1", Path.joinpath(project_base_path(),
                            configuration['path_to_keys']))['address']
    # swap ethr for secret20 token, deliver tokens to address of 'a'.
    # (we will use 'a' later to check it received the money)
    tx_hash = erc20_contract.contract.functions.transfer(multisig_wallet.address,
                                                         TRANSFER_AMOUNT,
                                                         t1_address.encode()). \
        transact({'from': web3_provider.eth.coinbase}).hex().lower()
    assert TRANSFER_AMOUNT == erc20_contract.contract.functions.balanceOf(
        multisig_wallet.address).call()

    # increase number of blocks to reach the confirmation threshold
    assert increase_block_number(web3_provider,
                                 configuration['eth_confirmations'] - 1)

    sleep(configuration['sleep_interval'] + 2)

    assert Swap.objects(src_tx_hash=tx_hash).count(
    ) == 0  # verify blocks confirmation threshold wasn't meet
    assert increase_block_number(web3_provider,
                                 1)  # add the 'missing' confirmation block

    # give event listener and manager time to process tx
    sleep(configuration['sleep_interval'] + 2)
    assert Swap.objects(
        src_tx_hash=tx_hash).count() == 1  # verify swap event recorded

    sleep(1)
    # check signers were notified of the tx and signed it
    assert Signatures.objects().count() == len(scrt_signers)

    # give time for manager to process the signatures
    sleep(configuration['sleep_interval'] + 2)
    assert Swap.objects().get().status == Status.SWAP_SUBMITTED

    # get tx details
    tx_hash = Swap.objects().get().src_tx_hash
    _, log = event_log(tx_hash, ['Transfer'], web3_provider,
                       erc20_contract.contract)
    transfer_amount = erc20_contract.extract_amount(log)
    dest = erc20_contract.extract_addr(log)

    # validate swap tx on ethr delivers to the destination
    balance_query = '{"balance": {}}'
    tx_hash = run(
        f"secretcli tx compute execute {configuration['secret_contract_address']} "
        f"'{balance_query}' --from {dest} -b block -y | jq '.txhash'",
        shell=True,
        stdout=PIPE)
    tx_hash = tx_hash.stdout.decode().strip()[1:-1]

    res = run(
        f"secretcli q compute tx {tx_hash} | jq '.output_log' | jq '.[0].attributes' "
        f"| jq '.[3].value'",
        shell=True,
        stdout=PIPE).stdout.decode().strip()[1:-1]
    end_index = res.find(' ')
    amount = Decimal(res[:end_index])

    print(f"tx amount: {transfer_amount}, swap amount: {amount}")
    # assert abs(transfer_amount - amount) < 1  ??????????????????????

    # give scrt_leader time to multi-sign already existing signatures
    sleep(configuration['sleep_interval'] + 3)
    assert Swap.objects().get().status == Status.SWAP_CONFIRMED
Пример #6
0
def test_1_swap_eth_to_s20(setup, scrt_signers, scrt_leader, web3_provider,
                           configuration: Config, multisig_wallet):

    secret_token_addr = TokenPairing.objects().get(src_network="Ethereum",
                                                   src_coin="ETH").dst_address

    scrt_leader.start()
    for signer in scrt_signers:
        signer.start()

    fee_collector = multisig_wallet.contract.functions.getFeeCollector().call()

    print(f"{fee_collector=}")

    t1_address = get_key_signer(
        "t1", Path.joinpath(project_base_path(),
                            configuration.path_to_keys))['address']
    # swap ethr for secret20 token, deliver tokens to address of 'a'
    # (we will use 'a' later to check it received the money)
    print(
        f"Creating new swap transaction at {web3_provider.eth.getBlock('latest').number + 1}"
    )
    tx_hash = multisig_wallet.contract.functions.swap(t1_address.encode()). \
        transact({'from': web3_provider.eth.coinbase, 'value': TRANSFER_AMOUNT_ETH}).hex().lower()
    # TODO: validate ethr increase of the smart contract
    # increase number of blocks to reach the confirmation threshold
    assert increase_block_number(web3_provider,
                                 configuration.eth_confirmations - 1)

    sleep(configuration.sleep_interval + 2)

    assert Swap.objects(src_tx_hash=tx_hash).count(
    ) == 0  # verify blocks confirmation threshold wasn't meet
    assert increase_block_number(web3_provider,
                                 1)  # add the 'missing' confirmation block

    # give event listener and manager time to process tx
    sleep(configuration.sleep_interval + 5)
    assert Swap.objects(
        src_tx_hash=tx_hash).count() == 1  # verify swap event recorded

    sleep(1)
    # check signers were notified of the tx and signed it
    assert Signatures.objects().count() == len(scrt_signers)

    sleep(5)

    if Swap.objects().get().status == Status.SWAP_SUBMITTED:
        sleep(configuration.sleep_interval + 5)
    assert Swap.objects().get().status == Status.SWAP_CONFIRMED

    # get tx details
    tx_hash = Swap.objects().get().src_tx_hash
    _, log = event_log(tx_hash, ['Swap'], web3_provider,
                       multisig_wallet.contract)
    transfer_amount = log.args.amount
    dest = log.args.recipient.decode()

    # validate swap tx on ethr delivers to the destination
    viewing_key_set = '{"set_viewing_key": {"key": "lol"}}'
    tx_hash = run(
        f"secretcli tx compute execute {secret_token_addr} "
        f"'{viewing_key_set}' --from {dest} -b block -y | jq '.txhash'",
        shell=True,
        stdout=PIPE)
    sleep(6)

    balance = f'{{"balance": {{"key": "lol", "address": "{dest}"}} }}'
    res = run(f"secretcli q compute query {secret_token_addr} "
              f"'{balance}'",
              shell=True,
              stdout=PIPE)

    print(f"{res.stdout=}")

    amount = json.loads(res.stdout)["balance"]["amount"]

    print(f"swap amount: {transfer_amount}, dest balance amount: {amount}")

    assert int(amount) == log.args.amount
Пример #7
0
def test_11_swap_erc_to_s20(scrt_leader, scrt_signers, web3_provider,
                            configuration: Config, erc20_contract,
                            multisig_wallet, ethr_leader):

    secret_token_addr = TokenPairing.objects().get(src_network="Ethereum",
                                                   src_coin="ERC").dst_address

    # scrt_leader.start()
    # for signer in scrt_signers:
    #     signer.start()

    t1_address = get_key_signer(
        "t1", Path.joinpath(project_base_path(),
                            configuration.path_to_keys))['address']
    # swap ethr for secret20 token, deliver tokens to address of 'a'.
    # (we will use 'a' later to check it received the money)

    # add usdt to the whitelisted token list
    account = web3_provider.eth.account.from_key(configuration.leader_key)
    nonce = web3_provider.eth.getTransactionCount(account.address, "pending")
    tx = multisig_wallet.contract.functions.addToken(erc20_contract.address, 1)
    raw_tx = tx.buildTransaction(transaction={
        'from': account.address,
        'gas': 3000000,
        'nonce': nonce
    })
    signed_tx = account.sign_transaction(raw_tx)
    tx_hash = web3_provider.eth.sendRawTransaction(signed_tx.rawTransaction)

    # Get transaction hash from deployed contract
    tx_receipt = web3_provider.eth.waitForTransactionReceipt(tx_hash)

    # this will likely fail since the test before also allocates the allowance - just ignore if it fails
    try:
        _ = erc20_contract.contract.functions.approve(multisig_wallet.address, TRANSFER_AMOUNT_ERC). \
            transact({'from': web3_provider.eth.coinbase})
    except ValueError:
        pass

    tx_hash = multisig_wallet\
        .contract\
        .functions\
        .swapToken(t1_address.encode(), TRANSFER_AMOUNT_ERC, erc20_contract.address)\
        .transact({'from': web3_provider.eth.coinbase}).hex().lower()

    assert TRANSFER_AMOUNT_ERC == erc20_contract.contract.functions.balanceOf(
        multisig_wallet.address).call()

    # increase number of blocks to reach the confirmation threshold
    assert increase_block_number(web3_provider,
                                 configuration.eth_confirmations - 1)

    sleep(configuration.sleep_interval + 2)

    assert Swap.objects(src_tx_hash=tx_hash).count(
    ) == 0  # verify blocks confirmation threshold wasn't meet
    assert increase_block_number(web3_provider,
                                 1)  # add the 'missing' confirmation block

    # give event listener and manager time to process tx
    sleep(configuration.sleep_interval + 2)
    assert Swap.objects(
        src_tx_hash=tx_hash).count() == 1  # verify swap event recorded

    swap = Swap.objects(src_tx_hash=tx_hash).get()

    sleep(1)
    # check signers were notified of the tx and signed it
    assert Signatures.objects(tx_id=swap.id).count() == len(scrt_signers)

    # give time for manager to process the signatures
    sleep(configuration.sleep_interval + 2)
    assert Swap.objects().get(
        src_tx_hash=tx_hash).status in (Status.SWAP_SUBMITTED,
                                        Status.SWAP_CONFIRMED)

    _, log = event_log(tx_hash, ['SwapToken'], web3_provider,
                       multisig_wallet.contract)
    transfer_amount = multisig_wallet.extract_amount(log)
    dest = multisig_wallet.extract_addr(log)

    # validate swap tx on ethr delivers to the destination
    viewing_key_set = '{"set_viewing_key": {"key": "lol"}}'
    _ = run(
        f"secretcli tx compute execute {secret_token_addr} "
        f"'{viewing_key_set}' --from {dest} -b block -y | jq '.txhash'",
        shell=True,
        stdout=PIPE)
    sleep(6)

    balance = f'{{"balance": {{"key": "lol", "address": "{dest}"}} }}'
    res = run(f"secretcli q compute query {secret_token_addr} "
              f"'{balance}'",
              shell=True,
              stdout=PIPE)

    print(f"{res.stdout=}")

    amount = json.loads(res.stdout)["balance"]["amount"]

    print(f"swap amount: {transfer_amount}, dest balance amount: {amount}")

    # give scrt_leader time to multi-sign already existing signatures
    sleep(configuration.sleep_interval + 5)
    assert Swap.objects().get(
        src_tx_hash=tx_hash).status == Status.SWAP_CONFIRMED