Пример #1
0
def get_domain_hash():
    return Web3.solidityKeccak(
        ['bytes32', 'bytes32', 'bytes32', 'uint256', 'bytes32'], [
            utils.hash_string(EIP712_DOMAIN_STRING),
            utils.hash_string('P1Orders'),
            utils.hash_string('1.0'), consts.NETWORK_ID,
            utils.address_to_bytes32(consts.P1_ORDERS_ADDRESS)
        ]).hex()
Пример #2
0
def get_domain_hash(pair):
    contract_name = ''
    contract_address = ''
    if pair == consts.PAIR_PBTC_USDC:
        contract_name = 'P1Orders'
        contract_address = consts.BTC_P1_ORDERS_ADDRESS
    elif pair == consts.PAIR_PLINK_USDC:
        contract_name = 'P1Orders'
        contract_address = consts.LINK_P1_ORDERS_ADDRESS
    elif pair == consts.PAIR_WETH_PUSD:
        contract_name = 'P1InverseOrders'
        contract_address = consts.ETH_P1_ORDERS_ADDRESS
    else:
        raise ValueError('Invalid perpetual pair')

    return Web3.solidityKeccak(
        ['bytes32', 'bytes32', 'bytes32', 'uint256', 'bytes32'], [
            utils.hash_string(EIP712_DOMAIN_STRING),
            utils.hash_string(contract_name),
            utils.hash_string('1.0'), consts.NETWORK_ID,
            utils.address_to_bytes32(contract_address)
        ]).hex()
Пример #3
0
def get_cancel_order_hash(order_hash):
    '''
    Returns the final signable EIP712 hash for a cancel order API call.
    '''
    action_hash = Web3.solidityKeccak(['string'], [EIP712_CANCEL_ACTION]).hex()
    orders_hash = Web3.solidityKeccak(['bytes32'], [order_hash]).hex()
    struct_hash = Web3.solidityKeccak([
        'bytes32',
        'bytes32',
        'bytes32',
    ], [
        utils.hash_string(EIP712_CANCEL_ORDER_STRUCT_STRING),
        action_hash,
        orders_hash,
    ]).hex()
    return utils.get_eip712_hash(get_domain_hash(), struct_hash)
Пример #4
0
def get_order_hash(order):
    '''
    Returns the final signable EIP712 hash for an order.
    '''

    struct_hash = Web3.solidityKeccak([
        'bytes32', 'bytes32', 'uint256', 'uint256', 'uint256', 'uint256',
        'bytes32', 'bytes32', 'uint256'
    ], [
        utils.hash_string(EIP712_ORDER_STRUCT_STRING),
        get_order_flags(order['salt'], order['isBuy'], order['limitFee']),
        int(order['amount']),
        int(order['limitPrice'] * consts.BASE_DECIMAL),
        int(order['triggerPrice'] * consts.BASE_DECIMAL),
        int(abs(order['limitFee']) * consts.BASE_DECIMAL),
        utils.address_to_bytes32(order['maker']),
        utils.address_to_bytes32(order['taker']),
        int(order['expiration'])
    ]).hex()
    return utils.get_eip712_hash(get_domain_hash(), struct_hash)
Пример #5
0
def get_cancel_order_hash(order_hash):
    '''
    Returns the final signable EIP712 hash for a cancel order API call.
    '''
    action_hash = Web3.solidityKeccak(['string'], [EIP712_CANCEL_ACTION]).hex()
    orders_hash = Web3.solidityKeccak(['bytes32'], [order_hash]).hex()
    struct_hash = Web3.solidityKeccak([
        'bytes32',
        'bytes32',
        'bytes32',
    ], [
        utils.hash_string(EIP712_CANCEL_ORDER_STRUCT_STRING),
        action_hash,
        orders_hash,
    ]).hex()
    return utils.get_eip712_hash(
        get_domain_hash(
            consts.
            PAIR_PBTC_USDC,  # Use BTC Market. Orderbook should accept it.
        ),
        struct_hash)
Пример #6
0
 def test_util_hash_string(self):
     assert (
         utils.hash_string("baconfries") ==
         "0xae5dd6cd2427c8b9f8600be5fe223f87913bb47c1b1cef18792a34033f6d752a"
     )  # noqa: E501
Пример #7
0
 def test_util_hash_string(self):
     assert utils.hash_string('baconfries') == \
         '0xae5dd6cd2427c8b9f8600be5fe223f87913bb47c1b1cef18792a34033f6d752a'  # noqa: E501