Skip to content
This repository has been archived by the owner on Apr 11, 2023. It is now read-only.

fix: return unconfirmed balances as part of account balance response #297

Merged
merged 4 commits into from
Jan 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions packages/chain-adapters/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,17 @@
"@shapeshiftoss/hdwallet-core": "^1.18.2",
"@shapeshiftoss/hdwallet-native": "^1.18.2",
"@shapeshiftoss/types": "^1.20.1",
"@shapeshiftoss/unchained-client": "^5.0.0",
"@shapeshiftoss/unchained-tx-parser": "^5.0.0",
"@shapeshiftoss/unchained-client": "^5.1.0",
"@shapeshiftoss/unchained-tx-parser": "^5.1.0",
"bs58check": "^2.0.0"
},
"devDependencies": {
"@shapeshiftoss/caip": "^1.5.1",
"@shapeshiftoss/hdwallet-core": "^1.18.2",
"@shapeshiftoss/hdwallet-native": "^1.18.2",
"@shapeshiftoss/types": "^1.20.0",
"@shapeshiftoss/unchained-client": "^5.0.0",
"@shapeshiftoss/unchained-tx-parser": "^5.0.0",
"@shapeshiftoss/unchained-client": "^5.1.0",
"@shapeshiftoss/unchained-tx-parser": "^5.1.0",
"@types/bs58check": "^2.1.0",
"@types/multicoin-address-validator": "^0.5.0"
}
Expand Down
21 changes: 16 additions & 5 deletions packages/chain-adapters/src/bitcoin/BitcoinChainAdapter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import { HDWallet } from '@shapeshiftoss/hdwallet-core'
import { NativeAdapterArgs, NativeHDWallet } from '@shapeshiftoss/hdwallet-native'
import { BIP44Params, chainAdapters, ChainTypes, UtxoAccountType } from '@shapeshiftoss/types'
import * as unchained from '@shapeshiftoss/unchained-client'

import * as bitcoin from './BitcoinChainAdapter'

