|
1 | 1 | /**
|
2 |
| - * This module handles EVM related functionality. |
| 2 | + * This module handles Sui related functionality. |
3 | 3 | *
|
4 | 4 | * @since 0.0.0
|
5 | 5 | */
|
6 |
| -import { SuiClient, SuiClientOptions } from "@mysten/sui/client" |
| 6 | +import { SuiClient } from "@mysten/sui/client" |
7 | 7 | import { Ed25519Keypair } from "@mysten/sui/keypairs/ed25519"
|
8 | 8 | 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" |
12 | 9 | 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" |
17 | 12 | import * as internal from "./internal/sui.js"
|
18 | 13 |
|
19 | 14 | /**
|
@@ -87,43 +82,10 @@ export namespace Sui {
|
87 | 82 | // return result
|
88 | 83 | // })
|
89 | 84 |
|
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 |
| - |
100 | 85 | export class ReadCoinError extends Data.TaggedError("ReadCoinError")<{
|
101 | 86 | cause: unknown
|
102 | 87 | }> {}
|
103 | 88 |
|
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 |
| - |
127 | 89 | export const readContract = <T>(
|
128 | 90 | client: SuiClient,
|
129 | 91 | sender: string,
|
@@ -604,243 +566,3 @@ export const readCoinSymbol = (address: string) =>
|
604 | 566 | })
|
605 | 567 | return symbol
|
606 | 568 | })
|
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 |
| -// }) |
0 commit comments