def test_transaction_apis(self): api = ZilliqaAPI("https://dev-api.zilliqa.com/") print(api) assert api.GetNetworkId() == "333" latest_txns = api.GetRecentTransactions() pprint(latest_txns) if latest_txns: txn_hash = latest_txns["TxnHashes"][0] txn_info = api.GetTransaction(txn_hash) pprint(txn_info) assert txn_info tx_block = txn_info["receipt"]["epoch_num"] txns = api.GetTransactionsForTxBlock(tx_block) pprint(txns) assert txns num_txns = api.GetNumTxnsTxEpoch() pprint(num_txns) assert num_txns num_txns = api.GetNumTxnsDSEpoch() pprint(num_txns) assert num_txns gas_price = api.GetMinimumGasPrice() pprint(gas_price) assert gas_price
def test_account_apis(self): api = ZilliqaAPI("https://dev-api.zilliqa.com/") print(api) assert api.GetNetworkId() == "333" balance = api.GetBalance("b50c2404e699fd985f71b2c3f032059f13d6543b") pprint(balance) assert "balance" in balance assert "nonce" in balance with pytest.raises(APIError): api.GetBalance("b50c2404e699fd985f71b2c3f032059f13d6543c")
def get_response_event(contract_addr): url = "https://dev-api.zilliqa.com/" api = ZilliqaAPI(url) cur_block_num = str(int(api.GetCurrentMiniEpoch()) - 10) while True & (int(cur_block_num) != 0): if int(cur_block_num) >= int(api.GetCurrentMiniEpoch()): time.sleep(1) else: if __has_txn(api, cur_block_num): if __get_response_from_block(contract_addr, api, cur_block_num): break else: cur_block_num = str(int(cur_block_num) + 1) else: cur_block_num = str(int(cur_block_num) + 1)
def monitor_swap_success_event(account_addr): url = "https://dev-api.zilliqa.com/" api = ZilliqaAPI(url) cur_block_num = str(int(api.GetCurrentMiniEpoch()) - 1) while True & (int(cur_block_num) != 0): if int(cur_block_num) >= int(api.GetCurrentMiniEpoch()): time.sleep(1) else: if __has_txn(api, cur_block_num): if __get_swap_request_event(account_addr, api, cur_block_num): break else: cur_block_num = str(int(cur_block_num) + 1) else: cur_block_num = str(int(cur_block_num) + 1)
class BlockChain: """Zilliqa Block Chain.""" def __init__(self, api_url: str, version: Union[str, int], network_id: Union[str, int]): self.api_url = api_url self.version = version self.network_id = network_id self.api = ZilliqaAPI(endpoint=self.api_url) def __str__(self): return "<BlockChain: {}>".format(self.api_url) def build_transaction_params(self, zil_key: ZilKey, to_addr: str, amount: Union[str, int], nonce: Union[str, int], gas_price: Union[str, int], gas_limit: Union[str, int], code="", data="", priority=False): if not is_valid_checksum_address(to_addr): raise ValueError("invalid checksum address") txn_proto = pb2.ProtoTransactionCoreInfo() txn_proto.version = self.version txn_proto.nonce = int(nonce) txn_proto.toaddr = utils.hex_str_to_bytes(to_addr) txn_proto.senderpubkey.data = zil_key.keypair_bytes.public txn_proto.amount.data = utils.int_to_bytes(int(amount), n_bytes=16) txn_proto.gasprice.data = utils.int_to_bytes(int(gas_price), n_bytes=16) txn_proto.gaslimit = int(gas_limit) if code: txn_proto.code = code.encode("utf-8") if data: txn_proto.data = data.encode("utf-8") data_to_sign = txn_proto.SerializeToString() signature = zil_key.sign_str(data_to_sign) assert zil_key.verify(signature, data_to_sign) params = { "version": txn_proto.version, "nonce": txn_proto.nonce, "toAddr": to_addr, "amount": str(amount), "pubKey": zil_key.keypair_str.public, "gasPrice": str(gas_price), "gasLimit": str(gas_limit), "code": code or None, "data": data or None, "signature": signature, "priority": priority, } return params def wait_txn_confirm(self, txn_id, timeout=60, sleep=5): start = time.time() while time.time() - start <= timeout: try: return self.api.GetTransaction(txn_id) except APIError: time.sleep(sleep) return None
def get_tx_block_range_for_date(provider_uri, date, output): """Outputs start and end tx blocks for a given date.""" block_timestamp_graph = TxBlockTimestampGraph(ZilliqaAPI(provider_uri)) zilliqa_service = BlockRangeService(block_timestamp_graph) start_block, end_block = zilliqa_service.get_block_range_for_date(date) with smart_open(output, 'w') as output_file: output_file.write('{},{}\n'.format(start_block, end_block))
def submit_chunk(tx_list, chunk_id=0): api = ZilliqaAPI(API_ENDPOINT) start = datetime.datetime.now() num_txs = 0 print_interval = int(0.1 * len(tx_list)) for i, tx in enumerate(tx_list): try: txn_info = api.CreateTransaction(tx) print(txn_info) num_txs += 1 # Within a ProcessPool, we can print the exception, but not raise it to parent except Exception as e: print("Exception from lookup: {}".format(e)) if i % print_interval == 0: td = datetime.datetime.now() - start print("Chunk {}: replayed {} transactions in {} => {:.2f} TPS".format(chunk_id, num_txs, td, num_txs/td.total_seconds()), file=sys.stderr) start = datetime.datetime.now() num_txs = 0
def export_ds_blocks(start_block, end_block, provider_uri, max_workers, output_dir, output_format): """Exports ds blocks.""" job = ExportDsBlocksJob( start_block=start_block, end_block=end_block, zilliqa_api=ThreadLocalProxy(lambda: ZilliqaAPI(provider_uri)), max_workers=max_workers, item_exporter=ZilliqaItemExporter(output_dir, output_format=output_format), ) job.run()
def __init__(self, account): # Set mainnet chain.set_active_chain(chain.MainNet) # Set contract _addr = "zil1hgg7k77vpgpwj3av7q7vv5dl4uvunmqqjzpv2w" self.contract = Contract.load_from_address(_addr, load_state=True) # Set account self.contract.account = account # Set Zilliqa API self.api = ZilliqaAPI("https://api.zilliqa.com/") # Load Zilgraph JSON fp_json = open("zilgraph.json") self.tokens = json.load(fp_json)["tokens"] # Setup dictionaries self.token = {} self.decimals = {"zil": 12} for tok in self.tokens: self.token[tok] = Account(address=self.tokens[tok]["addr"]) self.decimals[tok] = self.tokens[tok]["decimals"]
def export_tx_blocks(start_block, end_block, provider_uri, max_workers, output_dir, output_format, rate_limit=None): """Exports tx blocks.""" zilliqa_api = ThreadLocalProxy(lambda: ZilliqaAPI(provider_uri)) if rate_limit is not None and rate_limit > 0: zilliqa_api = RateLimitingProxy(zilliqa_api, max_per_second=rate_limit) job = ExportTxBlocksJob( start_block=start_block, end_block=end_block, zilliqa_api=zilliqa_api, max_workers=max_workers, item_exporter=ZilliqaItemExporter(output_dir, output_format=output_format), ) job.run()
def get_zilliqa_api(provider_type, read_resource_lambda=None): if provider_type == "mock": if read_resource_lambda is None: raise ValueError( 'read_resource_lambda must not be None for provider type {}'. format(provider_type)) rpc = MockZilliqaAPI(read_resource_lambda) elif provider_type == "online": env_variable_name = "ZILLIQAETL_PROVIDER_URI" provider_uri = os.environ.get(env_variable_name) if provider_uri is None or len(provider_uri) == 0: raise ValueError('{} is required environment variable'.format( env_variable_name)) rpc = ZilliqaAPI(provider_uri) else: raise ValueError('Unknown provider_type ' + provider_type) return rpc
def test_blockchain_apis(self): api = ZilliqaAPI("https://dev-api.zilliqa.com/") print(api) assert api.GetNetworkId() == "333" info = api.GetBlockchainInfo() pprint(info) assert info is not None assert "CurrentDSEpoch" in info assert "CurrentMiniEpoch" in info sharding = api.GetShardingStructure() pprint(sharding) assert sharding is not None assert "NumPeers" in sharding ds_block_info = api.GetDsBlock("20") pprint(ds_block_info) assert ds_block_info ds_block_info = api.GetLatestDsBlock() pprint(ds_block_info) assert ds_block_info num_ds_blocks = api.GetNumDSBlocks() pprint(num_ds_blocks) assert num_ds_blocks ds_block_rate = api.GetDSBlockRate() pprint(ds_block_rate) assert ds_block_rate ds_blocks = api.DSBlockListing(1) pprint(ds_blocks) assert ds_blocks assert len(ds_blocks["data"]) == 10 tx_block_info = api.GetTxBlock("1") pprint(tx_block_info) assert tx_block_info tx_block_info = api.GetLatestTxBlock() pprint(tx_block_info) assert tx_block_info num_tx_blocks = api.GetNumTxBlocks() pprint(num_tx_blocks) assert num_tx_blocks tx_block_rate = api.GetTxBlockRate() pprint(tx_block_rate) assert tx_block_rate tx_blocks = api.TxBlockListing(1) pprint(tx_blocks) assert tx_blocks assert len(tx_blocks["data"]) == 10 num_txns = api.GetNumTransactions() pprint(num_txns) assert num_txns txn_rate = api.GetTransactionRate() pprint(txn_rate) tx_block = api.GetCurrentMiniEpoch() pprint(tx_block) assert tx_block ds_block = api.GetCurrentDSEpoch() pprint(ds_block) assert ds_block shard_diff = api.GetPrevDifficulty() pprint(shard_diff) assert shard_diff ds_diff = api.GetPrevDSDifficulty() pprint(ds_diff) assert ds_diff
def __init__(self, url, contract_addr): super().__init__() self.api = ZilliqaAPI(url) self.contract_addr = contract_addr self.logger.info("ZilliqaMonitor Created~")
class ZilliqaMonitor(Monitor): def __init__(self, url, contract_addr): super().__init__() self.api = ZilliqaAPI(url) self.contract_addr = contract_addr self.logger.info("ZilliqaMonitor Created~") def __resolve_event_log(self, event_log): params = event_log["params"] request_id = 0 request_type = 0 gas_limit = 0 gas_price = 0 param_data = "" fee = 0 user_addr = "" for param in params: if param['vname'] == "id": request_id = int(param["value"]) if param['vname'] == "from": user_addr = param["value"] if param['vname'] == "reqtype": request_type = int(param["value"]) if request_type == 2: param_data = self.construct_swap_param_data(params) if param["vname"] == "gaslimit": gas_limit = int(param["value"]) if param["vname"] == "gasprice": gas_price = int(param["value"]) if param["vname"] == "paramdata": param_data = param["value"] if param["vname"] == "fee": fee = float(param["value"]) self.logger.info("get a new request: " + str(request_id) + " " + str(request_type) + " " + str(gas_limit) + " " + str(gas_price) + " " + param_data + " " + str(fee)) return Request(request_id, request_type, param_data, gas_price, gas_limit, fee, "Zilliqa", self.contract_addr, user_addr) @staticmethod def construct_swap_param_data(params): param_data = {} for param in params: if param['vname'] == "swapid": param_data['swap_id'] = param["value"] if param['vname'] == "swapchain": param_data['swap_chain'] = param["value"] if param['vname'] == "txhash": param_data['tx_hash'] = param["value"] if param['vname'] == "initialaddr": param_data['initial_addr'] = param["value"] if param['vname'] == "targetaddr": param_data['target_addr'] = param["value"] if param['vname'] == "swapmoney": param_data['swap_money'] = param["value"] if param['vname'] == "id": param_data['verify_id'] = param["value"] param_data['initial_chain'] = 'Zilliqa' return json.dumps(param_data) def __get_request_from_block(self, block_num): txns = self.api.GetTransactionsForTxBlock(block_num) for txn in txns: if len(txn) != 0: receipt = self.api.GetTransaction(txn[0])["receipt"] if "event_logs" in receipt: event_logs = receipt["event_logs"] for event_log in event_logs: if event_log["address"] == (zilkey.normalise_address( self.contract_addr)).lower(): if event_log[ "_eventname"] == "request" or event_log[ "_eventname"] == "verifyrequest": self.req_q.put( self.__resolve_event_log(event_log)) def __get_last_txn_block_num(self): last_txns = self.api.GetRecentTransactions() # self.logger.info(last_txns) if len(last_txns["TxnHashes"]) != 0: last_txn_info = self.api.GetTransaction(last_txns["TxnHashes"][0]) return last_txn_info["receipt"]["epoch_num"] else: return "0" def __has_txn(self, block_num): block_info = self.api.GetTxBlock(block_num) if block_info["header"]["NumTxns"] == 0: return False else: return True def run(self): cur_block_num = str(int(self.api.GetCurrentMiniEpoch()) - 1) # cur_block_num = str(int(1031258) - 1) while True & (int(cur_block_num) != 0): if int(cur_block_num) >= int(self.api.GetCurrentMiniEpoch()): time.sleep(1) else: if self.__has_txn(cur_block_num): self.__get_request_from_block(cur_block_num) cur_block_num = str(int(cur_block_num) + 1) else: cur_block_num = str(int(cur_block_num) + 1)
def __init__(self, api_url: str, version: Union[str, int], network_id: Union[str, int]): self.api_url = api_url self.version = version self.network_id = network_id self.api = ZilliqaAPI(endpoint=self.api_url)
class zilswap: def __init__(self, account): # Set mainnet chain.set_active_chain(chain.MainNet) # Set contract _addr = "zil1hgg7k77vpgpwj3av7q7vv5dl4uvunmqqjzpv2w" self.contract = Contract.load_from_address(_addr, load_state=True) # Set account self.contract.account = account # Set Zilliqa API self.api = ZilliqaAPI("https://api.zilliqa.com/") # Load Zilgraph JSON fp_json = open("zilgraph.json") self.tokens = json.load(fp_json)["tokens"] # Setup dictionaries self.token = {} self.decimals = {"zil": 12} for tok in self.tokens: self.token[tok] = Account(address=self.tokens[tok]["addr"]) self.decimals[tok] = self.tokens[tok]["decimals"] def get_contract(self): self.contract.get_state() pprint(self.contract.state) def zil_balance(self): balance = self.contract.account.get_balance() return balance def gzil_balance(self): gzil_contract = Contract.load_from_address( self.token["gzil"].bech32_address, load_state=True) balance = float(gzil_contract.state['balances'][ self.contract.account.address0x]) * 1e-15 return balance def buy_gzil(self, amount, max_price=2000): # Buy gZIL _min_token_amount = str(int(amount * 1e15)) _deadline_block = str(int(self.api.GetCurrentMiniEpoch()) + 15) _params = [ Contract.value_dict("token_address", "ByStr20", self.token["gzil"].address0x), Contract.value_dict("min_token_amount", "Uint128", _min_token_amount), Contract.value_dict("deadline_block", "BNum", _deadline_block), Contract.value_dict("recipient_address", "ByStr20", self.contract.account.address0x) ] pprint(_params) _zils = Zil(max_price * amount) resp = self.contract.call(method="SwapExactZILForTokens", params=_params, amount=_zils, gas_limit=30000) pprint(resp) pprint(self.contract.last_receipt) def sell_gzil(self, amount, min_price=4000): # Sell gZIL _token_amount = str(int(amount * 1e15)) _deadline_block = str(int(self.api.GetCurrentMiniEpoch()) + 15) _min_zil_amount = str(int(min_price * amount * 1e12)) _params = [ Contract.value_dict("token_address", "ByStr20", self.token["gzil"].address0x), Contract.value_dict("token_amount", "Uint128", _token_amount), Contract.value_dict("min_zil_amount", "Uint128", _min_zil_amount), Contract.value_dict("deadline_block", "BNum", _deadline_block), Contract.value_dict("recipient_address", "ByStr20", self.contract.account.address0x) ] pprint(_params) resp = self.contract.call(method="SwapExactTokensForZIL", params=_params, gas_limit=30000) pprint(resp) pprint(self.contract.last_receipt) def get_gzil_rate(self): self.contract.get_state() _poolsize = self.contract.state['pools'][ self.token["gzil"].address0x]['arguments'] _rate = (int(_poolsize[0]) * 1e-12) / (int(_poolsize[1]) * 1e-15) return _rate def get_gzil_market(self): self.contract.get_state() _poolsize = self.contract.state['pools'][ self.token["gzil"].address0x]['arguments'] _liq_zil = (int(_poolsize[0]) * 1e-12) _liq_gzil = (int(_poolsize[1]) * 1e-15) _rate = _liq_zil / _liq_gzil _market_data_point = { "_id": int(time.time()), "rate": _rate, "liq_zil": _liq_zil, "liq_gzil": _liq_gzil } return _market_data_point def get_market(self, tokenstr): _time = int(time.time()) self.contract.get_state() _poolsize = self.contract.state['pools'][ self.token[tokenstr].address0x]['arguments'] _liq_zil = int(_poolsize[0]) * 1e-12 _liq_token = int(_poolsize[1]) * pow(10, -self.decimals[tokenstr]) _rate = _liq_zil / _liq_token _market_data_point = { "_id": _time, "rate": _rate, "liq_zil": _liq_zil, "liq_" + tokenstr: _liq_token } return _market_data_point
BUILD_PATH = os.path.join(ZILLIQA_PATH, 'build/') BIN_PATH = os.path.join(BUILD_PATH, 'bin/') CONTRACTS_PATH = "contracts/" ACCOUNTS_FILE = "accounts.csv" PENDING_FILE = "pending.txt" NUM_ACCOUNTS = 25000 ACC_BATCH_SIZE = 1000 TX_TIMEOUT = 300 ACC_MIN_BALANCE = 1000 TOKEN_MIN_BALANCE = 1000000 LocalNet = chain.BlockChain(API_ENDPOINT, version=1, network_id=0) chain.set_active_chain(LocalNet) api = ZilliqaAPI(API_ENDPOINT) saved_accs = [] nonces = {} sent_by = {} with open(CONFIG_FILE) as f: conf = json.load(f) genesis = Account(private_key=conf['genesis']['privkey']) def new_nonce(acc): addr = acc.bech32_address nn = nonces.get(addr, acc.get_nonce()) + 1 nonces[addr] = nn