Skip to content

Commit

Permalink
Adding EvmChain class
Browse files Browse the repository at this point in the history
  • Loading branch information
ekenigs committed Jul 17, 2024
1 parent 49f3172 commit 91936be
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 52 deletions.
4 changes: 2 additions & 2 deletions packages/builder/src/contract/contracts/Xtokens/Xtokens.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable @typescript-eslint/no-use-before-define */
import { AnyChain } from '@moonbeam-network/xcm-types';
import { AnyChain, EvmParachain } from '@moonbeam-network/xcm-types';
import { formatAssetIdToERC20 } from '@moonbeam-network/xcm-utils';
import { isString, u8aToHex } from '@polkadot/util';
import { decodeAddress, evmToAddress } from '@polkadot/util-crypto';
Expand Down Expand Up @@ -132,7 +132,7 @@ function getDestinationMultilocation(
03: AccountKey20
https://docs.moonbeam.network/builders/interoperability/xcm/xc20/xtokens/#building-the-precompile-multilocation
*/
const accountType = destination.isEvmParachain() ? '03' : '01';
const accountType = EvmParachain.is(destination) ? '03' : '01';
const acc = `0x${accountType}${u8aToHex(
decodeAddress(address),
-1,
Expand Down
5 changes: 0 additions & 5 deletions packages/types/src/chain/Chain.interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@ import type { EvmParachain, Parachain } from './parachain';
export type AnyChain = Parachain | EvmParachain;
export type AnyParachain = Parachain | EvmParachain;

export enum ChainType {
'Parachain' = 'parachain',
'EvmParachain' = 'evm-parachain',
}

export enum Ecosystem {
Polkadot = 'polkadot',
Kusama = 'kusama',
Expand Down
16 changes: 1 addition & 15 deletions packages/types/src/chain/Chain.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Asset, AssetAmount, ChainAsset } from '../asset';
import { ChainType, Ecosystem } from './Chain.interfaces';
import type { EvmParachain, Parachain } from './parachain';
import { Ecosystem } from './Chain.interfaces';

export interface ChainConstructorParams {
assets: Map<string, ChainAsset> | ChainAsset[];
Expand All @@ -10,7 +9,6 @@ export interface ChainConstructorParams {
key: string;
name: string;
nativeAsset: Asset;
type: ChainType;
}

export abstract class Chain {
Expand All @@ -28,8 +26,6 @@ export abstract class Chain {

readonly #nativeAsset: Asset;

readonly type: ChainType;

constructor({
assets,
ecosystem,
Expand All @@ -38,7 +34,6 @@ export abstract class Chain {
key,
name,
nativeAsset,
type,
}: ChainConstructorParams) {
this.assets =
assets instanceof Map
Expand All @@ -50,7 +45,6 @@ export abstract class Chain {
this.key = key;
this.name = name;
this.#nativeAsset = nativeAsset;
this.type = type;
}

get nativeAsset(): ChainAsset {
Expand All @@ -69,12 +63,4 @@ export abstract class Chain {

return chainAsset;
}

isParachain(): this is Parachain {
return this.type === ChainType.Parachain;
}

isEvmParachain(): this is EvmParachain {
return this.type === ChainType.EvmParachain;
}
}
31 changes: 31 additions & 0 deletions packages/types/src/chain/Chain.utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { Chain, defineChain } from 'viem';
import { ChainAsset } from '../asset';

export interface GetViemChainParams {
id: number;
name: string;
nativeAsset: ChainAsset;
rpc: string;
}

export function getViemChain({
id,
name,
nativeAsset,
rpc,
}: GetViemChainParams): Chain {
return defineChain({
id,
name,
nativeCurrency: {
decimals: nativeAsset.decimals,
name: nativeAsset.originSymbol,
symbol: nativeAsset.originSymbol,
},
rpcUrls: {
default: {
http: [rpc],
},
},
});
}
29 changes: 29 additions & 0 deletions packages/types/src/chain/EvmChain.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Chain as ViemChain } from 'viem';
import { Chain, ChainConstructorParams } from './Chain';
import { getViemChain } from './Chain.utils';

export interface EvmChainConstructorParams extends ChainConstructorParams {
id: number;
rpc: string;
}

export class EvmChain extends Chain {
readonly id: number;

readonly rpc: string;

static is(obj: unknown): obj is EvmChain {
return obj instanceof EvmChain;
}

constructor({ id, rpc, ...others }: EvmChainConstructorParams) {
super(others);

this.id = id;
this.rpc = rpc;
}

getViemChain(): ViemChain {
return getViemChain(this);
}
}
1 change: 1 addition & 0 deletions packages/types/src/chain/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './Chain';
export * from './Chain.interfaces';
export * from './EvmChain';
export * from './parachain';
8 changes: 3 additions & 5 deletions packages/types/src/chain/parachain/EvmParachain.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,18 @@ import { describe, expect, it } from 'vitest';

import { Ecosystem } from '../Chain.interfaces';
import { EvmParachain } from './EvmParachain';
import { Asset } from '../../asset';

describe('evmParachain', () => {
const parachain = new EvmParachain({
assets: [],
ecosystem: Ecosystem.Polkadot,
genesisHash:
'0xfe58ea77779b7abda7da4ec526d14db9b1e9cd40a217c34892af80a9b332b76d',
id: 1284,
key: 'moonbeam',
name: 'Moonbeam',
nativeCurrency: {
decimals: 18,
name: 'Glimmer',
symbol: 'GLMR',
},
nativeAsset: new Asset({ key: 'glmr', originSymbol: 'GLMR' }),
parachainId: 2004,
rpc: 'https://rpc.api.moonbeam.network',
ss58Format: 1284,
Expand Down
28 changes: 9 additions & 19 deletions packages/types/src/chain/parachain/EvmParachain.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Address, defineChain } from 'viem';
import { Address } from 'viem';
import { Chain } from 'viem/chains';
import { ChainType } from '../Chain.interfaces';
import { Parachain, ParachainConstructorParams } from './Parachain';
import { getViemChain } from '../Chain.utils';

export interface EvmParachainConstructorParams
extends Omit<ParachainConstructorParams, 'type'> {
extends ParachainConstructorParams {
id: number;
rpc: string;
isEvmSigner?: boolean;
Expand All @@ -24,14 +24,18 @@ export class EvmParachain extends Parachain {

readonly contracts?: Contracts;

static is(obj: unknown): obj is EvmParachain {
return obj instanceof EvmParachain;
}

constructor({
id,
rpc,
isEvmSigner = false,
contracts,
...others
}: EvmParachainConstructorParams) {
super({ type: ChainType.EvmParachain, ...others });
super(others);

this.contracts = contracts;
this.id = id;
Expand All @@ -40,20 +44,6 @@ export class EvmParachain extends Parachain {
}

getViemChain(): Chain {
return defineChain({
id: this.id,
name: this.name,
nativeCurrency: {
decimals: this.nativeAsset.decimals,
name: this.nativeAsset.originSymbol,
symbol: this.nativeAsset.originSymbol,
},
rpcUrls: {
default: {
http: [this.rpc],
webSocket: Array.isArray(this.ws) ? this.ws : [this.ws],
},
},
});
return getViemChain(this);
}
}
3 changes: 3 additions & 0 deletions packages/types/src/chain/parachain/Parachain.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@ import { describe, expect, it } from 'vitest';

import { Ecosystem } from '../Chain.interfaces';
import { Parachain } from './Parachain';
import { Asset } from '../../asset';

describe('parachain', () => {
const parachain = new Parachain({
assets: [],
ecosystem: Ecosystem.Polkadot,
genesisHash:
'0x91b171bb158e2d3848fa23a9f1c25182fb8e20313b2c1eb49219da7a70ce90c3',
key: 'polkadot',
name: 'Polkadot',
nativeAsset: new Asset({ key: 'glmr', originSymbol: 'GLMR' }),
parachainId: 0,
ss58Format: 42,
ws: 'wss://rpc.polkadot.io',
Expand Down
12 changes: 6 additions & 6 deletions packages/types/src/chain/parachain/Parachain.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { SetOptional } from '../../common.interfaces';
import { Chain, ChainConstructorParams } from '../Chain';
import { ChainType } from '../Chain.interfaces';

export interface ParachainConstructorParams
extends SetOptional<ChainConstructorParams, 'type'> {
export interface ParachainConstructorParams extends ChainConstructorParams {
genesisHash: string;
parachainId: number;
ss58Format: number;
Expand All @@ -25,17 +22,20 @@ export class Parachain extends Chain {

readonly ws: string | string[];

static is(obj: unknown): obj is Parachain {
return obj instanceof Parachain;
}

constructor({
genesisHash,
parachainId,
usesChainDecimals,
ss58Format,
weight,
ws,
type = ChainType.Parachain,
...others
}: ParachainConstructorParams) {
super({ type, ...others });
super(others);

this.genesisHash = genesisHash;
this.parachainId = parachainId;
Expand Down

0 comments on commit 91936be

Please sign in to comment.