@@ -11,25 +11,29 @@ import Node from './Node';
11
11
import { TxParamsAsync } from './tx/builder/schema.generated' ;
12
12
import AccountBase from './account/Base' ;
13
13
import { Encoded } from './utils/encoder' ;
14
- import { ArgumentError , NotImplementedError , TypeError } from './utils/errors' ;
14
+ import { NotImplementedError } from './utils/errors' ;
15
+ import CompilerBase from './contract/compiler/Base' ;
15
16
16
17
export type OnAccount = Encoded . AccountAddress | AccountBase | undefined ;
17
18
18
- export function getValueOrErrorProxy < Value extends object > ( valueCb : ( ) => Value ) : Value {
19
+ export function getValueOrErrorProxy < Value extends object | undefined > (
20
+ valueCb : ( ) => Value ,
21
+ ) : NonNullable < Value > {
19
22
return new Proxy ( { } , {
20
23
...Object . fromEntries ( [
21
24
'apply' , 'construct' , 'defineProperty' , 'deleteProperty' , 'getOwnPropertyDescriptor' ,
22
25
'getPrototypeOf' , 'isExtensible' , 'ownKeys' , 'preventExtensions' , 'set' , 'setPrototypeOf' ,
23
26
] . map ( ( name ) => [ name , ( ) => { throw new NotImplementedError ( `${ name } proxy request` ) ; } ] ) ) ,
24
27
get ( t : { } , property : string | symbol , receiver : any ) {
25
- const target = valueCb ( ) ;
28
+ const target = valueCb ( ) as object ; // to get a native exception in case it missed
26
29
const value = Reflect . get ( target , property , receiver ) ;
27
30
return typeof value === 'function' ? value . bind ( target ) : value ;
28
31
} ,
29
32
has ( t : { } , property : string | symbol ) {
30
- return Reflect . has ( valueCb ( ) , property ) ;
33
+ const target = valueCb ( ) as object ; // to get a native exception in case it missed
34
+ return Reflect . has ( target , property ) ;
31
35
} ,
32
- } ) as Value ;
36
+ } ) as NonNullable < Value > ;
33
37
}
34
38
35
39
const { InvalidTxError : _2 , ...chainMethodsOther } = chainMethods ;
@@ -51,9 +55,8 @@ type GetMethodsOptions <Methods extends { [key: string]: Function }> =
51
55
? Args [ Decrement < Args [ 'length' ] > ] : never
52
56
} ;
53
57
type MethodsOptions = GetMethodsOptions < typeof methods > ;
54
- interface AeSdkMethodsOptions
58
+ export interface AeSdkMethodsOptions
55
59
extends Partial < UnionToIntersection < MethodsOptions [ keyof MethodsOptions ] > > {
56
- nodes ?: Array < { name : string ; instance : Node } > ;
57
60
}
58
61
59
62
/**
@@ -79,24 +82,15 @@ class AeSdkMethods {
79
82
Object . assign ( this . _options , options ) ;
80
83
}
81
84
82
- /**
83
- * Resolves an account
84
- * @param account - ak-address, instance of AccountBase, or keypair
85
- */
86
- // eslint-disable-next-line class-methods-use-this
87
- _resolveAccount ( account ?: OnAccount ) : AccountBase {
88
- if ( typeof account === 'string' ) throw new NotImplementedError ( 'Address in AccountResolver' ) ;
89
- if ( typeof account === 'object' ) return account ;
90
- throw new TypeError (
91
- 'Account should be an address (ak-prefixed string), '
92
- + `or instance of AccountBase, got ${ String ( account ) } instead` ,
93
- ) ;
94
- }
95
-
96
- _getOptions ( ) : AeSdkMethodsOptions & { onAccount : AccountBase } {
85
+ _getOptions (
86
+ callOptions : AeSdkMethodsOptions = { } ,
87
+ ) : AeSdkMethodsOptions & { onAccount : AccountBase ; onCompiler : CompilerBase ; onNode : Node } {
97
88
return {
98
89
...this . _options ,
99
- onAccount : getValueOrErrorProxy ( ( ) => this . _resolveAccount ( ) ) ,
90
+ onAccount : getValueOrErrorProxy ( ( ) => this . _options . onAccount ) ,
91
+ onNode : getValueOrErrorProxy ( ( ) => this . _options . onNode ) ,
92
+ onCompiler : getValueOrErrorProxy ( ( ) => this . _options . onCompiler ) ,
93
+ ...callOptions ,
100
94
} ;
101
95
}
102
96
@@ -107,16 +101,7 @@ class AeSdkMethods {
107
101
async initializeContract < Methods extends ContractMethodsBase > (
108
102
options ?: Omit < Parameters < typeof Contract . initialize > [ 0 ] , 'onNode' > & { onNode ?: Node } ,
109
103
) : Promise < Contract < Methods > > {
110
- const { onNode, onCompiler, ...otherOptions } = this . _getOptions ( ) ;
111
- if ( onCompiler == null || onNode == null ) {
112
- throw new ArgumentError ( 'onCompiler, onNode' , 'provided' , null ) ;
113
- }
114
- return Contract . initialize < Methods > ( {
115
- ...otherOptions ,
116
- onNode,
117
- onCompiler,
118
- ...options ,
119
- } ) ;
104
+ return Contract . initialize < Methods > ( this . _getOptions ( options as AeSdkMethodsOptions ) ) ;
120
105
}
121
106
}
122
107
@@ -150,15 +135,13 @@ Object.assign(AeSdkMethods.prototype, mapObject<Function, Function>(
150
135
function methodWrapper ( this : AeSdkMethods , ...args : any [ ] ) {
151
136
args . length = handler . length ;
152
137
const options = args [ args . length - 1 ] ;
153
- args [ args . length - 1 ] = {
154
- ...this . _getOptions ( ) ,
155
- ...options ,
156
- ...options ?. onAccount != null && { onAccount : this . _resolveAccount ( options . onAccount ) } ,
157
- } ;
138
+ args [ args . length - 1 ] = this . _getOptions ( options ) ;
158
139
return handler ( ...args ) ;
159
140
} ,
160
141
] ,
161
142
) ) ;
162
143
163
- export default AeSdkMethods as new ( options ?: ConstructorParameters < typeof AeSdkMethods > [ 0 ] ) =>
164
- AeSdkMethods & AeSdkMethodsTransformed ;
144
+ type AeSdkMethodsTyped = AeSdkMethods & AeSdkMethodsTransformed ;
145
+ // eslint-disable-next-line @typescript-eslint/no-redeclare
146
+ const AeSdkMethodsTyped = AeSdkMethods as new ( options ?: AeSdkMethodsOptions ) => AeSdkMethodsTyped ;
147
+ export default AeSdkMethodsTyped ;
0 commit comments