Python justswap自动化交易

因为平时要在justswap上做一些自动化交易,网上资料很少,看了justswapapi文档之后,发现只需要调用合约的方式就可以了。遂共享出自己的代码

from tronapi import Tron
from tronapi import HttpProvider
import time


def timestamp():
    return int(time.time())


full_node = HttpProvider('https://api.trongrid.io')
solidity_node = HttpProvider('https://api.trongrid.io')
event_server = HttpProvider('https://api.trongrid.io')
private_key = '4afdfe5c43caf81...........................'  # 你的私钥
tron = Tron(
    full_node=full_node,
    solidity_node=solidity_node,
    event_server=event_server,
    private_key=private_key)

tron.default_address = 'T...........'  # 你的地址

kwargs = dict()
kwargs["contract_address"] = tron.address.to_hex('.....')  # 交易对地址

# 具体合约函数接口可以参考justswap官方api文档
kwargs["function_selector"] = 'tokenToTrxSwapInput(uint256,uint256,uint256)'
kwargs["parameters"] = [{
     
    'type': 'uint256',
    'value': 12000000
}, {
     
    'type': 'uint256',
    'value': 1000000
}, {
     
    'type': 'uint256',
    'value': timestamp() + 60
}]
kwargs["fee_limit"] = 40000000
'''调用合约'''
raw_data = tron.transaction_builder.trigger_smart_contract(**kwargs)
sign = tron.trx.sign(raw_data["transaction"])
result = tron.trx.broadcast(sign)
print(result)

你可能感兴趣的:(python,区块链,接口,tron,比特币)