Skip to content

Commit

Permalink
Aug 5, 2024, 5:16 PM
Browse files Browse the repository at this point in the history
  • Loading branch information
actions-user committed Aug 5, 2024
1 parent a561d07 commit ca7e59b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
9 changes: 7 additions & 2 deletions src/balance-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import { Observable } from "@polkadot/types/types";

import { ChainId, chains } from "./configs";
import { TokenNotFound } from "./errors";
import { BalanceData, BasicToken, FN } from "./types";
import { BalanceData, BasicToken, FN, Relay } from "./types";
import { getAssetRegistryObject, getRelayForChainId } from "./custom-utils";

export interface BalanceAdapterConfigs {
chain: ChainId;
Expand Down Expand Up @@ -37,10 +38,14 @@ export abstract class BalanceAdapter {
// If token not ibn bridge database, check for asset in our asset registry. if it exists, create a new BasicToken with the appropriate values

if (!tokenConfig){
if(!tokenId) throw new TokenNotFound(token, this.chain);

// Get chain number
const paraID = chains[this.chain].paraChainId
const paraId = chains[this.chain].paraChainId
let relay: Relay = getRelayForChainId(this.chain)

// get asset registry object by chain and id
let assetRegistryObject = getAssetRegistryObject(paraId, tokenId, relay)

throw new TokenNotFound(token, this.chain);
}
Expand Down
28 changes: 22 additions & 6 deletions src/custom-utils.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { PolkadotAdapter, KiltAdapter, PendulumAdapter, NodleAdapter, SubsocialAdapter, BifrostPolkadotAdapter, ListenAdapter, KicoAdapter, KaruraAdapter, ShidenAdapter, BifrostAdapter, AltairAdapter, ShadowAdapter, CrabAdapter, BasiliskAdapter, IntegriteeAdapter, KintsugiAdapter, PichiuAdapter, MangataAdapter, CalamariAdapter, MoonriverAdapter, TuringAdapter, HeikoAdapter, KhalaAdapter, KusamaAdapter, RobonomicsAdapter, StatemineAdapter, TinkernetAdapter, QuartzAdapter, StatemintAdapter, AcalaAdapter, HydraDxAdapter, InterlayAdapter, MoonbeamAdapter, ParallelAdapter, UniqueAdapter, CentrifugeAdapter, AstarAdapter, PhalaAdapter, CrustAdapter, MantaAdapter, DarwiniaAdapter, OakAdapter, InvarchAdapter, ZeitgeistAdapter} from './adapters/index'
import { MyAssetRegistryObject } from './types'
import { MyAssetRegistryObject, Relay } from './types'
import fs from 'fs'
import path from 'path';
import { fileURLToPath } from 'url';
import { ChainId } from './configs';
import { kusamaChains } from './configs/chains/kusama-chains';
import { polkadotChains } from './configs/chains/polkadot-chains';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
type Relay = "kusama" | "polkadot"

export function getAdapter(relay: Relay, paraId: number){
if( relay == "kusama"){
if(paraId == 0){
Expand Down Expand Up @@ -157,17 +160,17 @@ export function getAdapter(relay: Relay, paraId: number){
}
}

export function getAssetRegistryObject(chainId: number, localId: string, relay: Relay): MyAssetRegistryObject{
export function getAssetRegistryObject(paraId: number, localId: string, relay: Relay): MyAssetRegistryObject{
let assetRegistry = getAssetRegistry(relay)
let asset = assetRegistry.find((assetRegistryObject: MyAssetRegistryObject) => {
if(chainId == 0 && assetRegistryObject.tokenData.chain == 0){
if(paraId == 0 && assetRegistryObject.tokenData.chain == 0){
return true
}
// console.log(JSON.stringify(assetRegistryObject.tokenData.localId).replace(/\\|"/g, ""))
return assetRegistryObject.tokenData.chain == chainId && JSON.stringify(assetRegistryObject.tokenData.localId).replace(/\\|"/g, "") == localId
return assetRegistryObject.tokenData.chain == paraId && JSON.stringify(assetRegistryObject.tokenData.localId).replace(/\\|"/g, "") == localId
})
if(asset == undefined){
throw new Error(`Asset not found in registry: chainId: ${chainId}, localId: ${localId} | localId stringify: ${JSON.stringify(localId)}`)
throw new Error(`Asset not found in registry: chainId: ${paraId}, localId: ${localId} | localId stringify: ${JSON.stringify(localId)}`)
}
return asset
}
Expand All @@ -177,4 +180,17 @@ export function getAssetRegistry(relay: Relay){
let assetRegistryPath = relay === 'kusama' ? '../../allAssets.json' : '../../polkadot_assets/assets/asset_registry/allAssetsPolkadotCollected.json'
let assetRegistry: MyAssetRegistryObject[] = JSON.parse(fs.readFileSync(path.join(__dirname, assetRegistryPath), 'utf8'));
return assetRegistry
}

export function getRelayForChainId(chainId: ChainId): Relay{
// Ensure chainId is of the correct type
if (chainId in kusamaChains) {
return 'kusama';
} else if (chainId in polkadotChains) {
return 'polkadot';
} else {
throw new Error(`Chain ID ${chainId} not found in any relay.`);
}


}
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,5 @@ export interface MyAsset {
isFrozen?: boolean;
contractAddress?: string;
}

export type Relay = "kusama" | "polkadot"

0 comments on commit ca7e59b

Please sign in to comment.