forked from wormhole-foundation/wormhole
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Client/js: Add Info Registrations (wormhole-foundation#3035)
* Client/js: Add Info Registrations Change-Id: Ib123dfe895d88c5574f575b16dfc3e6775d81f2a * Fix build errors Change-Id: Ifcacb564fc40f14337ab472ece617a2955b579c5
- Loading branch information
1 parent
7f74b92
commit 1ae38c8
Showing
13 changed files
with
868 additions
and
140 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
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
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,79 @@ | ||
import { getCosmWasmClient } from "@sei-js/core"; | ||
import { | ||
ChainName, | ||
CHAINS, | ||
CONTRACTS, | ||
} from "@certusone/wormhole-sdk/lib/esm/utils/consts"; | ||
import { NETWORKS } from "../../consts/networks"; | ||
import { Network } from "../../utils"; | ||
|
||
export async function queryRegistrationsSei( | ||
network: Network, | ||
module: "Core" | "NFTBridge" | "TokenBridge" | ||
): Promise<Object> { | ||
const chain = "sei" as ChainName; | ||
const n = NETWORKS[network][chain]; | ||
const contracts = CONTRACTS[network][chain]; | ||
|
||
let target_contract: string | undefined; | ||
|
||
switch (module) { | ||
case "TokenBridge": | ||
target_contract = contracts.token_bridge; | ||
break; | ||
case "NFTBridge": | ||
target_contract = contracts.nft_bridge; | ||
break; | ||
default: | ||
throw new Error(`Invalid module: ${module}`); | ||
} | ||
|
||
if (!target_contract) { | ||
throw new Error(`Contract for ${module} on ${network} does not exist`); | ||
} | ||
|
||
if (n.rpc === undefined) { | ||
throw new Error(`RPC for ${module} on ${network} does not exist`); | ||
} | ||
|
||
// Create a CosmWasmClient | ||
const client = await getCosmWasmClient(n.rpc); | ||
|
||
// Query the bridge registration for all the chains in parallel. | ||
const registrations = await Promise.all( | ||
Object.entries(CHAINS) | ||
.filter(([c_name, _]) => c_name !== chain && c_name !== "unset") | ||
.map(async ([c_name, c_id]) => [ | ||
c_name, | ||
await (async () => { | ||
let query_msg = { | ||
chain_registration: { | ||
chain: c_id, | ||
}, | ||
}; | ||
|
||
let result = null; | ||
try { | ||
result = await client.queryContractSmart( | ||
target_contract as string, | ||
query_msg | ||
); | ||
} catch { | ||
// Not logging anything because a chain not registered returns an error. | ||
} | ||
|
||
return result; | ||
})(), | ||
]) | ||
); | ||
|
||
const results: { [key: string]: string } = {}; | ||
for (let [c_name, queryResponse] of registrations) { | ||
if (queryResponse) { | ||
results[c_name] = Buffer.from(queryResponse.address, "base64").toString( | ||
"hex" | ||
); | ||
} | ||
} | ||
return results; | ||
} |
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,57 @@ | ||
import { getObjectFields } from "@certusone/wormhole-sdk/lib/esm/sui"; | ||
import { | ||
CHAIN_ID_TO_NAME, | ||
CONTRACTS, | ||
} from "@certusone/wormhole-sdk/lib/esm/utils/consts"; | ||
import { NETWORKS } from "../../consts/networks"; | ||
import { Network } from "../../utils"; | ||
import { getProvider } from "./utils"; | ||
import { ChainId } from "@certusone/wormhole-sdk"; | ||
|
||
export async function queryRegistrationsSui( | ||
network: Network, | ||
module: "Core" | "NFTBridge" | "TokenBridge" | ||
): Promise<Object> { | ||
const n = NETWORKS[network]["sui"]; | ||
const provider = getProvider(network, n.rpc); | ||
const contracts = CONTRACTS[network]["sui"]; | ||
let state_object_id: string; | ||
|
||
switch (module) { | ||
case "TokenBridge": | ||
state_object_id = contracts.token_bridge; | ||
if (state_object_id === undefined) { | ||
throw Error(`Unknown token bridge contract on ${network} for Sui`); | ||
} | ||
break; | ||
default: | ||
throw new Error(`Invalid module: ${module}`); | ||
} | ||
|
||
const state = await getObjectFields(provider, state_object_id); | ||
const emitterRegistryId = state!.emitter_registry.fields.id.id; | ||
|
||
// TODO: handle pagination | ||
// - recursive: https://github.com/wormhole-foundation/wormhole/blob/7608b2b740df5d4c2551daaf4d620eac81c07790/sdk/js/src/sui/utils.ts#L175 | ||
// - iterative: https://github.com/wormhole-foundation/wormhole/blob/7608b2b740df5d4c2551daaf4d620eac81c07790/sdk/js/src/sui/utils.ts#L199 | ||
const emitterRegistry = await provider.getDynamicFields({ | ||
parentId: emitterRegistryId, | ||
}); | ||
|
||
const results: { [key: string]: string } = {}; | ||
for (let idx = 0; idx < emitterRegistry.data.length; idx++) { | ||
const chainId = emitterRegistry.data[idx].name.value as ChainId; | ||
for (const { objectId } of emitterRegistry.data.slice(idx, idx + 1)) { | ||
const emitter = (await provider.getObject({ | ||
id: objectId, | ||
options: { showContent: true }, | ||
})) as any; | ||
const emitterAddress: Uint8Array = | ||
emitter.data?.content?.fields.value.fields.value.fields.data; | ||
const emitterAddrStr = Buffer.from(emitterAddress).toString("hex"); | ||
results[CHAIN_ID_TO_NAME[chainId]] = emitterAddrStr; | ||
} | ||
} | ||
|
||
return results; | ||
} |
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
Oops, something went wrong.