Last active
March 14, 2022 10:28
-
-
Save lokialice/9f4574c13d7c3a6dccf02750a038598e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import TronWeb from 'tronweb' | |
interface JustSwap { | |
tokenToTrxSwapInput(tokens_sold: number, min_trx: number, deadline: number): { send(any) } | |
getTokenToTrxInputPrice(tokens_sold: number): {call()} | |
tokenToTrxTransferInput(min_liquidity: number, max_tokens: number, deadline: number, recipient: string): { send(any) } | |
tokenToTokenSwapInput(tokens_sold: number, min_tokens_bought: number, min_trx_bought: number, deadline: number, token_addr: string): { send(any) } | |
}; | |
interface TRC20Token { | |
name(): {call()}, | |
symbol(): {call()}, | |
decimals(): {call()}, | |
totalSupply(): {call()}, | |
balanceOf(address: string): {call()} | |
transfer(toAddress: string, amount: number): {send(any)} | |
} | |
export default class SwapTokenClient { | |
mainNetProvider = 'https://api.trongrid.io'; | |
netProvider = this.mainNetProvider; | |
HttpProvider = TronWeb.providers.HttpProvider; // Optional provider, can just use a url for the nodes instead | |
fullNode = new this.HttpProvider(`${this.netProvider}`); // Full node http endpoint | |
solidityNode = new this.HttpProvider(`${this.netProvider}`); // Solidity node http endpoint | |
eventServer = `${this.netProvider}`; // Contract events http endpoint | |
privateKey = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; | |
tronWeb = new TronWeb( | |
this.fullNode, | |
this.solidityNode, | |
this.eventServer, | |
this.privateKey | |
); | |
ownerAddress = this.tronWeb.address.fromPrivateKey(this.privateKey); | |
swappExchangeAddressHex = this.tronWeb.address.toHex("TQn9Y2khEsLJW1ChVWFMSMeRDow5KcbLSE"); | |
tokenAddressHex = this.tronWeb.address.toHex("tokenaddress"); | |
/** | |
* Sell Token to get Trx | |
* @param tokenAmount | |
* @param min_trx số trx tối thiểu để bán | |
*/ | |
async tokenToTrx(tokenAmount: number = 1, min_trx: number = 1): Promise<any | null> { | |
try { | |
const args = { | |
callValue: 0, | |
shouldPollResponse: true | |
}; | |
const swapContractInstance: JustSwap = await this.tronWeb.contract().at(this.swappExchangeAddressHex); | |
const tokenContractInstance: TRC20Token = await this.tronWeb.contract().at(this.tokenAddressHex); | |
const decimals = await tokenContractInstance.decimals().call(); | |
const tokens_sold = tokenAmount * Math.pow(10, decimals); | |
min_trx = min_trx * Math.pow(10, 5); | |
const date = new Date(); | |
const swapTokenToTrx = await swapContractInstance.tokenToTrxSwapInput(tokens_sold, min_trx, new Date(date.getMinutes() + 30).getTime()).send(args); | |
console.log({ swapTokenToTrx }); | |
return swapTokenToTrx.success; | |
} catch (e) { | |
console.error(e); | |
return null; | |
} | |
} | |
/** | |
* Sell Token to get Trx then transfer to recipient address. | |
* @param tokenAmount | |
* @param min_trx so trx tối thiểu | |
* @param recipient địa chỉ nguoi nhận | |
*/ | |
async tokenToTrxTransfer(tokenAmount: number = 1, recipient: string, min_trx: number): Promise<any | null> { | |
try { | |
const args = { | |
callValue: 0, | |
shouldPollResponse: true | |
}; | |
const swapContractInstance: JustSwap = await this.tronWeb.contract().at(this.swappExchangeAddressHex); | |
const tokenContractInstance: TRC20Token = await this.tronWeb.contract().at(this.tokenAddressHex); | |
const decimals = await tokenContractInstance.decimals().call(); | |
const tokens_sold = tokenAmount * Math.pow(10, decimals); | |
min_trx = min_trx * Math.pow(10, 5); | |
const date = new Date(); | |
const swapTokenToTrx = await swapContractInstance.tokenToTrxTransferInput(tokens_sold, min_trx, new Date(date.getMinutes() + 30).getTime(), recipient).send(args); | |
console.log({ swapTokenToTrx }); | |
return swapTokenToTrx.success; | |
} catch (e) { | |
console.error(e); | |
return null; | |
} | |
} | |
/** | |
* Sell Token 1 to get Token 2 | |
* @param tokenAmount | |
* @param min_trx_bought số trx convert trong thời gian tạm thời, suggest 1 trx | |
* @param min_tokens_bought số token mua tối thiểu | |
* @param token_addr địa chỉ token thứ 2 | |
*/ | |
async tokenToTokenSwapInput(tokenAmount: number, min_tokens_bought: number, min_trx_bought: number = 1, token_addr: string): Promise<any | null> { | |
try { | |
const args = { | |
callValue: 0, | |
shouldPollResponse: true | |
}; | |
const swapContractInstance: JustSwap = await this.tronWeb.contract().at(this.swappExchangeAddressHex); | |
const tokenContractInstance: TRC20Token = await this.tronWeb.contract().at(this.tokenAddressHex); | |
const decimals = await tokenContractInstance.decimals().call(); | |
const tokens_sold = tokenAmount * Math.pow(10, decimals); | |
min_trx_bought = min_trx_bought * Math.pow(10, 5); | |
const date = new Date(); | |
const swapTokenToTrx = await swapContractInstance.tokenToTokenSwapInput( | |
tokens_sold, | |
min_tokens_bought, | |
min_trx_bought, | |
new Date(date.getMinutes() + 30).getTime(), | |
token_addr | |
).send(args); | |
console.log({ swapTokenToTrx }); | |
return swapTokenToTrx.success; | |
} catch (e) { | |
console.error(e); | |
return null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment