Skip to content

Commit 8be12f6

Browse files
committed
docs: cross chain contract calling guide
1 parent b13c6e2 commit 8be12f6

File tree

1 file changed

+153
-0
lines changed
  • docs/src/content/docs/integrations/typescript/guided-tutorial

1 file changed

+153
-0
lines changed
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
---
2+
title: Cross Chain Contract Calls
3+
description: Example call on Babylon to Ethereum contract.
4+
---
5+
6+
This guide walk's you through writing our example ["Call"](/integrations/typescript/examples/cosmos/call/). The goal of this guide is to show you how to conduct a cross chain contract call using [USC03 - ZKGM](/ucs/03/). In this case we will be calling a contract on Ethereum from Babylon.
7+
8+
Relevant imports will be included with each step. Outside of libraries provided by Union, this guide uses [Effect](https://effect.website/).
9+
10+
## Program
11+
12+
This first section is a walk through of creating the program section of the send funds example.
13+
14+
### 1. Program Declaration
15+
16+
Begin by using Effect to create a program function.
17+
18+
The signer used for this transaction is also declared here.
19+
20+
```ts
21+
import { DirectSecp256k1HdWallet } from "@cosmjs/proto-signing"
22+
import { Effect, Logger } from "effect"
23+
24+
const signer = await DirectSecp256k1HdWallet.fromMnemonic(
25+
process.env.MEMO ?? "memo memo memo",
26+
{ prefix: "bbn" },
27+
)
28+
29+
const program = Effect.gen(function*() {})
30+
```
31+
32+
### 2. Source and Destination
33+
34+
Using the `ChainRegistry` from the Union TS SDK, you can declare the source and destination chains used in this example. In this case, the source is Babylon (`babylon.bbn-1`) and the destination is Ethereum (`ethereum.1`).
35+
36+
```ts
37+
import { ChainRegistry } from "@unionlabs/sdk/ChainRegistry"
38+
import { UniversalChainId } from "@unionlabs/sdk/schema/chain"
39+
import { Effect, Logger } from "effect"
40+
41+
const program = Effect.gen(function*() {
42+
const source = yield* ChainRegistry.byUniversalId(
43+
UniversalChainId.make("babylon.bbn-1"),
44+
)
45+
46+
const destination = yield* ChainRegistry.byUniversalId(
47+
UniversalChainId.make("ethereum.1"),
48+
)
49+
})
50+
```
51+
52+
:::note
53+
The ID's provided here are Universal Chain ID's as defined by [UCS04](/ucs/04).
54+
:::
55+
56+
### 3. Call
57+
58+
To create a `Call`, we supply the `sender`, `contractAddress`, and `contractCalldata`.
59+
60+
More information about `Call` and its fields can be found in the [UCS03 - 0x01 Call Docs](/ucs/03/#0x01---call).
61+
62+
```ts
63+
import { Call, Ucs05, ZkgmClientRequest, ZkgmClientResponse } from "@unionlabs/sdk"
64+
import { Effect, Logger } from "effect"
65+
66+
const program = Effect.gen(function*() {
67+
68+
// ... snip ...
69+
70+
const call = Call.make({
71+
sender: Ucs05.CosmosDisplay.make({
72+
address: "bbn122ny3mep2l7nhtafpwav2y9e5jrslhekrn8frh",
73+
}),
74+
eureka: false,
75+
contractAddress: Ucs05.EvmDisplay.make({
76+
address: "0x921e5b5091f431f84f14423ec487783a853bc4b0",
77+
}),
78+
contractCalldata: "0xDEADBEEF",
79+
})
80+
})
81+
```
82+
83+
### 4. zkgm Request
84+
85+
Finally, with the `Call` ready - you can construct a `ZkgmClientRequest`.
86+
87+
```ts
88+
import { Call, Ucs05, ZkgmClientRequest, ZkgmClientResponse } from "@unionlabs/sdk"
89+
import { Effect, Logger } from "effect"
90+
91+
const program = Effect.gen(function*() {
92+
93+
// ... snip ...
94+
95+
const request = ZkgmClientRequest.make({
96+
source,
97+
destination,
98+
channelId: ChannelId.make(3),
99+
ucs03Address: "bbn1336jj8ertl8h7rdvnz4dh5rqahd09cy0x43guhsxx6xyrztx292q77945h",
100+
instruction: call,
101+
})
102+
})
103+
```
104+
105+
:::note
106+
UCS03 addresses can be found on the [Deployments page](/protocol/deployments/)
107+
:::
108+
109+
### 5. zkgm Execution and Response
110+
111+
Now that you’ve created a full zkgm request, you can execute it and wait on the response to close out the program.
112+
113+
```ts
114+
import { Call, Ucs05, ZkgmClientRequest, ZkgmClientResponse } from "@unionlabs/sdk"
115+
import { GasPrice } from "@cosmjs/stargate"
116+
import { Cosmos, CosmosZkgmClient } from "@unionlabs/sdk-cosmos"
117+
import { Effect, Logger } from "effect"
118+
const program = Effect.gen(function*() {
119+
120+
// ... snip ...
121+
122+
const client = yield* CosmosZkgmClient.make.pipe(
123+
Effect.provide(Cosmos.SigningClient.Live(
124+
"bbn122ny3mep2l7nhtafpwav2y9e5jrslhekrn8frh",
125+
"https://rpc.bbn-1.babylon.chain.kitchen",
126+
signer,
127+
{ gasPrice: GasPrice.fromString("0.0007ubbn") },
128+
)),
129+
Effect.provide(Cosmos.Client.Live("https://rpc.bbn-1.babylon.chain.kitchen")),
130+
)
131+
132+
const response: ZkgmClientResponse.ZkgmClientResponse = yield* client.execute(request)
133+
134+
yield* Effect.log("TX Hash:", response.txHash)
135+
})
136+
```
137+
138+
## Execution
139+
140+
With the program ready, we can now use Effect to execute our transfer and listen for a response.
141+
142+
```ts
143+
const program = Effect.gen(function*() {
144+
// ... program ...
145+
}).pipe(
146+
Effect.provide(ChainRegistry.Default),
147+
Effect.provide(Logger.replace(Logger.defaultLogger, Logger.prettyLoggerDefault)),
148+
)
149+
150+
Effect.runPromise(program)
151+
.then(console.log)
152+
.catch(console.error)
153+
```

0 commit comments

Comments
 (0)