【Tron】使用Python玩转SunSwap
创始人
2024-02-21 00:46:08
0

前言

在上一篇文章中,我们学习了如何使用Python调用PancakeSwap智能合约,实现代币价格监视,代币自动兑换,在本文中,我们来学习如何在Tron的SunSwap上实现一样的需求。

在BSC上,我们使用的SDK是【web3.py】

而在Tron上,我们使用的SDK是【tronpy】

总所周知,Tron其实抄袭的是以太坊,Tron上的SunSwap其实和PancakeSwap一样,都是从Uniswap克隆来的,所以他们的调用方法,函数、参数,都是一模一样的,在学会了Python调用PancakeSwap后,也能很快上手Python调用SunSwap

快捷方式

废话不多说,由于上一篇文章中已经讲解了学习过程,本文不再赘述,直接上干货。

完整的示例代码:

【python_sunswap】https://github.com/encoderlee/python_sunswap

一些参考资料:

Tron开发者文档: https://developers.tron.network/docs

Tron APIKEY: https://developers.tron.network/reference/select-network

tronpy文档: https://tronpy.readthedocs.io/en/latest/index.html

示例代码简析

import time
from decimal import Decimal
from typing import List
from datetime import datetime, timedelta
import os
from dataclasses import dataclass
from tronpy import Tron
from tronpy.keys import PrivateKey
from tronpy.providers import HTTPProvider@dataclass
class Contract:symbol: straddress: strdecimals: int = Noneclass Known:usdt = Contract("USDT", "TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t", 6)sun = Contract("SUN", "TSSMHYeV2uE9qYH95DqyoCuNCzEL1NvU3S", 18)sunswap = Contract("SunswapV2Router02", "TKzxdSv2FZKQrEqkKVgp5DcwEXBEKMg2Ax")class SunSwap:def __init__(self, address_wallet: str, private_key: str = None):self.wallet: str = address_walletprovider = HTTPProvider(timeout=30, api_key="b0ed0858-e287-45be-beec-57c6cb509c46")provider.sess.trust_env = Falseself.tron = Tron(provider)self.private_key = PrivateKey(bytes.fromhex(private_key))# get ERC20 token balance of the accountdef erc20_balance(self, erc20: Contract) -> Decimal:contract = self.tron.get_contract(erc20.address)# get the token decimals if notdecimals = erc20.decimalsif not decimals:decimals = contract.functions.decimals()#  get the balance of tokens and convert itbalance = contract.functions.balanceOf(self.wallet)balance = Decimal(balance) / (10 ** decimals)return balance# approve the pancakeswap contract to use erc20 tokensdef approve_erc20_to_sunswap(self, erc20: Contract):contract = self.tron.get_contract(erc20.address)approve_amount = 2 ** 256 - 1amount = contract.functions.allowance(self.wallet, Known.sunswap.address)if amount >= approve_amount / 2:print("already approved")return Nonetxn = (contract.functions.approve(Known.sunswap.address, approve_amount).with_owner(self.wallet).fee_limit(100 * 1000000).build().sign(self.private_key))result = txn.broadcast().wait()if result["receipt"]["result"] == "SUCCESS":print("transaction ok: {0}".format(result))else:print("transaction error: {0}".format(result))return result# query the price of token pairdef query_price(self, token_path: List[Contract]) -> Decimal:contract = self.tron.get_contract(Known.sunswap.address)path = [item.address for item in token_path]amount = contract.functions.getAmountsOut(1 * 10 ** token_path[0].decimals, path)amount_in = Decimal(amount[0]) / (10 ** token_path[0].decimals)amount_out = Decimal(amount[1]) / (10 ** token_path[-1].decimals)return amount_in / amount_out# swap tokendef swap_token(self, amount_in: Decimal, token_path: List[Contract]):# approve token to pancakeswap if notself.approve_erc20_to_sunswap(token_path[0])contract = self.tron.get_contract(Known.sunswap.address)path = [item.address for item in token_path]amount_in = int(amount_in * 10 ** token_path[0].decimals)amount = contract.functions.getAmountsOut(amount_in, path)# slippage 0.5% fee 0.3% ,minimum received 99.2 %minimum_out = int(amount[1] * (1 - Decimal("0.005") - Decimal("0.003")))deadline = datetime.now() + timedelta(minutes=5)txn = (contract.functions.swapExactTokensForTokens(amount_in, minimum_out, path, self.wallet,int(deadline.timestamp())).with_owner(self.wallet).fee_limit(100 * 1000000).build().sign(self.private_key))result = txn.broadcast().wait()if result["receipt"]["result"] == "SUCCESS":print("transaction ok: {0}".format(result))else:print("transaction error: {0}".format(result))return resultdef main():# change it to your wallet addressaddress_wallet = "TGrDfWjBrefFdsT6VNB4ZpN9qBpmfM6Smo"# set your private key to the environment variable 'key'private_key = os.getenv("key")sunswap = SunSwap(address_wallet, private_key)balance = sunswap.erc20_balance(Known.usdt)print("usdt balance: {0}".format(balance))limit_price = Decimal("0.0054")amount_buy = Decimal(1)print("if the price of SUN is lower than {0} USDT/SUN, buy {1} USDT of SUN".format(limit_price, amount_buy))token_path = [Known.usdt, Known.sun]while True:price = sunswap.query_price(token_path)print("sun price: {0} USDT/SUN".format(price))if price <= limit_price:print("price ok, buy {0} USDT of SUN".format(amount_buy))sunswap.swap_token(amount_buy, token_path)breaktime.sleep(2)if __name__ == '__main__':main()

