Skip to content

Commit

Permalink
Enforce id and rpc props if isEvmSigner is true in runtime, due to co…
Browse files Browse the repository at this point in the history
…mplications of adapting the class in projects that use the ñibrary
  • Loading branch information
mmaurello committed Jan 31, 2025
1 parent 4862b81 commit 236797f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 24 deletions.
4 changes: 2 additions & 2 deletions mkdocs/docs/reference/xcm.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,9 @@ These are parachains that use EVM signers or Ethereum type addresses.

**Attributes**

- `id` ++"number"++ - The chain Id in the Ethereum ecosystem
- `rpc` ++"string"++ - The RPC URL
- `isEvmSigner` ++"boolean"++ - Whether the chain uses an EVM signer
- `id` ++"number"++ - The chain Id in the Ethereum ecosystem. Optional, only required if `isEvmSigner` is true
- `rpc` ++"string"++ - The RPC URL. Optional, only required if `isEvmSigner` is true
- `contracts` ++"Contracts"++ - Some contract addresses for the chain, used for building the transactions

---
Expand Down
46 changes: 24 additions & 22 deletions packages/types/src/chain/parachain/EvmParachain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,14 @@ import { getViemChain } from '../Chain.utils';
import { EvmChain } from '../EvmChain';
import { Parachain, type ParachainConstructorParams } from './Parachain';

interface EvmParachainConstructorParams extends ParachainConstructorParams {
export interface EvmParachainConstructorParams
extends ParachainConstructorParams {
id?: number;
rpc?: string;
isEvmSigner?: boolean;
contracts?: Contracts;
}

type EvmParachainConstructorParamsForEvmSigner =
EvmParachainConstructorParams & {
id: number;
rpc: string;
};

type EvmParachainConstructorParamsForNonEvmSigner = Omit<
EvmParachainConstructorParams,
'id' | 'rpc'
>;

type EvmParachainConstructorParamsConditional =
| (EvmParachainConstructorParamsForEvmSigner & { isEvmSigner: true })
| (EvmParachainConstructorParamsForNonEvmSigner & { isEvmSigner?: false });

type Contracts = {
Batch?: Address;
XcmUtils?: Address;
Expand Down Expand Up @@ -51,13 +39,27 @@ export class EvmParachain extends Parachain {
return obj instanceof EvmParachain || obj instanceof EvmChain;
}

constructor(params: EvmParachainConstructorParamsConditional) {
super(params);
constructor({
id,
rpc,
isEvmSigner = false,
contracts,
...others
}: EvmParachainConstructorParams) {
super(others);

if (isEvmSigner) {
if (!id || !rpc) {
throw new Error(
`'id' and 'rpc' must be provided for ${this.name} if 'isEvmSigner' is true`,
);
}
}

this.contracts = params.contracts;
this.id = 'id' in params ? params.id : 0;
this.rpc = 'rpc' in params ? params.rpc : '';
this.isEvmSigner = params.isEvmSigner ?? false;
this.contracts = contracts;
this.id = id ?? 0;
this.rpc = rpc ?? '';
this.isEvmSigner = isEvmSigner;
}

getViemChain(): Chain {
Expand Down

0 comments on commit 236797f

Please sign in to comment.