Expand Down Expand Up @@ -163,18 +162,30 @@ describe('BitcoinChainAdapter', () => {
getAccount: jest.fn().mockResolvedValue({
data: {
pubkey: '1EjpFGTWJ9CGRJUMA3SdQSdigxM31aXAFx',
balance: '0'
balance: '100',
unconfirmedBalance: '50',
addresses: [],
nextChangeAddressIndex: 0,
nextReceiveAddressIndex: 0
}
})
} as any

const adapter = new bitcoin.ChainAdapter(args)
const exampleResponse: unchained.bitcoin.api.BitcoinAccount = {
const expected: chainAdapters.Account<ChainTypes.Bitcoin> = {
pubkey: '1EjpFGTWJ9CGRJUMA3SdQSdigxM31aXAFx',
balance: '0'
chain: ChainTypes.Bitcoin,
balance: '150',
caip2: 'bip122:000000000019d6689c085ae165831e93',
caip19: 'bip122:000000000019d6689c085ae165831e93/slip44:0',
chainSpecific: {
addresses: [],
nextChangeAddressIndex: 0,
nextReceiveAddressIndex: 0
}
}
const data = await adapter.getAccount('SomeFakeAddress')
expect(data).toMatchObject(exampleResponse)
expect(data).toMatchObject(expected)
expect(args.providers.http.getAccount).toHaveBeenCalled()
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { ErrorHandler } from '../error/ErrorHandler'
import {
accountTypeToOutputScriptType,
accountTypeToScriptType,
bnOrZero,
convertXpubVersion,
getStatus,
getType,
Expand Down Expand Up @@ -115,8 +116,10 @@ export class ChainAdapter implements IChainAdapter<ChainTypes.Bitcoin> {
const { chain, network } = caip2.fromCAIP2(caip)
const { data } = await this.providers.http.getAccount({ pubkey: pubkey })

const balance = bnOrZero(data.balance).plus(bnOrZero(data.unconfirmedBalance))

return {
balance: data.balance,
balance: balance.toString(),
asamere marked this conversation as resolved.
Show resolved Hide resolved
chain: ChainTypes.Bitcoin,
caip2: caip,
caip19: caip19.toCAIP19({ chain, network }),
Expand Down
13 changes: 11 additions & 2 deletions packages/chain-adapters/src/ethereum/EthereumChainAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,14 @@ import { numberToHex } from 'web3-utils'

import { ChainAdapter as IChainAdapter } from '../api'
import { ErrorHandler } from '../error/ErrorHandler'
import { getContractType, getStatus, getType, toPath, toRootDerivationPath } from '../utils'
import {
bnOrZero,
getContractType,
getStatus,
getType,
toPath,
toRootDerivationPath
} from '../utils'
import erc20Abi from './erc20Abi.json'

export interface ChainAdapterArgs {
Expand Down Expand Up @@ -71,8 +78,10 @@ export class ChainAdapter implements IChainAdapter<ChainTypes.Ethereum> {
const { chain, network } = caip2.fromCAIP2(caip)
const { data } = await this.providers.http.getAccount({ pubkey })

const balance = bnOrZero(data.balance).plus(bnOrZero(data.unconfirmedBalance))

return {
balance: data.balance,
balance: balance.toString(),
caip2: caip,
caip19: caip19.toCAIP19({ chain, network }),
chain: ChainTypes.Ethereum,
Expand Down
10 changes: 10 additions & 0 deletions packages/chain-adapters/src/utils/bignumber.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import BigNumber from 'bignumber.js'

export type BN = BigNumber

export const bn = (n: BigNumber.Value, base = 10): BN => new BigNumber(n, base)

export const bnOrZero = (n: BigNumber.Value | null | undefined): BN => {
const value = bn(n || 0)
return value.isNaN() ? bn(0) : value
}
1 change: 1 addition & 0 deletions packages/chain-adapters/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Status, TransferType } from '@shapeshiftoss/unchained-tx-parser'

export * from './bip44'
export * from './utxoUtils'
export * from './bignumber'

export const getContractType = (type?: string): ContractTypes => {
if (!type) return ContractTypes.NONE
Expand Down
45 changes: 23 additions & 22 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2580,10 +2580,10 @@
varuint-bitcoin "^1.0.4"
wif "^2.0.1"

"@shapeshiftoss/blockbook@^5.0.0":
version "5.0.0"
resolved "https://registry.yarnpkg.com/@shapeshiftoss/blockbook/-/blockbook-5.0.0.tgz#90e6ff789be89f591babffd724da69e5deadd751"
integrity sha512-Dk0gbNeaV9Tm/5xQQkEadnALW/bPJcrlXJ28eTCUKvG4SC3WLovCxqqkKAnHsv9GwFz59zZD1iIYMfZmJjxC9g==
"@shapeshiftoss/blockbook@^5.1.0":
version "5.1.0"
resolved "https://registry.yarnpkg.com/@shapeshiftoss/blockbook/-/blockbook-5.1.0.tgz#378ae5a614ae0aadd6192b92d8d0b84b12ce2d30"
integrity sha512-9sDYtXOmF3l64YEVLVJkOHzz5BqS8/QXReWLQmA0mbOHers0NBdrzNweDWyww3ge/Re1/dnSUATX2tQSa0h/fg==
dependencies:
axios "^0.21.2"
express "^4.17.1"
Expand Down Expand Up @@ -2655,32 +2655,32 @@
tiny-secp256k1 "^1.1.6"
web-encoding "^1.1.0"

"@shapeshiftoss/thorchain@^5.0.0":
version "5.0.0"
resolved "https://registry.yarnpkg.com/@shapeshiftoss/thorchain/-/thorchain-5.0.0.tgz#cccc562675922ab9033211ff70c15e705ff50774"
integrity sha512-shwl9axPIxOi6fDlvq1foWd9t4yiSD8auheiIY/I6UPjqQ2E1DLiKpsRHTgs8OJ+RIJxyCaVhitnNbhpHO1/pg==
"@shapeshiftoss/thorchain@^5.1.0":
version "5.1.0"
resolved "https://registry.yarnpkg.com/@shapeshiftoss/thorchain/-/thorchain-5.1.0.tgz#352ec65d51cfa31dad1f112a0011be0bc8f4b1aa"
integrity sha512-u6NwiTXGk1CuQ6JkZYjXfh9Obn5sutZYiMFcwyhghClPGGKa1gfau4E5SJUCVNjFgw1OctA/yegxF2ZRTomdwA==
dependencies:
bignumber.js "^9.0.1"
ethers "^5.4.0"
ethers "^5.5.3"

"@shapeshiftoss/unchained-client@^5.0.0":
version "5.0.0"
resolved "https://registry.yarnpkg.com/@shapeshiftoss/unchained-client/-/unchained-client-5.0.0.tgz#8dcb4f4674c3f9f4749190eb799d7c9741934474"
integrity sha512-x/UY/3FcrouID4W00+t8swd7aTadoJDim6LFsq63IERGvM2n44QqLj77hsuMVO4CBCM6y1pZSkVwd9hQci6a3A==
"@shapeshiftoss/unchained-client@^5.1.0":
version "5.1.0"
resolved "https://registry.yarnpkg.com/@shapeshiftoss/unchained-client/-/unchained-client-5.1.0.tgz#c2fe567286a81370c37aff36d824c690f11d142f"
integrity sha512-kUzvbbPxmHMAp9uBMNrVrXM6YvfTVnv6Mxg6dAGWWV+4L4iiVlyMhmgcKwyBYnujpQQRRFQjn1WLmsbhf/XDXQ==
dependencies:
"@shapeshiftoss/blockbook" "^5.0.0"
"@shapeshiftoss/unchained-tx-parser" "^5.0.0"
"@shapeshiftoss/blockbook" "^5.1.0"
"@shapeshiftoss/unchained-tx-parser" "^5.1.0"
isomorphic-ws "^4.0.1"
ws "^8.3.0"

"@shapeshiftoss/unchained-tx-parser@^5.0.0":
version "5.0.0"
resolved "https://registry.yarnpkg.com/@shapeshiftoss/unchained-tx-parser/-/unchained-tx-parser-5.0.0.tgz#ba3c3dc14994818a4fa1114ada2ebdb2c1b12077"
integrity sha512-77BkxvwEm9RgpHo4BGzh+XCrVJGsABnezXXJHnGwM+QNr09P8xI7rvXRJpUZ4dhWzyAim2TeAdcNedXL8xgBwA==
"@shapeshiftoss/unchained-tx-parser@^5.1.0":
version "5.1.0"
resolved "https://registry.yarnpkg.com/@shapeshiftoss/unchained-tx-parser/-/unchained-tx-parser-5.1.0.tgz#bf669344d1b24ed58de708761864e6bd350efc51"
integrity sha512-Xvi70RLzZUdPfYImBszu3wKqJhxB7CzxOxzkbReyvffuFI84ypKMwq+II44XWmQ1gZgytMAu99y8rYjDWdyYPw==
dependencies:
"@shapeshiftoss/blockbook" "^5.0.0"
"@shapeshiftoss/blockbook" "^5.1.0"
"@shapeshiftoss/caip" "^1.6.1"
"@shapeshiftoss/thorchain" "^5.0.0"
"@shapeshiftoss/thorchain" "^5.1.0"
"@shapeshiftoss/types" "^1.17.0"
bignumber.js "^9.0.1"

Expand Down Expand Up @@ -5628,7 +5628,7 @@ [email protected]:
"@ethersproject/web" "5.4.0"
"@ethersproject/wordlists" "5.4.0"

ethers@^5.4.0, ethers@^5.4.7:
ethers@^5.4.7, ethers@^5.5.3:
version "5.5.3"
resolved "https://registry.yarnpkg.com/ethers/-/ethers-5.5.3.tgz#1e361516711c0c3244b6210e7e3ecabf0c75fca0"
integrity sha512-fTT4WT8/hTe/BLwRUtl7I5zlpF3XC3P/Xwqxc5AIP2HGlH15qpmjs0Ou78az93b1rLITzXLFxoNX63B8ZbUd7g==
Expand Down Expand Up @@ -11416,6 +11416,7 @@ type-fest@^0.8.1:
integrity sha512-4dbzIzqvjtgiM5rw1k5rEHtBANKmdudhGyBEajN01fEyhaAIhsoKNy6y7+IN93IfpFtwY9iqi7kD+xwKhQsNJA==

type-fest@^1.1.1, type-is@~1.6.18:
name type-fest
version "1.6.18"
resolved "https://registry.yarnpkg.com/type-is/-/type-is-1.6.18.tgz#4e552cd05df09467dcbc4ef739de89f2cf37c131"
integrity sha512-TkRKr9sUTxEH8MdfuCSP7VizJyzRNMjj2J2do2Jr3Kym598JVdEksuzPQCnlFPW4ky9Q+iA+ma9BGm06XQBy8g==
Expand Down