Skip to content

Commit

Permalink
Fix Config & Rename Multichain Contract (#90)
Browse files Browse the repository at this point in the history
- use mainnet as networkId instead of near
- use mainnet whenever accountId doesn't end in testnet.
- Breaking Change Rename MultichainContract as MpcContract
  • Loading branch information
bh2smith authored Aug 9, 2024
1 parent b8939f3 commit 6942481
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 33 deletions.
22 changes: 19 additions & 3 deletions src/chains/near.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
import { keyStores, KeyPair, connect, Account } from "near-api-js";
import { NearConfig } from "near-api-js/lib/near";

export const TGAS = 1000000000000n;
export const NO_DEPOSIT = "0";
export const ONE_YOCTO = "1";

export interface NearConfig {
networkId: string;
nodeUrl: string;
type NetworkId = "mainnet" | "testnet";

export function getNetworkId(accountId: string): NetworkId {
const accountExt = accountId.split(".").pop() || "";
if (!["near", "testnet"].includes(accountExt)) {
console.warn(
`Unusual or invalid network extracted from accountId ${accountId}`
);
}
// Consider anything that isn't testnet as mainnet.
return accountExt !== "testnet" ? "mainnet" : accountExt;
}

export function configFromNetworkId(networkId: NetworkId): NearConfig {
return {
networkId,
nodeUrl: `https://rpc.${networkId}.near.org`,
};
}

/**
Expand Down
31 changes: 9 additions & 22 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { Account, KeyPair } from "near-api-js";
import { NearEthAdapter } from "./chains/ethereum";
import { MultichainContract } from "./mpcContract";
import { MultichainContract as MpcContract } from "./mpcContract";
import { NearConfig } from "near-api-js/lib/near";
import { createNearAccount } from "./chains/near";
import {
configFromNetworkId,
createNearAccount,
getNetworkId,
} from "./chains/near";

export * from "./chains/ethereum";
export * from "./chains/near";
Expand All @@ -20,24 +24,6 @@ interface SetupConfig {
derivationPath?: string;
}

type NetworkId = "near" | "testnet";

function getNetworkId(accountId: string): NetworkId {
const networkId = accountId.split(".").pop() || "";
if (!["near", "testnet"].includes(networkId)) {
throw new Error(`Invalid network extracted from accountId ${accountId}`);
}
return networkId as NetworkId;
}

export function configFromNetworkId(networkId: NetworkId): NearConfig {
const network = networkId === "near" ? "mainnet" : "testnet";
return {
networkId,
nodeUrl: `https://rpc.${network}.near.org`,
};
}

export async function setupAdapter(args: SetupConfig): Promise<NearEthAdapter> {
const {
accountId,
Expand All @@ -50,7 +36,7 @@ export async function setupAdapter(args: SetupConfig): Promise<NearEthAdapter> {
const config = args.network ?? configFromNetworkId(accountNetwork);
if (accountNetwork !== config.networkId) {
throw new Error(
`The accountId ${accountId} does not match the networkId ${config.networkId}. Please ensure that your accountId is correct and corresponds to the intended network.`
`accountId ${accountId} doesn't match the networkId ${config.networkId}. Please ensure that your accountId is correct and corresponds to the intended network.`
);
}

Expand All @@ -59,14 +45,15 @@ export async function setupAdapter(args: SetupConfig): Promise<NearEthAdapter> {
account = await createNearAccount(
accountId,
config,
// Without private key, MPC contract connection is read-only.
privateKey ? KeyPair.fromString(privateKey) : undefined
);
} catch (error: unknown) {
console.error(`Failed to create NEAR account: ${error}`);
throw error;
}
return NearEthAdapter.fromConfig({
mpcContract: new MultichainContract(account, mpcContractId),
mpcContract: new MpcContract(account, mpcContractId),
derivationPath: derivationPath,
});
}
15 changes: 7 additions & 8 deletions tests/unit/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,21 @@ describe("index", () => {
});

it("setupAdapter fails", async () => {
const accountId = "your-account.testnet";
const networkId = "mainnet";
const config = {
accountId: "your-account.testnet",
network: {
networkId: "near",
nodeUrl: "https://rpc.mainnet.near.org",
},
accountId,
network: configFromNetworkId(networkId),
mpcContractId: "v1.signer-prod.testnet",
};
await expect(() => setupAdapter(config)).rejects.toThrow(
"The accountId your-account.testnet does not match the networkId near."
`accountId ${accountId} doesn't match the networkId ${networkId}. Please ensure that your accountId is correct and corresponds to the intended network.`
);
});

it("configFromNetworkId", async () => {
expect(configFromNetworkId("near")).toStrictEqual({
networkId: "near",
expect(configFromNetworkId("mainnet")).toStrictEqual({
networkId: "mainnet",
nodeUrl: "https://rpc.mainnet.near.org",
});

Expand Down

0 comments on commit 6942481

Please sign in to comment.