Skip to content

Commit

Permalink
standard balances (#1266)
Browse files Browse the repository at this point in the history
* standard balances

* standard cosmos

* add changeset
  • Loading branch information
juanfri-web3 authored Sep 12, 2024
1 parent f3e0d5e commit b4327b9
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 28 deletions.
5 changes: 5 additions & 0 deletions .changeset/moody-plums-sniff.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@xchainjs/xchain-cosmos-sdk': patch
---

Return balances including native and those that are 0
5 changes: 5 additions & 0 deletions .changeset/red-dogs-hug.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@xchainjs/xchain-evm-providers': patch
---

Return balances including native and those that are 0
5 changes: 5 additions & 0 deletions .changeset/small-beds-allow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@xchainjs/xchain-evm': patch
---

Change type in roundRobinGetBalance to TokenAsset
19 changes: 17 additions & 2 deletions packages/xchain-base/__e2e__/base.e2e.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { Balance, FeeOption, Network, TxType } from '@xchainjs/xchain-client'
import { AssetType, TokenAsset, assetAmount, assetToBase, assetToString } from '@xchainjs/xchain-util'
import {
AssetType,
TokenAsset,
assetAmount,
assetFromStringEx,
assetToBase,
assetToString,
} from '@xchainjs/xchain-util'

import { BASEChain, defaultBaseParams } from '../src/const'
import { Client as BaseClient } from '../src/index'
Expand Down Expand Up @@ -33,7 +40,15 @@ describe('Base', () => {
it('should fetch Base balances', async () => {
const address = testnetClient.getAddress(0)
console.log(address)
const balances = await testnetClient.getBalance(address)
const balances = await testnetClient.getBalance('0x585142ebBA458B681caea61Bb178E529EdAd23f4')
balances.forEach((bal: Balance) => {
console.log(`${assetToString(bal.asset)} = ${bal.amount.amount()}`)
})
})
it('should fetch filter balances', async () => {
const balances = await testnetClient.getBalance('0x585142ebBA458B681caea61Bb178E529EdAd23f4', [
assetFromStringEx('BASE.WETH-0x4200000000000000000000000000000000000006') as TokenAsset,
])
balances.forEach((bal: Balance) => {
console.log(`${assetToString(bal.asset)} = ${bal.amount.amount()}`)
})
Expand Down
23 changes: 12 additions & 11 deletions packages/xchain-cosmos-sdk/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,18 +273,19 @@ export default abstract class Client extends BaseXChainClient implements XChainC
* @param {Asset[] | undefined} _assets An array of assets. Ignored in this implementation.
* @returns {Balance[]} A promise that resolves to an array of balances.
*/
public async getBalance(address: string, _assets?: CompatibleAsset[]): Promise<Balance[]> {
const result = await this.roundRobinGetBalance(address)
// TODO: Filter using assets
public async getBalance(address: string, assets?: CompatibleAsset[]): Promise<Balance[]> {
const results = await this.roundRobinGetBalance(address)
const balances: Balance[] = []
result.forEach((balance) => {
const asset = this.assetFromDenom(balance.denom)
if (asset) {
balances.push({
asset,
amount: baseAmount(balance.amount, this.getAssetDecimals(asset)),
})
}
const nativeAssetInfo = this.getAssetInfo()

const allAssets = [nativeAssetInfo.asset, ...(assets || [])]

allAssets.forEach((asset) => {
const assetBalance = results.find((result) => result.denom === this.getDenom(asset))
balances.push({
asset,
amount: baseAmount(assetBalance?.amount || 0, this.getAssetDecimals(asset)),
})
})
return balances
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export class CovalentProvider implements EvmOnlineDataProvider {

if (assets) {
finalBalances = balances.filter((balance) => {
return assets.some((asset) => asset.symbol === balance.asset.symbol)
return assets.some((asset) => asset.symbol === balance.asset.symbol || asset.type === AssetType.NATIVE)
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,28 +44,22 @@ export class EtherscanProvider implements EvmOnlineDataProvider {
this.nativeAsset
this.chain
}

async getBalance(address: Address, assets?: CompatibleAsset[]): Promise<Balance[]> {
//validate assets are for the correct chain
assets?.forEach((i) => {
if (i.chain !== this.chain) throw Error(`${assetToString(i)} is not an asset of ${this.chain}`)
})
const balances: Balance[] = []
balances.push(await this.getNativeAssetBalance(address))

if (assets) {
for (const asset of assets) {
if (asset.symbol === this.nativeAsset.symbol) {
balances.push(await this.getNativeAssetBalance(address))
} else {
const splitSymbol = asset.symbol.split('-')
const tokenSymbol = splitSymbol[0]
const contractAddress = splitSymbol[1]
balances.push(await this.getTokenBalance(address, contractAddress, tokenSymbol))
}
const splitSymbol = asset.symbol.split('-')
const tokenSymbol = splitSymbol[0]
const contractAddress = splitSymbol[1]
balances.push(await this.getTokenBalance(address, contractAddress, tokenSymbol))
}
} else {
//get nativeAsset
balances.push(await this.getNativeAssetBalance(address))
// Get All Erc-20 txs
const response = (
await axios.get<GetERC20TxsResponse>(
Expand Down Expand Up @@ -112,6 +106,7 @@ export class EtherscanProvider implements EvmOnlineDataProvider {
amount,
}
}

private getUniqueContractAddresses(array: ERC20Tx[]): ERC20Tx[] {
const mySet = new Set<string>()
return array.filter((x) => {
Expand Down
15 changes: 12 additions & 3 deletions packages/xchain-evm/src/clients/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,16 @@ import {
standardFeeRates,
} from '@xchainjs/xchain-client'
import { EvmOnlineDataProviders } from '@xchainjs/xchain-evm-providers'
import { Address, Asset, CachedValue, Chain, assetToString, baseAmount, eqAsset } from '@xchainjs/xchain-util'
import {
Address,
Asset,
CachedValue,
Chain,
TokenAsset,
assetToString,
baseAmount,
eqAsset,
} from '@xchainjs/xchain-util'
import { BigNumber, ethers } from 'ethers'
import { toUtf8Bytes } from 'ethers/lib/utils'

Expand Down Expand Up @@ -197,7 +206,7 @@ export class Client extends BaseXChainClient implements EVMClient {
* @returns {Promise<Balance[]>} An array containing the balance of the address.
* @throws {"Invalid asset"} Thrown when the provided asset is invalid.
*/
async getBalance(address: Address, assets?: Asset[]): Promise<Balance[]> {
async getBalance(address: Address, assets?: TokenAsset[]): Promise<Balance[]> {
return await this.roundRobinGetBalance(address, assets)
}

Expand Down Expand Up @@ -481,7 +490,7 @@ export class Client extends BaseXChainClient implements EVMClient {
* @returns {Promise<Balance[]>} The balance information for the address.
* @throws Error Thrown if no provider is able to retrieve the balance.
*/
protected async roundRobinGetBalance(address: Address, assets?: Asset[]) {
protected async roundRobinGetBalance(address: Address, assets?: TokenAsset[]) {
for (const provider of this.config.dataProviders) {
try {
const prov = provider[this.network]
Expand Down

0 comments on commit b4327b9

Please sign in to comment.