Merge branch 'release-8.0' into 8.0-to-8.1-merge
This commit is contained in:
commit
e7c2456ff7
25 changed files with 149 additions and 75 deletions
|
|
@ -11,7 +11,7 @@ const bitpay = require('../ticker/bitpay')
|
|||
const binance = require('../exchange/binance')
|
||||
const logger = require('../../logger')
|
||||
|
||||
const { BTC, BCH, DASH, ETH, LTC, ZEC } = COINS
|
||||
const { BTC, BCH, DASH, ETH, LTC, ZEC, USDT } = COINS
|
||||
|
||||
const ALL = {
|
||||
cex: cex,
|
||||
|
|
@ -22,7 +22,7 @@ const ALL = {
|
|||
itbit: itbit,
|
||||
bitpay: bitpay,
|
||||
coinbase: {
|
||||
CRYPTO: [BTC, ETH, LTC, DASH, ZEC, BCH],
|
||||
CRYPTO: [BTC, ETH, LTC, DASH, ZEC, BCH, USDT],
|
||||
FIAT: 'ALL_CURRENCIES'
|
||||
},
|
||||
binance: binance
|
||||
|
|
|
|||
|
|
@ -4,8 +4,8 @@ const _ = require('lodash/fp')
|
|||
const { ORDER_TYPES } = require('./consts')
|
||||
|
||||
const ORDER_TYPE = ORDER_TYPES.MARKET
|
||||
const { BTC, BCH, DASH, ETH, LTC, ZEC } = COINS
|
||||
const CRYPTO = [BTC, ETH, LTC, DASH, ZEC, BCH]
|
||||
const { BTC, BCH, DASH, ETH, LTC, ZEC, USDT } = COINS
|
||||
const CRYPTO = [BTC, ETH, LTC, DASH, ZEC, BCH, USDT]
|
||||
const FIAT = ['USD']
|
||||
const REQUIRED_CONFIG_FIELDS = ['apiKey', 'privateKey']
|
||||
|
||||
|
|
|
|||
|
|
@ -4,8 +4,8 @@ const _ = require('lodash/fp')
|
|||
const { ORDER_TYPES } = require('./consts')
|
||||
|
||||
const ORDER_TYPE = ORDER_TYPES.MARKET
|
||||
const { BTC, ETH, LTC, BCH } = COINS
|
||||
const CRYPTO = [BTC, ETH, LTC, BCH]
|
||||
const { BTC, ETH, LTC, BCH, USDT } = COINS
|
||||
const CRYPTO = [BTC, ETH, LTC, BCH, USDT]
|
||||
const FIAT = ['USD', 'EUR']
|
||||
const AMOUNT_PRECISION = 8
|
||||
const REQUIRED_CONFIG_FIELDS = ['key', 'secret', 'clientId']
|
||||
|
|
|
|||
|
|
@ -4,8 +4,8 @@ const _ = require('lodash/fp')
|
|||
const { ORDER_TYPES } = require('./consts')
|
||||
|
||||
const ORDER_TYPE = ORDER_TYPES.MARKET
|
||||
const { BTC, BCH, DASH, ETH, LTC } = COINS
|
||||
const CRYPTO = [BTC, ETH, LTC, DASH, BCH]
|
||||
const { BTC, BCH, DASH, ETH, LTC, USDT } = COINS
|
||||
const CRYPTO = [BTC, ETH, LTC, DASH, BCH, USDT]
|
||||
const FIAT = ['USD', 'EUR']
|
||||
const REQUIRED_CONFIG_FIELDS = ['apiKey', 'privateKey']
|
||||
|
||||
|
|
|
|||
|
|
@ -4,8 +4,8 @@ const _ = require('lodash/fp')
|
|||
const { ORDER_TYPES } = require('./consts')
|
||||
|
||||
const ORDER_TYPE = ORDER_TYPES.MARKET
|
||||
const { BTC, BCH, ETH, LTC } = COINS
|
||||
const CRYPTO = [BTC, ETH, LTC, BCH]
|
||||
const { BTC, BCH, ETH, LTC, USDT } = COINS
|
||||
const CRYPTO = [BTC, ETH, LTC, BCH, USDT]
|
||||
const FIAT = ['USD']
|
||||
const REQUIRED_CONFIG_FIELDS = ['apiKey', 'privateKey']
|
||||
|
||||
|
|
|
|||
|
|
@ -4,8 +4,8 @@ const { ORDER_TYPES } = require('./consts')
|
|||
const { COINS } = require('@lamassu/coins')
|
||||
|
||||
const ORDER_TYPE = ORDER_TYPES.LIMIT
|
||||
const { BTC, ETH } = COINS
|
||||
const CRYPTO = [BTC, ETH]
|
||||
const { BTC, ETH, USDT } = COINS
|
||||
const CRYPTO = [BTC, ETH, USDT]
|
||||
const FIAT = ['USD']
|
||||
const AMOUNT_PRECISION = 4
|
||||
const REQUIRED_CONFIG_FIELDS = ['clientKey', 'clientSecret', 'userId', 'walletId']
|
||||
|
|
|
|||
|
|
@ -4,8 +4,8 @@ const { ORDER_TYPES } = require('./consts')
|
|||
const { COINS } = require('@lamassu/coins')
|
||||
|
||||
const ORDER_TYPE = ORDER_TYPES.MARKET
|
||||
const { BTC, BCH, DASH, ETH, LTC, ZEC, XMR } = COINS
|
||||
const CRYPTO = [BTC, ETH, LTC, DASH, ZEC, BCH, XMR]
|
||||
const { BTC, BCH, DASH, ETH, LTC, ZEC, XMR, USDT } = COINS
|
||||
const CRYPTO = [BTC, ETH, LTC, DASH, ZEC, BCH, XMR, USDT]
|
||||
const FIAT = ['USD', 'EUR']
|
||||
const AMOUNT_PRECISION = 6
|
||||
const REQUIRED_CONFIG_FIELDS = ['apiKey', 'privateKey']
|
||||
|
|
|
|||
|
|
@ -6,13 +6,15 @@ const web3 = new Web3()
|
|||
const hdkey = require('ethereumjs-wallet/hdkey')
|
||||
const { FeeMarketEIP1559Transaction } = require('@ethereumjs/tx')
|
||||
const { default: Common, Chain, Hardfork } = require('@ethereumjs/common')
|
||||
const Tx = require('ethereumjs-tx')
|
||||
const { default: PQueue } = require('p-queue')
|
||||
const util = require('ethereumjs-util')
|
||||
const coins = require('@lamassu/coins')
|
||||
const { default: PQueue } = require('p-queue')
|
||||
|
||||
const pify = require('pify')
|
||||
const _pify = require('pify')
|
||||
const BN = require('../../../bn')
|
||||
const ABI = require('../../tokens')
|
||||
const logger = require('../../../logger')
|
||||
|
||||
exports.SUPPORTED_MODULES = ['wallet']
|
||||
|
||||
|
|
@ -40,6 +42,19 @@ const SWEEP_QUEUE = new PQueue({
|
|||
interval: 250,
|
||||
})
|
||||
|
||||
const infuraCalls = {}
|
||||
|
||||
const pify = _function => {
|
||||
if (_.isString(_function.call)) logInfuraCall(_function.call)
|
||||
return _pify(_function)
|
||||
}
|
||||
|
||||
const logInfuraCall = call => {
|
||||
if (!_.includes('infura', web3.currentProvider.host)) return
|
||||
_.isNil(infuraCalls[call]) ? infuraCalls[call] = 1 : infuraCalls[call]++
|
||||
logger.info(`Calling web3 method ${call} via Infura. Current count for this session: ${JSON.stringify(infuraCalls)}`)
|
||||
}
|
||||
|
||||
function connect (url) {
|
||||
web3.setProvider(new web3.providers.HttpProvider(url))
|
||||
}
|
||||
|
|
@ -56,7 +71,9 @@ function isStrictAddress (cryptoCode, toAddress, settings, operatorId) {
|
|||
|
||||
function sendCoins (account, tx, settings, operatorId, feeMultiplier) {
|
||||
const { toAddress, cryptoAtoms, cryptoCode } = tx
|
||||
return generateTx(toAddress, defaultWallet(account), cryptoAtoms, false, cryptoCode)
|
||||
const isErc20Token = coins.utils.isErc20Token(cryptoCode)
|
||||
|
||||
return (isErc20Token ? generateErc20Tx : generateTx)(toAddress, defaultWallet(account), cryptoAtoms, false, cryptoCode)
|
||||
.then(pify(web3.eth.sendSignedTransaction))
|
||||
.then(txid => {
|
||||
return pify(web3.eth.getTransaction)(txid)
|
||||
|
|
@ -84,14 +101,16 @@ function balance (account, cryptoCode, settings, operatorId) {
|
|||
|
||||
const pendingBalance = (address, cryptoCode) => {
|
||||
const promises = [_balance(true, address, cryptoCode), _balance(false, address, cryptoCode)]
|
||||
return Promise.all(promises).then(([pending, confirmed]) => pending.minus(confirmed))
|
||||
return Promise.all(promises).then(([pending, confirmed]) => BN(pending).minus(confirmed))
|
||||
}
|
||||
const confirmedBalance = (address, cryptoCode) => _balance(false, address, cryptoCode)
|
||||
|
||||
function _balance (includePending, address, cryptoCode) {
|
||||
if (coins.utils.isErc20Token(cryptoCode)) {
|
||||
const contract = web3.eth.contract(ABI.ERC20).at(coins.utils.getErc20Token(cryptoCode).contractAddress)
|
||||
return contract.balanceOf(address.toLowerCase())
|
||||
const contract = new web3.eth.Contract(ABI.ERC20, coins.utils.getErc20Token(cryptoCode).contractAddress)
|
||||
return contract.methods.balanceOf(address.toLowerCase()).call((_, balance) => {
|
||||
return contract.methods.decimals().call((_, decimals) => BN(balance).div(10 ** decimals))
|
||||
})
|
||||
}
|
||||
const block = includePending ? 'pending' : undefined
|
||||
return pify(web3.eth.getBalance)(address.toLowerCase(), block)
|
||||
|
|
@ -99,17 +118,66 @@ function _balance (includePending, address, cryptoCode) {
|
|||
.then(balance => balance ? BN(balance) : BN(0))
|
||||
}
|
||||
|
||||
function generateErc20Tx (_toAddress, wallet, amount, includesFee, cryptoCode) {
|
||||
const fromAddress = '0x' + wallet.getAddress().toString('hex')
|
||||
|
||||
const toAddress = coins.utils.getErc20Token(cryptoCode).contractAddress
|
||||
|
||||
const contract = new web3.eth.Contract(ABI.ERC20, toAddress)
|
||||
const contractData = contract.methods.transfer(_toAddress.toLowerCase(), hex(amount))
|
||||
|
||||
const txTemplate = {
|
||||
from: fromAddress,
|
||||
to: toAddress,
|
||||
value: hex(BN(0)),
|
||||
data: contractData.encodeABI()
|
||||
}
|
||||
|
||||
const common = new Common({ chain: Chain.Mainnet, hardfork: Hardfork.London })
|
||||
|
||||
const promises = [
|
||||
pify(contractData.estimateGas)(txTemplate),
|
||||
pify(web3.eth.getTransactionCount)(fromAddress),
|
||||
pify(web3.eth.getBlock)('pending')
|
||||
]
|
||||
|
||||
return Promise.all(promises)
|
||||
.then(([gas, txCount, { baseFeePerGas }]) => [
|
||||
BN(gas),
|
||||
_.max([0, txCount, lastUsedNonces[fromAddress] + 1]),
|
||||
BN(baseFeePerGas)
|
||||
])
|
||||
.then(([gas, txCount, baseFeePerGas]) => {
|
||||
lastUsedNonces[fromAddress] = txCount
|
||||
|
||||
const maxPriorityFeePerGas = new BN(web3.utils.toWei('2.5', 'gwei')) // web3 default value
|
||||
const maxFeePerGas = new BN(2).times(baseFeePerGas).plus(maxPriorityFeePerGas)
|
||||
|
||||
const rawTx = {
|
||||
chainId: 1,
|
||||
nonce: txCount,
|
||||
maxPriorityFeePerGas: web3.utils.toHex(maxPriorityFeePerGas),
|
||||
maxFeePerGas: web3.utils.toHex(maxFeePerGas),
|
||||
gasLimit: hex(gas),
|
||||
to: toAddress,
|
||||
from: fromAddress,
|
||||
value: hex(BN(0)),
|
||||
data: contractData.encodeABI()
|
||||
}
|
||||
|
||||
const tx = FeeMarketEIP1559Transaction.fromTxData(rawTx, { common })
|
||||
const privateKey = wallet.getPrivateKey()
|
||||
|
||||
const signedTx = tx.sign(privateKey)
|
||||
|
||||
return '0x' + signedTx.serialize().toString('hex')
|
||||
})
|
||||
}
|
||||
|
||||
function generateTx (_toAddress, wallet, amount, includesFee, cryptoCode) {
|
||||
const fromAddress = '0x' + wallet.getAddress().toString('hex')
|
||||
|
||||
const isErc20Token = coins.utils.isErc20Token(cryptoCode)
|
||||
const toAddress = isErc20Token ? coins.utils.getErc20Token(cryptoCode).contractAddress : _toAddress.toLowerCase()
|
||||
|
||||
let contract, contractData
|
||||
if (isErc20Token) {
|
||||
contract = web3.eth.contract(ABI.ERC20).at(toAddress)
|
||||
contractData = isErc20Token && contract.transfer.getData(_toAddress.toLowerCase(), hex(toSend))
|
||||
}
|
||||
const toAddress = _toAddress.toLowerCase()
|
||||
|
||||
const txTemplate = {
|
||||
from: fromAddress,
|
||||
|
|
@ -117,9 +185,7 @@ function generateTx (_toAddress, wallet, amount, includesFee, cryptoCode) {
|
|||
value: amount.toString()
|
||||
}
|
||||
|
||||
if (isErc20Token) txTemplate.data = contractData
|
||||
|
||||
const common = new Common({ chain: Chain.Ropsten, hardfork: Hardfork.London })
|
||||
const common = new Common({ chain: Chain.Mainnet, hardfork: Hardfork.London })
|
||||
|
||||
const promises = [
|
||||
pify(web3.eth.estimateGas)(txTemplate),
|
||||
|
|
@ -129,10 +195,11 @@ function generateTx (_toAddress, wallet, amount, includesFee, cryptoCode) {
|
|||
]
|
||||
|
||||
return Promise.all(promises)
|
||||
.then(([gas, gasPrice, txCount]) => [
|
||||
.then(([gas, gasPrice, txCount, { baseFeePerGas }]) => [
|
||||
BN(gas),
|
||||
BN(gasPrice),
|
||||
_.max([0, txCount, lastUsedNonces[fromAddress] + 1])
|
||||
_.max([0, txCount, lastUsedNonces[fromAddress] + 1]),
|
||||
BN(baseFeePerGas)
|
||||
])
|
||||
.then(([gas, gasPrice, txCount, baseFeePerGas]) => {
|
||||
lastUsedNonces[fromAddress] = txCount
|
||||
|
|
@ -141,7 +208,7 @@ function generateTx (_toAddress, wallet, amount, includesFee, cryptoCode) {
|
|||
? amount.minus(gasPrice.times(gas))
|
||||
: amount
|
||||
|
||||
const maxPriorityFeePerGas = new BN(2.5) // web3 default value
|
||||
const maxPriorityFeePerGas = new BN(web3.utils.toWei('2.5', 'gwei')) // web3 default value
|
||||
const maxFeePerGas = new BN(2).times(baseFeePerGas).plus(maxPriorityFeePerGas)
|
||||
|
||||
const rawTx = {
|
||||
|
|
@ -152,11 +219,7 @@ function generateTx (_toAddress, wallet, amount, includesFee, cryptoCode) {
|
|||
gasLimit: hex(gas),
|
||||
to: toAddress,
|
||||
from: fromAddress,
|
||||
value: isErc20Token ? hex(BN(0)) : hex(toSend)
|
||||
}
|
||||
|
||||
if (isErc20Token) {
|
||||
rawTx.data = contractData
|
||||
value: hex(toSend)
|
||||
}
|
||||
|
||||
const tx = FeeMarketEIP1559Transaction.fromTxData(rawTx, { common })
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue