Skip to content

Commit

Permalink
Start to add client to wrap contract
Browse files Browse the repository at this point in the history
  • Loading branch information
barnjamin committed Jan 28, 2024
1 parent a59ea08 commit 9a580a6
Show file tree
Hide file tree
Showing 10 changed files with 337 additions and 243 deletions.
17 changes: 17 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@
},
"dependencies": {
"@typechain/ethers-v6": "^0.4.0",
"ethers": "^6",
"ts-node": "^10.9.1",
"typechain": "^8.2.0",
"@wormhole-foundation/connect-sdk": "^0.4.0-beta.2",
"@wormhole-foundation/connect-sdk-evm": "^0.4.0-beta.2",
"@wormhole-foundation/connect-sdk-evm-cctp": "^0.4.0-beta.2",
"@wormhole-foundation/connect-sdk-evm-core": "^0.4.0-beta.2",
"@wormhole-foundation/connect-sdk-evm-cctp": "^0.4.0-beta.2"
"dotenv": "^16.4.1",
"ethers": "^6",
"ts-node": "^10.9.1",
"typechain": "^8.2.0"
},
"devDependencies": {
"@types/jest": "^29.5.2",
Expand All @@ -31,4 +32,4 @@
"ts-jest": "^29.1.0",
"typescript": "^5.1.3"
}
}
}
88 changes: 88 additions & 0 deletions ts-scripts/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import {
CONFIG,
Chain,
toChain,
toChainId,
} from "@wormhole-foundation/connect-sdk";
import "@wormhole-foundation/connect-sdk-evm-core";
import { readFileSync, writeFileSync } from "fs";

export const NETWORK = "Testnet";
export const CHAINS: Chain[] = ["Avalanche", "Celo"];
export const NetworkConfig = CONFIG[NETWORK];

let _config: Config | undefined;
let _deployed: DeployedAddresses | undefined;

export interface ChainInfo {
chain: Chain;
description: string;
chainId: number;
rpc: string;
tokenBridge: string;
wormholeRelayer: string;
wormhole: string;
}

export interface Config {
chains: ChainInfo[];
}

export interface DeployedAddresses {
helloWormhole: Record<number, string>;
erc20s: Record<number, string[]>;
}

export function loadConfig(): Config {
_config = _config ?? { chains: CHAINS.map((chain) => getChainInfo(chain)) };
return _config!;
}

export function loadDeployedAddresses(
fileMustBePresent?: "fileMustBePresent"
): DeployedAddresses {
if (!_deployed) {
try {
_deployed = JSON.parse(
readFileSync("ts-scripts/testnet/deployedAddresses.json", {
encoding: "utf-8",
})
);
} catch (e) {
if (fileMustBePresent) {
throw e;
}
}
if (!_deployed) {
_deployed = {
erc20s: [],
helloWormhole: [],
};
}
}
return _deployed!;
}

export function storeDeployedAddresses(deployed: DeployedAddresses) {
writeFileSync(
"ts-scripts/testnet/deployedAddresses.json",
JSON.stringify(deployed, undefined, 2)
);
}

export function getChainInfo(c: number | Chain): ChainInfo {
const chain = typeof c === "number" ? toChain(c) : c;
const chainId = toChainId(chain);
const conf = NetworkConfig.chains[chain]!;
const info: ChainInfo = {
chain,
chainId,
description: `${chain}:${conf.network}`,
rpc: conf.rpc,
tokenBridge: conf.contracts.tokenBridge!,
wormholeRelayer: conf.contracts.relayer!,
wormhole: conf.contracts.coreBridge!,
};

return info;
}
32 changes: 0 additions & 32 deletions ts-scripts/deploy.ts

This file was deleted.

39 changes: 0 additions & 39 deletions ts-scripts/getStatus.ts

This file was deleted.

90 changes: 90 additions & 0 deletions ts-scripts/helloWormhole.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import {
Chain,
ChainAddress,
Network,
canonicalAddress,
nativeChainIds,
toChainId,
} from "@wormhole-foundation/connect-sdk";
import {
EvmChains,
EvmPlatform,
EvmUnsignedTransaction,
addChainId,
} from "@wormhole-foundation/connect-sdk-evm";
import { Provider, TransactionRequest } from "ethers";
import { HelloWormhole, HelloWormhole__factory } from "./ethers-contracts";

export class HelloWormholeClient<N extends Network, C extends EvmChains> {
readonly helloWormhole: HelloWormhole;
readonly helloWormholeAddress: string;
readonly chainId: bigint;

constructor(
readonly network: N,
readonly chain: C,
readonly provider: Provider,
readonly address: string
) {
this.chainId = nativeChainIds.networkChainToNativeChainId.get(
network,
chain
) as bigint;

this.helloWormholeAddress = address;
this.helloWormhole = HelloWormhole__factory.connect(
this.helloWormholeAddress,
provider
);
}

static async fromRpc<N extends Network>(
provider: Provider,
address: string
): Promise<HelloWormholeClient<N, EvmChains>> {
const [network, chain] = await EvmPlatform.chainFromRpc(provider);
return new HelloWormholeClient(network as N, chain, provider, address);
}

async quoteCrossChainGreeting(to: Chain): Promise<bigint> {
return await this.helloWormhole.quoteCrossChainGreeting(toChainId(to));
}

async latestGreeting(): Promise<string> {
return await this.helloWormhole.latestGreeting();
}

async *sendCrossChainGreeting(
to: ChainAddress,
message: string,
value?: bigint
): AsyncGenerator<EvmUnsignedTransaction<N, C>> {
if (!value) {
value = await this.quoteCrossChainGreeting(to.chain);
}

const tx =
await this.helloWormhole.sendCrossChainGreeting.populateTransaction(
toChainId(to.chain),
canonicalAddress(to),
message,
{ value }
);

yield this.createUnsignedTx(tx, "HelloWormhole.SendCrossChainGreeting");
}

private createUnsignedTx(
txReq: TransactionRequest,
description: string,
parallelizable: boolean = false
): EvmUnsignedTransaction<N, C> {
return new EvmUnsignedTransaction(
addChainId(txReq, this.chainId),
this.network,
this.chain,
description,
parallelizable
);
}
}
Loading

0 comments on commit 9a580a6

Please sign in to comment.