1
1
import {
2
2
Operation ,
3
- Transaction ,
4
- TransactionBuilder ,
5
3
xdr ,
6
- Account ,
7
4
hash ,
8
5
Address ,
9
6
} from "@stellar/stellar-base" ;
@@ -12,10 +9,38 @@ import { Server } from '../rpc';
12
9
import { AssembledTransaction } from "./assembled_transaction" ;
13
10
import type { ClientOptions , MethodOptions } from "./types" ;
14
11
import { processSpecEntryStream } from './utils' ;
15
- import { DEFAULT_TIMEOUT } from "./types" ;
16
12
17
13
const CONSTRUCTOR_FUNC = "__constructor" ;
18
14
15
+ async function specFromWasm ( wasm : Buffer ) {
16
+ const wasmModule = await WebAssembly . compile ( wasm ) ;
17
+ const xdrSections = WebAssembly . Module . customSections (
18
+ wasmModule ,
19
+ "contractspecv0"
20
+ ) ;
21
+ if ( xdrSections . length === 0 ) {
22
+ throw new Error ( "Could not obtain contract spec from wasm" ) ;
23
+ }
24
+ const bufferSection = Buffer . from ( xdrSections [ 0 ] ) ;
25
+ const specEntryArray = processSpecEntryStream ( bufferSection ) ;
26
+ const spec = new Spec ( specEntryArray ) ;
27
+ return spec ;
28
+ }
29
+
30
+ async function specFromWasmHash (
31
+ wasmHash : Buffer | string ,
32
+ options : Server . Options & { rpcUrl : string } ,
33
+ format : "hex" | "base64" = "hex"
34
+ ) : Promise < Spec > {
35
+ if ( ! options || ! options . rpcUrl ) {
36
+ throw new TypeError ( "options must contain rpcUrl" ) ;
37
+ }
38
+ const { rpcUrl, allowHttp } = options ;
39
+ const serverOpts : Server . Options = { allowHttp } ;
40
+ const server = new Server ( rpcUrl , serverOpts ) ;
41
+ const wasm = await server . getContractWasmByHash ( wasmHash , format ) ;
42
+ return specFromWasm ( wasm ) ;
43
+ }
19
44
20
45
/**
21
46
* Generate a class from the contract spec that where each contract method
@@ -82,7 +107,7 @@ export class Client {
82
107
...options ,
83
108
contractId,
84
109
method : CONSTRUCTOR_FUNC ,
85
- parseResultXdr : ( result : xdr . ScVal ) =>
110
+ parseResultXdr : ( ) =>
86
111
new Client ( spec , { ...options , contractId } ) ,
87
112
} ) as unknown as AssembledTransaction < T > ;
88
113
}
@@ -193,34 +218,4 @@ export class Client {
193
218
} ;
194
219
195
220
txFromXDR = < T > ( xdrBase64 : string ) : AssembledTransaction < T > => AssembledTransaction . fromXDR ( this . options , xdrBase64 , this . spec ) ;
196
-
197
- }
198
- async function specFromWasm ( wasm : Buffer ) {
199
- const wasmModule = await WebAssembly . compile ( wasm ) ;
200
- const xdrSections = WebAssembly . Module . customSections (
201
- wasmModule ,
202
- "contractspecv0"
203
- ) ;
204
- if ( xdrSections . length === 0 ) {
205
- throw new Error ( "Could not obtain contract spec from wasm" ) ;
206
- }
207
- const bufferSection = Buffer . from ( xdrSections [ 0 ] ) ;
208
- const specEntryArray = processSpecEntryStream ( bufferSection ) ;
209
- const spec = new Spec ( specEntryArray ) ;
210
- return spec ;
211
- }
212
-
213
- async function specFromWasmHash (
214
- wasmHash : Buffer | string ,
215
- options : Server . Options & { rpcUrl : string } ,
216
- format : "hex" | "base64" = "hex"
217
- ) : Promise < Spec > {
218
- if ( ! options || ! options . rpcUrl ) {
219
- throw new TypeError ( "options must contain rpcUrl" ) ;
220
- }
221
- const { rpcUrl, allowHttp } = options ;
222
- const serverOpts : Server . Options = { allowHttp } ;
223
- const server = new Server ( rpcUrl , serverOpts ) ;
224
- const wasm = await server . getContractWasmByHash ( wasmHash , format ) ;
225
- return specFromWasm ( wasm ) ;
226
221
}
0 commit comments