-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- add query examples from a contract - update config - update readme - update requirements
- Loading branch information
1 parent
a962eca
commit c567ebf
Showing
7 changed files
with
216 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,31 @@ | ||
import logging | ||
import sys | ||
from dotenv import dotenv_values | ||
|
||
from cyber_sdk.client.lcd import LCDClient | ||
from cyber_sdk.key.mnemonic import MnemonicKey | ||
|
||
|
||
# logging config | ||
logging.basicConfig(level=logging.INFO, | ||
format='%(asctime)s - %(levelname)s - %(message)s', | ||
datefmt='%d-%m-%Y %H:%M:%S', | ||
handlers=[logging.FileHandler("arbitrage_osmosis.log"), | ||
handlers=[logging.FileHandler("on-chain-registry.log"), | ||
logging.StreamHandler(sys.stdout)]) | ||
|
||
# extra chains | ||
PUSSY_CHAIN_ID = 'space-pussy' | ||
PUSSY_CHAIN_NAME = 'space-pussy' | ||
PUSSY_NODE_LCD_URL = 'https://lcd.space-pussy.cybernode.ai' | ||
|
||
INIT_CHAIN_ID_NAME_DICT = {PUSSY_CHAIN_ID: PUSSY_CHAIN_NAME} | ||
INIT_CHAIN_ID_LCD_DICT = {PUSSY_CHAIN_ID: [PUSSY_NODE_LCD_URL]} | ||
|
||
CONTRACTS = { | ||
'bostrom': 'bostrom1eeahgvdsun8a04rh5vy9je49nllq6nj8ljmaslsvjeyg0j0063mssjcjmt' | ||
} | ||
# for export to a contract | ||
CHAIN_ID = 'bostrom' | ||
NODE_RPC_URL = 'https://rpc.bostrom.cybernode.ai:443' | ||
NODE_LCD_URL = 'https://lcd.bostrom.cybernode.ai' | ||
CONTRACT_ADDRESS = 'bostrom1eeahgvdsun8a04rh5vy9je49nllq6nj8ljmaslsvjeyg0j0063mssjcjmt' | ||
LCD_CLIENT = LCDClient(url=NODE_LCD_URL, chain_id=CHAIN_ID) | ||
WALLET_SEED = dotenv_values('.env')['WALLET_SEED'] | ||
WALLET = LCD_CLIENT.wallet(MnemonicKey(mnemonic=WALLET_SEED)) | ||
WALLET_ADDRESS = WALLET.key.acc_address |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
pandas | ||
tqdm | ||
cyberutils | ||
cyber_sdk | ||
python-dotenv |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import json | ||
import pandas as pd | ||
from tqdm import tqdm | ||
import requests | ||
import base64 | ||
|
||
from cyber_sdk.client.lcd import LCDClient, Wallet | ||
from cyber_sdk.client.lcd.api.tx import BlockTxBroadcastResult | ||
from cyberutils.contract import execute_contract | ||
|
||
from config import logging, CONTRACT_ADDRESS, LCD_CLIENT, WALLET, WALLET_ADDRESS, NODE_LCD_URL | ||
|
||
|
||
def batch(x: list, batch_size: int) -> list[list]: | ||
return [x[_i: _i + batch_size] for _i in range(0, len(x), batch_size)] | ||
|
||
|
||
def contract_query( | ||
contract_address: str, | ||
query: dict, | ||
node_lcd_url: str = NODE_LCD_URL, | ||
display_query: bool = False) -> dict: | ||
""" | ||
Query contract | ||
:param contract_address: contract address | ||
:param query: contract query | ||
:param node_lcd_url: node lcd url | ||
:param display_query: display a query url or not | ||
:return: query result | ||
""" | ||
_query_msg = base64.b64encode(json.dumps(query).encode("utf-8")).decode("utf-8") | ||
_query = f'{node_lcd_url}/cosmwasm/wasm/v1/contract/{contract_address}/smart/{_query_msg}' | ||
if display_query: | ||
logging.info(_query) | ||
return requests.get(_query).json() | ||
|
||
|
||
def save_to_contract( | ||
all_asset_path: str = 'data_json/all_assets.json', | ||
batch_size: int = 150, | ||
contract_address: str = CONTRACT_ADDRESS, | ||
lcd_client: LCDClient = LCD_CLIENT, | ||
wallet: Wallet = WALLET, | ||
wallet_address: str = WALLET_ADDRESS, | ||
fee_denom: str = 'boot', | ||
gas: int = 20_000_000, | ||
memo: str = 'update assets in on-chain-registry') -> list[BlockTxBroadcastResult]: | ||
""" | ||
Save asset data to a contract | ||
:param all_asset_path: path of file with all assets | ||
:param batch_size: number of updated assets in one transaction | ||
:param contract_address: contract address | ||
:param lcd_client: LCD client | ||
:param wallet: sender wallet | ||
:param wallet_address: sender address | ||
:param fee_denom: transaction fee denom | ||
:param gas: gas amount | ||
:param memo: transaction memo | ||
:return: list of transaction results | ||
""" | ||
with open(all_asset_path, 'r') as _all_assets_file: | ||
_all_assets_json = json.load(_all_assets_file) | ||
|
||
_assets_list = [] | ||
for _assets_json in tqdm(_all_assets_json): | ||
_assets = _assets_json['assets'] | ||
for i in range(len(_assets)): | ||
_assets[i]['supply'] = str(_assets[i]['supply']) | ||
_assets[i]['chain_name'] = _assets_json['chain_name'] | ||
_assets[i]['chain_id'] = _assets_json['chain_id'] | ||
if 'traces' in _assets[i].keys(): | ||
for _trace in _assets[i]['traces']: | ||
if 'base_supply' in _trace.keys(): | ||
_trace['base_supply'] = str(_trace['base_supply']) | ||
if 'counterparty' in _trace.keys() and 'base_supply' in _trace['counterparty'].keys(): | ||
_trace['counterparty']['base_supply'] = str(_trace['counterparty']['base_supply']) | ||
if 'type' in _trace.keys(): | ||
_trace['trace_type'] = _trace.pop('type') | ||
_assets_list.extend(_assets) | ||
|
||
_res_list = [] | ||
for _assets_batch in tqdm(batch(_assets_list, batch_size)): | ||
logging.info('Export to contract ' + ', '.join( | ||
[f'{k} {v:>,}' | ||
for k, v in pd.DataFrame(_assets_batch).groupby('chain_name')['chain_name'].agg(pd.value_counts).to_dict().items()] | ||
) | ||
) | ||
_res = execute_contract( | ||
execute_msgs=[{'UpdateAssets': { | ||
'assets': _assets_batch}}], | ||
contract_address=contract_address, | ||
lcd_client=lcd_client, | ||
fee_denom=fee_denom, | ||
wallet=wallet, | ||
sender=wallet_address, | ||
memo=memo, | ||
gas=gas | ||
) | ||
_res_list.append(_res) | ||
if len(str(_res)) < 500: | ||
logging.error(_res) | ||
|
||
return _res_list |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters