From 9f2dce3d337e0b6ff11bda721b9d353b233806bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Von=C3=A1=C5=A1ek?= Date: Fri, 13 Sep 2024 13:52:24 +0200 Subject: [PATCH 1/2] Support ERC20 tokens --- .changeset/lazy-days-applaud.md | 5 +++++ packages/sdk/src/client/AssetClient.ts | 8 +++++++- 2 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 .changeset/lazy-days-applaud.md diff --git a/.changeset/lazy-days-applaud.md b/.changeset/lazy-days-applaud.md new file mode 100644 index 00000000..0192f271 --- /dev/null +++ b/.changeset/lazy-days-applaud.md @@ -0,0 +1,5 @@ +--- +'@galacticcouncil/sdk': minor +--- + +Added support for ERC20 tokens diff --git a/packages/sdk/src/client/AssetClient.ts b/packages/sdk/src/client/AssetClient.ts index de7fea0d..4ea07b74 100644 --- a/packages/sdk/src/client/AssetClient.ts +++ b/packages/sdk/src/client/AssetClient.ts @@ -15,7 +15,13 @@ import { findNestedKey } from '../utils/json'; import { PolkadotApiClient } from './PolkadotApi'; export class AssetClient extends PolkadotApiClient { - private SUPPORTED_TYPES = ['StableSwap', 'Bond', 'Token', 'External']; + private SUPPORTED_TYPES = [ + 'StableSwap', + 'Bond', + 'Token', + 'External', + 'Erc20', + ]; constructor(api: ApiPromise) { super(api); From 0c3290f71d0c7e81d47464672fc7ef249eeca527 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Von=C3=A1=C5=A1ek?= Date: Wed, 30 Oct 2024 14:13:12 +0100 Subject: [PATCH 2/2] Use CurrenciesApi in BalanceClient --- .changeset/fresh-chairs-carry.md | 5 +++ .changeset/lazy-days-applaud.md | 5 --- packages/sdk/src/client/BalanceClient.ts | 46 +++++++++++++++--------- 3 files changed, 35 insertions(+), 21 deletions(-) create mode 100644 .changeset/fresh-chairs-carry.md delete mode 100644 .changeset/lazy-days-applaud.md diff --git a/.changeset/fresh-chairs-carry.md b/.changeset/fresh-chairs-carry.md new file mode 100644 index 00000000..026e02bb --- /dev/null +++ b/.changeset/fresh-chairs-carry.md @@ -0,0 +1,5 @@ +--- +'@galacticcouncil/sdk': minor +--- + +Use CurrenciesApi in BalanceClient, support for ERC20 tokens diff --git a/.changeset/lazy-days-applaud.md b/.changeset/lazy-days-applaud.md deleted file mode 100644 index 0192f271..00000000 --- a/.changeset/lazy-days-applaud.md +++ /dev/null @@ -1,5 +0,0 @@ ---- -'@galacticcouncil/sdk': minor ---- - -Added support for ERC20 tokens diff --git a/packages/sdk/src/client/BalanceClient.ts b/packages/sdk/src/client/BalanceClient.ts index fdc02a05..3a86fdf2 100644 --- a/packages/sdk/src/client/BalanceClient.ts +++ b/packages/sdk/src/client/BalanceClient.ts @@ -1,4 +1,7 @@ import { ApiPromise } from '@polkadot/api'; +import { u32, Vec } from '@polkadot/types'; +import { ITuple } from '@polkadot/types-codec/types'; +import { OrmlTokensAccountData } from '@polkadot/types/lookup'; import { UnsubscribePromise } from '@polkadot/api-base/types'; import { SYSTEM_ASSET_ID } from '../consts'; import { BigNumber } from '../utils/bignumber'; @@ -10,26 +13,37 @@ export class BalanceClient extends PolkadotApiClient { super(api); } - async getBalance(accountId: string, tokenKey: string): Promise { - return tokenKey === SYSTEM_ASSET_ID - ? await this.getSystemBalance(accountId) - : await this.getTokenBalance(accountId, tokenKey); + async getTokenBalanceData(accountId: string, tokenKey: string) { + const params = this.api.createType('(AssetId, AccountId)', [ + tokenKey, + accountId, + ]); + const result = await this.api.rpc.state.call( + 'CurrenciesApi_account', + params.toHex() + ); + return this.api.createType( + 'OrmlTokensAccountData', + result + ); } - async getSystemBalance(accountId: string): Promise { - const { data } = await this.api.query.system.account(accountId); - return this.calculateFreeBalance(data); - } + async getAccountBalanceData(accountId: string) { + const params = this.api.createType('AccountId', accountId); + const result = await this.api.rpc.state.call( + 'CurrenciesApi_accounts', + params.toHex() + ); - async getTokenBalance( - accountId: string, - tokenKey: string - ): Promise { - const { free, reserved, frozen } = await this.api.query.tokens.accounts( - accountId, - tokenKey + return this.api.createType>>( + 'Vec<(AssetId, OrmlTokensAccountData)>', + result ); - return this.calculateFreeBalance({ free, feeFrozen: reserved, frozen }); + } + + async getBalance(accountId: string, tokenKey: string): Promise { + const data = this.getTokenBalanceData(accountId, tokenKey); + return this.calculateFreeBalance(data); } async subscribeBalance(