Skip to content

Commit 0622d51

Browse files
committed
chore(ts-sdk-sui): fromWallet improvements
Signed-off-by: kaancaglan <[email protected]>
1 parent 3f8be40 commit 0622d51

File tree

1 file changed

+80
-46
lines changed

1 file changed

+80
-46
lines changed

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

Lines changed: 80 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import * as S from "effect/Schema"
2020
import * as Stream from "effect/Stream"
2121
import * as Sui from "../Sui.js"
2222
import * as Safe from "../Safe.js"
23+
import { Transaction } from "@mysten/sui/transactions"
24+
2325
// import { Sui } from "../index.js"
2426

2527
export const fromWallet = (
@@ -96,7 +98,6 @@ export const fromWallet = (
9698
)
9799

98100
console.log("[@unionlabs/sdk-sui/internal/zkgmClient]", { salt, timeoutTimestamp })
99-
100101
const operand = yield* pipe(
101102
encodeInstruction(request.instruction),
102103
Effect.flatMap(S.encode(Ucs03.Ucs03FromHex)),
@@ -112,59 +113,92 @@ export const fromWallet = (
112113

113114
console.log("[@unionlabs/sdk-sui/internal/zkgmClient]", { operand })
114115

115-
const funds = ClientRequest.requiredFunds(request).pipe(
116-
O.map(A.filter(([x]) => Token.isNative(x))),
117-
O.flatMap(O.liftPredicate(A.isNonEmptyReadonlyArray)),
118-
O.map(A.map(flow(Tuple.getSecond))),
119-
O.map(A.reduce(0n, (acc, n) => acc + n)),
120-
O.getOrUndefined,
121-
)
116+
// ---- Sui PTB: begin_send -> send_with_coin -> end_send ----
122117

123-
console.log("[@unionlabs/sdk-sui/internal/zkgmClient]", { funds })
124-
125-
const args = [
126-
request.channelId,
127-
0n,
128-
timeoutTimestamp,
129-
salt,
130-
{
131-
opcode: request.instruction.opcode,
132-
version: request.instruction.version,
133-
operand,
134-
},
135-
] as const
136-
137-
console.log("[@unionlabs/sdk-sui/internal/zkgmClient]", { args })
138-
139-
// TODO: Fix writecontract calling, decide parameters etc.
140-
const sendInstruction = Sui.writeContract({
141-
client: client,
142-
account: wallet.signer,
143-
abi: Ucs03.Abi,
144-
chain: wallet.chain,
145-
functionName: "send",
146-
address: request.ucs03Address as unknown as any,
147-
args,
148-
value: funds,
149-
}).pipe(
150-
Effect.mapError((cause) =>
118+
const tx = new Transaction()
119+
const CLOCK_OBJECT_ID = "0x6" // Sui system clock
120+
const tHeight = 0n
121+
const packageId = "0x8675045186976da5b60baf20dc94413fb5415a7054052dc14d93c13d3dbdf830" //zkgm package id TODO: This should be fetched from somewhere
122+
const module = "zkgm" //zkgm module name
123+
const typeArg = "0x2::sui::SUI" // TODO: This should be dynamic based on the token sent
124+
const relayStoreId = "0x393a99c6d55d9a79efa52dea6ea253fef25d2526787127290b985222cc20a924" // TODO: This should be fetched from somewhere
125+
const vaultId = "0x7c4ade19208295ed6bf3c4b58487aa4b917ba87d31460e9e7a917f7f12207ca3" // TODO: This should be fetched from somewhere
126+
const ibcStoreId = "0xac7814eebdfbf975235bbb796e07533718a9d83201346769e5f281dc90009175" // TODO: This should be fetched from somewhere
127+
const coinObjectId = "0x3997d4c40cb190283291270d326401fd77320af42cd7a891ded2eba3e52f4b16" // TODO: This should be given by user
128+
129+
// helpers
130+
const hexToBytes = (hex: `0x${string}`): Uint8Array => {
131+
const s = hex.slice(2)
132+
const out = new Uint8Array(s.length / 2)
133+
for (let i = 0; i < out.length; i++) out[i] = parseInt(s.slice(i * 2, i * 2 + 2), 16)
134+
return out
135+
}
136+
137+
// 1) begin_send(channel_id: u32, salt: vector<u8>) -> SendCtx
138+
let sendCtx = tx.moveCall({
139+
target: `${packageId}::${module}::begin_send`,
140+
typeArguments: [],
141+
arguments: [
142+
tx.pure.u32(Number(request.channelId)), // channel id (u32)
143+
tx.pure.vector("u8", hexToBytes(salt as `0x${string}`)), // salt bytes
144+
],
145+
})
146+
147+
// 2) send_with_coin<T>(relay_store, vault, ibc_store, coin, version, opcode, operand, ctx) -> SendCtx
148+
sendCtx = tx.moveCall({
149+
target: `${packageId}::${module}::send_with_coin`,
150+
typeArguments: [typeArg],
151+
arguments: [
152+
tx.object(relayStoreId),
153+
tx.object(vaultId),
154+
tx.object(ibcStoreId),
155+
tx.object(coinObjectId),
156+
tx.pure.u8(Number(request.instruction.version)),
157+
tx.pure.u8(Number(request.instruction.opcode)),
158+
tx.pure.vector("u8", hexToBytes(operand as `0x${string}`)),
159+
sendCtx,
160+
],
161+
})
162+
163+
// 3) end_send(ibc_store, clock, t_height: u64, timeout_ns: u64, ctx)
164+
tx.moveCall({
165+
target: `${packageId}::${module}::end_send`,
166+
typeArguments: [],
167+
arguments: [
168+
tx.object(ibcStoreId),
169+
tx.object(CLOCK_OBJECT_ID),
170+
tx.pure.u64(tHeight),
171+
tx.pure.u64(BigInt(timeoutTimestamp)),
172+
sendCtx,
173+
],
174+
})
175+
176+
// sign & execute
177+
const submit = Effect.tryPromise({
178+
try: async () =>
179+
wallet.client.signAndExecuteTransaction({
180+
signer: wallet.signer,
181+
transaction: tx,
182+
}),
183+
catch: (cause) =>
151184
new ClientError.RequestError({
152185
reason: "Transport",
153186
request,
154187
cause,
155-
description: "writeContract",
156-
})
157-
),
158-
Effect.provideService(Evm.WalletClient, wallet),
159-
)
188+
description: "signAndExecuteTransaction",
189+
}),
190+
})
160191

161-
return yield* pipe(
162-
sendInstruction,
163-
Effect.map((txHash) => new ClientResponseImpl(request, client, txHash)),
164-
)
165-
})
192+
const res = yield* submit
193+
194+
console.log("Res.transaction:", res.transaction)
195+
const txHash = (res.digest ?? res.transaction?.txSignatures[0] ?? "") as Hex
196+
197+
return new ClientResponseImpl(request, client, txHash)
198+
}),
166199
)
167200

201+
168202
/** @internal */
169203
export abstract class IncomingMessageImpl<E> extends Inspectable.Class
170204
implements IncomingMessage.ZkgmIncomingMessage<E>

0 commit comments

Comments
 (0)