diff --git a/vea-sdk/examples/index.ts b/vea-sdk/examples/index.ts index ab280a4b..1850cf57 100644 --- a/vea-sdk/examples/index.ts +++ b/vea-sdk/examples/index.ts @@ -2,14 +2,24 @@ import { Wallet } from "@ethersproject/wallet"; import VeaSdk from "../src/index"; import envVar from "../src/utils/envVar"; -// Create the Vea client -const vea = VeaSdk.ClientFactory.arbitrumGoerliToChiadoDevnet(envVar("RPC_ARB_GOERLI"), envVar("RPC_CHIADO")); +async function main() { + // Create the Vea client + const vea = VeaSdk.ClientFactory.arbitrumGoerliToChiadoDevnet(envVar("RPC_ARB_GOERLI"), envVar("RPC_CHIADO")); -// Get the message info -const messageId = 42; -const messageInfo = await vea.getMessageInfo(messageId); + // Get the current state root + console.log(`stateRoot=${await vea.outbox.stateRoot()}`); -// Relay the message -const privateKey = envVar("PRIVATE_KEY"); -const wallet = new Wallet(privateKey, vea.outboxProvider); -await vea.relay(messageInfo, wallet); + // Get a message info + const messageId = 1; + const messageInfo = await vea.getMessageInfo(messageId); + + // Relay the message + const privateKey = envVar("PRIVATE_KEY"); + const wallet = new Wallet(privateKey, vea.outboxProvider); + await vea.relay(messageInfo, wallet); +} + +main().catch((e) => { + console.error(e); + process.exit(1); +}); diff --git a/vea-sdk/package.json b/vea-sdk/package.json index 38ed5d78..8abb8d59 100644 --- a/vea-sdk/package.json +++ b/vea-sdk/package.json @@ -28,7 +28,7 @@ "tag": "latest" }, "scripts": { - "start": "NODE_NO_WARNINGS=1 NODE_OPTIONS=--experimental-fetch ts-node ./src/run.ts", + "start": "NODE_NO_WARNINGS=1 NODE_OPTIONS=--experimental-fetch ts-node ./examples/index.ts", "release:patch": "yarn version patch && yarn npm publish", "release:minor": "yarn version minor && yarn npm publish", "release:major": "yarn version major && yarn npm publish", diff --git a/vea-sdk/src/bridges.ts b/vea-sdk/src/bridges.ts index 93d3a2bb..157c4cd1 100644 --- a/vea-sdk/src/bridges.ts +++ b/vea-sdk/src/bridges.ts @@ -1,8 +1,8 @@ -import VeaInboxArbToGnosisDevnetDeployment from "@kleros/vea-contracts/deployments/arbitrumGoerli/VeaInboxArbToGnosisDevnet.json"; -import VeaOutboxArbToGnosisDevnetDeployment from "@kleros/vea-contracts/deployments/chiado/VeaOutboxArbToGnosisDevnet.json"; +import { address as veaInboxArbToGnosisDevnet } from "@kleros/vea-contracts/deployments/arbitrumGoerli/VeaInboxArbToGnosisDevnet.json"; +import { address as veaOutboxArbToGnosisDevnet } from "@kleros/vea-contracts/deployments/chiado/VeaOutboxArbToGnosisDevnet.json"; -import VeaInboxArbToEthDevnetDeployment from "@kleros/vea-contracts/deployments/arbitrumGoerli/VeaInboxArbToEthDevnet.json"; -import VeaOutboxArbToEthDevnetDeployment from "@kleros/vea-contracts/deployments/goerli/VeaOutboxArbToEthDevnet.json"; +import { address as veaInboxArbToEthDevnet } from "@kleros/vea-contracts/deployments/arbitrumGoerli/VeaInboxArbToEthDevnet.json"; +import { address as veaOutboxArbToEthDevnet } from "@kleros/vea-contracts/deployments/goerli/VeaOutboxArbToEthDevnet.json"; import { VeaInboxArbToEth__factory, @@ -10,8 +10,8 @@ import { VeaOutboxArbToEthDevnet__factory, } from "@kleros/vea-contracts/typechain-types"; -export type VeaInboxFactory = typeof VeaInboxArbToEth__factory; -export type VeaOutboxFactory = typeof VeaOutboxArbToGnosisDevnet__factory | typeof VeaOutboxArbToEthDevnet__factory; +export type VeaInboxFactory = VeaInboxArbToEth__factory; +export type VeaOutboxFactory = VeaOutboxArbToGnosisDevnet__factory | VeaOutboxArbToEthDevnet__factory; export type Bridge = { label: string; @@ -33,10 +33,10 @@ export const arbitrumGoerliToChiadoDevnet: Bridge = { label: "Arbitrum to Chiado Devnet", inboxChainId: 421613, outboxChainId: 10200, - inboxAddress: VeaInboxArbToGnosisDevnetDeployment.address as `0x${string}`, - outboxAddress: VeaOutboxArbToGnosisDevnetDeployment.address as `0x${string}`, - inboxFactory: VeaInboxArbToEth__factory, - outboxFactory: VeaOutboxArbToGnosisDevnet__factory, + inboxAddress: veaInboxArbToGnosisDevnet as `0x${string}`, + outboxAddress: veaOutboxArbToGnosisDevnet as `0x${string}`, + inboxFactory: new VeaInboxArbToEth__factory(), + outboxFactory: new VeaOutboxArbToGnosisDevnet__factory(), inboxSubgraph: getSubgraphUrl("vea-inbox-arbgoerli-to-chiado"), outboxSubgraph: getSubgraphUrl("FIX ME"), // TODO }; @@ -45,10 +45,10 @@ export const arbitrumGoerliToGoerliDevnet: Bridge = { label: "Arbitrum to Goerli Devnet", inboxChainId: 5, outboxChainId: 10200, - inboxAddress: VeaInboxArbToEthDevnetDeployment.address as `0x${string}`, - outboxAddress: VeaOutboxArbToEthDevnetDeployment.address as `0x${string}`, - inboxFactory: VeaInboxArbToEth__factory, - outboxFactory: VeaOutboxArbToEthDevnet__factory, + inboxAddress: veaInboxArbToEthDevnet as `0x${string}`, + outboxAddress: veaOutboxArbToEthDevnet as `0x${string}`, + inboxFactory: new VeaInboxArbToEth__factory(), + outboxFactory: new VeaOutboxArbToEthDevnet__factory(), inboxSubgraph: getSubgraphUrl("vea-inbox-arbgoerli-to-goerli"), outboxSubgraph: getSubgraphUrl("FIXME"), // TODO, }; diff --git a/vea-sdk/src/message.ts b/vea-sdk/src/message.ts index 475d6523..a9f2a98e 100644 --- a/vea-sdk/src/message.ts +++ b/vea-sdk/src/message.ts @@ -33,7 +33,7 @@ export const getMessageInfo = async (client: VeaClient, messageId: number): Prom }; const getCount = async (client: VeaClient): Promise => { - const subgraph = client.config.bridge.outboxSubgraph; + const subgraph = client.config.bridge.inboxSubgraph; const stateRoot = await client.outbox.stateRoot(); const query = `{ snapshotSaveds(first: 1, where: { stateRoot: "${stateRoot}" }) { @@ -58,7 +58,7 @@ const getProofAtCount = async (client: VeaClient, messageId: number, count: numb const proof: any[] = []; try { - const subgraph = client.config.bridge.outboxSubgraph; + const subgraph = client.config.bridge.inboxSubgraph; const result: any = await request(subgraph, query); for (let i = 0; i < proofIndices.length; i++) { proof.push(result[`layer${i}`][0].hash); @@ -90,10 +90,10 @@ const getProofIndices = (messageId: number, count: number) => { const getMessageDataToRelay = async (client: VeaClient, messageId: number): Promise<[string, string]> => { let dataAndTo: [string, string] = ["", ""]; try { - const subgraph = client.config.bridge.outboxSubgraph; + const subgraph = client.config.bridge.inboxSubgraph; const query = `{ - messageSents(first: 5, where: {messageId: ${messageId}}) { - messageId + messageSents(first: 5, where: {nonce: ${messageId}}) { + nonce to { id } diff --git a/vea-sdk/src/sdk.ts b/vea-sdk/src/sdk.ts index 06b806d2..c5593b10 100644 --- a/vea-sdk/src/sdk.ts +++ b/vea-sdk/src/sdk.ts @@ -11,9 +11,17 @@ export class ClientFactory { }; const inboxProvider = new JsonRpcProvider(inboxRpc); const outboxProvider = new JsonRpcProvider(outboxRpc); - const veaInbox = bridge.inboxFactory.connect(bridge.inboxAddress, inboxProvider); - const veaOutbox = bridge.outboxFactory.connect(bridge.outboxAddress, outboxProvider); - // TODO: Check if the RPCs chainIds match the bridge chainIds + const veaInbox = Object.getPrototypeOf(bridge.inboxFactory).constructor.connect(bridge.inboxAddress, inboxProvider); + const veaOutbox = Object.getPrototypeOf(bridge.outboxFactory).constructor.connect( + bridge.outboxAddress, + outboxProvider + ); + inboxProvider.getNetwork().then((network) => { + if (network.chainId !== bridge.inboxChainId) throw new Error("Incorrect Inbox RPC"); + }); + outboxProvider.getNetwork().then((network) => { + if (network.chainId !== bridge.outboxChainId) throw new Error("Incorrect Outbox RPC"); + }); return new VeaClient(config, inboxProvider, outboxProvider, veaInbox, veaOutbox); }