@satoshai/kit 0.4.1 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +85 -0
- package/dist/index.cjs +388 -2
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +126 -5
- package/dist/index.d.ts +126 -5
- package/dist/index.js +386 -4
- package/dist/index.js.map +1 -1
- package/package.json +3 -2
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
|
-
import { PostCondition, PostConditionMode
|
|
2
|
+
import { ClarityValue, TupleCV, PostCondition, PostConditionMode } from '@stacks/transactions';
|
|
3
|
+
import { ClarityAbi, ExtractAbiFunctionNames, Pretty, ExtractAbiFunction, ClarityAbiArgToPrimitiveTypeValue } from 'clarity-abitype';
|
|
4
|
+
export { ClarityAbi } from 'clarity-abitype';
|
|
3
5
|
|
|
4
6
|
declare const SUPPORTED_STACKS_WALLETS: readonly ["xverse", "leather", "asigna", "fordefi", "wallet-connect", "okx"];
|
|
5
7
|
type SupportedStacksWallet = (typeof SUPPORTED_STACKS_WALLETS)[number];
|
|
@@ -122,26 +124,134 @@ declare const useSignMessage: () => {
|
|
|
122
124
|
status: MutationStatus;
|
|
123
125
|
};
|
|
124
126
|
|
|
127
|
+
interface SignStructuredMessageVariables {
|
|
128
|
+
message: ClarityValue;
|
|
129
|
+
domain: TupleCV;
|
|
130
|
+
}
|
|
131
|
+
interface SignStructuredMessageData {
|
|
132
|
+
publicKey: string;
|
|
133
|
+
signature: string;
|
|
134
|
+
}
|
|
135
|
+
interface SignStructuredMessageOptions {
|
|
136
|
+
onSuccess?: (data: SignStructuredMessageData) => void;
|
|
137
|
+
onError?: (error: Error) => void;
|
|
138
|
+
onSettled?: (data: SignStructuredMessageData | undefined, error: Error | null) => void;
|
|
139
|
+
}
|
|
140
|
+
declare const useSignStructuredMessage: () => {
|
|
141
|
+
signStructuredMessage: (variables: SignStructuredMessageVariables, options?: SignStructuredMessageOptions) => void;
|
|
142
|
+
signStructuredMessageAsync: (variables: SignStructuredMessageVariables) => Promise<SignStructuredMessageData>;
|
|
143
|
+
reset: () => void;
|
|
144
|
+
data: SignStructuredMessageData | undefined;
|
|
145
|
+
error: Error | null;
|
|
146
|
+
isError: boolean;
|
|
147
|
+
isIdle: boolean;
|
|
148
|
+
isPending: boolean;
|
|
149
|
+
isSuccess: boolean;
|
|
150
|
+
status: MutationStatus;
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
interface SignTransactionVariables {
|
|
154
|
+
transaction: string;
|
|
155
|
+
broadcast?: boolean;
|
|
156
|
+
}
|
|
157
|
+
interface SignTransactionData {
|
|
158
|
+
transaction: string;
|
|
159
|
+
txid?: string;
|
|
160
|
+
}
|
|
161
|
+
interface SignTransactionOptions {
|
|
162
|
+
onSuccess?: (data: SignTransactionData) => void;
|
|
163
|
+
onError?: (error: Error) => void;
|
|
164
|
+
onSettled?: (data: SignTransactionData | undefined, error: Error | null) => void;
|
|
165
|
+
}
|
|
166
|
+
declare const useSignTransaction: () => {
|
|
167
|
+
signTransaction: (variables: SignTransactionVariables, options?: SignTransactionOptions) => void;
|
|
168
|
+
signTransactionAsync: (variables: SignTransactionVariables) => Promise<SignTransactionData>;
|
|
169
|
+
reset: () => void;
|
|
170
|
+
data: SignTransactionData | undefined;
|
|
171
|
+
error: Error | null;
|
|
172
|
+
isError: boolean;
|
|
173
|
+
isIdle: boolean;
|
|
174
|
+
isPending: boolean;
|
|
175
|
+
isSuccess: boolean;
|
|
176
|
+
status: MutationStatus;
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
interface TransferSTXVariables {
|
|
180
|
+
recipient: string;
|
|
181
|
+
amount: bigint | number | string;
|
|
182
|
+
memo?: string;
|
|
183
|
+
fee?: bigint | number | string;
|
|
184
|
+
nonce?: bigint | number | string;
|
|
185
|
+
}
|
|
186
|
+
interface TransferSTXOptions {
|
|
187
|
+
onSuccess?: (txid: string) => void;
|
|
188
|
+
onError?: (error: Error) => void;
|
|
189
|
+
onSettled?: (txid: string | undefined, error: Error | null) => void;
|
|
190
|
+
}
|
|
191
|
+
declare const useTransferSTX: () => {
|
|
192
|
+
transferSTX: (variables: TransferSTXVariables, options?: TransferSTXOptions) => void;
|
|
193
|
+
transferSTXAsync: (variables: TransferSTXVariables) => Promise<string>;
|
|
194
|
+
reset: () => void;
|
|
195
|
+
data: string | undefined;
|
|
196
|
+
error: Error | null;
|
|
197
|
+
isError: boolean;
|
|
198
|
+
isIdle: boolean;
|
|
199
|
+
isPending: boolean;
|
|
200
|
+
isSuccess: boolean;
|
|
201
|
+
status: MutationStatus;
|
|
202
|
+
};
|
|
203
|
+
|
|
204
|
+
/** Contract principal string in the format `address.contract-name` */
|
|
205
|
+
type TraitReference = `${string}.${string}`;
|
|
206
|
+
/** Union of public function names from a Clarity ABI */
|
|
207
|
+
type PublicFunctionName<TAbi extends ClarityAbi> = ExtractAbiFunctionNames<TAbi, 'public'>;
|
|
208
|
+
/** Named args object for a specific public function, derived from ABI */
|
|
209
|
+
type PublicFunctionArgs<TAbi extends ClarityAbi, TFn extends ExtractAbiFunctionNames<TAbi, 'public'>> = Pretty<{
|
|
210
|
+
[K in ExtractAbiFunction<TAbi, TFn, 'public'>['args'][number] as K['name']]: ClarityAbiArgToPrimitiveTypeValue<K>;
|
|
211
|
+
}>;
|
|
212
|
+
|
|
125
213
|
interface PostConditionConfig {
|
|
126
214
|
postConditions: PostCondition[];
|
|
127
215
|
mode: PostConditionMode;
|
|
128
216
|
}
|
|
129
|
-
|
|
217
|
+
/** Typed mode: ABI present, args is a named object with autocomplete. */
|
|
218
|
+
interface TypedWriteContractVariables<TAbi extends ClarityAbi, TFn extends ExtractAbiFunctionNames<TAbi, 'public'>> {
|
|
219
|
+
abi: TAbi;
|
|
220
|
+
address: string;
|
|
221
|
+
contract: string;
|
|
222
|
+
functionName: TFn;
|
|
223
|
+
args: PublicFunctionArgs<TAbi, TFn>;
|
|
224
|
+
pc: PostConditionConfig;
|
|
225
|
+
}
|
|
226
|
+
/** Untyped mode: no ABI, args is ClarityValue[] (original behavior). */
|
|
227
|
+
interface UntypedWriteContractVariables {
|
|
130
228
|
address: string;
|
|
131
229
|
contract: string;
|
|
132
230
|
functionName: string;
|
|
133
231
|
args: ClarityValue[];
|
|
134
232
|
pc: PostConditionConfig;
|
|
135
233
|
}
|
|
234
|
+
/** Backward-compatible alias for the untyped variant. */
|
|
235
|
+
type WriteContractVariables = UntypedWriteContractVariables;
|
|
136
236
|
interface WriteContractOptions {
|
|
137
237
|
onSuccess?: (txHash: string) => void;
|
|
138
238
|
onError?: (error: Error) => void;
|
|
139
239
|
onSettled?: (txHash: string | undefined, error: Error | null) => void;
|
|
140
240
|
}
|
|
241
|
+
/** Overloaded async function: typed mode (with ABI) or untyped mode. */
|
|
242
|
+
interface WriteContractAsyncFn {
|
|
243
|
+
<const TAbi extends ClarityAbi, TFn extends ExtractAbiFunctionNames<TAbi, 'public'>>(variables: TypedWriteContractVariables<TAbi, TFn>): Promise<string>;
|
|
244
|
+
(variables: UntypedWriteContractVariables): Promise<string>;
|
|
245
|
+
}
|
|
246
|
+
/** Overloaded fire-and-forget function: typed mode or untyped mode. */
|
|
247
|
+
interface WriteContractFn {
|
|
248
|
+
<const TAbi extends ClarityAbi, TFn extends ExtractAbiFunctionNames<TAbi, 'public'>>(variables: TypedWriteContractVariables<TAbi, TFn>, options?: WriteContractOptions): void;
|
|
249
|
+
(variables: UntypedWriteContractVariables, options?: WriteContractOptions): void;
|
|
250
|
+
}
|
|
141
251
|
|
|
142
252
|
declare const useWriteContract: () => {
|
|
143
|
-
writeContract:
|
|
144
|
-
writeContractAsync:
|
|
253
|
+
writeContract: WriteContractFn;
|
|
254
|
+
writeContractAsync: WriteContractAsyncFn;
|
|
145
255
|
reset: () => void;
|
|
146
256
|
data: string | undefined;
|
|
147
257
|
error: Error | null;
|
|
@@ -174,4 +284,15 @@ interface StacksWallets {
|
|
|
174
284
|
}
|
|
175
285
|
declare const getStacksWallets: () => StacksWallets;
|
|
176
286
|
|
|
177
|
-
|
|
287
|
+
/** Pre-bind ABI + address + contract for reuse with useWriteContract. */
|
|
288
|
+
declare function createContractConfig<const TAbi extends ClarityAbi>(config: {
|
|
289
|
+
abi: TAbi;
|
|
290
|
+
address: string;
|
|
291
|
+
contract: string;
|
|
292
|
+
}): {
|
|
293
|
+
abi: TAbi;
|
|
294
|
+
address: string;
|
|
295
|
+
contract: string;
|
|
296
|
+
};
|
|
297
|
+
|
|
298
|
+
export { type ConnectOptions, type MutationStatus, type PostConditionConfig, type PublicFunctionArgs, type PublicFunctionName, SUPPORTED_STACKS_WALLETS, type SignMessageData, type SignMessageOptions, type SignMessageVariables, type SignStructuredMessageData, type SignStructuredMessageOptions, type SignStructuredMessageVariables, type SignTransactionData, type SignTransactionOptions, type SignTransactionVariables, type StacksChain, StacksWalletProvider, type StacksWallets, type SupportedStacksWallet, type TraitReference, type TransferSTXOptions, type TransferSTXVariables, type TypedWriteContractVariables, type UntypedWriteContractVariables, type WalletConnectMetadata, type WalletContextValue, type WalletInfo, type WalletState, type WriteContractOptions, type WriteContractVariables, createContractConfig, getLocalStorageWallet, getNetworkFromAddress, getStacksWallets, useAddress, useBnsName, useConnect, useDisconnect, useSignMessage, useSignStructuredMessage, useSignTransaction, useTransferSTX, useWallets, useWriteContract };
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { WalletConnect, DEFAULT_PROVIDERS, WALLET_CONNECT_PROVIDER, clearSelectedProviderId, request, getSelectedProviderId, getSelectedProvider, setSelectedProviderId } from '@stacks/connect';
|
|
2
2
|
import { createContext, useState, useRef, useEffect, useCallback, useMemo, useContext } from 'react';
|
|
3
3
|
import { jsx } from 'react/jsx-runtime';
|
|
4
|
-
import { PostConditionMode, postConditionToHex, cvToHex } from '@stacks/transactions';
|
|
4
|
+
import { PostConditionMode, postConditionToHex, cvToHex, noneCV, contractPrincipalCV, standardPrincipalCV, boolCV, intCV, uintCV, bufferCV, stringAsciiCV, stringUtf8CV, someCV, listCV, tupleCV } from '@stacks/transactions';
|
|
5
5
|
import { getPrimaryName } from 'bns-v2-sdk';
|
|
6
6
|
|
|
7
7
|
// src/constants/stacks-provider-mapping.ts
|
|
@@ -750,6 +750,153 @@ var useSignMessage = () => {
|
|
|
750
750
|
[signMessage, signMessageAsync, reset, data, error, status]
|
|
751
751
|
);
|
|
752
752
|
};
|
|
753
|
+
var useSignStructuredMessage = () => {
|
|
754
|
+
const { isConnected, provider } = useAddress();
|
|
755
|
+
const [data, setData] = useState(
|
|
756
|
+
void 0
|
|
757
|
+
);
|
|
758
|
+
const [error, setError] = useState(null);
|
|
759
|
+
const [status, setStatus] = useState("idle");
|
|
760
|
+
const signStructuredMessageAsync = useCallback(
|
|
761
|
+
async (variables) => {
|
|
762
|
+
if (!isConnected) {
|
|
763
|
+
throw new Error("Wallet is not connected");
|
|
764
|
+
}
|
|
765
|
+
if (provider === "okx") {
|
|
766
|
+
throw new Error(
|
|
767
|
+
"Structured message signing is not supported by OKX wallet"
|
|
768
|
+
);
|
|
769
|
+
}
|
|
770
|
+
setStatus("pending");
|
|
771
|
+
setError(null);
|
|
772
|
+
setData(void 0);
|
|
773
|
+
try {
|
|
774
|
+
const result = await request("stx_signStructuredMessage", {
|
|
775
|
+
message: variables.message,
|
|
776
|
+
domain: variables.domain
|
|
777
|
+
});
|
|
778
|
+
setData(result);
|
|
779
|
+
setStatus("success");
|
|
780
|
+
return result;
|
|
781
|
+
} catch (err) {
|
|
782
|
+
const error2 = err instanceof Error ? err : new Error(String(err));
|
|
783
|
+
setError(error2);
|
|
784
|
+
setStatus("error");
|
|
785
|
+
throw error2;
|
|
786
|
+
}
|
|
787
|
+
},
|
|
788
|
+
[isConnected, provider]
|
|
789
|
+
);
|
|
790
|
+
const signStructuredMessage = useCallback(
|
|
791
|
+
(variables, options) => {
|
|
792
|
+
signStructuredMessageAsync(variables).then((data2) => {
|
|
793
|
+
options?.onSuccess?.(data2);
|
|
794
|
+
options?.onSettled?.(data2, null);
|
|
795
|
+
}).catch((error2) => {
|
|
796
|
+
options?.onError?.(error2);
|
|
797
|
+
options?.onSettled?.(void 0, error2);
|
|
798
|
+
});
|
|
799
|
+
},
|
|
800
|
+
[signStructuredMessageAsync]
|
|
801
|
+
);
|
|
802
|
+
const reset = useCallback(() => {
|
|
803
|
+
setData(void 0);
|
|
804
|
+
setError(null);
|
|
805
|
+
setStatus("idle");
|
|
806
|
+
}, []);
|
|
807
|
+
return useMemo(
|
|
808
|
+
() => ({
|
|
809
|
+
signStructuredMessage,
|
|
810
|
+
signStructuredMessageAsync,
|
|
811
|
+
reset,
|
|
812
|
+
data,
|
|
813
|
+
error,
|
|
814
|
+
isError: status === "error",
|
|
815
|
+
isIdle: status === "idle",
|
|
816
|
+
isPending: status === "pending",
|
|
817
|
+
isSuccess: status === "success",
|
|
818
|
+
status
|
|
819
|
+
}),
|
|
820
|
+
[
|
|
821
|
+
signStructuredMessage,
|
|
822
|
+
signStructuredMessageAsync,
|
|
823
|
+
reset,
|
|
824
|
+
data,
|
|
825
|
+
error,
|
|
826
|
+
status
|
|
827
|
+
]
|
|
828
|
+
);
|
|
829
|
+
};
|
|
830
|
+
var useSignTransaction = () => {
|
|
831
|
+
const { isConnected, provider } = useAddress();
|
|
832
|
+
const [data, setData] = useState(void 0);
|
|
833
|
+
const [error, setError] = useState(null);
|
|
834
|
+
const [status, setStatus] = useState("idle");
|
|
835
|
+
const signTransactionAsync = useCallback(
|
|
836
|
+
async (variables) => {
|
|
837
|
+
if (!isConnected) {
|
|
838
|
+
throw new Error("Wallet is not connected");
|
|
839
|
+
}
|
|
840
|
+
if (provider === "okx") {
|
|
841
|
+
throw new Error(
|
|
842
|
+
"Transaction signing is not supported by OKX wallet"
|
|
843
|
+
);
|
|
844
|
+
}
|
|
845
|
+
setStatus("pending");
|
|
846
|
+
setError(null);
|
|
847
|
+
setData(void 0);
|
|
848
|
+
try {
|
|
849
|
+
const result = await request("stx_signTransaction", {
|
|
850
|
+
transaction: variables.transaction,
|
|
851
|
+
...variables.broadcast !== void 0 && {
|
|
852
|
+
broadcast: variables.broadcast
|
|
853
|
+
}
|
|
854
|
+
});
|
|
855
|
+
setData(result);
|
|
856
|
+
setStatus("success");
|
|
857
|
+
return result;
|
|
858
|
+
} catch (err) {
|
|
859
|
+
const error2 = err instanceof Error ? err : new Error(String(err));
|
|
860
|
+
setError(error2);
|
|
861
|
+
setStatus("error");
|
|
862
|
+
throw error2;
|
|
863
|
+
}
|
|
864
|
+
},
|
|
865
|
+
[isConnected, provider]
|
|
866
|
+
);
|
|
867
|
+
const signTransaction = useCallback(
|
|
868
|
+
(variables, options) => {
|
|
869
|
+
signTransactionAsync(variables).then((data2) => {
|
|
870
|
+
options?.onSuccess?.(data2);
|
|
871
|
+
options?.onSettled?.(data2, null);
|
|
872
|
+
}).catch((error2) => {
|
|
873
|
+
options?.onError?.(error2);
|
|
874
|
+
options?.onSettled?.(void 0, error2);
|
|
875
|
+
});
|
|
876
|
+
},
|
|
877
|
+
[signTransactionAsync]
|
|
878
|
+
);
|
|
879
|
+
const reset = useCallback(() => {
|
|
880
|
+
setData(void 0);
|
|
881
|
+
setError(null);
|
|
882
|
+
setStatus("idle");
|
|
883
|
+
}, []);
|
|
884
|
+
return useMemo(
|
|
885
|
+
() => ({
|
|
886
|
+
signTransaction,
|
|
887
|
+
signTransactionAsync,
|
|
888
|
+
reset,
|
|
889
|
+
data,
|
|
890
|
+
error,
|
|
891
|
+
isError: status === "error",
|
|
892
|
+
isIdle: status === "idle",
|
|
893
|
+
isPending: status === "pending",
|
|
894
|
+
isSuccess: status === "success",
|
|
895
|
+
status
|
|
896
|
+
}),
|
|
897
|
+
[signTransaction, signTransactionAsync, reset, data, error, status]
|
|
898
|
+
);
|
|
899
|
+
};
|
|
753
900
|
|
|
754
901
|
// src/utils/get-network-from-address.ts
|
|
755
902
|
var getNetworkFromAddress = (address) => {
|
|
@@ -761,10 +908,239 @@ var getNetworkFromAddress = (address) => {
|
|
|
761
908
|
}
|
|
762
909
|
throw new Error(`Invalid Stacks address: ${address}`);
|
|
763
910
|
};
|
|
911
|
+
|
|
912
|
+
// src/hooks/use-transfer-stx.ts
|
|
913
|
+
var useTransferSTX = () => {
|
|
914
|
+
const { isConnected, address, provider } = useAddress();
|
|
915
|
+
const [data, setData] = useState(void 0);
|
|
916
|
+
const [error, setError] = useState(null);
|
|
917
|
+
const [status, setStatus] = useState("idle");
|
|
918
|
+
const transferSTXAsync = useCallback(
|
|
919
|
+
async (variables) => {
|
|
920
|
+
if (!isConnected || !address) {
|
|
921
|
+
throw new Error("Wallet is not connected");
|
|
922
|
+
}
|
|
923
|
+
setStatus("pending");
|
|
924
|
+
setError(null);
|
|
925
|
+
setData(void 0);
|
|
926
|
+
try {
|
|
927
|
+
if (provider === "okx") {
|
|
928
|
+
if (!window.okxwallet) {
|
|
929
|
+
throw new Error("OKX wallet not found");
|
|
930
|
+
}
|
|
931
|
+
const response2 = await window.okxwallet.stacks.signTransaction({
|
|
932
|
+
txType: "token_transfer",
|
|
933
|
+
recipient: variables.recipient,
|
|
934
|
+
amount: String(variables.amount),
|
|
935
|
+
memo: variables.memo ?? "",
|
|
936
|
+
stxAddress: address,
|
|
937
|
+
anchorMode: 3
|
|
938
|
+
});
|
|
939
|
+
setData(response2.txHash);
|
|
940
|
+
setStatus("success");
|
|
941
|
+
return response2.txHash;
|
|
942
|
+
}
|
|
943
|
+
const response = await request("stx_transferStx", {
|
|
944
|
+
recipient: variables.recipient,
|
|
945
|
+
amount: variables.amount,
|
|
946
|
+
...variables.memo !== void 0 && {
|
|
947
|
+
memo: variables.memo
|
|
948
|
+
},
|
|
949
|
+
...variables.fee !== void 0 && {
|
|
950
|
+
fee: variables.fee
|
|
951
|
+
},
|
|
952
|
+
...variables.nonce !== void 0 && {
|
|
953
|
+
nonce: variables.nonce
|
|
954
|
+
},
|
|
955
|
+
network: getNetworkFromAddress(address)
|
|
956
|
+
});
|
|
957
|
+
if (!response.txid) {
|
|
958
|
+
throw new Error("No transaction ID returned");
|
|
959
|
+
}
|
|
960
|
+
setData(response.txid);
|
|
961
|
+
setStatus("success");
|
|
962
|
+
return response.txid;
|
|
963
|
+
} catch (err) {
|
|
964
|
+
const error2 = err instanceof Error ? err : new Error(String(err));
|
|
965
|
+
setError(error2);
|
|
966
|
+
setStatus("error");
|
|
967
|
+
throw error2;
|
|
968
|
+
}
|
|
969
|
+
},
|
|
970
|
+
[isConnected, address, provider]
|
|
971
|
+
);
|
|
972
|
+
const transferSTX = useCallback(
|
|
973
|
+
(variables, options) => {
|
|
974
|
+
transferSTXAsync(variables).then((txid) => {
|
|
975
|
+
options?.onSuccess?.(txid);
|
|
976
|
+
options?.onSettled?.(txid, null);
|
|
977
|
+
}).catch((error2) => {
|
|
978
|
+
options?.onError?.(error2);
|
|
979
|
+
options?.onSettled?.(void 0, error2);
|
|
980
|
+
});
|
|
981
|
+
},
|
|
982
|
+
[transferSTXAsync]
|
|
983
|
+
);
|
|
984
|
+
const reset = useCallback(() => {
|
|
985
|
+
setData(void 0);
|
|
986
|
+
setError(null);
|
|
987
|
+
setStatus("idle");
|
|
988
|
+
}, []);
|
|
989
|
+
return useMemo(
|
|
990
|
+
() => ({
|
|
991
|
+
transferSTX,
|
|
992
|
+
transferSTXAsync,
|
|
993
|
+
reset,
|
|
994
|
+
data,
|
|
995
|
+
error,
|
|
996
|
+
isError: status === "error",
|
|
997
|
+
isIdle: status === "idle",
|
|
998
|
+
isPending: status === "pending",
|
|
999
|
+
isSuccess: status === "success",
|
|
1000
|
+
status
|
|
1001
|
+
}),
|
|
1002
|
+
[transferSTX, transferSTXAsync, reset, data, error, status]
|
|
1003
|
+
);
|
|
1004
|
+
};
|
|
1005
|
+
function toClarityValue(value, abiType) {
|
|
1006
|
+
if (typeof abiType === "string") {
|
|
1007
|
+
switch (abiType) {
|
|
1008
|
+
case "uint128": {
|
|
1009
|
+
if (typeof value !== "bigint") {
|
|
1010
|
+
throw new Error(
|
|
1011
|
+
`@satoshai/kit: Expected bigint (uint128), got ${typeof value}`
|
|
1012
|
+
);
|
|
1013
|
+
}
|
|
1014
|
+
return uintCV(value);
|
|
1015
|
+
}
|
|
1016
|
+
case "int128": {
|
|
1017
|
+
if (typeof value !== "bigint") {
|
|
1018
|
+
throw new Error(
|
|
1019
|
+
`@satoshai/kit: Expected bigint (int128), got ${typeof value}`
|
|
1020
|
+
);
|
|
1021
|
+
}
|
|
1022
|
+
return intCV(value);
|
|
1023
|
+
}
|
|
1024
|
+
case "bool": {
|
|
1025
|
+
if (typeof value !== "boolean") {
|
|
1026
|
+
throw new Error(
|
|
1027
|
+
`@satoshai/kit: Expected boolean (bool), got ${typeof value}`
|
|
1028
|
+
);
|
|
1029
|
+
}
|
|
1030
|
+
return boolCV(value);
|
|
1031
|
+
}
|
|
1032
|
+
case "principal":
|
|
1033
|
+
case "trait_reference": {
|
|
1034
|
+
if (typeof value !== "string") {
|
|
1035
|
+
throw new Error(
|
|
1036
|
+
`@satoshai/kit: Expected string (${abiType}), got ${typeof value}`
|
|
1037
|
+
);
|
|
1038
|
+
}
|
|
1039
|
+
if (value.includes(".")) {
|
|
1040
|
+
const [addr, ...rest] = value.split(".");
|
|
1041
|
+
return contractPrincipalCV(addr, rest.join("."));
|
|
1042
|
+
}
|
|
1043
|
+
return standardPrincipalCV(value);
|
|
1044
|
+
}
|
|
1045
|
+
case "none":
|
|
1046
|
+
return noneCV();
|
|
1047
|
+
default:
|
|
1048
|
+
throw new Error(
|
|
1049
|
+
`@satoshai/kit: Unsupported ABI type "${abiType}"`
|
|
1050
|
+
);
|
|
1051
|
+
}
|
|
1052
|
+
}
|
|
1053
|
+
if ("buffer" in abiType) {
|
|
1054
|
+
if (!(value instanceof Uint8Array)) {
|
|
1055
|
+
throw new Error(
|
|
1056
|
+
`@satoshai/kit: Expected Uint8Array (buffer), got ${typeof value}`
|
|
1057
|
+
);
|
|
1058
|
+
}
|
|
1059
|
+
return bufferCV(value);
|
|
1060
|
+
}
|
|
1061
|
+
if ("string-ascii" in abiType) {
|
|
1062
|
+
if (typeof value !== "string") {
|
|
1063
|
+
throw new Error(
|
|
1064
|
+
`@satoshai/kit: Expected string (string-ascii), got ${typeof value}`
|
|
1065
|
+
);
|
|
1066
|
+
}
|
|
1067
|
+
return stringAsciiCV(value);
|
|
1068
|
+
}
|
|
1069
|
+
if ("string-utf8" in abiType) {
|
|
1070
|
+
if (typeof value !== "string") {
|
|
1071
|
+
throw new Error(
|
|
1072
|
+
`@satoshai/kit: Expected string (string-utf8), got ${typeof value}`
|
|
1073
|
+
);
|
|
1074
|
+
}
|
|
1075
|
+
return stringUtf8CV(value);
|
|
1076
|
+
}
|
|
1077
|
+
if ("optional" in abiType) {
|
|
1078
|
+
if (value === null || value === void 0) return noneCV();
|
|
1079
|
+
return someCV(toClarityValue(value, abiType.optional));
|
|
1080
|
+
}
|
|
1081
|
+
if ("list" in abiType) {
|
|
1082
|
+
if (!Array.isArray(value)) {
|
|
1083
|
+
throw new Error(
|
|
1084
|
+
`@satoshai/kit: Expected array (list), got ${typeof value}`
|
|
1085
|
+
);
|
|
1086
|
+
}
|
|
1087
|
+
const listType = abiType.list;
|
|
1088
|
+
return listCV(
|
|
1089
|
+
value.map((item) => toClarityValue(item, listType.type))
|
|
1090
|
+
);
|
|
1091
|
+
}
|
|
1092
|
+
if ("tuple" in abiType) {
|
|
1093
|
+
if (typeof value !== "object" || value === null) {
|
|
1094
|
+
throw new Error(
|
|
1095
|
+
`@satoshai/kit: Expected object (tuple), got ${value === null ? "null" : typeof value}`
|
|
1096
|
+
);
|
|
1097
|
+
}
|
|
1098
|
+
const entries = abiType.tuple;
|
|
1099
|
+
const obj = value;
|
|
1100
|
+
const result = {};
|
|
1101
|
+
for (const entry of entries) {
|
|
1102
|
+
result[entry.name] = toClarityValue(obj[entry.name], entry.type);
|
|
1103
|
+
}
|
|
1104
|
+
return tupleCV(result);
|
|
1105
|
+
}
|
|
1106
|
+
throw new Error(
|
|
1107
|
+
`@satoshai/kit: Unsupported ABI type: ${JSON.stringify(abiType)}`
|
|
1108
|
+
);
|
|
1109
|
+
}
|
|
1110
|
+
function namedArgsToClarityValues(args, abiArgs) {
|
|
1111
|
+
return abiArgs.map((abiArg) => {
|
|
1112
|
+
if (!(abiArg.name in args)) {
|
|
1113
|
+
throw new Error(
|
|
1114
|
+
`@satoshai/kit: Missing argument "${abiArg.name}"`
|
|
1115
|
+
);
|
|
1116
|
+
}
|
|
1117
|
+
return toClarityValue(
|
|
1118
|
+
args[abiArg.name],
|
|
1119
|
+
abiArg.type
|
|
1120
|
+
);
|
|
1121
|
+
});
|
|
1122
|
+
}
|
|
764
1123
|
var preparePostConditionsForOKX = (postConditions) => postConditions.map((pc) => postConditionToHex(pc));
|
|
765
1124
|
var prepareArgsForOKX = (args) => args.map((arg) => cvToHex(arg));
|
|
766
1125
|
|
|
767
1126
|
// src/hooks/use-write-contract/use-write-contract.ts
|
|
1127
|
+
function resolveArgs(variables) {
|
|
1128
|
+
if (!variables.abi) {
|
|
1129
|
+
return variables.args;
|
|
1130
|
+
}
|
|
1131
|
+
const fn = variables.abi.functions.find(
|
|
1132
|
+
(f) => f.name === variables.functionName && f.access === "public"
|
|
1133
|
+
);
|
|
1134
|
+
if (!fn) {
|
|
1135
|
+
throw new Error(
|
|
1136
|
+
`@satoshai/kit: Public function "${variables.functionName}" not found in ABI`
|
|
1137
|
+
);
|
|
1138
|
+
}
|
|
1139
|
+
return namedArgsToClarityValues(
|
|
1140
|
+
variables.args,
|
|
1141
|
+
fn.args
|
|
1142
|
+
);
|
|
1143
|
+
}
|
|
768
1144
|
var useWriteContract = () => {
|
|
769
1145
|
const { isConnected, address, provider } = useAddress();
|
|
770
1146
|
const [data, setData] = useState(void 0);
|
|
@@ -778,6 +1154,7 @@ var useWriteContract = () => {
|
|
|
778
1154
|
setStatus("pending");
|
|
779
1155
|
setError(null);
|
|
780
1156
|
setData(void 0);
|
|
1157
|
+
const resolvedArgs = resolveArgs(variables);
|
|
781
1158
|
try {
|
|
782
1159
|
if (provider === "okx") {
|
|
783
1160
|
if (!window.okxwallet) {
|
|
@@ -787,7 +1164,7 @@ var useWriteContract = () => {
|
|
|
787
1164
|
contractAddress: variables.address,
|
|
788
1165
|
contractName: variables.contract,
|
|
789
1166
|
functionName: variables.functionName,
|
|
790
|
-
functionArgs: prepareArgsForOKX(
|
|
1167
|
+
functionArgs: prepareArgsForOKX(resolvedArgs),
|
|
791
1168
|
postConditions: preparePostConditionsForOKX(
|
|
792
1169
|
variables.pc.postConditions
|
|
793
1170
|
),
|
|
@@ -804,7 +1181,7 @@ var useWriteContract = () => {
|
|
|
804
1181
|
address,
|
|
805
1182
|
contract: `${variables.address}.${variables.contract}`,
|
|
806
1183
|
functionName: variables.functionName,
|
|
807
|
-
functionArgs:
|
|
1184
|
+
functionArgs: resolvedArgs,
|
|
808
1185
|
postConditions: variables.pc.postConditions,
|
|
809
1186
|
postConditionMode: variables.pc.mode === PostConditionMode.Allow ? "allow" : "deny",
|
|
810
1187
|
network: getNetworkFromAddress(address)
|
|
@@ -896,6 +1273,11 @@ var useWallets = () => {
|
|
|
896
1273
|
return useMemo(() => ({ wallets }), [wallets]);
|
|
897
1274
|
};
|
|
898
1275
|
|
|
899
|
-
|
|
1276
|
+
// src/utils/create-contract-config.ts
|
|
1277
|
+
function createContractConfig(config) {
|
|
1278
|
+
return config;
|
|
1279
|
+
}
|
|
1280
|
+
|
|
1281
|
+
export { SUPPORTED_STACKS_WALLETS, StacksWalletProvider, createContractConfig, getLocalStorageWallet, getNetworkFromAddress, getStacksWallets, useAddress, useBnsName, useConnect, useDisconnect, useSignMessage, useSignStructuredMessage, useSignTransaction, useTransferSTX, useWallets, useWriteContract };
|
|
900
1282
|
//# sourceMappingURL=index.js.map
|
|
901
1283
|
//# sourceMappingURL=index.js.map
|