@kontor/kontor-sdk 1.0.0-alpha.27 → 1.0.0-alpha.29

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.
Files changed (191) hide show
  1. package/dist/cjs/exports/index.js.map +1 -1
  2. package/dist/cjs/sdk/accounts/hd-key-to-account.js +1 -0
  3. package/dist/cjs/sdk/accounts/hd-key-to-account.js.map +1 -1
  4. package/dist/cjs/sdk/accounts/mnemonic-to-account.js +2 -4
  5. package/dist/cjs/sdk/accounts/mnemonic-to-account.js.map +1 -1
  6. package/dist/cjs/sdk/accounts/private-key-to-account.js +3 -8
  7. package/dist/cjs/sdk/accounts/private-key-to-account.js.map +1 -1
  8. package/dist/cjs/sdk/accounts/to-local-account.js.map +1 -1
  9. package/dist/cjs/sdk/actions/get-contract.js +36 -16
  10. package/dist/cjs/sdk/actions/get-contract.js.map +1 -1
  11. package/dist/cjs/sdk/actions/kontor/public/build-compose-query.js +9 -12
  12. package/dist/cjs/sdk/actions/kontor/public/build-compose-query.js.map +1 -1
  13. package/dist/cjs/sdk/actions/kontor/public/build-instruction-query.js +13 -33
  14. package/dist/cjs/sdk/actions/kontor/public/build-instruction-query.js.map +1 -1
  15. package/dist/cjs/sdk/actions/kontor/public/call-compose.js +6 -4
  16. package/dist/cjs/sdk/actions/kontor/public/call-compose.js.map +1 -1
  17. package/dist/cjs/sdk/actions/kontor/public/call-view.js.map +1 -1
  18. package/dist/cjs/sdk/actions/kontor/public/inspect.js +1 -2
  19. package/dist/cjs/sdk/actions/kontor/public/inspect.js.map +1 -1
  20. package/dist/cjs/sdk/actions/kontor/public/proc-contract.js +5 -5
  21. package/dist/cjs/sdk/actions/kontor/public/proc-contract.js.map +1 -1
  22. package/dist/cjs/sdk/actions/kontor/public/view-contract.js +2 -1
  23. package/dist/cjs/sdk/actions/kontor/public/view-contract.js.map +1 -1
  24. package/dist/cjs/sdk/chains/definitions/signet.js +0 -1
  25. package/dist/cjs/sdk/chains/definitions/signet.js.map +1 -1
  26. package/dist/cjs/sdk/clients/decorators/kontor/public.js.map +1 -1
  27. package/dist/cjs/sdk/errors/chain.js +44 -0
  28. package/dist/cjs/sdk/errors/chain.js.map +1 -0
  29. package/dist/cjs/sdk/utils/chain/assert-current-chain.js +11 -0
  30. package/dist/cjs/sdk/utils/chain/assert-current-chain.js.map +1 -0
  31. package/dist/cjs/sdk/utils/kontor/build-compose-query.js +19 -0
  32. package/dist/cjs/sdk/utils/kontor/build-compose-query.js.map +1 -0
  33. package/dist/cjs/sdk/utils/kontor/build-instruction-query.js +36 -0
  34. package/dist/cjs/sdk/utils/kontor/build-instruction-query.js.map +1 -0
  35. package/dist/cjs/sdk/utils/wit/codecs/bool.js +29 -0
  36. package/dist/cjs/sdk/utils/wit/codecs/bool.js.map +1 -0
  37. package/dist/cjs/sdk/utils/wit/codecs/s64.js +57 -0
  38. package/dist/cjs/sdk/utils/wit/codecs/s64.js.map +1 -0
  39. package/dist/cjs/sdk/utils/wit/decode-wit-parameter.js +134 -131
  40. package/dist/cjs/sdk/utils/wit/decode-wit-parameter.js.map +1 -1
  41. package/dist/cjs/sdk/utils/wit/encode-wit-parameters.js +74 -26
  42. package/dist/cjs/sdk/utils/wit/encode-wit-parameters.js.map +1 -1
  43. package/dist/cjs/tsconfig.build.tsbuildinfo +1 -1
  44. package/dist/cjs/wit/wit-parser/core/errors/{splitParameters.js → split-parameters.js} +1 -1
  45. package/dist/cjs/wit/wit-parser/core/errors/split-parameters.js.map +1 -0
  46. package/dist/cjs/wit/wit-parser/core/utils.js +3 -3
  47. package/dist/cjs/wit/wit-parser/core/utils.js.map +1 -1
  48. package/dist/esm/exports/index.js +1 -1
  49. package/dist/esm/exports/index.js.map +1 -1
  50. package/dist/esm/sdk/accounts/hd-key-to-account.js +2 -5
  51. package/dist/esm/sdk/accounts/hd-key-to-account.js.map +1 -1
  52. package/dist/esm/sdk/accounts/mnemonic-to-account.js +2 -10
  53. package/dist/esm/sdk/accounts/mnemonic-to-account.js.map +1 -1
  54. package/dist/esm/sdk/accounts/private-key-to-account.js +3 -15
  55. package/dist/esm/sdk/accounts/private-key-to-account.js.map +1 -1
  56. package/dist/esm/sdk/accounts/to-local-account.js +0 -5
  57. package/dist/esm/sdk/accounts/to-local-account.js.map +1 -1
  58. package/dist/esm/sdk/actions/get-contract.js +37 -35
  59. package/dist/esm/sdk/actions/get-contract.js.map +1 -1
  60. package/dist/esm/sdk/actions/kontor/public/build-compose-query.js +9 -13
  61. package/dist/esm/sdk/actions/kontor/public/build-compose-query.js.map +1 -1
  62. package/dist/esm/sdk/actions/kontor/public/build-instruction-query.js +13 -36
  63. package/dist/esm/sdk/actions/kontor/public/build-instruction-query.js.map +1 -1
  64. package/dist/esm/sdk/actions/kontor/public/call-compose.js +6 -4
  65. package/dist/esm/sdk/actions/kontor/public/call-compose.js.map +1 -1
  66. package/dist/esm/sdk/actions/kontor/public/call-view.js +1 -0
  67. package/dist/esm/sdk/actions/kontor/public/call-view.js.map +1 -1
  68. package/dist/esm/sdk/actions/kontor/public/inspect.js +9 -4
  69. package/dist/esm/sdk/actions/kontor/public/inspect.js.map +1 -1
  70. package/dist/esm/sdk/actions/kontor/public/proc-contract.js +5 -6
  71. package/dist/esm/sdk/actions/kontor/public/proc-contract.js.map +1 -1
  72. package/dist/esm/sdk/actions/kontor/public/view-contract.js +3 -5
  73. package/dist/esm/sdk/actions/kontor/public/view-contract.js.map +1 -1
  74. package/dist/esm/sdk/chains/definitions/signet.js +0 -1
  75. package/dist/esm/sdk/chains/definitions/signet.js.map +1 -1
  76. package/dist/esm/sdk/clients/decorators/kontor/public.js.map +1 -1
  77. package/dist/esm/sdk/errors/chain.js +37 -0
  78. package/dist/esm/sdk/errors/chain.js.map +1 -0
  79. package/dist/esm/sdk/utils/chain/assert-current-chain.js +8 -0
  80. package/dist/esm/sdk/utils/chain/assert-current-chain.js.map +1 -0
  81. package/dist/esm/sdk/utils/kontor/build-compose-query.js +16 -0
  82. package/dist/esm/sdk/utils/kontor/build-compose-query.js.map +1 -0
  83. package/dist/esm/sdk/utils/kontor/build-instruction-query.js +36 -0
  84. package/dist/esm/sdk/utils/kontor/build-instruction-query.js.map +1 -0
  85. package/dist/esm/sdk/utils/wit/codecs/bool.js +26 -0
  86. package/dist/esm/sdk/utils/wit/codecs/bool.js.map +1 -0
  87. package/dist/esm/sdk/utils/wit/codecs/s64.js +65 -0
  88. package/dist/esm/sdk/utils/wit/codecs/s64.js.map +1 -0
  89. package/dist/esm/sdk/utils/wit/decode-wit-parameter.js +155 -147
  90. package/dist/esm/sdk/utils/wit/decode-wit-parameter.js.map +1 -1
  91. package/dist/esm/sdk/utils/wit/encode-wit-parameters.js +74 -72
  92. package/dist/esm/sdk/utils/wit/encode-wit-parameters.js.map +1 -1
  93. package/dist/esm/tsconfig.build.tsbuildinfo +1 -1
  94. package/dist/esm/wit/wit-parser/core/errors/{splitParameters.js → split-parameters.js} +1 -1
  95. package/dist/esm/wit/wit-parser/core/errors/split-parameters.js.map +1 -0
  96. package/dist/esm/wit/wit-parser/core/utils.js +1 -1
  97. package/dist/esm/wit/wit-parser/core/utils.js.map +1 -1
  98. package/dist/types/exports/index.d.ts +1 -1
  99. package/dist/types/exports/index.d.ts.map +1 -1
  100. package/dist/types/sdk/accounts/hd-key-to-account.d.ts +0 -5
  101. package/dist/types/sdk/accounts/hd-key-to-account.d.ts.map +1 -1
  102. package/dist/types/sdk/accounts/mnemonic-to-account.d.ts +1 -6
  103. package/dist/types/sdk/accounts/mnemonic-to-account.d.ts.map +1 -1
  104. package/dist/types/sdk/accounts/private-key-to-account.d.ts +5 -3
  105. package/dist/types/sdk/accounts/private-key-to-account.d.ts.map +1 -1
  106. package/dist/types/sdk/accounts/to-local-account.d.ts +0 -5
  107. package/dist/types/sdk/accounts/to-local-account.d.ts.map +1 -1
  108. package/dist/types/sdk/actions/get-contract.d.ts +28 -5
  109. package/dist/types/sdk/actions/get-contract.d.ts.map +1 -1
  110. package/dist/types/sdk/actions/kontor/public/build-compose-query.d.ts +5 -5
  111. package/dist/types/sdk/actions/kontor/public/build-compose-query.d.ts.map +1 -1
  112. package/dist/types/sdk/actions/kontor/public/build-instruction-query.d.ts +6 -6
  113. package/dist/types/sdk/actions/kontor/public/build-instruction-query.d.ts.map +1 -1
  114. package/dist/types/sdk/actions/kontor/public/call-compose.d.ts +1 -1
  115. package/dist/types/sdk/actions/kontor/public/call-compose.d.ts.map +1 -1
  116. package/dist/types/sdk/actions/kontor/public/call-view.d.ts +3 -3
  117. package/dist/types/sdk/actions/kontor/public/call-view.d.ts.map +1 -1
  118. package/dist/types/sdk/actions/kontor/public/inspect.d.ts +3 -3
  119. package/dist/types/sdk/actions/kontor/public/inspect.d.ts.map +1 -1
  120. package/dist/types/sdk/actions/kontor/public/proc-contract.d.ts +3 -3
  121. package/dist/types/sdk/actions/kontor/public/proc-contract.d.ts.map +1 -1
  122. package/dist/types/sdk/actions/kontor/public/view-contract.d.ts +3 -3
  123. package/dist/types/sdk/actions/kontor/public/view-contract.d.ts.map +1 -1
  124. package/dist/types/sdk/chains/definitions/signet.d.ts +0 -1
  125. package/dist/types/sdk/chains/definitions/signet.d.ts.map +1 -1
  126. package/dist/types/sdk/clients/decorators/kontor/public.d.ts +6 -6
  127. package/dist/types/sdk/clients/decorators/kontor/public.d.ts.map +1 -1
  128. package/dist/types/sdk/errors/chain.d.ts +32 -0
  129. package/dist/types/sdk/errors/chain.d.ts.map +1 -0
  130. package/dist/types/sdk/types/chain.d.ts +0 -2
  131. package/dist/types/sdk/types/chain.d.ts.map +1 -1
  132. package/dist/types/sdk/types/contract.d.ts.map +1 -1
  133. package/dist/types/sdk/utils/chain/assert-current-chain.d.ts +10 -0
  134. package/dist/types/sdk/utils/chain/assert-current-chain.d.ts.map +1 -0
  135. package/dist/types/sdk/utils/kontor/build-compose-query.d.ts +13 -0
  136. package/dist/types/sdk/utils/kontor/build-compose-query.d.ts.map +1 -0
  137. package/dist/types/sdk/utils/kontor/build-instruction-query.d.ts +22 -0
  138. package/dist/types/sdk/utils/kontor/build-instruction-query.d.ts.map +1 -0
  139. package/dist/types/sdk/utils/wit/codecs/bool.d.ts +3 -0
  140. package/dist/types/sdk/utils/wit/codecs/bool.d.ts.map +1 -0
  141. package/dist/types/sdk/utils/wit/codecs/s64.d.ts +23 -0
  142. package/dist/types/sdk/utils/wit/codecs/s64.d.ts.map +1 -0
  143. package/dist/types/sdk/utils/wit/decode-wit-parameter.d.ts +12 -3
  144. package/dist/types/sdk/utils/wit/decode-wit-parameter.d.ts.map +1 -1
  145. package/dist/types/sdk/utils/wit/encode-wit-parameters.d.ts.map +1 -1
  146. package/dist/types/wit/type-utils.d.ts.map +1 -1
  147. package/dist/types/wit/utils.d.ts.map +1 -1
  148. package/dist/types/wit/wit-parser/core/errors/{splitParameters.d.ts → split-parameters.d.ts} +1 -1
  149. package/dist/types/wit/wit-parser/core/errors/split-parameters.d.ts.map +1 -0
  150. package/package.json +1 -1
  151. package/src/exports/index.ts +1 -1
  152. package/src/sdk/accounts/hd-key-to-account.ts +2 -5
  153. package/src/sdk/accounts/mnemonic-to-account.test.ts +7 -81
  154. package/src/sdk/accounts/mnemonic-to-account.ts +2 -11
  155. package/src/sdk/accounts/private-key-to-account.ts +10 -16
  156. package/src/sdk/accounts/to-local-account.ts +0 -5
  157. package/src/sdk/actions/get-contract.test-d.ts +10 -9
  158. package/src/sdk/actions/get-contract.test.ts +49 -0
  159. package/src/sdk/actions/get-contract.ts +312 -91
  160. package/src/sdk/actions/kontor/public/build-compose-query.ts +23 -25
  161. package/src/sdk/actions/kontor/public/build-instruction-query.ts +27 -53
  162. package/src/sdk/actions/kontor/public/call-compose.ts +8 -4
  163. package/src/sdk/actions/kontor/public/call-view.ts +5 -4
  164. package/src/sdk/actions/kontor/public/inspect.ts +20 -9
  165. package/src/sdk/actions/kontor/public/proc-contract.test.ts +1 -2
  166. package/src/sdk/actions/kontor/public/proc-contract.ts +21 -55
  167. package/src/sdk/actions/kontor/public/{view-contract.test-d.ts → view-contract.test.ts} +2 -2
  168. package/src/sdk/actions/kontor/public/view-contract.ts +10 -9
  169. package/src/sdk/chains/definitions/signet.ts +0 -1
  170. package/src/sdk/clients/base/create-rpc-client.test.ts +0 -1
  171. package/src/sdk/clients/create-public-client.test.ts +0 -1
  172. package/src/sdk/clients/decorators/kontor/public.ts +21 -12
  173. package/src/sdk/clients/kontor/create-public-client.test.ts +0 -7
  174. package/src/sdk/errors/chain.ts +64 -0
  175. package/src/sdk/test/e2e.test.ts +25 -1
  176. package/src/sdk/types/chain.ts +0 -2
  177. package/src/sdk/types/contract.ts +3 -14
  178. package/src/sdk/utils/chain/assert-current-chain.ts +28 -0
  179. package/src/sdk/utils/kontor/build-compose-query.ts +33 -0
  180. package/src/sdk/utils/kontor/build-instruction-query.ts +75 -0
  181. package/src/sdk/utils/wit/codecs/bool.ts +29 -0
  182. package/src/sdk/utils/wit/codecs/s64.ts +76 -0
  183. package/src/sdk/utils/wit/decode-wit-parameter.ts +167 -164
  184. package/src/sdk/utils/wit/encode-wit-parameters.ts +78 -75
  185. package/src/wit/type-utils.ts +0 -2
  186. package/src/wit/utils.ts +3 -6
  187. package/src/wit/wit-parser/core/utils.ts +1 -1
  188. package/dist/cjs/wit/wit-parser/core/errors/splitParameters.js.map +0 -1
  189. package/dist/esm/wit/wit-parser/core/errors/splitParameters.js.map +0 -1
  190. package/dist/types/wit/wit-parser/core/errors/splitParameters.d.ts.map +0 -1
  191. /package/src/wit/wit-parser/core/errors/{splitParameters.ts → split-parameters.ts} +0 -0
@@ -15,12 +15,23 @@ import type {
15
15
  ContractFunctionName,
16
16
  } from "../types/contract.js";
17
17
 
18
- // import { getAction } from "../utils/getAction.js";
18
+ import { getKontorAction } from "../utils/get-action.js";
19
19
  import {
20
+ viewContract,
20
21
  // viewContract,
21
22
  type ViewContractParameters,
22
23
  type ViewContractReturnType,
23
24
  } from "./kontor/public/view-contract.js";
25
+ import type { HttpTransport } from "../clients/transports/create-http-transport.js";
26
+ import type { Chain } from "../types/chain.js";
27
+ import type { Account } from "../accounts/types.js";
28
+ import type { KontorPublicClient } from "../clients/kontor/create-public-client.js";
29
+ import type { ResolvedRegister } from "../../wit/register.js";
30
+ import {
31
+ procContract,
32
+ type ProcContractParameters,
33
+ type ProcContractReturnType,
34
+ } from "./kontor/public/proc-contract.js";
24
35
 
25
36
  // TODO: move to utils
26
37
  function camelToKebab(str: string): string {
@@ -32,45 +43,29 @@ export type KebabToCamel<S extends string> =
32
43
  ? `${Head}${Capitalize<KebabToCamel<Tail>>}`
33
44
  : S;
34
45
 
35
- export type GetContractParameters<wit extends Wit | readonly unknown[] = Wit> =
36
- {
37
- wit: wit;
38
- };
39
-
40
- export type GetViewFunction<
41
- narrowable extends boolean,
42
- wit extends Wit | readonly unknown[],
43
- functionName extends ContractFunctionName<wit, "view">,
44
- args extends ContractFunctionArgs<
45
- wit,
46
- "view",
47
- functionName
48
- > = ContractFunctionArgs<wit, "view", functionName>,
49
- witFunction extends WitFunction = wit extends Wit
50
- ? ExtractWitFunction<wit, functionName>
51
- : WitFunction,
52
- //
53
- _args = WitParametersToPrimitiveTypes<witFunction["inputs"]>,
54
- _options = Prettify<
55
- UnionOmit<
56
- ViewContractParameters<wit, functionName, args>,
57
- "contractAddress" | "args" | "functionName"
58
- >
59
- >,
60
- > = narrowable extends true
61
- ? (
62
- ...parameters: _args extends readonly []
63
- ? [options?: _options]
64
- : [args: _args, options?: _options]
65
- ) => Promise<ViewContractReturnType<wit, functionName, args>>
66
- : (
67
- ...parameters:
68
- | [options?: _options]
69
- | [args: readonly unknown[], options?: _options]
70
- ) => Promise<ViewContractReturnType>;
46
+ export type GetContractParameters<
47
+ transport extends HttpTransport = HttpTransport,
48
+ chain extends Chain | undefined = Chain | undefined,
49
+ account extends Account | undefined = Account | undefined,
50
+ wit extends Wit | readonly unknown[] = Wit,
51
+ client extends KontorPublicClient<
52
+ transport,
53
+ chain,
54
+ account
55
+ > = KontorPublicClient<transport, chain, account>,
56
+ contractAddress extends
57
+ ResolvedRegister["contractAddress"] = ResolvedRegister["contractAddress"],
58
+ > = {
59
+ wit: wit;
60
+ contractAddress: contractAddress;
61
+ client: client;
62
+ };
71
63
 
72
64
  export type GetContractReturnType<
73
65
  wit extends Wit | readonly unknown[] = Wit,
66
+ client extends KontorPublicClient = KontorPublicClient,
67
+ contractAddress extends
68
+ ResolvedRegister["contractAddress"] = ResolvedRegister["contractAddress"],
74
69
  _viewFunctionNames extends string = wit extends Wit
75
70
  ? Wit extends wit
76
71
  ? string
@@ -85,74 +80,300 @@ export type GetContractReturnType<
85
80
  > = {
86
81
  view: {
87
82
  // K is the *original* wit function name ("cached-values")
88
- [K in _viewFunctionNames as KebabToCamel<K>]: GetViewFunction<
83
+ [functionName in _viewFunctionNames as KebabToCamel<functionName>]: GetViewFunction<
89
84
  _narrowable,
85
+ client["chain"],
90
86
  wit,
91
87
  // and we pass K directly into GetViewFunction
92
- K extends ContractFunctionName<wit, "view"> ? K : never
88
+ functionName extends ContractFunctionName<wit, "view">
89
+ ? functionName
90
+ : never
93
91
  >;
94
92
  };
93
+ proc: {
94
+ // K is the *original* wit function name ("cached-values")
95
+ [functionName in _procFunctionNames as KebabToCamel<functionName>]: GetProcFunction<
96
+ _narrowable,
97
+ client["chain"],
98
+ client["account"],
99
+ wit,
100
+ // and we pass K directly into GetProcFunction
101
+ functionName extends ContractFunctionName<wit, "proc">
102
+ ? functionName
103
+ : never
104
+ >;
105
+ };
106
+ contractAddress: contractAddress;
107
+ wit: wit;
95
108
  };
96
109
 
97
- export function getContract<const wit extends Wit | readonly unknown[]>({
98
- wit: wit,
99
- }: GetContractParameters<wit>): GetContractReturnType<wit> {
100
- const hasPublicClient = true;
110
+ type ViewOptions<
111
+ wit extends Wit | readonly unknown[],
112
+ functionName extends ContractFunctionName<wit, "view">,
113
+ args extends ContractFunctionArgs<wit, "view", functionName>,
114
+ chain extends Chain | undefined,
115
+ chainOverride extends Chain | undefined,
116
+ > = Prettify<
117
+ UnionOmit<
118
+ ViewContractParameters<wit, functionName, args, chain, chainOverride>,
119
+ "wit" | "contractAddress" | "args" | "functionName"
120
+ >
121
+ >;
101
122
 
123
+ type OptTuple<chain extends Chain | undefined, O> = chain extends Chain
124
+ ? [options?: O]
125
+ : [options: O];
126
+
127
+ export type GetViewFunction<
128
+ narrowable extends boolean,
129
+ chain extends Chain | undefined,
130
+ wit extends Wit | readonly unknown[],
131
+ functionName extends ContractFunctionName<wit, "view">,
132
+ args extends ContractFunctionArgs<
133
+ wit,
134
+ "view",
135
+ functionName
136
+ > = ContractFunctionArgs<wit, "view", functionName>,
137
+ witFunction extends WitFunction = wit extends Wit
138
+ ? ExtractWitFunction<wit, functionName>
139
+ : WitFunction,
140
+ _args = WitParametersToPrimitiveTypes<witFunction["inputs"]>,
141
+ > = narrowable extends true
142
+ ? <chainOverride extends Chain | undefined = undefined>(
143
+ ...parameters: _args extends readonly []
144
+ ? OptTuple<
145
+ chain,
146
+ ViewOptions<wit, functionName, args, chain, chainOverride>
147
+ >
148
+ : [
149
+ args: _args,
150
+ ...options: OptTuple<
151
+ chain,
152
+ ViewOptions<wit, functionName, args, chain, chainOverride>
153
+ >,
154
+ ]
155
+ ) => Promise<ViewContractReturnType<wit, functionName, args>>
156
+ : <chainOverride extends Chain | undefined = undefined>(
157
+ ...parameters:
158
+ | OptTuple<
159
+ chain,
160
+ ViewOptions<wit, functionName, args, chain, chainOverride>
161
+ >
162
+ | [
163
+ args: readonly unknown[],
164
+ ...options: OptTuple<
165
+ chain,
166
+ ViewOptions<wit, functionName, args, chain, chainOverride>
167
+ >,
168
+ ]
169
+ ) => Promise<ViewContractReturnType>;
170
+
171
+ // export type GetViewFunction<
172
+ // narrowable extends boolean,
173
+ // chain extends Chain | undefined,
174
+ // wit extends Wit | readonly unknown[],
175
+ // functionName extends ContractFunctionName<wit, "view">,
176
+ // args extends ContractFunctionArgs<
177
+ // wit,
178
+ // "view",
179
+ // functionName
180
+ // > = ContractFunctionArgs<wit, "view", functionName>,
181
+ // witFunction extends WitFunction = wit extends Wit
182
+ // ? ExtractWitFunction<wit, functionName>
183
+ // : WitFunction,
184
+ // //
185
+ // _args = WitParametersToPrimitiveTypes<witFunction["inputs"]>,
186
+ // > = narrowable extends true
187
+ // ? <
188
+ // chainOverride extends Chain | undefined,
189
+ // options extends Prettify<
190
+ // UnionOmit<
191
+ // ViewContractParameters<wit, functionName, args, chain, chainOverride>,
192
+ // "wit" | "contractAddress" | "args" | "functionName"
193
+ // >
194
+ // >,
195
+ // >(
196
+ // ...parameters: _args extends readonly []
197
+ // ? chain extends Chain
198
+ // ? [options?: options]
199
+ // : [options: options]
200
+ // : [args: _args, options?: options]
201
+ // ) => Promise<ViewContractReturnType<wit, functionName, args>>
202
+ // : <
203
+ // chainOverride extends Chain | undefined,
204
+ // options extends Prettify<
205
+ // UnionOmit<
206
+ // ViewContractParameters<wit, functionName, args, chain, chainOverride>,
207
+ // "wit" | "contractAddress" | "args" | "functionName"
208
+ // >
209
+ // >,
210
+ // Rest extends unknown[] = [options: options],
211
+ // >(
212
+ // ...parameters: chain extends Chain
213
+ // ? [options?: options] | [args: readonly unknown[], ...parameters: Rest]
214
+ // : [options: options] | [args: readonly unknown[], ...parameters: Rest]
215
+ // ) => Promise<ViewContractReturnType>;
216
+
217
+ export type GetProcFunction<
218
+ narrowable extends boolean,
219
+ chain extends Chain | undefined,
220
+ account extends Account | undefined,
221
+ wit extends Wit | readonly unknown[],
222
+ functionName extends ContractFunctionName<wit, "proc">,
223
+ args extends ContractFunctionArgs<
224
+ wit,
225
+ "proc",
226
+ functionName
227
+ > = ContractFunctionArgs<wit, "proc", functionName>,
228
+ witFunction extends WitFunction = wit extends Wit
229
+ ? ExtractWitFunction<wit, functionName>
230
+ : WitFunction,
231
+ //
232
+ _args = WitParametersToPrimitiveTypes<witFunction["inputs"]>,
233
+ > = narrowable extends true
234
+ ? <
235
+ chainOverride extends Chain | undefined,
236
+ options extends Prettify<
237
+ UnionOmit<
238
+ ProcContractParameters<
239
+ wit,
240
+ functionName,
241
+ args,
242
+ chain,
243
+ account,
244
+ chainOverride
245
+ >,
246
+ "wit" | "contractAddress" | "args" | "functionName"
247
+ >
248
+ >,
249
+ >(
250
+ ...parameters: _args extends readonly []
251
+ ? [options: options]
252
+ : [args: _args, options: options]
253
+ ) => Promise<ProcContractReturnType>
254
+ : <
255
+ chainOverride extends Chain | undefined,
256
+ options extends Prettify<
257
+ UnionOmit<
258
+ ProcContractParameters<
259
+ wit,
260
+ functionName,
261
+ args,
262
+ chain,
263
+ account,
264
+ chainOverride
265
+ >,
266
+ "contractAddress" | "args" | "functionName"
267
+ >
268
+ >,
269
+ Rest extends unknown[] = [options: options],
270
+ >(
271
+ ...parameters:
272
+ | [options: options]
273
+ | [args: readonly unknown[], ...parameters: Rest]
274
+ ) => Promise<ProcContractReturnType>;
275
+
276
+ export function getContract<
277
+ transport extends HttpTransport,
278
+ contractAddress extends ResolvedRegister["contractAddress"],
279
+ const wit extends Wit | readonly unknown[],
280
+ const client extends KontorPublicClient<transport, chain, account>,
281
+ chain extends Chain | undefined = Chain | undefined,
282
+ account extends Account | undefined = Account | undefined,
283
+ >({
284
+ wit,
285
+ contractAddress,
286
+ client: client_,
287
+ }: GetContractParameters<
288
+ transport,
289
+ chain,
290
+ account,
291
+ wit,
292
+ client,
293
+ contractAddress
294
+ >): GetContractReturnType<wit, client, contractAddress> {
102
295
  const contract: {
103
- [_ in "wit" | "view"]?: unknown;
296
+ [_ in "wit" | "contractAddress" | "view" | "proc"]?: unknown;
104
297
  } = {};
105
298
 
106
- // TODO: actually introspect this
107
- let hasReadFunction = true;
108
-
109
- if (hasPublicClient) {
110
- if (hasReadFunction) {
111
- console.log("we are defining a proxy");
112
-
113
- contract.view = new Proxy(
114
- {},
115
- {
116
- get(_, functionName: string) {
117
- // @ts-ignore
118
- const witFunctionName = camelToKebab(functionName);
119
-
120
- return (
121
- ...parameters: [
122
- args?: readonly unknown[] | undefined,
123
- options?: UnionOmit<
124
- ViewContractParameters,
125
- "wit" | "contractAddress" | "functionName" | "args"
126
- >,
127
- ]
128
- ) => {
129
- // @ts-ignore
130
- const { args, options } = getFunctionParameters(parameters);
131
-
132
- // return getAction(
133
- // {
134
- // viewContract: () => {},
135
- // call: () => {},
136
- // },
137
- // viewContract,
138
- // "viewContract",
139
- // )({
140
- // wit,
141
- // functionName: witFunctionName,
142
- // // address,
143
- // args,
144
- // ...options,
145
- // } as ViewContractParameters);
146
- };
147
- },
148
- },
149
- );
150
- }
151
- }
299
+ console.log("we are defining a proxy");
300
+
301
+ contract.view = new Proxy(
302
+ {},
303
+ {
304
+ get(_, functionName: string) {
305
+ const witFunctionName = camelToKebab(functionName);
306
+
307
+ return (
308
+ ...parameters: [
309
+ args?: readonly unknown[] | undefined,
310
+ options?: UnionOmit<
311
+ ViewContractParameters,
312
+ "wit" | "contractAddress" | "functionName" | "args"
313
+ >,
314
+ ]
315
+ ) => {
316
+ const { args, options } = getFunctionParameters(parameters);
317
+
318
+ return getKontorAction(
319
+ client_,
320
+ viewContract,
321
+ "viewContract",
322
+ )({
323
+ wit,
324
+ functionName: witFunctionName,
325
+ contractAddress,
326
+ // address,
327
+ args,
328
+ ...options,
329
+ } as ViewContractParameters);
330
+ };
331
+ },
332
+ },
333
+ );
334
+
335
+ contract.proc = new Proxy(
336
+ {},
337
+ {
338
+ get(_, functionName: string) {
339
+ const witFunctionName = camelToKebab(functionName);
340
+
341
+ return (
342
+ ...parameters: [
343
+ args?: readonly unknown[] | undefined,
344
+ options?: UnionOmit<
345
+ ProcContractParameters,
346
+ "wit" | "contractAddress" | "functionName" | "args"
347
+ >,
348
+ ]
349
+ ) => {
350
+ const { args, options } = getFunctionParameters(parameters);
351
+
352
+ return getKontorAction(
353
+ client_,
354
+ procContract,
355
+ "procContract",
356
+ )({
357
+ wit,
358
+ functionName: witFunctionName,
359
+ contractAddress,
360
+ args,
361
+
362
+ ...options,
363
+ } as any);
364
+ };
365
+ },
366
+ },
367
+ );
152
368
 
153
369
  contract.wit = wit;
370
+ contract.contractAddress = contractAddress;
154
371
 
155
- return contract as unknown as GetContractReturnType<wit>;
372
+ return contract as unknown as GetContractReturnType<
373
+ wit,
374
+ client,
375
+ contractAddress
376
+ >;
156
377
  }
157
378
 
158
379
  export function getFunctionParameters(
@@ -1,4 +1,3 @@
1
- // import type { ResolvedRegister } from "../../../../wit/register.js";
2
1
  import type { KontorPublicClient } from "../../../clients/kontor/create-public-client.js";
3
2
  import type { HttpTransport } from "../../../clients/transports/create-http-transport.js";
4
3
  import type { Chain } from "../../../types/chain.js";
@@ -7,42 +6,40 @@ import { buildInstructionQuery } from "./build-instruction-query.js";
7
6
  import { getKontorAction } from "../../../utils/get-action.js";
8
7
  import type { ComposeQuery } from "@kontor/kontor-ts";
9
8
  import type { Inst } from "../../../../sdk/types/kontor.js";
9
+ import type { GetAccountParameter } from "../../../../sdk/types/account.js";
10
10
 
11
+ import type { Account } from "../../../accounts/types.js";
12
+ import { parseAccount } from "../../../accounts/utils/parse-account.js";
11
13
  export type BuildComposeQueryParameters<
12
- _chain extends Chain | undefined = Chain | undefined,
14
+ account extends Account | undefined = Account | undefined,
15
+ accountOverride extends Account | [Address, XOnlyPubKey] | undefined =
16
+ | Account
17
+ | [Address, XOnlyPubKey]
18
+ | undefined,
13
19
  > = {
14
20
  instruction: Inst;
15
- address?: Address;
16
- xOnlyPubKey?: XOnlyPubKey; // TODO: maybe this should be wrapped in a single object
17
21
  utxos: UtxoId[];
18
22
  satsPerVByte: number;
19
- };
20
-
23
+ } & GetAccountParameter<account, accountOverride, true, true>;
21
24
  export type BuildComposeQueryReturnType = ComposeQuery;
22
25
 
23
- export async function buildComposeQuery<
24
- chain extends Chain | undefined = Chain | undefined,
26
+ export function buildComposeQuery<
27
+ chain extends Chain | undefined,
28
+ account extends Account | undefined,
25
29
  >(
26
30
  client: KontorPublicClient<HttpTransport, chain>,
27
- params: BuildComposeQueryParameters<chain>,
28
- ): Promise<BuildComposeQueryReturnType> {
29
- const account = client.account;
30
-
31
+ params: BuildComposeQueryParameters<account>,
32
+ ): BuildComposeQueryReturnType {
31
33
  const {
32
34
  instruction: inst,
33
35
  utxos,
34
- address = account?.address,
35
- xOnlyPubKey = account?.xOnlyPubKey,
36
+ satsPerVByte,
37
+ account: account_ = client.account,
36
38
  } = params;
37
39
 
38
- if (!xOnlyPubKey) {
39
- // TODO: add custom error
40
- throw new Error("x_only_public_key is required");
41
- }
42
-
43
- if (!address) {
40
+ if (!account_) {
44
41
  // TODO: add custom error
45
- throw new Error("address is required");
42
+ throw new Error("Account is required");
46
43
  }
47
44
 
48
45
  if (utxos.length === 0) {
@@ -50,20 +47,21 @@ export async function buildComposeQuery<
50
47
  throw new Error("at least one funding UTXO is required");
51
48
  }
52
49
 
53
- const instruction = await getKontorAction(
50
+ const account = parseAccount(account_);
51
+
52
+ const instruction = getKontorAction(
54
53
  client,
55
54
  buildInstructionQuery,
56
55
  "buildInstructionQuery",
57
56
  )({
58
57
  instruction: inst,
59
58
  utxos,
60
- xOnlyPubKey,
61
- address,
59
+ account,
62
60
  });
63
61
 
64
62
  return {
65
63
  instructions: [instruction],
66
- sat_per_vbyte: params.satsPerVByte,
64
+ sat_per_vbyte: satsPerVByte,
67
65
  envelope: null,
68
66
  };
69
67
  }
@@ -2,17 +2,24 @@ import type { Inst, InstructionQuery } from "../../../types/kontor.js";
2
2
  import type { KontorPublicClient } from "../../../clients/kontor/create-public-client.js";
3
3
  import type { HttpTransport } from "../../../clients/transports/create-http-transport.js";
4
4
  import type { Chain } from "../../../types/chain.js";
5
- import type { Address, UtxoId, XOnlyPubKey } from "../../../types/misc.js";
5
+ import type { Address, XOnlyPubKey, UtxoId } from "../../../types/misc.js";
6
+ import type { Account } from "../../../../sdk/accounts/types.js";
7
+ import { buildInstructionQuery as buildInstructionQuery_ } from "../../../utils/kontor/build-instruction-query.js";
8
+
9
+ import { parseAccount } from "../../../../sdk/accounts/utils/parse-account.js";
10
+ import type { GetAccountParameter } from "../../../../sdk/types/account.js";
6
11
 
7
12
  // If InstructionQuery already uses this Inst shape internally, you don't need to touch it.
8
13
  // Otherwise you can make InstructionQuery generic over Inst and plug this in.
9
14
  export type BuildInstructionQueryReturnType = InstructionQuery;
10
15
 
11
16
  export type BuildInstructionQueryParameters<
12
- _chain extends Chain | undefined = Chain | undefined,
17
+ account extends Account | undefined = Account | undefined,
18
+ accountOverride extends Account | [Address, XOnlyPubKey] | undefined =
19
+ | Account
20
+ | [Address, XOnlyPubKey]
21
+ | undefined,
13
22
  > = {
14
- address?: Address;
15
- xOnlyPubKey?: XOnlyPubKey; // TODO: maybe this should be wrapped in a single object
16
23
  utxos: UtxoId[];
17
24
  /**
18
25
  * The concrete instruction to send (Publish | Call | Issuance).
@@ -26,57 +33,25 @@ export type BuildInstructionQueryParameters<
26
33
  * Optional chained instruction if you want to send two instructions atomically.
27
34
  */
28
35
  chainedInstruction?: Inst | null;
29
- };
30
-
31
- // ------------------ helpers ------------------
32
-
33
- function normalizeXOnly(pub: string): string {
34
- if (!/^[0-9a-fA-F]{64}$/.test(pub)) {
35
- throw new Error(
36
- "x_only_public_key must be 32-byte (64 hex chars) x-only pubkey",
37
- );
38
- }
39
- return pub.toLowerCase();
40
- }
41
-
42
- function normalizeUtxoId(u: UtxoId): string {
43
- if (typeof u === "string") return u;
44
- if (!u?.txid || typeof u.vout !== "number") {
45
- throw new Error("funding UTXO must be 'txid:vout' or { txid, vout }");
46
- }
47
- return `${u.txid}:${u.vout}`;
48
- }
36
+ } & GetAccountParameter<account, accountOverride, true, true>;
49
37
 
50
- function normalizeUtxoIds(u: UtxoId | UtxoId[]): string[] {
51
- return Array.isArray(u) ? u.map(normalizeUtxoId) : [normalizeUtxoId(u)];
52
- }
53
-
54
- // ------------------ main builder ------------------
55
-
56
- export async function buildInstructionQuery<
57
- chain extends Chain | undefined = Chain | undefined,
38
+ export function buildInstructionQuery<
39
+ chain extends Chain | undefined,
40
+ account extends Account | undefined,
58
41
  >(
59
42
  client: KontorPublicClient<HttpTransport, chain>,
60
- params: BuildInstructionQueryParameters<chain>,
61
- ): Promise<BuildInstructionQueryReturnType> {
62
- const account = client.account;
63
-
43
+ params: BuildInstructionQueryParameters<account>,
44
+ ): BuildInstructionQueryReturnType {
64
45
  const {
65
46
  utxos,
66
47
  instruction,
67
48
  chainedInstruction = null,
68
- address = account?.address,
69
- xOnlyPubKey = account?.xOnlyPubKey,
49
+ account: account_ = client.account,
70
50
  } = params;
71
51
 
72
- if (!xOnlyPubKey) {
52
+ if (!account_) {
73
53
  // TODO: add custom error
74
- throw new Error("x_only_public_key is required");
75
- }
76
-
77
- if (!address) {
78
- // TODO: add custom error
79
- throw new Error("address is required");
54
+ throw new Error("Account is required");
80
55
  }
81
56
 
82
57
  if (utxos.length === 0) {
@@ -84,14 +59,13 @@ export async function buildInstructionQuery<
84
59
  throw new Error("at least one funding UTXO is required");
85
60
  }
86
61
 
87
- const xOnly = normalizeXOnly(xOnlyPubKey);
88
- const fundingIds = normalizeUtxoIds(utxos);
62
+ const account = parseAccount(account_);
89
63
 
90
- return {
91
- address,
92
- x_only_public_key: xOnly,
93
- funding_utxo_ids: fundingIds.join(","),
64
+ return buildInstructionQuery_({
65
+ utxos,
94
66
  instruction,
95
- chained_instruction: chainedInstruction ?? null,
96
- };
67
+ chainedInstruction,
68
+ address: account.address,
69
+ xOnlyPubKey: account.xOnlyPubKey,
70
+ });
97
71
  }