Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix testnet deployment and use bsctesnet as main #3

Merged
merged 1 commit into from
Sep 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions packages/hyperlane-contracts/src/InterchainAccountRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
getLocalInterchainAccount_uint32_bytes32_bytes32_address as getLocalInterchainAccountAbi,
quoteGasPayment as quoteGasPaymentAbi,
callRemoteWithOverrides as callRemoteWithOverridesAbi,
callRemote_uint32__bytes32_uint256_bytes_array_bytes as callRemoteAbi,
} from "./artifacts/InterchainAccountRouter.js";

export interface GetRemoteInterchainAccountParams {
Expand Down Expand Up @@ -72,6 +73,37 @@ export async function quoteGasPayment(params: QuoteGasPaymentParams) {
});
}

export interface EncodeCallRemoteParams {
destination: number;
hookMetadata: Hex;
calls: { to: Address; value?: bigint; data: Hex }[];
}

/**
* Encode `callRemote` call
* @param params
* @returns data
*/
export function encodeCallRemote(params: EncodeCallRemoteParams): Hex {
const { destination, hookMetadata, calls } = params;

return encodeFunctionData({
abi: [callRemoteAbi],
functionName: "callRemote",
args: [
destination,
calls.map((c) => {
return {
to: padHex(c.to, { size: 32 }),
value: c.value ?? 0n,
data: c.data,
};
}),
hookMetadata,
],
});
}

