Skip to content

Instantly share code, notes, and snippets.

@VeryCB
Created October 3, 2020 09:53
Show Gist options
  • Save VeryCB/df502ae51e9ed5e1956cb72ac8ccaa15 to your computer and use it in GitHub Desktop.
Save VeryCB/df502ae51e9ed5e1956cb72ac8ccaa15 to your computer and use it in GitHub Desktop.
import requests
COLA_TRX_CONTRACT_ADDRESS = 'TKH4HPMPjxR2Q93XBVfQrpGiBpyjBwBG6P'
USDT_TRX_CONTRACT_ADDRESS = 'TQn9Y2khEsLJW1ChVWFMSMeRDow5KcbLSE'
COLA_CONTRACT_ADDRESS = 'TSNWgunSeGUQqBKK4bM31iLw3bn9SBWWTG'
USDT_CONTRACT_ADDRESS = 'TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t'
def fetch_token_balances(token_addresses, pair_address):
API_URL = 'https://apilist.tronscan.io/api/account?address=%s' % pair_address
res = requests.get(API_URL)
tokens = res.json()['trc20token_balances']
data = {t['contract_address']: float(t['balance']) / (10 ** t['decimals'])
for t in tokens if t['contract_address'] in token_addresses}
if len(res.json()['tokenBalances']) > 0:
data['TRX'] = res.json()['tokenBalances'][0]['balance'] / 10 ** 6
return data
if __name__ == '__main__':
try:
cola_data = fetch_token_balances([COLA_CONTRACT_ADDRESS], COLA_TRX_CONTRACT_ADDRESS)
trx_data = fetch_token_balances([USDT_CONTRACT_ADDRESS], USDT_TRX_CONTRACT_ADDRESS)
cola_price_in_trx = cola_data['TRX'] / cola_data[COLA_CONTRACT_ADDRESS]
trx_price_in_usdt = trx_data[USDT_CONTRACT_ADDRESS] / trx_data['TRX']
cola_price_in_usdt = cola_price_in_trx * trx_price_in_usdt
print('1 COLA = %.2f USDT' % cola_price_in_usdt)
except KeyboardInterrupt:
print('You killed it.')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment