feat: cash-in implementation for trx + usdt
This commit is contained in:
parent
b399ff0110
commit
a1a27826b8
10 changed files with 5282 additions and 186 deletions
|
|
@ -88,10 +88,6 @@ function sweep (account, txId, cryptoCode, hdIndex, settings, operatorId) {
|
|||
throw new E.NotImplementedError()
|
||||
}
|
||||
|
||||
function isStrictAddress (cryptoCode, toAddress, settings, operatorId) {
|
||||
throw new E.NotImplementedError()
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
balance,
|
||||
sendCoins,
|
||||
|
|
@ -100,5 +96,4 @@ module.exports = {
|
|||
getStatus,
|
||||
sweep,
|
||||
supportsHd: true,
|
||||
isStrictAddress
|
||||
}
|
||||
|
|
|
|||
122
lib/plugins/wallet/tron/base.js
Normal file
122
lib/plugins/wallet/tron/base.js
Normal file
|
|
@ -0,0 +1,122 @@
|
|||
const TronWeb = require('tronweb')
|
||||
const coins = require('@lamassu/coins')
|
||||
|
||||
let tronWeb = null
|
||||
|
||||
const PAYMENT_PREFIX_PATH = "m/44'/195'/0'/0"
|
||||
const DEFAULT_PREFIX_PATH = "m/44'/195'/1'/0"
|
||||
|
||||
function checkCryptoCode (cryptoCode) {
|
||||
if (cryptoCode === 'TRX' || coins.utils.isTrc20Token(cryptoCode)) {
|
||||
return Promise.resolve(cryptoCode)
|
||||
}
|
||||
return Promise.reject(new Error('cryptoCode must be TRX'))
|
||||
}
|
||||
|
||||
function defaultWallet (account) {
|
||||
const mnemonic = account.mnemonic
|
||||
if (!mnemonic) throw new Error('No mnemonic seed!')
|
||||
|
||||
const key = TronWeb.fromMnemonic(masterSeed, `${DEFAULT_PREFIX_PATH}\\0`)
|
||||
|
||||
return key
|
||||
}
|
||||
|
||||
function defaultAddress (account) {
|
||||
return defaultWallet(account).address
|
||||
}
|
||||
|
||||
function balance (account, cryptoCode, settings, operatorId) {
|
||||
return checkCryptoCode(cryptoCode)
|
||||
.then(code => confirmedBalance(defaultAddress(account), code))
|
||||
}
|
||||
|
||||
const confirmedBalance = (address, cryptoCode) => _balance(address, cryptoCode)
|
||||
|
||||
const _balance = async (address, cryptoCode) => {
|
||||
if (coins.utils.isTrc20Token(cryptoCode)) {
|
||||
const contractAddress = coins.utils.getTrc20Token(cryptoCode).contractAddress
|
||||
const { abi } = await tronWeb.trx.getContract(contractAddress)
|
||||
const contract = tronWeb.contract(abi.entrys, contractAddress)
|
||||
|
||||
const balance = await contract.methods.balanceOf(account).call()
|
||||
const decimals = await contract.methods.decimals().call()
|
||||
return BN(balance).div(10 ** decimals)
|
||||
}
|
||||
|
||||
const balance = await tronWeb.trx.getBalance(address)
|
||||
return balance ? BN(balance) : BN(0)
|
||||
}
|
||||
|
||||
const sendCoins = async (account, tx) => {
|
||||
const { toAddress, cryptoAtoms, cryptoCode } = tx
|
||||
const isTrc20Token = coins.utils.isTrc20Token(cryptoCode)
|
||||
|
||||
const txFunction = isTrc20Token ? generateTrc20Tx : generateTx
|
||||
const tx = await txFunction(toAddress, defaultWallet(account), cryptoAtoms, cryptoCode)
|
||||
const { transaction } = await tronWeb.trx.sendRawTransaction(tx)
|
||||
const txId = transaction.txId
|
||||
const transactionInfo = tronWeb.trx.getTransactionInfo(txId)
|
||||
|
||||
if (!transactionInfo) return { txId }
|
||||
|
||||
const fee = new BN(tx.fee).decimalPlaces(0)
|
||||
return { txid, fee }
|
||||
}
|
||||
|
||||
const generateTrc20Tx = async (toAddress, wallet, amount, includesFee, cryptoCode) => {
|
||||
const contractAddress = coins.utils.getTrc20Token(cryptoCode).contractAddress
|
||||
const functionSelector = 'transferFrom(address,address,uint256)'
|
||||
const parameters = [
|
||||
{ type: 'address', value: wallet.address },
|
||||
{ type: 'address', value: toAddress},
|
||||
{ type: 'uint256', value: amount }
|
||||
]
|
||||
|
||||
const tx = await tronWeb.transactionBuilder.triggerSmartContract(contractAddress, functionSelector, {}, parameters)
|
||||
|
||||
return tronWeb.trx.sign(tx.transaction, privateKey)
|
||||
}
|
||||
|
||||
function generateTx (toAddress, wallet, amount) {
|
||||
const transaction = tronWeb.transactionBuilder.sendTrx(toAddress, amount, wallet.address)
|
||||
|
||||
const privateKey = wallet.getPrivateKey()
|
||||
return tronWeb.trx.sign(transaction, privateKey)
|
||||
}
|
||||
|
||||
function newFunding (account, cryptoCode) {
|
||||
return checkCryptoCode(cryptoCode)
|
||||
.then(code => {
|
||||
const fundingAddress = defaultAddress(account)
|
||||
|
||||
return confirmedBalance(fundingAddress, code)
|
||||
.then((balance) => ({
|
||||
fundingPendingBalance: 0,
|
||||
fundingConfirmedBalance: balance,
|
||||
fundingAddress
|
||||
}))
|
||||
})
|
||||
}
|
||||
|
||||
function connect(account) {
|
||||
const apiKey = account.apiKey
|
||||
tronWeb = new TronWeb({
|
||||
fullHost: 'https://api.trongrid.io',
|
||||
headers: { "TRON-PRO-API-KEY": apiKey }
|
||||
})
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
balance,
|
||||
sendCoins,
|
||||
// newAddress,
|
||||
// getStatus,
|
||||
// sweep,
|
||||
defaultAddress,
|
||||
supportsHd: true,
|
||||
newFunding,
|
||||
connect,
|
||||
// getTxHashesByAddress,
|
||||
// _balance
|
||||
}
|
||||
1
lib/plugins/wallet/trongrid/trongrid.js
Normal file
1
lib/plugins/wallet/trongrid/trongrid.js
Normal file
|
|
@ -0,0 +1 @@
|
|||
const NAME = 'trongrid'
|
||||
Loading…
Add table
Add a link
Reference in a new issue