可以看到 tronpy 的代码写起来更简单,tronpy 的 get_contract 方法,可以从网络上获取合约ABI,从而让我们在调用合约方法的时候,无需关心ABI这回事,如同调用本地方法一样去调用智能合约。而在web3.py中,这个自动获取ABI的功能我们需要自行实现。同时Tron的开发者文档也有全中文版本,对新手更加友好。

相关内容

热门资讯

喜欢穿一身黑的男生性格(喜欢穿... 今天百科达人给各位分享喜欢穿一身黑的男生性格的知识,其中也会对喜欢穿一身黑衣服的男人人好相处吗进行解...
发春是什么意思(思春和发春是什... 本篇文章极速百科给大家谈谈发春是什么意思,以及思春和发春是什么意思对应的知识点,希望对各位有所帮助,...
网络用语zl是什么意思(zl是... 今天给各位分享网络用语zl是什么意思的知识,其中也会对zl是啥意思是什么网络用语进行解释,如果能碰巧...
为什么酷狗音乐自己唱的歌不能下... 本篇文章极速百科小编给大家谈谈为什么酷狗音乐自己唱的歌不能下载到本地?,以及为什么酷狗下载的歌曲不是...
家里可以做假山养金鱼吗(假山能... 今天百科达人给各位分享家里可以做假山养金鱼吗的知识,其中也会对假山能放鱼缸里吗进行解释,如果能碰巧解...
华为下载未安装的文件去哪找(华... 今天百科达人给各位分享华为下载未安装的文件去哪找的知识,其中也会对华为下载未安装的文件去哪找到进行解...
四分五裂是什么生肖什么动物(四... 本篇文章极速百科小编给大家谈谈四分五裂是什么生肖什么动物,以及四分五裂打一生肖是什么对应的知识点,希...
怎么往应用助手里添加应用(应用... 今天百科达人给各位分享怎么往应用助手里添加应用的知识,其中也会对应用助手怎么添加微信进行解释,如果能...
苏州离哪个飞机场近(苏州离哪个... 本篇文章极速百科小编给大家谈谈苏州离哪个飞机场近,以及苏州离哪个飞机场近点对应的知识点,希望对各位有...
客厅放八骏马摆件可以吗(家里摆... 今天给各位分享客厅放八骏马摆件可以吗的知识,其中也会对家里摆八骏马摆件好吗进行解释,如果能碰巧解决你...