export interface EncodeCallRemoteWithOverridesParams {
destination: number;
router: Address;
Expand Down
114 changes: 71 additions & 43 deletions packages/hyperlane-contracts/src/scripts/testnets.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
import { Address, createPublicClient, createWalletClient, encodeFunctionData, http } from "viem";
import {
Address,
createPublicClient,
createWalletClient,
encodeFunctionData,
http,
parseEther,
parseEventLogs,
} from "viem";
import { privateKeyToAccount } from "viem/accounts";
import { scrollSepolia, bscTestnet } from "viem/chains";
import { bscTestnet, scrollSepolia } from "viem/chains";
import { GithubRegistry } from "@hyperlane-xyz/registry";
import {
encodeCallRemoteWithOverrides,
getLocalInterchainAccount,
getRemoteInterchainAccount,
quoteGasPayment,
} from "../InterchainAccountRouter.js";
import { defaultIsm as defaultIsmAbi } from "../artifacts/IMailbox.js";
import { defaultIsm as defaultIsmAbi, DispatchId as DispatchIdEvent } from "../artifacts/IMailbox.js";
import { domains } from "../artifacts/IRouter.js";
import "dotenv/config";

Expand All @@ -21,21 +28,21 @@ interface InterchainAddresses {
interchainAccountRouter: Address;
}

const scrollSepoliaClient = createPublicClient({
chain: scrollSepolia,
transport: http(),
});

const bscTestnetClient = createPublicClient({
chain: bscTestnet,
transport: http(),
});

const scrollSepoliaWalletClient = createWalletClient({
const scrollSepoliaClient = createPublicClient({
chain: scrollSepolia,
transport: http(),
});

const bscTestnetWalletClient = createWalletClient({
chain: bscTestnet,
transport: http(),
//anvil
account: privateKeyToAccount("0x92db14e403b83dfe3df233f83dfa3a0d7096f21ca9b0d6d6b8d88b2b4ec1564e"),
account: privateKeyToAccount(PRIVATE_KEY as Address),
});

const PROXY_DEPLOYED_URL = "https://proxy.hyperlane.xyz";
Expand All @@ -56,41 +63,41 @@ export async function getInterchainChainAddresses(chainName: string) {
}

async function main() {
const scrollSepoliaAddresses = await getInterchainChainAddresses("scrollsepolia");
const bscTestnetAddresses = await getInterchainChainAddresses("bsctestnet");
const scrollSepoliaAddresses = await getInterchainChainAddresses("scrollsepolia");

// Get the interchain account address from the main chain using its owner
const interchainAccountFromMain = await getRemoteInterchainAccount({
publicClient: scrollSepoliaClient,
remoteRouter: bscTestnetAddresses.interchainAccountRouter,
remoteIsm: bscTestnetAddresses.interchainAccountIsm,
mainRouter: scrollSepoliaAddresses.interchainAccountRouter,
owner: scrollSepoliaWalletClient.account.address,
publicClient: bscTestnetClient,
remoteRouter: scrollSepoliaAddresses.interchainAccountRouter,
remoteIsm: scrollSepoliaAddresses.interchainAccountIsm,
mainRouter: bscTestnetAddresses.interchainAccountRouter,
owner: bscTestnetWalletClient.account.address,
});

// Get interchain account address from the remote chain using the owner & origin
const interchainAccountFromRemote = await getLocalInterchainAccount({
publicClient: bscTestnetClient,
origin: scrollSepolia.id,
router: bscTestnetAddresses.interchainAccountRouter,
owner: scrollSepoliaWalletClient.account.address,
ism: bscTestnetAddresses.interchainAccountIsm,
publicClient: scrollSepoliaClient,
origin: bscTestnet.id,
router: scrollSepoliaAddresses.interchainAccountRouter,
owner: bscTestnetWalletClient.account.address,
ism: scrollSepoliaAddresses.interchainAccountIsm,
});

//should be the same
console.debug({ interchainAccountFromMain, interchainAccountFromRemote });

// get default ISM
const defaultIsm = await bscTestnetClient.readContract({
address: bscTestnetAddresses.mailbox,
const defaultIsm = await scrollSepoliaClient.readContract({
address: scrollSepoliaAddresses.mailbox,
abi: [defaultIsmAbi],
functionName: "defaultIsm",
});

// we test out a simple read call from the interchain account
const calls = [
{
to: bscTestnetAddresses.interchainAccountRouter,
to: scrollSepoliaAddresses.interchainAccountRouter,
data: encodeFunctionData({
abi: [domains],
functionName: "domains",
Expand All @@ -99,39 +106,60 @@ async function main() {
];

// call remote data
// const callRemoteData = encodeCallRemote({
// destination: scrollSepolia.id,
// calls,
// hookMetadata: "0x",
// });

const callRemoteData = encodeCallRemoteWithOverrides({
destination: bscTestnet.id,
destination: scrollSepolia.id,
router: scrollSepoliaAddresses.interchainAccountRouter,
ism: defaultIsm,
calls,
});

const remoteGasEstimate = await bscTestnetClient.estimateGas(calls[0]);
const remoteGasEstimate = await scrollSepoliaClient.estimateGas(calls[0]);

console.log({ remoteGasEstimate });

const gasQuote = await quoteGasPayment({
publicClient: scrollSepoliaClient,
destination: bscTestnet.id,
gasLimit: remoteGasEstimate,
router: scrollSepoliaAddresses.interchainAccountRouter,
});

console.log({ gasQuote });

// 130%
const gasQuoteOverestimate = (gasQuote * 13n) / 10n;
// const gasQuote = await quoteGasPayment({
// publicClient: bscTestnetClient,
// destination: scrollSepolia.id,
// gasLimit: remoteGasEstimate,
// router: bscTestnetAddresses.interchainAccountRouter,
// });
//
// console.log({ gasQuote });
//
// //execute
const hash = await scrollSepoliaWalletClient.sendTransaction({
to: scrollSepoliaAddresses.interchainAccountRouter,
// // 130%
// const gasQuoteOverestimate = (gasQuote * 13n) / 10n;
// console.log({gasQuoteOverestimate})

//execute
const hash = await bscTestnetWalletClient.sendTransaction({
to: bscTestnetAddresses.interchainAccountRouter,
data: callRemoteData,
value: gasQuoteOverestimate,
value: parseEther("0.001"),
});

console.debug({ hash });
const receipt = await scrollSepoliaClient.waitForTransactionReceipt({ hash });
const receipt = await bscTestnetClient.waitForTransactionReceipt({ hash });
console.debug({ receipt });

// const remoteCallReceipt = await bscTestnetClient.waitForTransactionReceipt({
// hash: "0x7341b7be338e459352bc2173c2842748abad58f9df2a65f201c155f9d44875c9",
// });
//
const remoteCallReceipt = receipt;

const dispatchIdEvent = parseEventLogs({
abi: [DispatchIdEvent],
logs: remoteCallReceipt.logs,
eventName: "DispatchId",
})[0];
const messageId = dispatchIdEvent.args.messageId;
console.log({ messageId });
}

main();
Loading