Skip to content

Commit 65981ce

Browse files
committed
chore(ts-sdk-sui): added transport interface for extra params
Signed-off-by: kaancaglan <[email protected]>
1 parent 1c2ba77 commit 65981ce

File tree

4 files changed

+142
-37
lines changed

4 files changed

+142
-37
lines changed

ts-sdk-sui/examples/sui-create-client-generate-instruction.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import * as TokenOrder from "@unionlabs/sdk/TokenOrder"
3030

3131
const MNEMONIC = process.env.SUI_MNEMONIC ?? "..."
3232
const RECIPIENT = process.env.RECIPIENT
33-
?? "0x03ff9dd9e093387bdd4432c6a3eb6a1bd5a8f39a530042ac7efe576f18d3232b"
33+
?? "union1wycy8g8v5sff6gsjl9yhjs43q98xpl05p3gn2s"
3434

3535
const keypair = Ed25519Keypair.deriveKeypair(MNEMONIC)
3636

@@ -56,7 +56,7 @@ const program = Effect.gen(function*() {
5656
source,
5757
destination,
5858
sender: sender,
59-
receiver: "union1wycy8g8v5sff6gsjl9yhjs43q98xpl05p3gn2s",
59+
receiver: RECIPIENT,
6060
baseToken: "0x2::sui::SUI",
6161
baseAmount: 10000000n,
6262
quoteToken: "union1y05e0p2jcvhjzf7kcqsrqx93d4g3u93hc2hykaq8hrvkqrp5ltrssagzyd",
@@ -71,8 +71,27 @@ const program = Effect.gen(function*() {
7171
source,
7272
destination,
7373
channelId: ChannelId.make(5),
74-
ucs03Address: "union1rfz3ytg6l60wxk5rxsk27jvn2907cyav04sz8kde3xhmmf9nplxqr8y05c",
74+
ucs03Address: "0x8675045186976da5b60baf20dc94413fb5415a7054052dc14d93c13d3dbdf830",
7575
instruction: tokenOrder,
76+
77+
// NEW — only read by the Sui client
78+
transport: {
79+
sui: {
80+
relayStoreId:
81+
"0x393a99c6d55d9a79efa52dea6ea253fef25d2526787127290b985222cc20a924",
82+
vaultId:
83+
"0x7c4ade19208295ed6bf3c4b58487aa4b917ba87d31460e9e7a917f7f12207ca3",
84+
ibcStoreId:
85+
"0xac7814eebdfbf975235bbb796e07533718a9d83201346769e5f281dc90009175",
86+
coins: [
87+
{
88+
typeArg: "0x2::sui::SUI",
89+
objectId:
90+
"0xab70523198047a482febffab381a2a564002459bdcfa98991c747a013b3fd3e4",
91+
},
92+
],
93+
},
94+
},
7695
})
7796

7897
const zkgmClient = yield* ZkgmClient.ZkgmClient

ts-sdk-sui/src/internal/zkgmClient.ts

Lines changed: 52 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import { Transaction } from "@mysten/sui/transactions"
2-
import { Indexer, ZkgmIncomingMessage } from "@unionlabs/sdk"
32
import * as Call from "@unionlabs/sdk/Call"
43
import type { Hex } from "@unionlabs/sdk/schema/hex"
5-
import * as Token from "@unionlabs/sdk/Token"
64
import * as TokenOrder from "@unionlabs/sdk/TokenOrder"
75
import * as Ucs03 from "@unionlabs/sdk/Ucs03"
86
import * as Utils from "@unionlabs/sdk/Utils"
@@ -12,17 +10,14 @@ import * as ClientRequest from "@unionlabs/sdk/ZkgmClientRequest"
1210
import * as ClientResponse from "@unionlabs/sdk/ZkgmClientResponse"
1311
import * as IncomingMessage from "@unionlabs/sdk/ZkgmIncomingMessage"
1412
import * as ZkgmInstruction from "@unionlabs/sdk/ZkgmInstruction"
15-
import { Brand, Chunk, flow, Match, ParseResult, pipe, Predicate, Tuple } from "effect"
13+
import { Match, ParseResult, pipe, Predicate } from "effect"
1614
import * as A from "effect/Array"
1715
import * as Effect from "effect/Effect"
1816
import * as Inspectable from "effect/Inspectable"
19-
import * as O from "effect/Option"
2017
import * as S from "effect/Schema"
2118
import * as Stream from "effect/Stream"
22-
import * as Safe from "../Safe.js"
2319
import * as Sui from "../Sui.js"
2420

25-
// import { Sui } from "../index.js"
2621

2722
export const fromWallet = (
2823
opts: { client: Sui.Sui.PublicClient; wallet: Sui.Sui.WalletClient },
@@ -84,6 +79,8 @@ export const fromWallet = (
8479

8580
console.log("[@unionlabs/sdk-sui/internal/zkgmClient]", { wallet, client })
8681

82+
83+
8784
const timeoutTimestamp = Utils.getTimeoutInNanoseconds24HoursFromNow()
8885
const salt = yield* Utils.generateSalt("sui").pipe(
8986
Effect.mapError((cause) =>
@@ -117,18 +114,36 @@ export const fromWallet = (
117114
const tHeight = 0n
118115
const module = "zkgm" // zkgm module name
119116

120-
// These will be fetched from hubble or from deployments.json
121-
const packageId = "0x8675045186976da5b60baf20dc94413fb5415a7054052dc14d93c13d3dbdf830" // zkgm package id
122-
// TODO: packageId can be changed when zkgm updated
123-
const relayStoreId = "0x393a99c6d55d9a79efa52dea6ea253fef25d2526787127290b985222cc20a924" // This won't be changed for a while
124-
const vaultId = "0x7c4ade19208295ed6bf3c4b58487aa4b917ba87d31460e9e7a917f7f12207ca3" // This won't be changed for a while
125-
const ibcStoreId = "0xac7814eebdfbf975235bbb796e07533718a9d83201346769e5f281dc90009175" // This won't be changed
126117

127-
// This 2 will be get by user all the time
128-
const typeArg = "0x2::sui::SUI" // TODO: This should be dynamic based on the token sent
129-
const coinObjectId = "0x89c430d35fa9f2778b0a635027b178146eb26d70d16292c289304d476ecf76cd" // TODO: This should be given by user
130-
// Note: There can be multiple coins, for simplicity we are using one coin here
131-
// User should be able to provide typeArgs and coinObjectIds array
118+
119+
const suiParams = request.transport?.sui
120+
console.log("request.transport:", request.transport)
121+
if (!suiParams) {
122+
return yield* Effect.fail(
123+
new ClientError.RequestError({
124+
reason: "Transport",
125+
request,
126+
cause: new Error("Missing Sui transport params on ZkgmClientRequest.transport.sui"),
127+
description: "Provide relayStoreId/vaultId/ibcStoreId and coins[]",
128+
}),
129+
)
130+
}
131+
132+
const { relayStoreId, vaultId, ibcStoreId, coins } = suiParams
133+
134+
console.log("[@unionlabs/sdk-sui/internal/zkgmClient]", { relayStoreId, vaultId, ibcStoreId, coins })
135+
// // These will be fetched from hubble or from deployments.json
136+
// const packageId = "0x8675045186976da5b60baf20dc94413fb5415a7054052dc14d93c13d3dbdf830" // zkgm package id
137+
// // TODO: packageId can be changed when zkgm updated
138+
// const relayStoreId = "0x393a99c6d55d9a79efa52dea6ea253fef25d2526787127290b985222cc20a924" // This won't be changed for a while
139+
// const vaultId = "0x7c4ade19208295ed6bf3c4b58487aa4b917ba87d31460e9e7a917f7f12207ca3" // This won't be changed for a while
140+
// const ibcStoreId = "0xac7814eebdfbf975235bbb796e07533718a9d83201346769e5f281dc90009175" // This won't be changed
141+
142+
// // This 2 will be get by user all the time
143+
// const typeArg = "0x2::sui::SUI" // TODO: This should be dynamic based on the token sent
144+
// const coinObjectId = "0x89c430d35fa9f2778b0a635027b178146eb26d70d16292c289304d476ecf76cd" // TODO: This should be given by user
145+
// // Note: There can be multiple coins, for simplicity we are using one coin here
146+
// // User should be able to provide typeArgs and coinObjectIds array
132147

133148
const hexToBytes = (hex: `0x${string}`): Uint8Array => {
134149
const s = hex.slice(2)
@@ -141,33 +156,36 @@ export const fromWallet = (
141156

142157
// 1) begin_send(channel_id: u32, salt: vector<u8>) -> SendCtx
143158
let sendCtx = tx.moveCall({
144-
target: `${packageId}::${module}::begin_send`,
159+
target: `${request.ucs03Address}::${module}::begin_send`,
145160
typeArguments: [],
146161
arguments: [
147162
tx.pure.u32(Number(request.channelId)),
148163
tx.pure.vector("u8", hexToBytes(salt as `0x${string}`)),
149164
],
150165
})
151166

152-
// 2) send_with_coin<T>(relay_store, vault, ibc_store, coin, version, opcode, operand, ctx) -> SendCtx
153-
sendCtx = tx.moveCall({
154-
target: `${packageId}::${module}::send_with_coin`,
155-
typeArguments: [typeArg],
156-
arguments: [
157-
tx.object(relayStoreId),
158-
tx.object(vaultId),
159-
tx.object(ibcStoreId),
160-
tx.object(coinObjectId),
161-
tx.pure.u8(Number(request.instruction.version)),
162-
tx.pure.u8(Number(request.instruction.opcode)),
163-
tx.pure.vector("u8", hexToBytes(operand as `0x${string}`)),
164-
sendCtx,
165-
],
166-
})
167+
// 2) For each coin: send_with_coin<T>(relay_store, vault, ibc_store, coin, version, opcode, operand, ctx) -> SendCtx
168+
for (const { typeArg, objectId } of coins) {
169+
sendCtx = tx.moveCall({
170+
target: `${request.ucs03Address}::${module}::send_with_coin`,
171+
typeArguments: [typeArg],
172+
arguments: [
173+
tx.object(relayStoreId),
174+
tx.object(vaultId),
175+
tx.object(ibcStoreId),
176+
tx.object(objectId),
177+
tx.pure.u8(Number(request.instruction.version)),
178+
tx.pure.u8(Number(request.instruction.opcode)),
179+
tx.pure.vector("u8", hexToBytes(operand as `0x${string}`)),
180+
sendCtx,
181+
],
182+
})
183+
}
184+
167185

168186
// 3) end_send(ibc_store, clock, t_height: u64, timeout_ns: u64, ctx)
169187
tx.moveCall({
170-
target: `${packageId}::${module}::end_send`,
188+
target: `${request.ucs03Address}::${module}::end_send`,
171189
typeArguments: [],
172190
arguments: [
173191
tx.object(ibcStoreId),

ts-sdk/src/ZkgmClientRequest.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,38 @@ import { ChannelId } from "./schema/channel.js"
1313
import type * as Token from "./Token.js"
1414
import type * as ZkgmInstruction from "./ZkgmInstruction.js"
1515

16+
17+
/** @since 2.0.0 */
18+
export namespace Transport {
19+
20+
export interface Sui {
21+
readonly relayStoreId: string
22+
readonly vaultId: string
23+
readonly ibcStoreId: string
24+
/** One or more coins a user wants to spend. Keep array for multi-coin support. */
25+
readonly coins: ReadonlyArray<{
26+
/** e.g. "0x2::sui::SUI" or a custom coin type */
27+
readonly typeArg: string
28+
/** Concrete coin object id(s) for spending */
29+
readonly objectId: string
30+
}>
31+
}
32+
33+
export interface Evm {
34+
readonly _?: never
35+
}
36+
37+
export interface Cosmos {
38+
readonly _?: never
39+
}
40+
41+
export interface Params {
42+
readonly sui?: Sui | undefined
43+
readonly evm?: Evm | undefined
44+
readonly cosmos?: Cosmos | undefined
45+
}
46+
}
47+
1648
/**
1749
* @category type ids
1850
* @since 2.0.0
@@ -40,6 +72,8 @@ export interface ZkgmClientRequest extends Inspectable, Pipeable {
4072
* **NOTE:** only for EVM submission
4173
*/
4274
readonly kind: "execute" | "simulateAndExecute"
75+
/** NEW: optional, per-runtime parameters (non-breaking) */
76+
readonly transport?: Transport.Params | undefined
4377
}
4478

4579
/**
@@ -53,6 +87,8 @@ export interface Options {
5387
readonly ucs03Address: string // XXX: narrow
5488
readonly instruction?: ZkgmInstruction.ZkgmInstruction | undefined
5589
readonly kind?: "execute" | "simulateAndExecute" | undefined
90+
/** NEW: optional, per-runtime parameters (non-breaking) */
91+
readonly transport?: Transport.Params | undefined
5692
}
5793

5894
/**
@@ -66,8 +102,10 @@ export const make: (options: {
66102
ucs03Address: string // XXX: narrow
67103
instruction: ZkgmInstruction.ZkgmInstruction
68104
kind?: "execute" | "simulateAndExecute" | undefined
105+
transport?: Transport.Params | undefined
69106
}) => ZkgmClientRequest = internal.make
70107

108+
71109
/**
72110
* @category combinators
73111
* @since 2.0.0

ts-sdk/src/internal/zkgmClientRequest.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { Hex } from "../schema/hex.js"
99
import type * as Token from "../Token.js"
1010
import type * as ClientRequest from "../ZkgmClientRequest.js"
1111
import { ZkgmInstruction } from "../ZkgmInstruction.js"
12+
import type { Transport } from "../ZkgmClientRequest.js"
1213

1314
/** @internal */
1415
export const TypeId: ClientRequest.TypeId = Symbol.for(
@@ -44,6 +45,7 @@ function makeProto(
4445
ucs03Address: string,
4546
instruction: ZkgmInstruction,
4647
kind: "execute" | "simulateAndExecute",
48+
transport?: Transport.Params | undefined
4749
): ClientRequest.ZkgmClientRequest {
4850
const self = Object.create(Proto)
4951
self.source = source
@@ -52,6 +54,7 @@ function makeProto(
5254
self.ucs03Address = ucs03Address
5355
self.instruction = instruction
5456
self.kind = kind
57+
self.transport = transport
5558
return self
5659
}
5760

@@ -67,6 +70,7 @@ export const empty: ClientRequest.ZkgmClientRequest = makeProto(
6770
void 0 as unknown as Hex,
6871
void 0 as unknown as ZkgmInstruction,
6972
"execute",
73+
undefined,
7074
)
7175

7276
/** @internal */
@@ -77,6 +81,7 @@ export const make = (options: {
7781
ucs03Address: string
7882
instruction: ZkgmInstruction
7983
kind?: "execute" | "simulateAndExecute" | undefined
84+
transport?: Transport.Params | undefined
8085
}) => modify(empty, options)
8186

8287
/** @internal */
@@ -110,6 +115,8 @@ export const modify = dual<
110115
result = setKind(result, options.kind)
111116
}
112117

118+
if (options.transport) result = setTransport(result, options.transport)
119+
113120
return result
114121
})
115122

@@ -127,6 +134,7 @@ export const setSource = dual<
127134
self.ucs03Address,
128135
self.instruction,
129136
self.kind,
137+
self.transport,
130138
))
131139

132140
/** @internal */
@@ -143,8 +151,26 @@ export const setDestination = dual<
143151
self.ucs03Address,
144152
self.instruction,
145153
self.kind,
154+
self.transport,
146155
))
147156

157+
export const setTransport = dual<
158+
(transport: Transport.Params) =>
159+
(self: ClientRequest.ZkgmClientRequest) => ClientRequest.ZkgmClientRequest,
160+
(self: ClientRequest.ZkgmClientRequest, transport: Transport.Params) =>
161+
ClientRequest.ZkgmClientRequest
162+
>(2, (self, transport) =>
163+
makeProto(
164+
self.source,
165+
self.destination,
166+
self.channelId,
167+
self.ucs03Address,
168+
self.instruction,
169+
self.kind,
170+
transport,
171+
))
172+
173+
148174
/** @internal */
149175
export const setChannelId = dual<
150176
(
@@ -159,6 +185,7 @@ export const setChannelId = dual<
159185
self.ucs03Address,
160186
self.instruction,
161187
self.kind,
188+
self.transport,
162189
))
163190
/** @internal */
164191
export const setUcs03Address = dual<
@@ -174,6 +201,7 @@ export const setUcs03Address = dual<
174201
ucs03Address,
175202
self.instruction,
176203
self.kind,
204+
self.transport,
177205
))
178206

179207
/** @internal */
@@ -193,6 +221,7 @@ export const setInstruction = dual<
193221
self.ucs03Address,
194222
instruction,
195223
self.kind,
224+
self.transport,
196225
))
197226

198227
/** @internal */
@@ -212,6 +241,7 @@ export const setKind = dual<
212241
self.ucs03Address,
213242
self.instruction,
214243
kind,
244+
self.transport,
215245
))
216246

217247
/** @internal */

0 commit comments

Comments
 (0)