Skip to content

Commit f019376

Browse files
committed
chore(ts-sdk-sui): some cleanup
Signed-off-by: kaancaglan <[email protected]>
1 parent 65981ce commit f019376

File tree

10 files changed

+41
-349
lines changed

10 files changed

+41
-349
lines changed

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

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ const program = Effect.gen(function*() {
4242

4343
console.log("source", source)
4444

45-
// TODO: Destination will be somewhere
4645
const destination = yield* ChainRegistry.byUniversalId(
4746
UniversalChainId.make("union.union-1"),
4847
)
@@ -73,38 +72,30 @@ const program = Effect.gen(function*() {
7372
channelId: ChannelId.make(5),
7473
ucs03Address: "0x8675045186976da5b60baf20dc94413fb5415a7054052dc14d93c13d3dbdf830",
7574
instruction: tokenOrder,
76-
75+
7776
// NEW — only read by the Sui client
7877
transport: {
7978
sui: {
80-
relayStoreId:
81-
"0x393a99c6d55d9a79efa52dea6ea253fef25d2526787127290b985222cc20a924",
82-
vaultId:
83-
"0x7c4ade19208295ed6bf3c4b58487aa4b917ba87d31460e9e7a917f7f12207ca3",
84-
ibcStoreId:
85-
"0xac7814eebdfbf975235bbb796e07533718a9d83201346769e5f281dc90009175",
79+
relayStoreId: "0x393a99c6d55d9a79efa52dea6ea253fef25d2526787127290b985222cc20a924",
80+
vaultId: "0x7c4ade19208295ed6bf3c4b58487aa4b917ba87d31460e9e7a917f7f12207ca3",
81+
ibcStoreId: "0xac7814eebdfbf975235bbb796e07533718a9d83201346769e5f281dc90009175",
8682
coins: [
8783
{
8884
typeArg: "0x2::sui::SUI",
89-
objectId:
90-
"0xab70523198047a482febffab381a2a564002459bdcfa98991c747a013b3fd3e4",
85+
objectId: "0x266d00c4b329111255339c041cc57a1b616cfeddafdae47df8f814002578e95b",
9186
},
9287
],
9388
},
9489
},
9590
})
9691

92+
yield* Effect.log("ZKGM Client Request", request)
93+
9794
const zkgmClient = yield* ZkgmClient.ZkgmClient
9895

9996
const response: ZkgmClientResponse.ZkgmClientResponse = yield* zkgmClient.execute(request)
10097

10198
yield* Effect.log("Submission Hash", response.txHash)
102-
103-
const completion = yield* response.waitFor(
104-
ZkgmIncomingMessage.LifecycleEvent.$is("EvmTransactionReceiptComplete"),
105-
)
106-
107-
yield* Effect.log("Completion", completion)
10899
}).pipe(
109100
Effect.provide(layerWithoutWallet),
110101
Effect.provide(PublicClient.Live({ url: getFullnodeUrl("testnet") })),

ts-sdk-sui/examples/sui-create-client-write-contract.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ import {
1717
writeContract,
1818
} from "../src/Sui.js"
1919

20-
const MNEMONIC = process.env.MNEMONIC
21-
?? "fix auto gallery heart practice drip joke nice decline lift attend bread"
20+
const MNEMONIC = process.env.SUI_MNEMONIC ?? "..."
2221
const RECIPIENT = process.env.RECIPIENT
2322
?? "0x03ff9dd9e093387bdd4432c6a3eb6a1bd5a8f39a530042ac7efe576f18d3232b"
2423

ts-sdk-sui/src/Sui.ts

Lines changed: 4 additions & 282 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,14 @@
11
/**
2-
* This module handles EVM related functionality.
2+
* This module handles Sui related functionality.
33
*
44
* @since 0.0.0
55
*/
6-
import { SuiClient, SuiClientOptions } from "@mysten/sui/client"
6+
import { SuiClient } from "@mysten/sui/client"
77
import { Ed25519Keypair } from "@mysten/sui/keypairs/ed25519"
88
import { Transaction } from "@mysten/sui/transactions"
9-
import { GAS_DENOMS } from "@unionlabs/sdk/Constants"
10-
import { UniversalChainId } from "@unionlabs/sdk/schema/chain"
11-
import * as Ucs03 from "@unionlabs/sdk/Ucs03"
129
import { extractErrorDetails } from "@unionlabs/sdk/Utils"
13-
import * as Utils from "@unionlabs/sdk/Utils"
14-
import { Context, Data, Effect, flow, Layer, pipe, Schema as S } from "effect"
15-
import { type Address, erc20Abi } from "viem"
16-
import type { Hex } from "viem"
10+
import { Context, Data, Effect, flow, Layer } from "effect"
11+
import { type Address } from "viem"
1712
import * as internal from "./internal/sui.js"
1813

1914
/**
@@ -87,43 +82,10 @@ export namespace Sui {
8782
// return result
8883
// })
8984

90-
/**
91-
* @category errors
92-
* @since 0.0.0
93-
*/
94-
export class WaitForTransactionReceiptError extends Data.TaggedError(
95-
"WaitForTransactionReceiptError",
96-
)<{
97-
cause: unknown
98-
}> {}
99-
10085
export class ReadCoinError extends Data.TaggedError("ReadCoinError")<{
10186
cause: unknown
10287
}> {}
10388

104-
// /**
105-
// * Wait for a transaction receipt
106-
// * @param hash The transaction hash for which to wait
107-
// * @returns An Effect that resolves to the transaction receipt
108-
// *
109-
// * @category utils
110-
// * @since 0.0.0
111-
// */
112-
// export const waitForTransactionReceipt = (hash: Hash) =>
113-
// Effect.gen(function*() {
114-
// const client = (yield* PublicClient).client
115-
116-
// const receipt = yield* Effect.tryPromise({
117-
// try: () => client.waitForTransactionReceipt({ hash }),
118-
// catch: err =>
119-
// new WaitForTransactionReceiptError({
120-
// cause: Utils.extractErrorDetails(err as WaitForTransactionReceiptTimeoutErrorType),
121-
// }),
122-
// })
123-
124-
// return receipt
125-
// })
126-
12789
export const readContract = <T>(
12890
client: SuiClient,
12991
sender: string,
@@ -604,243 +566,3 @@ export const readCoinSymbol = (address: string) =>
604566
})
605567
return symbol
606568
})
607-
608-
/**
609-
* PTB: begin_send -> (send_with_coin<T> ...)* -> end_send
610-
*
611-
* Mirrors:
612-
* sui client ptb \
613-
* --move-call "$PKG::$MOD::begin_send" $CHANNEL_ID $SALT \
614-
* --assign send_ctx1 \
615-
* --move-call "$PKG::$MOD::send_with_coin" "<$TYPE_T>" \
616-
* $RELAY_STORE $VAULT $IBC_STORE $COIN $VERSION $OPCODE $OPERAND send_ctx1 \
617-
* --assign send_ctx2 \
618-
* --move-call "$PKG::$MOD::end_send" $IBC_STORE $CLOCK $T_HEIGHT $T_TS send_ctx2 \
619-
* --gas-budget 150000000
620-
*/
621-
export const sendInstruction = (params: {
622-
packageId: string
623-
624-
/** coin type, e.g. "0x2::sui::SUI" (used in typeArguments of send_with_coin) */
625-
typeArg: string
626-
627-
relayStoreId: string // $RELAY_STORE
628-
vaultId: string // $VAULT
629-
ibcStoreId: string // $IBC_STORE
630-
coinObjectId: string // $COIN
631-
632-
// extraSendCalls?: Array<{
633-
// relayStoreId?: string
634-
// vaultId?: string
635-
// ibcStoreId?: string
636-
// coinObjectId?: string
637-
// version?: number
638-
// opcode?: number
639-
// operandHex?: `0x${string}`
640-
// typeArg?: string
641-
// }>
642-
643-
instruction: Ucs03.Instruction
644-
}) =>
645-
Effect.gen(function*() {
646-
const module = "zkgm"
647-
const clockObjectId = "0x6" // Sui system clock object
648-
649-
const { client, signer } = yield* WalletClient
650-
const channelId = (yield* ChannelSource).channelId
651-
652-
const salt = yield* Utils.generateSalt("evm") // TODO: check if evm will work here or not
653-
const timeoutNs = Utils.getTimeoutInNanoseconds24HoursFromNow()
654-
const tHeight = BigInt(0)
655-
656-
const operandHex =
657-
(yield* S.encode(Ucs03.InstructionFromHex)(params.instruction)) as `0x${string}`
658-
659-
// helpers
660-
const hexToBytes = (hex: `0x${string}`): Uint8Array => {
661-
const s = hex.slice(2)
662-
const out = new Uint8Array(s.length / 2)
663-
for (let i = 0; i < out.length; i++) {
664-
out[i] = parseInt(s.slice(i * 2, i * 2 + 2), 16)
665-
}
666-
return out
667-
}
668-
669-
const tx = new Transaction()
670-
// if (params.gasBudget !== undefined) tx.setGasBudget(BigInt(params.gasBudget as any))
671-
// adress: 0x1234
672-
let sendCtx = tx.moveCall({
673-
target: `${params.packageId}::${module}::begin_send`,
674-
typeArguments: [],
675-
arguments: [
676-
tx.pure.u32(channelId),
677-
tx.pure.vector("u8", hexToBytes(salt as `0x${string}`)),
678-
],
679-
})
680-
681-
const pushSendWithCoin = (cfg: {
682-
relayStoreId: string
683-
vaultId: string
684-
ibcStoreId: string
685-
coinObjectId: string
686-
version: number
687-
opcode: number
688-
operandHex: `0x${string}`
689-
typeArg: string
690-
}) => {
691-
sendCtx = tx.moveCall({
692-
target: `${params.packageId}::${module}::send_with_coin`,
693-
typeArguments: [cfg.typeArg],
694-
arguments: [
695-
tx.object(cfg.relayStoreId),
696-
tx.object(cfg.vaultId),
697-
tx.object(cfg.ibcStoreId),
698-
tx.object(cfg.coinObjectId),
699-
tx.pure.u8(cfg.version),
700-
tx.pure.u8(cfg.opcode),
701-
tx.pure.vector("u8", hexToBytes(cfg.operandHex)),
702-
sendCtx,
703-
],
704-
})
705-
}
706-
707-
pushSendWithCoin({
708-
relayStoreId: params.relayStoreId,
709-
vaultId: params.vaultId,
710-
ibcStoreId: params.ibcStoreId,
711-
coinObjectId: params.coinObjectId,
712-
version: params.instruction.version,
713-
opcode: params.instruction.opcode,
714-
operandHex,
715-
typeArg: params.typeArg,
716-
})
717-
718-
// TODO: multiple send_with_coin calls if needed??? will this work?
719-
// for (const extra of params.extraSendCalls ?? []) {
720-
// pushSendWithCoin({
721-
// relayStoreId: extra.relayStoreId ?? params.relayStoreId,
722-
// vaultId: extra.vaultId ?? params.vaultId,
723-
// ibcStoreId: extra.ibcStoreId ?? params.ibcStoreId,
724-
// coinObjectId: extra.coinObjectId ?? params.coinObjectId,
725-
// version: extra.version ?? params.version,
726-
// opcode: extra.opcode ?? params.opcode,
727-
// operandHex: (extra.operandHex ?? operandHex) as `0x${string}`,
728-
// typeArg: extra.typeArg ?? params.typeArg,
729-
// })
730-
// }
731-
732-
tx.moveCall({
733-
target: `${params.packageId}::${module}::end_send`,
734-
typeArguments: [],
735-
arguments: [
736-
tx.object(params.ibcStoreId),
737-
tx.object(clockObjectId),
738-
tx.pure.u64(tHeight),
739-
tx.pure.u64(BigInt(timeoutNs)), // ns
740-
sendCtx,
741-
],
742-
})
743-
744-
const res = yield* Effect.tryPromise({
745-
try: async () =>
746-
client.signAndExecuteTransaction({
747-
signer,
748-
transaction: tx,
749-
}),
750-
catch: (e) => new WriteContractError({ cause: extractErrorDetails(e as Error) }),
751-
})
752-
753-
return res
754-
})
755-
756-
// turn a hex string like "0xdeadbeef" into a number[] of bytes
757-
function hexToBytes(hex: string): number[] {
758-
const h = hex.startsWith("0x") ? hex.slice(2) : hex
759-
return h.match(/.{1,2}/g)!.map(b => parseInt(b, 16))
760-
}
761-
762-
// // TODO: Decide the parameters here.
763-
// /**
764-
// * @category utils
765-
// * @since 0.0.0
766-
// */
767-
// export const sendInstruction = (instruction: Ucs03.Instruction) =>
768-
// Effect.gen(function*() {
769-
// const walletClient = yield* WalletClient
770-
// const sourceConfig = yield* ChannelSource
771-
772-
// const timeoutTimestamp = Utils.getTimeoutInNanoseconds24HoursFromNow()
773-
// const salt = yield* Utils.generateSalt("evm")
774-
775-
// const operand = yield* S.encode(Ucs03.InstructionFromHex)(instruction)
776-
777-
// return yield* writeContract({
778-
// client: walletClient.client,
779-
// signer: walletClient.signer,
780-
// account: walletClient.account,
781-
// abi: Ucs03.Abi,
782-
// // chain: walletClient.chain, TODO: Do we need this?
783-
// fn: "send",
784-
// address: sourceConfig.ucs03address,
785-
// args: [
786-
// sourceConfig.channelId,
787-
// 0n,
788-
// timeoutTimestamp,
789-
// salt,
790-
// {
791-
// opcode: instruction.opcode,
792-
// version: instruction.version,
793-
// operand,
794-
// },
795-
// ],
796-
// value: 10n,
797-
// })
798-
// })
799-
800-
// /**
801-
// * Read the balance of an ERC20 token for a specific address
802-
// * @param tokenAddress The address of the ERC20 token
803-
// * @param ownerAddress The address to check the balance for
804-
// * @param blockNumber The blockNumber at certain point
805-
// * @returns An Effect that resolves to the token balance
806-
// *
807-
// * @category utils
808-
// * @since 0.0.0
809-
// */
810-
// export const readErc20BalanceAtBlock = (
811-
// tokenAddress: Address,
812-
// ownerAddress: Address,
813-
// blockNumber: bigint,
814-
// ) =>
815-
// Effect.gen(function*() {
816-
// const client = (yield* PublicClient).client
817-
818-
// return yield* readContract(client, {
819-
// address: tokenAddress,
820-
// abi: erc20Abi,
821-
// functionName: "balanceOf",
822-
// args: [ownerAddress],
823-
// blockNumber: blockNumber,
824-
// })
825-
// })
826-
827-
// /**
828-
// * Read the TotalSupply of an ERC20 token
829-
// * @param tokenAddress The address of the ERC20 token
830-
// * @param blockNumber The blockNumber at certain point
831-
// * @returns An Effect that resolves to the totalSupply
832-
// *
833-
// * @category utils
834-
// * @since 0.0.0
835-
// */
836-
// export const readErc20TotalSupplyAtBlock = (tokenAddress: Address, blockNumber: bigint) =>
837-
// Effect.gen(function*() {
838-
// const client = (yield* PublicClient).client
839-
840-
// return yield* readContract(client, {
841-
// address: tokenAddress,
842-
// abi: erc20Abi,
843-
// functionName: "totalSupply",
844-
// blockNumber: blockNumber,
845-
// })
846-
// })

ts-sdk-sui/src/SuiZkgmClient.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* This module defines a concrete {@link ZkgmClient} for EVM source chain usage.
2+
* This module defines a concrete {@link ZkgmClient} for Sui source chain usage.
33
*
44
* @since 0.0.0
55
*/

0 commit comments

Comments
 (0)