|
1 |
| -import { Decimal } from "@cosmjs/math" |
| 1 | +// CosmWasm |
2 | 2 | import { DirectSecp256k1HdWallet } from "@cosmjs/proto-signing"
|
3 |
| -import { Cosmos, Evm, FungibleAssetOrder, Ucs03, Ucs05 } from "@unionlabs/sdk" |
4 |
| -import { UniversalChainId } from "@unionlabs/sdk/schema/chain" |
5 |
| -import { ChannelId } from "@unionlabs/sdk/schema/channel" |
6 |
| -import { Effect, Exit, pipe } from "effect" |
| 3 | +import { GasPrice } from "@cosmjs/stargate" |
| 4 | +// EVM |
7 | 5 | import { http, toHex } from "viem"
|
8 | 6 | import { bobSepolia } from "viem/chains"
|
| 7 | +// Union |
| 8 | +import { Cosmos, Evm, FungibleAssetOrder, Ucs05 } from "@unionlabs/sdk" |
| 9 | +import { UniversalChainId } from "@unionlabs/sdk/schema/chain" |
| 10 | +import { ChannelId } from "@unionlabs/sdk/schema/channel" |
| 11 | +import { Effect, pipe } from "effect" |
9 | 12 |
|
10 |
| -const MNEMONIC = process.env.MNEMONIC || "memo memo memo" |
11 |
| - |
| 13 | +// We will send funds from sender to receiver |
12 | 14 | const SENDER = Ucs05.AddressCosmosZkgm.make(toHex("bbn122ny3mep2l7nhtafpwav2y9e5jrslhekrn8frh"))
|
13 | 15 | const RECEIVER = Ucs05.AddressEvmZkgm.make("0xfaebe5bf141cc04a3f0598062b98d2df01ab3c4d")
|
14 |
| -const SOURCE_UCS03_ADDRESS = |
15 |
| - "bbn15zcptld878lux44lvc0chzhz7dcdh62nh0xehwa8y7czuz3yljlspm2re6" as const |
16 | 16 |
|
17 |
| -const main = Effect.gen(function*() { |
18 |
| - const sourceClient = Cosmos.ClientSource.Live( |
19 |
| - "https://rpc.bbn-1.babylon.chain.kitchen", |
20 |
| - ) |
21 |
| - const destinationClient = Evm.PublicClientDestination.Live({ |
22 |
| - chain: bobSepolia, |
23 |
| - transport: http(), |
24 |
| - }) |
25 |
| - const wallet = yield* Effect.tryPromise(() => |
26 |
| - DirectSecp256k1HdWallet.fromMnemonic(MNEMONIC, { prefix: "bbn" }) |
27 |
| - ) |
28 |
| - const signingClient = Cosmos.SigningClientContext.Live( |
29 |
| - "https://rest.bbn-1.babylon.chain.kitchen", |
30 |
| - wallet, |
31 |
| - { gasPrice: { amount: Decimal.fromUserInput("1", 6), denom: "ubbn" } }, |
32 |
| - ) |
| 17 | +const wallet = await DirectSecp256k1HdWallet.fromMnemonic(process.env.MNEMONIC as string, { |
| 18 | + prefix: "bbn", |
| 19 | +}) |
| 20 | + |
| 21 | +const accounts = await wallet.getAccounts() |
| 22 | + |
| 23 | +console.log({ accounts }) |
33 | 24 |
|
34 |
| - return yield* pipe( |
35 |
| - FungibleAssetOrder.cosmosToEvm({ |
36 |
| - sender: SENDER, |
37 |
| - receiver: RECEIVER, |
38 |
| - baseToken: "ubbn", |
39 |
| - baseAmount: 100n, |
40 |
| - quoteAmount: 100n, |
41 |
| - sourceChainId: UniversalChainId.make("babylon.bbn-1"), |
42 |
| - sourceChannelId: ChannelId.make(1), |
43 |
| - }), |
44 |
| - Effect.map((intent) => Ucs03.Batch.make({ operand: [intent] })), |
45 |
| - Effect.andThen((instruction) => Cosmos.sendInstruction(instruction, SENDER)), |
46 |
| - Effect.provide(signingClient), |
47 |
| - Effect.provide(sourceClient), |
48 |
| - Effect.provide(destinationClient), |
49 |
| - Effect.provideService(Evm.ChannelDestination, { |
50 |
| - ucs03address: "0xe33534b7f8D38C6935a2F6Ad35E09228dA239962", |
51 |
| - channelId: 1, |
52 |
| - }), |
53 |
| - Effect.provideService(Cosmos.ChannelSource, { |
54 |
| - ucs03address: SOURCE_UCS03_ADDRESS, |
55 |
| - channelId: 1, |
56 |
| - }), |
57 |
| - ) |
| 25 | +// Create clients from source to destination ("Live" means "not mocked") |
| 26 | +const sourceClient = Cosmos.ClientSource.Live("https://rpc.bbn-1.babylon.chain.kitchen") |
| 27 | +const destinationClient = Evm.PublicClientDestination.Live({ chain: bobSepolia, transport: http() }) |
| 28 | +const signingClient = Cosmos.SigningClient.Live( |
| 29 | + "https://rpc.bbn-1.babylon.chain.kitchen", |
| 30 | + // await DirectSecp256k1HdWallet.fromMnemonic("memo memo memo", { prefix: "bbn" }), |
| 31 | + await DirectSecp256k1HdWallet.fromMnemonic(process.env.MNEMONIC as string, { prefix: "bbn" }), |
| 32 | + { gasPrice: GasPrice.fromString("0.000006ubbn") }, |
| 33 | +) |
| 34 | +// Specify the channel over which to send funds |
| 35 | +const sourceChannel = Cosmos.ChannelSource.Live({ |
| 36 | + // @ts-ignore |
| 37 | + ucs03address: "bbn15zcptld878lux44lvc0chzhz7dcdh62nh0xehwa8y7czuz3yljlspm2re6", |
| 38 | + channelId: 1, |
58 | 39 | })
|
| 40 | +const destinationChannel = Evm.ChannelDestination.Live({ |
| 41 | + ucs03address: "0xe33534b7f8D38C6935a2F6Ad35E09228dA239962", |
| 42 | + channelId: 1, |
| 43 | +}) |
| 44 | + |
| 45 | +// Build main program |
| 46 | +const main = pipe( |
| 47 | + // 1. Create order instruction |
| 48 | + FungibleAssetOrder.cosmosToEvm({ |
| 49 | + sender: SENDER, |
| 50 | + receiver: RECEIVER, |
| 51 | + baseToken: "ubbn", |
| 52 | + baseAmount: 100n, |
| 53 | + quoteAmount: 100n, |
| 54 | + sourceChainId: UniversalChainId.make("babylon.bbn-1"), |
| 55 | + sourceChannelId: ChannelId.make(1), |
| 56 | + }), |
| 57 | + // 2. Send order instruction |
| 58 | + Effect.andThen((instruction) => Cosmos.sendInstruction(instruction, SENDER)), |
| 59 | + // 3. Provide clients & channel configuration |
| 60 | + Effect.provide(signingClient), |
| 61 | + Effect.provide(sourceClient), |
| 62 | + Effect.provide(destinationClient), |
| 63 | + Effect.provide(sourceChannel), |
| 64 | + Effect.provide(destinationChannel), |
| 65 | +) |
59 | 66 |
|
60 |
| -Effect.runPromiseExit(main).then(Exit.match({ |
61 |
| - onFailure: (cause) => console.error(JSON.stringify(cause, null, 2)), |
62 |
| - onSuccess: (result) => console.log(result), |
63 |
| -})) |
| 67 | +// Run main program |
| 68 | +Effect.runPromise(main) |
| 69 | + .then(console.log) |
| 70 | + .catch(console.error) |
0 commit comments