Skip to content

Instantly share code, notes, and snippets.

@freeatnet
Created January 13, 2018 21:15
Show Gist options
  • Save freeatnet/4c294f75d270adf2ceb2a2a2752d7849 to your computer and use it in GitHub Desktop.
Save freeatnet/4c294f75d270adf2ceb2a2a2752d7849 to your computer and use it in GitHub Desktop.
Order fill inquiry
from decimal import Decimal
import json
from sha256 import sha256_like_solidity
from time import sleep, time
from web3 import Web3, HTTPProvider
HTTP_PROVIDER_URL = 'http://159.203.19.200:8545/'
ED_CONTRACT_ADDR = '0x8d12a197cb00d4747a1fe03395095ce2a5cc6819'
with open('etherdelta/abi.json') as f:
ED_CONTRACT_ABI = json.load(f)
instance = Web3(HTTPProvider(HTTP_PROVIDER_URL))
ed_contract = instance.eth.contract(ED_CONTRACT_ADDR, abi=ED_CONTRACT_ABI)
"""
bytes32 hash = sha256(
address this,
address tokenGet,
uint amountGet,
address tokenGive,
uint amountGive,
uint expires,
uint nonce);
"""
with open("sample-orders.json") as f:
sample_orders = json.load(f)
def make_order_hash(order):
hash_parts = [
('address', ED_CONTRACT_ADDR),
('address', order["tokenGet"]),
('uint256', Web3.toInt(Decimal(order["amountGet"]))),
('address', order["tokenGive"]),
('uint256', Web3.toInt(Decimal(order["amountGive"]))),
('uint256', Web3.toInt(Decimal(order["expires"]))),
('uint256', Web3.toInt(Decimal(order["nonce"])))
]
print(hash_parts)
return sha256_like_solidity(*list(zip(*hash_parts)))
for order in sample_orders:
order_hash = make_order_hash(order)
print(order["price"], order["nonce"], ed_contract.call().orderFills(order["user"], Web3.toBytes(hexstr=order_hash)))
from eth_utils import add_0x_prefix, remove_0x_prefix
from hashlib import sha256
from web3 import Web3
from web3.utils.encoding import hex_encode_abi_type, to_bytes
def sha256_like_solidity(abi_types, values):
if len(abi_types) != len(values):
raise ValueError(
"Length mismatch between provided abi types and values. Got "
"{0} types and {1} values.".format(len(abi_types), len(values))
)
hex_string = add_0x_prefix(''.join(
remove_0x_prefix(hex_encode_abi_type(abi_type, value))
for abi_type, value
in zip(abi_types, values)))
return sha256(to_bytes(hex_string.encode('utf8'))).hexdigest()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment