Skip to content

Commit

Permalink
Merge pull request #155 from novasamatech/feat/networks
Browse files Browse the repository at this point in the history
  • Loading branch information
tuul-wq authored Jul 24, 2024
2 parents 1de0448 + 086d2a9 commit 9714953
Show file tree
Hide file tree
Showing 72 changed files with 1,793 additions and 1,085 deletions.
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
# set during github workflow through `npm pkg get version`
PUBLIC_APP_VERSION=$npm_package_version

# chains_(dev|prod).json
PUBLIC_CHAINS_FILE=<CHAIN_MODE>

# mercuryo salt for address
# ask team to provide you some
PUBLIC_WIDGET_SECRET=<SALT_HASH>
Expand Down
27 changes: 26 additions & 1 deletion .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ module.exports = {
'plugin:import/recommended',
'plugin:import/errors',
'plugin:import/warnings',
'plugin:effector/recommended',
'prettier',
],
plugins: ['prettier', 'import'],
plugins: ['effector', 'prettier', 'import'],
parserOptions: { ecmaVersion: 2022 },
settings: {
react: { version: 'detect' },
Expand Down Expand Up @@ -53,6 +54,12 @@ module.exports = {
files: ['**/*.json'],
plugins: ['json'],
},
{
files: ['**/*.test.ts'],
rules: {
'no-restricted-properties': 'off',
},
},
],
rules: {
'import/order': [
Expand All @@ -71,6 +78,24 @@ module.exports = {
],
'sort-imports': ['error', { ignoreDeclarationSort: true }],

"no-unused-vars": "off",
"@typescript-eslint/no-unused-vars": [
"warn",
{
"argsIgnorePattern": "^_",
"varsIgnorePattern": "^_",
"caughtErrorsIgnorePattern": "^_"
}
],

"no-restricted-properties": [
"error",
{
"property": "_internal",
"message": "It's a hidden API for unit testing",
}
],

'newline-before-return': 'error',
'prettier/prettier': ['error', prettierOptions],
'react/no-array-index-key': 'warn',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { useEffect, useState } from 'react';

import { TransactionType, useExtrinsic } from '@/common/extrinsicService';
import { useQueryService } from '@/common/queryService/QueryService.ts';
import { useQueryService } from '@/common/queryService/QueryService';
import { type TransferAsset } from '@/common/types';
import { formatAmount, formatBalance, isStatemineAsset } from '@/common/utils';
import { useAssetHub } from '@/common/utils/hooks/useAssetHub.ts';
import { useAssetHub } from '@/common/utils/hooks/useAssetHub';

type AmountLogicParams = {
selectedAsset?: TransferAsset;
Expand Down
34 changes: 19 additions & 15 deletions app/common/balances/BalanceProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { type PropsWithChildren, createContext, useCallback, useContext, useRef } from 'react';

import { useUnit } from 'effector-react';

import { encodeAddress } from '@polkadot/util-crypto';

import { useChainRegistry } from '@/common/chainRegistry';
import { networkModel } from '@/common/network/network-model';
import { AssetType, type ChainAssetAccount } from '@/common/types';
import { chainAssetAccountIdToString } from '@/common/utils';
import { useNumId } from '@/common/utils/hooks/useNumId';
Expand All @@ -23,7 +25,9 @@ const BalanceProviderContext = createContext<BalanceProviderContextProps>({} as

export const BalanceProvider = ({ children }: PropsWithChildren) => {
const { nextId } = useNumId();
const { getConnection, getChain } = useChainRegistry();
const chains = useUnit(networkModel.$chains);
const connections = useUnit(networkModel.$connections);

const unsubscribeToAccount = useRef<Record<number, ChainAssetAccount>>({});
const accountToState = useRef<StateStore>({});

Expand All @@ -32,9 +36,7 @@ export const BalanceProvider = ({ children }: PropsWithChildren) => {
const key = chainAssetAccountIdToString(account);
const currentState = accountToState.current[key];

if (!currentState) {
registerNewSubscription(account, onUpdate, unsubscribeId);
} else {
if (currentState?.current) {
const currentBalance = currentState.current;

currentState.updaters[unsubscribeId] = onUpdate;
Expand All @@ -45,6 +47,8 @@ export const BalanceProvider = ({ children }: PropsWithChildren) => {
if (currentBalance) {
onUpdate(currentBalance);
}
} else {
registerNewSubscription(account, onUpdate, unsubscribeId).catch(error => console.error(error));
}

return unsubscribeId;
Expand All @@ -54,9 +58,7 @@ export const BalanceProvider = ({ children }: PropsWithChildren) => {
const account = unsubscribeToAccount.current[unsubscribeId];
delete unsubscribeToAccount.current[unsubscribeId];

if (!account) {
return;
}
if (!account) return;

const stateKey = chainAssetAccountIdToString(account);
const state = accountToState.current[stateKey];
Expand Down Expand Up @@ -89,19 +91,21 @@ export const BalanceProvider = ({ children }: PropsWithChildren) => {
): Promise<void> {
setupSubscriptionState(account, unsubscribeId, onUpdate);

const chain = await getChain(account.chainId);
const chain = chains[account.chainId];
const api = connections[account.chainId].api;

if (!chain) {
throw `No chain found ${account.chainId}`;
}

const address = encodeAddress(account.publicKey, chain.addressPrefix);

const connection = await getConnection(account.chainId);
if (!api || !api.isConnected) {
throw `No ApiPromise or it's not connected for ${account.chainId}`;
}

unsubscribeToAccount.current[unsubscribeId] = account;

const service = createBalanceService(connection);
const address = encodeAddress(account.publicKey, chain.addressPrefix);
const service = createBalanceService(api);

const handleUpdate = (balance: IAssetBalance) => {
console.log(`New balance: ${balance.total().toString()}`);
Expand Down Expand Up @@ -132,9 +136,9 @@ export const BalanceProvider = ({ children }: PropsWithChildren) => {
accountToState.current[stateKey] = state;

return true;
} else {
return false;
}

return false;
}

function updateBalance(account: ChainAssetAccount, newBalance: IAssetBalance) {
Expand Down
41 changes: 13 additions & 28 deletions app/common/balances/BalanceService.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { type ApiPromise } from '@polkadot/api';
import { BN } from '@polkadot/util';

import { type Asset, type Connection } from '@/common/chainRegistry/types';
import { type Address } from '@/common/types';
import { type Asset } from '@/types/substrate';

import { type IAssetBalance } from './types';

Expand All @@ -14,9 +14,9 @@ export interface IBalanceService {
) => Promise<() => void>;
}

export const createBalanceService = (connection: Connection): IBalanceService => {
export const createBalanceService = (api: ApiPromise): IBalanceService => {
async function subscribe(address: Address, onUpdate: (result: IAssetBalance) => void): Promise<() => void> {
return connection.api.query.system.account.multi([address], (accountInfoList: any[]) => {
return api.query.system.account.multi([address], (accountInfoList: any[]) => {
let frozen: BN;

const data = accountInfoList[0].data;
Expand All @@ -33,19 +33,10 @@ export const createBalanceService = (connection: Connection): IBalanceService =>
const total = free.add(reserved);
const transferable = free.gt(frozen) ? free.sub(frozen) : new BN(0);

const totalString = total.toString();
const transferableString = transferable.toString();

const assetBalance: IAssetBalance = {
total: () => {
return totalString;
},
transferable: () => {
return transferableString;
},
};

onUpdate(assetBalance);
onUpdate({
total: () => total.toString(),
transferable: () => transferable.toString(),
});
});
}

Expand All @@ -54,20 +45,14 @@ export const createBalanceService = (connection: Connection): IBalanceService =>
asset: Asset,
onUpdate: (result: IAssetBalance) => void,
): Promise<() => void> {
return connection.api.query.assets.account.multi([[asset.typeExtras?.assetId, address]], data => {
return api.query.assets.account.multi([[asset.typeExtras?.assetId, address]], data => {
const balance = data[0].isNone ? '0' : data[0].unwrap().balance.toString();

// No frozen and lock for statemine
const assetBalance: IAssetBalance = {
total: () => {
return balance;
},
transferable: () => {
return balance;
},
};

onUpdate(assetBalance);
onUpdate({
total: () => balance,
transferable: () => balance,
});
});
}

Expand Down
29 changes: 0 additions & 29 deletions app/common/chainRegistry/CachedMetadataConnection.ts

This file was deleted.

52 changes: 0 additions & 52 deletions app/common/chainRegistry/ChainProvider.ts

This file was deleted.

Loading

0 comments on commit 9714953

Please sign in to comment.