|
| 1 | +import { Decimal } from "@cosmjs/math" |
| 2 | +import { DirectSecp256k1HdWallet } from "@cosmjs/proto-signing" |
| 3 | +import { getFullnodeUrl } from "@mysten/sui/client" |
| 4 | +import { AddressCosmosZkgm } from "@unionlabs/sdk/schema/address" |
| 5 | +import { Instruction } from "@unionlabs/sdk/ucs03" |
| 6 | +import { Effect } from "effect" |
| 7 | +import { CosmosChannelDestination } from "../src/cosmos/channel.js" |
| 8 | +import { CosmosChannelSource } from "../src/cosmos/channel.js" |
| 9 | +import { |
| 10 | + CosmWasmClientDestination, |
| 11 | + CosmWasmClientSource, |
| 12 | + createCosmWasmClient, |
| 13 | + createSigningCosmWasmClient, |
| 14 | + SigningCosmWasmClientContext, |
| 15 | +} from "../src/cosmos/client.js" |
| 16 | +import { SuiChannelDestination } from "../src/sui/channel.js" |
| 17 | +import { |
| 18 | + createSuiPublicClient, |
| 19 | + SuiPublicClient, |
| 20 | + SuiPublicClientDestination, |
| 21 | +} from "../src/sui/client.js" |
| 22 | + |
| 23 | +import { toHex } from "viem" |
| 24 | +import { |
| 25 | + createCosmosToSuiFungibleAssetOrder, |
| 26 | + createSuiToCosmosFungibleAssetOrder, |
| 27 | +} from "../src/ucs03/fungible-asset-order.js" |
| 28 | +// @ts-ignore |
| 29 | +BigInt["prototype"].toJSON = function() { |
| 30 | + return this.toString() |
| 31 | +} |
| 32 | +const MNEMONIC = process.env.MNEMONIC || "memo memo memo" |
| 33 | + |
| 34 | +// Define token transfers |
| 35 | +const TRANSFERS = [ |
| 36 | + { |
| 37 | + sender: AddressCosmosZkgm.make(toHex("union14qemq0vw6y3gc3u3e0aty2e764u4gs5lnxk4rv")), |
| 38 | + receiver: "0x97c9e78b9c3b18f3714544e300234ea873e0904032cf3706fd4e5fd30605df7e", |
| 39 | + baseToken: "muno", |
| 40 | + baseAmount: 11n, |
| 41 | + quoteAmount: 11n, |
| 42 | + }, |
| 43 | + { |
| 44 | + sender: "0x97c9e78b9c3b18f3714544e300234ea873e0904032cf3706fd4e5fd30605df7e", |
| 45 | + receiver: AddressCosmosZkgm.make(toHex("union1jk9psyhvgkrt2cumz8eytll2244m2nnz4yt2g2")), |
| 46 | + baseTokenType: |
| 47 | + "0x76b0a4a20519477bb4dd1dc4215cddabad5bfe92ef9f791a78507f60da07c371::fungible_token::FUNGIBLE_TOKEN", |
| 48 | + baseAmount: 11n, |
| 49 | + quoteAmount: 11n, |
| 50 | + }, |
| 51 | +] as const |
| 52 | + |
| 53 | +const createFungibleAssetOrderCosmosToSui = Effect.gen(function*() { |
| 54 | + yield* Effect.log("creating transfer 1") |
| 55 | + return yield* createCosmosToSuiFungibleAssetOrder(TRANSFERS[0]) |
| 56 | +}).pipe(Effect.withLogSpan("Cosmos to sui fungible asset order creation")) |
| 57 | + |
| 58 | +const createFungibleAssetOrderSuiToCosmos = Effect.gen(function*() { |
| 59 | + yield* Effect.log("creating transfer 1") |
| 60 | + return yield* createSuiToCosmosFungibleAssetOrder(TRANSFERS[1]) |
| 61 | +}).pipe(Effect.withLogSpan("Cosmos to sui fungible asset order creation")) |
| 62 | + |
| 63 | +Effect.runPromiseExit( |
| 64 | + Effect.gen(function*() { |
| 65 | + const config = { |
| 66 | + url: getFullnodeUrl("testnet"), |
| 67 | + } |
| 68 | + const publicClient = yield* createSuiPublicClient(config) |
| 69 | + |
| 70 | + const sender = "union1jk9psyhvgkrt2cumz8eytll2244m2nnz4yt2g2" |
| 71 | + const receiver = "0x97c9e78b9c3b18f3714544e300234ea873e0904032cf3706fd4e5fd30605df7e" |
| 72 | + const base_token = "muno" |
| 73 | + const baseAmount = 11n |
| 74 | + const quoteAmount = 11n |
| 75 | + |
| 76 | + const cosmWasmClientSource = yield* createCosmWasmClient( |
| 77 | + "https://rpc.rpc-node.union-testnet-10.union.build", |
| 78 | + ) |
| 79 | + // Create a wallet from mnemonic (in a real app, use a secure method to get this) |
| 80 | + const wallet = yield* Effect.tryPromise(() => |
| 81 | + DirectSecp256k1HdWallet.fromMnemonic(MNEMONIC, { prefix: "union" }) |
| 82 | + ) |
| 83 | + |
| 84 | + // Get the first account address |
| 85 | + const [firstAccount] = yield* Effect.tryPromise(() => wallet.getAccounts()) |
| 86 | + |
| 87 | + // Create a signing client |
| 88 | + const signingClient = yield* createSigningCosmWasmClient( |
| 89 | + "https://rpc.rpc-node.union-testnet-10.union.build", |
| 90 | + wallet, |
| 91 | + { |
| 92 | + gasPrice: { amount: Decimal.fromUserInput("1", 6), denom: "muno" }, |
| 93 | + }, |
| 94 | + ) |
| 95 | + |
| 96 | + yield* Effect.log("creating batch") |
| 97 | + const assetOrder = yield* createFungibleAssetOrderCosmosToSui.pipe( |
| 98 | + Effect.provideService(SigningCosmWasmClientContext, { |
| 99 | + client: signingClient, |
| 100 | + address: firstAccount.address, |
| 101 | + }), |
| 102 | + Effect.provideService(CosmWasmClientSource, { client: cosmWasmClientSource }), |
| 103 | + Effect.provideService(SuiPublicClientDestination, { client: publicClient }), |
| 104 | + Effect.provideService(SuiChannelDestination, { |
| 105 | + ucs03address: "0xf8e63d8dd3c083d0c87554a984d14cbbf6c3b314207a7ddde035ac33ea757d8a", |
| 106 | + channelId: 2, |
| 107 | + }), |
| 108 | + Effect.provideService(CosmosChannelSource, { |
| 109 | + ucs03address: "union15zcptld878lux44lvc0chzhz7dcdh62nh0xehwa8y7czuz3yljls7u4ry6", |
| 110 | + channelId: 1, |
| 111 | + }), |
| 112 | + ) |
| 113 | + yield* Effect.log("assetOrder created", JSON.stringify(assetOrder, null, 2)) |
| 114 | + const encoded = Instruction.encodeAbi(assetOrder) |
| 115 | + yield* Effect.log("Encoded:", encoded) |
| 116 | + |
| 117 | + const assetOrder2 = yield* createFungibleAssetOrderSuiToCosmos.pipe( |
| 118 | + Effect.provideService(SigningCosmWasmClientContext, { |
| 119 | + client: signingClient, |
| 120 | + address: firstAccount.address, |
| 121 | + }), |
| 122 | + Effect.provideService(SuiPublicClient, { client: publicClient }), |
| 123 | + Effect.provideService(CosmWasmClientDestination, { client: cosmWasmClientSource }), |
| 124 | + Effect.provideService(CosmosChannelDestination, { |
| 125 | + ucs03address: "union15zcptld878lux44lvc0chzhz7dcdh62nh0xehwa8y7czuz3yljls7u4ry6", |
| 126 | + channelId: 3, |
| 127 | + }), |
| 128 | + Effect.catchAllCause(cause => { |
| 129 | + console.error("cause is:", cause) |
| 130 | + }), |
| 131 | + ) |
| 132 | + yield* Effect.log("assetOrder2 created", JSON.stringify(assetOrder2, null, 2)) |
| 133 | + const encoded2 = Instruction.encodeAbi(assetOrder2) |
| 134 | + yield* Effect.log("Encoded:", encoded2) |
| 135 | + }), |
| 136 | +).then(exit => console.log(JSON.stringify(exit, null, 2))) |
0 commit comments