Skip to content

Instantly share code, notes, and snippets.

@youngkiu
Last active June 14, 2021 06:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save youngkiu/9a9aa929e464a096ea628e4ad7bc5d82 to your computer and use it in GitHub Desktop.
Save youngkiu/9a9aa929e464a096ea628e4ad7bc5d82 to your computer and use it in GitHub Desktop.
NCG
import requests
import json
from functools import reduce
def get_gold_balance(agent_address, type_id):
query = '''
query($agent: Address!) {
goldBalance(address: $agent)
chainQuery {
blockQuery {
blocks(miner: $agent) {
hash
}
}
transactionQuery{
signerTransactions: transactions(signer: $agent) {
...transactionFields
}
involvedAddressTransactions: transactions(involvedAddress: $agent) {
...transactionFields
}
}
}
}
fragment transactionFields on Transaction {
id
actions {
inspection
}
}
'''
variables = {'agent': agent_address}
response = requests.post('http://localhost:23061/graphql',
json={'query': query, 'variables': variables})
results = response.json()
if 'errors' in results:
print(f'agent: {agent_address} - errors:',
json.dumps(results['errors'], indent=2))
return None
gold_balance = results['data']['goldBalance']
blocks = results['data']['chainQuery']['blockQuery']['blocks']
hashes = reduce(lambda acc, cur: acc + [cur['hash']], blocks, [])
transactions = \
results['data']['chainQuery']['transactionQuery']['signerTransactions'] + \
results['data']['chainQuery']['transactionQuery']['involvedAddressTransactions']
type_actions = []
for transaction in transactions:
for action in transaction['actions']:
if type_id in action['inspection']:
type_actions.append(action['inspection'])
return int(gold_balance), hashes, type_actions
if __name__ == '__main__':
_agent_address = '0x66D8A5E165196310497b0fc1Efc10B65d8538563'
_type_id = 'transfer_asset'
_gold_balance, _hashes, _type_actions = get_gold_balance(_agent_address, _type_id)
print('Gold Balance', _gold_balance)
print('\n'.join(_hashes))
print('Number of mined blocks', len(_hashes))
print('\n'.join(_type_actions))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment