|
| 1 | +import type { CommandModule, InferredOptionTypes } from "yargs"; |
| 2 | +import { createClient, getAddress, http } from "viem"; |
| 3 | +import chalk from "chalk"; |
| 4 | +import { getChainId } from "viem/actions"; |
| 5 | +import { defaultChains } from "../defaultChains"; |
| 6 | +import { mirror } from "../mirror/mirror"; |
| 7 | + |
| 8 | +const options = { |
| 9 | + rpcBatch: { |
| 10 | + type: "boolean", |
| 11 | + desc: "Enable batch processing of RPC requests via Viem client (defaults to batch size of 100 and wait of 1s).", |
| 12 | + }, |
| 13 | + kms: { |
| 14 | + type: "boolean", |
| 15 | + desc: "Deploy the world with an AWS KMS key instead of local private key.", |
| 16 | + }, |
| 17 | + fromWorld: { |
| 18 | + type: "string", |
| 19 | + desc: "Source world address to mirror from.", |
| 20 | + required: true, |
| 21 | + }, |
| 22 | + fromRpc: { |
| 23 | + type: "string", |
| 24 | + desc: "RPC URL of source chain to mirror from.", |
| 25 | + required: true, |
| 26 | + }, |
| 27 | + fromIndexer: { |
| 28 | + type: "string", |
| 29 | + desc: "MUD indexer URL of source chain to mirror from.", |
| 30 | + }, |
| 31 | + toRpc: { |
| 32 | + type: "string", |
| 33 | + desc: "RPC URL of target chain to mirror to.", |
| 34 | + required: true, |
| 35 | + }, |
| 36 | +} as const; |
| 37 | + |
| 38 | +type Options = InferredOptionTypes<typeof options>; |
| 39 | + |
| 40 | +const commandModule: CommandModule<Options, Options> = { |
| 41 | + command: "mirror", |
| 42 | + |
| 43 | + describe: "Mirror an existing world and its data to another chain.", |
| 44 | + |
| 45 | + builder(yargs) { |
| 46 | + return yargs.options(options); |
| 47 | + }, |
| 48 | + |
| 49 | + async handler(opts) { |
| 50 | + const fromWorld = getAddress(opts.fromWorld); |
| 51 | + const fromClient = createClient({ |
| 52 | + transport: http(opts.fromRpc, { |
| 53 | + batch: opts.rpcBatch ? { batchSize: 100, wait: 1000 } : undefined, |
| 54 | + }), |
| 55 | + }); |
| 56 | + const fromChainId = await getChainId(fromClient); |
| 57 | + const fromIndexer = opts.fromIndexer ?? defaultChains.find((chain) => chain.id === fromChainId)?.indexerUrl; |
| 58 | + |
| 59 | + const toClient = createClient({ |
| 60 | + transport: http(opts.toRpc, { |
| 61 | + batch: opts.rpcBatch ? { batchSize: 100, wait: 1000 } : undefined, |
| 62 | + }), |
| 63 | + }); |
| 64 | + const toChainId = await getChainId(toClient); |
| 65 | + |
| 66 | + console.log( |
| 67 | + chalk.bgBlue( |
| 68 | + chalk.whiteBright( |
| 69 | + `\n Mirroring MUD world at ${opts.fromWorld} from chain ${fromChainId} to chain ${toChainId} \n`, |
| 70 | + ), |
| 71 | + ), |
| 72 | + ); |
| 73 | + |
| 74 | + // TODO: load up account from KMS or private key |
| 75 | + |
| 76 | + await mirror({ |
| 77 | + from: { world: fromWorld, client: fromClient, indexer: fromIndexer }, |
| 78 | + to: { client: toClient }, |
| 79 | + }); |
| 80 | + }, |
| 81 | +}; |
| 82 | + |
| 83 | +export default commandModule; |
0 commit comments