Esempio n. 1
0
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)
Esempio n. 2
0
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)
Esempio n. 3
0
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 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
Esempio n. 5
0
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