Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat/get all balances #214

Merged
merged 20 commits into from
Oct 25, 2023
Merged
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
7f48992
feat: add service to fetch evm token balances
genaroibc Oct 10, 2023
7a38420
feat: add methods to fetch cosmos and evm token balances
genaroibc Oct 10, 2023
68adfe4
refactor: simplify types and responses
genaroibc Oct 10, 2023
462eedc
fix: change wrong import path
genaroibc Oct 10, 2023
62c6197
refactor: extract constants
genaroibc Oct 10, 2023
937b9dd
feat: filter tokens by chain ids
genaroibc Oct 11, 2023
432984a
feat: fetch native tokens balances and refactor services
genaroibc Oct 11, 2023
a48f7eb
refactor: rpc urls
genaroibc Oct 11, 2023
651d27a
feat: add service to fetch cosmos balances
genaroibc Oct 14, 2023
4134c9c
feat: add method to get cosmos balances
genaroibc Oct 14, 2023
d8e69ad
feat: return balance in wei instead of formatted
genaroibc Oct 14, 2023
0716155
feat: use cointype and derived address to fetch cosmos balances
genaroibc Oct 20, 2023
bd9dd74
feat: add method to get all balances
genaroibc Oct 24, 2023
9d29205
feat: fetch balances for evm and cosmos chains
genaroibc Oct 24, 2023
8b66179
fix: compare chain ids as strings
genaroibc Oct 24, 2023
81c5267
fix: correctly filter chains
genaroibc Oct 24, 2023
4ff3b47
Merge branch 'main' into feat/get-all-balances
genaroibc Oct 25, 2023
4ad643a
feat: return all chain balances, rename amount to balance, and return…
genaroibc Oct 25, 2023
a9c8b73
Merge branch 'feat/get-all-balances' of https://github.com/0xsquid/sq…
genaroibc Oct 25, 2023
dbc7b65
Merge branch 'main' into feat/get-all-balances
genaroibc Oct 25, 2023
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
Prev Previous commit
Next Next commit
feat: add method to get all balances
genaroibc committed Oct 24, 2023
commit bd9dd7426ea0c38ed82af12b96ba5bdd70975d4c
53 changes: 46 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -33,7 +33,9 @@ import {
ValidateBalanceAndApproval,
WASM_TYPE,
WasmHookMsg,
CosmosChain
CosmosChain,
CosmosAddress,
CosmosBalance
} from "./types";

import { TxRaw } from "cosmjs-types/cosmos/tx/v1beta1/tx";
@@ -743,27 +745,64 @@ export class Squid {
userAddress: string;
chains: number[];
}): Promise<TokenBalance[]> {
// remove invalid and duplicate chains
const filteredChains = new Set(
chains.filter(c => !Number.isNaN(c) && !isNaN(c))
);

return getAllEvmTokensBalance(
this.tokens.filter(t => chains.includes(Number(t.chainId))),
this.tokens.filter(t => filteredChains.has(Number(t.chainId))),
userAddress
);
}

public async getAllCosmosBalances({
addresses
}: {
addresses: Array<{
coinType: number;
chainId: string;
address: string;
}>;
addresses: CosmosAddress[];
}) {
const cosmosChains = this.chains.filter(
c => c.chainType === ChainType.Cosmos
) as CosmosChain[];

return getCosmosBalances({ addresses, cosmosChains });
}

public async getAllBalances({
chainIds,
cosmosAddresses,
evmAddress
}: {
chainIds?: string[];
cosmosAddresses?: CosmosAddress[];
evmAddress?: string;
}): Promise<{
cosmosBalances?: CosmosBalance[];
evmBalances?: TokenBalance[];
}> {
if (!chainIds) {
// fetch balances for all chains compatible with provided addresses
const evmBalances = evmAddress
? await this.getAllEvmBalances({
chains: this.tokens.map(t => Number(t.chainId)),
userAddress: evmAddress
})
: [];

const cosmosBalances = cosmosAddresses
? await this.getAllCosmosBalances({
addresses: cosmosAddresses
})
: [];

return {
evmBalances,
cosmosBalances
};
}

return {};
}
}

export * from "./types";
17 changes: 5 additions & 12 deletions src/services/getCosmosBalances.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { StargateClient } from "@cosmjs/stargate";
import { ChainType, CosmosChain } from "../types";
import { ChainType, CosmosAddress, CosmosBalance, CosmosChain } from "../types";
import { fromBech32, toBech32 } from "@cosmjs/encoding";

const deriveCosmosAddress = (chainPrefix: string, address: string): string => {
@@ -10,18 +10,11 @@ export async function getCosmosBalances({
addresses,
cosmosChains
}: {
addresses: Array<{
coinType: number;
chainId: string;
address: string;
}>;
addresses: CosmosAddress[];
cosmosChains: CosmosChain[];
}) {
const balances: {
amount: string;
denom: string;
chainId: string;
}[] = [];
}): Promise<CosmosBalance[]> {
const balances: CosmosBalance[] = [];

for (const chain of cosmosChains) {
if (chain.chainType !== ChainType.Cosmos) continue;

12 changes: 12 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
@@ -515,3 +515,15 @@ export type AllBalancesResult = {
cosmosBalances: TokenBalance[];
evmBalances: TokenBalance[];
};

export type CosmosAddress = {
coinType: number;
chainId: string;
address: string;
};

export type CosmosBalance = {
amount: string;
denom: string;
chainId: string;
};