Skip to content

Commit

Permalink
add logic to get tokens balances for chain and address, change getBal…
Browse files Browse the repository at this point in the history
…ance and getDecimals to work without signer
  • Loading branch information
Rihyx committed Mar 14, 2024
1 parent 3ace99b commit b62f491
Show file tree
Hide file tree
Showing 12 changed files with 180 additions and 62 deletions.
12 changes: 7 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions packages/builder/fixtures/builderParamsMock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ export const moonbaseAlphaMock = new EvmParachain({
isTestChain: true,
key: 'moonbase-alpha',
name: 'Moonbase Alpha',
nativeCurrency: {
decimals: 18,
name: 'MOCK',
symbol: 'MOCK',
},
parachainId: 1000,
rpc: 'https://rpc.api.moonbase.moonbeam.network',
ss58Format: 1287,
Expand Down
3 changes: 2 additions & 1 deletion packages/config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@
"main": "./build/index.cjs",
"dependencies": {
"@moonbeam-network/xcm-builder": "2.0.1",
"@moonbeam-network/xcm-types": "2.0.1"
"@moonbeam-network/xcm-types": "2.0.1",
"viem": "^2.8.6"
},
"peerDependencies": {
"@polkadot/types": "^10.10.1",
Expand Down
5 changes: 5 additions & 0 deletions packages/config/src/ConfigService/ConfigService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ describe('config service', () => {
isTestChain: true,
key: 'test',
name: 'test',
nativeCurrency: {
decimals: 18,
name: 'TEST',
symbol: 'TEST',
},
parachainId: 1000,
rpc: '',
ss58Format: 1287,
Expand Down
36 changes: 36 additions & 0 deletions packages/config/src/chains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,11 @@ export const darwinia = new EvmParachain({
id: 46,
key: 'darwinia',
name: 'Darwinia',
nativeCurrency: {
decimals: 18,
name: 'RING',
symbol: 'RING',
},
parachainId: 2046,
rpc: 'https://rpc.darwinia.network',
ss58Format: 18,
Expand All @@ -355,12 +360,18 @@ export const darwiniaCrab = new EvmParachain({
id: 44,
key: 'darwinia-crab',
name: 'Darwinia Crab',
nativeCurrency: {
decimals: 18,
name: 'CRAB',
symbol: 'CRAB',
},
parachainId: 2105,
rpc: 'https://crab-rpc.darwinia.network',
ss58Format: 18,
ws: 'wss://crab-parachain-rpc.darwinia.network',
});

// FIXME: do we need this chain? it looks dead to me - https://pangoro.subscan.io/
export const darwiniaPangoro = new EvmParachain({
assetsData: [
{
Expand All @@ -382,6 +393,11 @@ export const darwiniaPangoro = new EvmParachain({
isTestChain: true,
key: 'darwinia-pangoro',
name: 'Pangoro',
nativeCurrency: {
decimals: 18,
name: '???',
symbol: '???',
},
parachainId: 2105,
rpc: 'https://pangoro-rpc.darwinia.network',
ss58Format: 18,
Expand Down Expand Up @@ -729,6 +745,11 @@ export const moonbaseAlpha = new EvmParachain({
isTestChain: true,
key: 'moonbase-alpha',
name: 'Moonbase Alpha',
nativeCurrency: {
decimals: 18,
name: 'DEV',
symbol: 'DEV',
},
parachainId: 1000,
rpc: 'https://rpc.api.moonbase.moonbeam.network',
ss58Format: 1287,
Expand Down Expand Up @@ -759,6 +780,11 @@ export const moonbaseBeta = new EvmParachain({
isTestChain: true,
key: 'moonbase-beta',
name: 'Moonbase Beta',
nativeCurrency: {
decimals: 18,
name: 'DEV',
symbol: 'DEV',
},
parachainId: 888,
rpc: 'https://frag-moonbase-beta-rpc.g.moonbase.moonbeam.network',
ss58Format: 1287,
Expand Down Expand Up @@ -931,6 +957,11 @@ export const moonbeam = new EvmParachain({
id: 1284,
key: 'moonbeam',
name: 'Moonbeam',
nativeCurrency: {
decimals: 18,
name: 'Glimmer',
symbol: 'GLMR',
},
parachainId: 2004,
rpc: 'https://rpc.api.moonbeam.network',
ss58Format: 1284,
Expand Down Expand Up @@ -1046,6 +1077,11 @@ export const moonriver = new EvmParachain({
id: 1285,
key: 'moonriver',
name: 'Moonriver',
nativeCurrency: {
decimals: 18,
name: 'MOVR',
symbol: 'MOVR',
},
parachainId: 2023,
rpc: 'https://rpc.api.moonriver.moonbeam.network',
ss58Format: 1285,
Expand Down
13 changes: 13 additions & 0 deletions packages/sdk/src/contract/contract.factory.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { ContractConfig } from '@moonbeam-network/xcm-builder';
import { EvmParachain } from '@moonbeam-network/xcm-types';
import { EvmSigner } from '../sdk.interfaces';
import {
BalanceContractInterface,
TransferContractInterface,
} from './contract.interfaces';
import { Erc20, Xtokens } from './contracts';
import { Erc20Public } from './contracts/Erc20/Erc20Public';

export function createContract(
config: ContractConfig,
Expand All @@ -20,3 +22,14 @@ export function createContract(

throw new Error(`Contract ${config.module} not found`);
}

export function createContractWithoutSigner(
config: ContractConfig,
chain: EvmParachain,
): TransferContractInterface | BalanceContractInterface {
if (config.module === 'Erc20') {
return new Erc20Public(config, chain.getViemChain());
}

throw new Error(`Public Contract ${config.module} not found`);
}
46 changes: 46 additions & 0 deletions packages/sdk/src/contract/contracts/Erc20/Erc20Public.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { ContractConfig } from '@moonbeam-network/xcm-builder';
import { Chain, PublicClient, createPublicClient, http } from 'viem';
import { BalanceContractInterface } from '../../contract.interfaces';
import abi from './Erc20ABI.json';

export class Erc20Public implements BalanceContractInterface {
readonly address: string;

readonly #config: ContractConfig;

readonly #publicClient: PublicClient;

constructor(config: ContractConfig, chain: Chain) {
if (!config.address) {
throw new Error('Contract address is required');
}
this.address = config.address;

this.#config = config;

this.#publicClient = createPublicClient({
chain,
transport: http(),
});
}

async getBalance(): Promise<bigint> {
const data = await this.#publicClient.readContract({
abi,
address: this.#config.address as `0x${string}`,
functionName: 'balanceOf',
});

return data as unknown as bigint;
}

async getDecimals(): Promise<number> {
const data = await this.#publicClient.readContract({
abi,
address: this.#config.address as `0x${string}`,
functionName: 'decimals',
});

return data as unknown as number;
}
}
50 changes: 17 additions & 33 deletions packages/sdk/src/getTransferData/getSourceData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,6 @@ export async function getAssetsBalances({
address,
chain,
assets,
evmSigner,
polkadot,
}: GetAssetsBalancesParams) {
const result = [];
Expand All @@ -358,40 +357,25 @@ export async function getAssetsBalances({

// eslint-disable-next-line no-restricted-syntax
for (const asset of uniqueAssets) {
try {
console.log(asset);

// eslint-disable-next-line no-await-in-loop
const decimals = await getDecimals({
address,
asset: asset.asset,
chain,
config: asset,
evmSigner,
polkadot,
});
// eslint-disable-next-line no-await-in-loop
const decimals = await getDecimals({
address,
asset: asset.asset,
chain,
config: asset,
polkadot,
});

// eslint-disable-next-line no-await-in-loop
const balance = await getBalance({
address,
chain,
config: asset,
decimals,
evmSigner,
polkadot,
});
// eslint-disable-next-line no-await-in-loop
const balance = await getBalance({
address,
chain,
config: asset,
decimals,
polkadot,
});

result.push({ balance, asset: asset.asset });
} catch (error) {
console.log(
'\x1b[34m████████████████████▓▓▒▒░ getSourceData.ts:406 ░▒▒▓▓████████████████████\x1b[0m',
);
console.log('* error = ');
console.log(error);
console.log(
'\x1b[34m▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄\x1b[0m',
);
}
result.push({ asset: asset.asset, balance, decimals });
}
return result;
}
32 changes: 14 additions & 18 deletions packages/sdk/src/getTransferData/getTransferData.utils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { CallType, SubstrateQueryConfig } from '@moonbeam-network/xcm-builder';
import { AssetConfig } from '@moonbeam-network/xcm-config';
import { AnyChain, Asset } from '@moonbeam-network/xcm-types';
import { AnyChain, Asset, EvmParachain } from '@moonbeam-network/xcm-types';
import { convertDecimals, toBigInt } from '@moonbeam-network/xcm-utils';
import { BalanceContractInterface, createContract } from '../contract';
import {
BalanceContractInterface,
createContractWithoutSigner,
} from '../contract';
import { PolkadotService } from '../polkadot';
import { EvmSigner } from '../sdk.interfaces';

Expand All @@ -23,7 +26,6 @@ export async function getBalance({
chain,
config,
decimals,
evmSigner,
polkadot,
}: GetBalancesParams) {
const cfg = config.balance.build({
Expand All @@ -38,13 +40,10 @@ export async function getBalance({
: balance;
}

if (!evmSigner) {
throw new Error(
`getBalance: Evm signer must be provided. Asset: ${config.asset.key}`,
);
}

const contract = createContract(cfg, evmSigner) as BalanceContractInterface;
const contract = createContractWithoutSigner(
cfg,
chain as EvmParachain,
) as BalanceContractInterface;

return contract.getBalance();
}
Expand All @@ -53,8 +52,8 @@ export async function getDecimals({
address,
asset,
config,
evmSigner,
polkadot,
chain,
}: GetDecimalsParams) {
const cfg = config.balance.build({
address,
Expand All @@ -65,13 +64,10 @@ export async function getDecimals({
return polkadot.getAssetDecimals(asset || config.asset);
}

if (!evmSigner) {
throw new Error(
`getDecimals: Evm signer must be provided. Asset: ${asset?.key}`,
);
}

const contract = createContract(cfg, evmSigner) as BalanceContractInterface;
const contract = createContractWithoutSigner(
cfg,
chain as EvmParachain,
) as BalanceContractInterface;

return contract.getDecimals();
}
Expand Down
4 changes: 1 addition & 3 deletions packages/sdk/src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { AnyChain, Asset, Ecosystem } from '@moonbeam-network/xcm-types';
import { getAssetsBalances } from './getTransferData/getSourceData';
import { getTransferData as gtd } from './getTransferData/getTransferData';
import { PolkadotService } from './polkadot';
import { EvmSigner, Signers, TransferData } from './sdk.interfaces';
import { Signers, TransferData } from './sdk.interfaces';

export interface SdkOptions extends Partial<Signers> {
configService?: IConfigService;
Expand Down Expand Up @@ -88,7 +88,6 @@ export function Sdk(options?: SdkOptions) {
export async function getParachainBalances(
chain: AnyChain,
address: string,
evmSigner?: EvmSigner,
): Promise<any> {
const configService = new ConfigService();
const chainsConfig = configService.getChainConfig(chain);
Expand All @@ -100,7 +99,6 @@ export async function getParachainBalances(
chain,
assets,
address,
evmSigner,
polkadot,
});

Expand Down
Loading

0 comments on commit b62f491

Please sign in to comment.