@ -11,8 +11,12 @@ import libcoreSyncAccount from 'commands/libcoreSyncAccount'
import libcoreSignAndBroadcast from 'commands/libcoreSignAndBroadcast'
import libcoreSignAndBroadcast from 'commands/libcoreSignAndBroadcast'
import libcoreGetFees from 'commands/libcoreGetFees'
import libcoreGetFees from 'commands/libcoreGetFees'
import libcoreValidAddress from 'commands/libcoreValidAddress'
import libcoreValidAddress from 'commands/libcoreValidAddress'
import { createCustomErrorClass } from 'helpers/errors'
import type { WalletBridge , EditProps } from './types'
import type { WalletBridge , EditProps } from './types'
const NOT_ENOUGH_FUNDS = 52
const NotEnoughBalance = createCustomErrorClass ( 'NotEnoughBalance' )
const notImplemented = new Error ( 'LibcoreBridge: not implemented' )
const notImplemented = new Error ( 'LibcoreBridge: not implemented' )
type Transaction = {
type Transaction = {
@ -65,10 +69,13 @@ const isRecipientValid = (currency, recipient) => {
const feesLRU = LRU ( { max : 100 } )
const feesLRU = LRU ( { max : 100 } )
const getFeesKey = ( a , t ) =>
` ${ a . id } _ ${ a . blockHeight || 0 } _ ${ t . amount } _ ${ t . recipient } _ ${ t . feePerByte } `
const getFees = async ( a , transaction ) => {
const getFees = async ( a , transaction ) => {
const isValid = await isRecipientValid ( a . currency , transaction . recipient )
const isValid = await isRecipientValid ( a . currency , transaction . recipient )
if ( ! isValid ) return null
if ( ! isValid ) return null
const key = ` ${ a . id } _ ${ transaction . amount } _ ${ transaction . recipient } _ ${ transaction . feePerByte } `
const key = getFeesKey ( a , transaction )
let promise = feesLRU . get ( key )
let promise = feesLRU . get ( key )
if ( promise ) return promise
if ( promise ) return promise
promise = libcoreGetFees
promise = libcoreGetFees
@ -79,6 +86,19 @@ const getFees = async (a, transaction) => {
return promise
return promise
}
}
const checkCanBeSpent = ( a , t ) =>
! t . amount
? Promise . resolve ( )
: getFees ( a , t )
. then ( ( ) => { } )
. catch ( e => {
if ( e . code === NOT_ENOUGH_FUNDS ) {
throw new NotEnoughBalance ( )
}
feesLRU . del ( getFeesKey ( a , t ) )
throw e
} )
const LibcoreBridge : WalletBridge < Transaction > = {
const LibcoreBridge : WalletBridge < Transaction > = {
scanAccountsOnDevice ( currency , devicePath ) {
scanAccountsOnDevice ( currency , devicePath ) {
return libcoreScanAccounts
return libcoreScanAccounts
@ -173,12 +193,7 @@ const LibcoreBridge: WalletBridge<Transaction> = {
isValidTransaction : ( a , t ) => ( t . amount > 0 && t . recipient && true ) || false ,
isValidTransaction : ( a , t ) => ( t . amount > 0 && t . recipient && true ) || false ,
canBeSpent : ( a , t ) =>
checkCanBeSpent ,
! t . amount
? Promise . resolve ( true )
: getFees ( a , t )
. then ( ( ) => true )
. catch ( ( ) => false ) ,
getTotalSpent : ( a , t ) =>
getTotalSpent : ( a , t ) =>
! t . amount
! t . amount