feat: tron cash-out

This commit is contained in:
Rafael Taranto 2023-09-11 12:13:14 +01:00
parent bb8b6ce354
commit 7f2963bc85
15 changed files with 795 additions and 9637 deletions

View file

@ -1,11 +1,18 @@
const TronWeb = require('tronweb')
const coins = require('@lamassu/coins')
const { default: PQueue } = require('p-queue')
const BN = require('../../../bn')
let tronWeb = null
const DEFAULT_PREFIX_PATH = "m/44'/195'/1'/0"
const DEFAULT_PREFIX_PATH = "m/44'/195'/0'/0"
const PAYMENT_PREFIX_PATH = "m/44'/195'/1'/0"
const SWEEP_QUEUE = new PQueue({
concurrency: 3,
interval: 250,
})
function checkCryptoCode (cryptoCode) {
if (cryptoCode === 'TRX' || coins.utils.isTrc20Token(cryptoCode)) {
@ -18,9 +25,19 @@ function defaultWallet (account) {
const mnemonic = account.mnemonic
if (!mnemonic) throw new Error('No mnemonic seed!')
const key = TronWeb.fromMnemonic(mnemonic.replace(/[\r\n]/gm, ' ').trim(), `${DEFAULT_PREFIX_PATH}\/0`)
return TronWeb.fromMnemonic(mnemonic.replace(/[\r\n]/gm, ' ').trim(), `${DEFAULT_PREFIX_PATH}\/0`)
}
return key
function paymentWallet (account, index) {
const mnemonic = account.mnemonic
if (!mnemonic) throw new Error('No mnemonic seed!')
return TronWeb.fromMnemonic(mnemonic.replace(/[\r\n]/gm, ' ').trim(), `${PAYMENT_PREFIX_PATH}\/${index}`)
}
function newAddress (account, info, tx, settings, operatorId) {
const wallet = paymentWallet(account, info.hdIndex)
return Promise.resolve(wallet.address)
}
function defaultAddress (account) {
@ -41,8 +58,6 @@ const _balance = async (address, cryptoCode) => {
const contract = tronWeb.contract(abi.entrys, contractAddress)
const balance = await contract.methods.balanceOf(address).call()
// const decimals = await contract.methods.decimals().call()
// BN(balance.toString()).div(10 ** decimals).toString()
return BN(balance.toString())
}
@ -61,6 +76,7 @@ const sendCoins = async (account, tx) => {
try {
response = await tronWeb.trx.sendRawTransaction(rawTx)
if (!response.result) throw new Error(response.code)
} catch (err) {
// for some reason err here is just a string
throw new Error(err)
@ -112,6 +128,29 @@ function newFunding (account, cryptoCode) {
})
}
function sweep (account, txId, cryptoCode, hdIndex) {
const wallet = paymentWallet(account, hdIndex)
const fromAddress = wallet.address
const isTrc20Token = coins.utils.isTrc20Token(cryptoCode)
const txFunction = isTrc20Token ? generateTrc20Tx : generateTx
return SWEEP_QUEUE.add(async () => {
const r = await confirmedBalance(fromAddress, cryptoCode)
if (r.eq(0)) return
const signedTx = await txFunction(defaultAddress(account), wallet, r.toString(), cryptoCode)
let response = null
try {
response = await tronWeb.trx.sendRawTransaction(signedTx)
if (!response.result) throw new Error(response.code)
} catch (err) {
// for some reason err here is just a string
throw new Error(err)
}
return response
})
}
function connect(account) {
if (tronWeb != null) return
const endpoint = account.endpoint
@ -133,15 +172,19 @@ function getStatus (account, tx, requested, settings, operatorId) {
})
}
function getTxHashesByAddress (cryptoCode, address) {
throw new Error(`Transactions hash retrieval is not implemented for this coin!`)
}
module.exports = {
balance,
sendCoins,
// newAddress,
newAddress,
getStatus,
// sweep,
sweep,
defaultAddress,
supportsHd: true,
newFunding,
connect,
// getTxHashesByAddress,
}
getTxHashesByAddress,
}