-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Start to add client to wrap contract
- Loading branch information
Showing
10 changed files
with
337 additions
and
243 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); | ||
} | ||
} |
Oops, something went wrong.