actAddress = “TGXuuKAb4bnrn137u39EKbYzKNXvdCes98”
sunSwapEx.multicallContractAddress = multicallContractAddress
let multicallContractABI = [{"inputs":[{"components":[{"internalType":"address","name":"target","type":"address"},{"internalType":"bytes","name":"callData","type":"bytes"}],"internalType":"struct TronMulticall.Call[]","name":"calls","type":"tuple[]"}],"name":"aggregate","outputs":[{"internalType":"uint256","name":"blockNumber","type":"uint256"},{"internalType":"bytes[]","name":"returnData","type":"bytes[]"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getBasefee","outputs":[{"internalType":"uint256","name":"basefee","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"blockNumber","type":"uint256"}],"name":"getBlockHash","outputs":[{"internalType":"bytes32","name":"blockHash","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getBlockNumber","outputs":[{"internalType":"uint256","name":"blockNumber","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getChainId","outputs":[{"internalType":"uint256","name":"chainid","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getCurrentBlockCoinbase","outputs":[{"internalType":"address","name":"coinbase","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getCurrentBlockDifficulty","outputs":[{"internalType":"uint256","name":"difficulty","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getCurrentBlockTimestamp","outputs":[{"internalType":"uint256","name":"timestamp","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"addr","type":"address"}],"name":"getEthBalance","outputs":[{"internalType":"uint256","name":"balance","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getLastBlockHash","outputs":[{"internalType":"bytes32","name":"blockHash","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"accountAddress","type":"address"},{"internalType":"trcToken","name":"id","type":"trcToken"}],"name":"getTokenBalance","outputs":[{"internalType":"uint256","name":"balance","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"addr","type":"address"}],"name":"isContract","outputs":[{"internalType":"bool","name":"result","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes[]","name":"data","type":"bytes[]"}],"name":"multicall","outputs":[{"internalType":"bytes[]","name":"results","type":"bytes[]"}],"stateMutability":"view","type":"function"}]
sunSwapEx.registerABI(multicallContractAddress, multicallContractABI)
let smartRouterAddress = "TCFNp179Lg46D16zKoumd4Poa2WFFdtqYj"
sunSwapEx.smartRouterAddress = smartRouterAddress
let smartRouterABI = `[{"outputs":[{"name":"amountsOut","type":"uint256[]"}],"inputs":[{"name":"path","type":"address[]"},{"name":"poolVersion","type":"string[]"},{"name":"versionLen","type":"uint256[]"},{"name":"fees","type":"uint24[]"},{"name":"data","type":"tuple"}],"name":"swapExactInput","stateMutability":"payable","type":"function","payable":true}]`
sunSwapEx.registerABI(smartRouterAddress, smartRouterABI)
// get sunswap_markets
sunSwapEx.markets = _G("sunswap_markets")
return sunSwapEx
}
// for test function main() { // reset log LogReset(1) // reset _G() // _G(null)
// Create SunswapEx
let e = $.CreateSunSwapEx()
// let e = $.CreateSunSwapEx(exchanges[1])
// /* GetMarkets
let markets = e.GetMarkets()
let idx = 0
let tbl = {"type": "table", "title": "test GetMarkets", "cols": ["Index", "PoolAddress", "Symbol", "BaseAsset", "QuoteAsset", "BaseName", "QuoteName", "BaseDecimals", "QuoteDecimals", "BaseAddress", "QuoteAddress"], "rows": []}
for (let currency in markets) {
let arrMarket = markets[currency]
for (let market of arrMarket) {
tbl["rows"].push([idx, market["PoolAddress"], market["Symbol"], market["BaseAsset"], market["QuoteAsset"], market["BaseName"], market["QuoteName"], market["BaseDecimals"], market["QuoteDecimals"], market["BaseAddress"], market["QuoteAddress"]])
idx++
}
}
LogStatus("`" + JSON.stringify(tbl) + "`")
// */
/* GetTicker
let ticker1 = e.GetTicker("WTRX_USDT")
Log("symbol:", "WTRX_USDT", ", ticker:", ticker1)
let ticker2 = e.GetTicker("iCMX_USDT", "TLVDozYEBgeaJXH7oKBsougzEJKNrogFun") // iCMX_USDT
Log("symbol:", "iCMX_USDT", ", ticker:", ticker2)
*/
/* multicall
let data = []
let walletAddress = exchange.IO("address")
data.push(["TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t", "0x" + exchange.IO("encode", "TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t", "balanceOf", walletAddress)])
data.push(["TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t", "0x" + exchange.IO("encode", "TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t", "balanceOf", walletAddress)])
Log(data)
let ret = multicall(exchange, "TGXuuKAb4bnrn137u39EKbYzKNXvdCes98", data, ["uint256", "uint256"])
Log(ret, toAmount(ret[0], 6))
*/
/* GetSwapOutAmount
let retBuy = e.GetSwapOutAmount("WTRX_USDT", 1, "buy") // 1 quote -> base
Log("WTRX_USDT", "buy outAmount:", retBuy["outAmount"], ", ask:", (1 / retBuy["outAmount"]))
let retSell = e.GetSwapOutAmount("WTRX_USDT", 1, "sell") // 1 base -> quote
Log("WTRX_USDT", "sell outAmount:", retSell["outAmount"], ", bid:", (retSell["outAmount"] / 1))
*/
/* GetAssets
let assets = e.GetAssets()
let tbl = {"type": "table", "title": "test GetAssets", "cols": ["Name", "Address", "Decimals", "Balance"], "rows": []}
for (let asset of assets) {
tbl["rows"].push([asset.Currency, asset.Address, asset.Decimals, asset.Amount])
}
LogStatus("`" + JSON.stringify(tbl) + "`")
*/
/* Swap
// let ret = e.Swap("WTRX_USDT", 6, "buy")
// let ret = e.Swap("WTRX_TUSD", 6, "buy")
// let ret = e.Swap("WTRX_TUSD", 10, "sell")
// let ret = e.Swap("TRX_WTRX", 10, "sell")
// let ret = e.Swap("TRX_WTRX", 10, "buy")
// let ret = e.Swap("TUSD_TRX", 0.3, "sell")
// let ret = e.Swap("WTRX_USDT", 20, "sell")
// let ret = e.Swap("USDT_TRX", 6, "sell")
Log(ret)
*/
/* check token
let mapFilter = {}
let checkMarkets = e.GetMarkets()
for (let currency in checkMarkets) {
for (let market of checkMarkets[currency]) {
let base = market["BaseAsset"]
let quote = market["QuoteAsset"]
if (typeof(mapFilter[base]) == "undefined") {
mapFilter[base] = market["BaseAddress"]
}
if (typeof(mapFilter[quote]) == "undefined") {
mapFilter[quote] = market["QuoteAddress"]
}
if (market["BaseAddress"] != mapFilter[base]) {
Log(market["BaseAsset"], market["BaseAddress"], " --- ", base, mapFilter[base])
}
if (market["QuoteAddress"] != mapFilter[quote]) {
Log(market["QuoteAsset"], market["QuoteAddress"], " --- ", quote, mapFilter[quote])
}
}
}
*/
/* GetAccount
let account = e.GetAccount()
Log(account)
*/
/* StakeTRX
// BANDWIDTH, ENERGY, TRON_POWER
let retStakeTRX = e.StakeTRX("ENERGY", 10) // Stake TRX
Log("retStakeTRX:", retStakeTRX)
// let retUnStakeTRX = e.UnStakeTRX("ENERGY", 100) // UnStake TRX
// Log("retUnStakeTRX:", retUnStakeTRX)
*/
/* send
// let ret = e.SendTRX("...", 180)
// Log("SendTRX ret:", ret)
*/
} “`
Through this article, we have realized the Web3 Tron package based on the FMZ Quant platform, and connected to SunSwap DEX successfully, completing the complete process from querying the trading pool to executing the exchange and parsing the transaction results. This not only provides an efficient automation solution for DEX transactions in the Tron ecosystem, but also demonstrates the powerful scalability of the FMZ Quant platform.
I hope that the practical experience in this article can provide valuable reference for your quantitative trading system. Next, I will study the DEX on SOL. Thank you for reading~