Skip to content

Commit

Permalink
Merge pull request #76 from galacticcouncil/erc20-tokens
Browse files Browse the repository at this point in the history
Use CurrenciesApi in BalanceClient, Support ERC20 tokens
  • Loading branch information
nohaapav authored Nov 7, 2024
2 parents 9d695ad + 0c3290f commit e5f35a6
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 17 deletions.
5 changes: 5 additions & 0 deletions .changeset/fresh-chairs-carry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@galacticcouncil/sdk': minor
---

Use CurrenciesApi in BalanceClient, support for ERC20 tokens
8 changes: 7 additions & 1 deletion packages/sdk/src/client/AssetClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
46 changes: 30 additions & 16 deletions packages/sdk/src/client/BalanceClient.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -10,26 +13,37 @@ export class BalanceClient extends PolkadotApiClient {
super(api);
}

async getBalance(accountId: string, tokenKey: string): Promise<BigNumber> {
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>(
'OrmlTokensAccountData',
result
);
}

async getSystemBalance(accountId: string): Promise<BigNumber> {
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<BigNumber> {
const { free, reserved, frozen } = await this.api.query.tokens.accounts(
accountId,
tokenKey
return this.api.createType<Vec<ITuple<[u32, OrmlTokensAccountData]>>>(
'Vec<(AssetId, OrmlTokensAccountData)>',
result
);
return this.calculateFreeBalance({ free, feeFrozen: reserved, frozen });
}

async getBalance(accountId: string, tokenKey: string): Promise<BigNumber> {
const data = this.getTokenBalanceData(accountId, tokenKey);
return this.calculateFreeBalance(data);
}

async subscribeBalance(
Expand Down

0 comments on commit e5f35a6

Please sign in to comment.