@@ -11,25 +11,29 @@ import Node from './Node';
1111import { TxParamsAsync } from './tx/builder/schema.generated' ;
1212import AccountBase from './account/Base' ;
1313import { 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' ;
1516
1617export type OnAccount = Encoded . AccountAddress | AccountBase | undefined ;
1718
18- export function getValueOrErrorProxy < Value extends object > ( valueCb : ( ) => Value ) : Value {
19+ export function getValueOrErrorProxy < Value extends object | undefined > (
20+ valueCb : ( ) => Value ,
21+ ) : NonNullable < Value > {
1922 return new Proxy ( { } , {
2023 ...Object . fromEntries ( [
2124 'apply' , 'construct' , 'defineProperty' , 'deleteProperty' , 'getOwnPropertyDescriptor' ,
2225 'getPrototypeOf' , 'isExtensible' , 'ownKeys' , 'preventExtensions' , 'set' , 'setPrototypeOf' ,
2326 ] . map ( ( name ) => [ name , ( ) => { throw new NotImplementedError ( `${ name } proxy request` ) ; } ] ) ) ,
2427 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
2629 const value = Reflect . get ( target , property , receiver ) ;
2730 return typeof value === 'function' ? value . bind ( target ) : value ;
2831 } ,
2932 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 ) ;
3135 } ,
32- } ) as Value ;
36+ } ) as NonNullable < Value > ;
3337}
3438
3539const { InvalidTxError : _2 , ...chainMethodsOther } = chainMethods ;
@@ -51,9 +55,8 @@ type GetMethodsOptions <Methods extends { [key: string]: Function }> =
5155 ? Args [ Decrement < Args [ 'length' ] > ] : never
5256 } ;
5357type MethodsOptions = GetMethodsOptions < typeof methods > ;
54- interface AeSdkMethodsOptions
58+ export interface AeSdkMethodsOptions
5559 extends Partial < UnionToIntersection < MethodsOptions [ keyof MethodsOptions ] > > {
56- nodes ?: Array < { name : string ; instance : Node } > ;
5760}
5861
5962/**
@@ -79,24 +82,15 @@ class AeSdkMethods {
7982 Object . assign ( this . _options , options ) ;
8083 }
8184
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 } {
9788 return {
9889 ...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 ,
10094 } ;
10195 }
10296
@@ -107,16 +101,7 @@ class AeSdkMethods {
107101 async initializeContract < Methods extends ContractMethodsBase > (
108102 options ?: Omit < Parameters < typeof Contract . initialize > [ 0 ] , 'onNode' > & { onNode ?: Node } ,
109103 ) : 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 ) ) ;
120105 }
121106}
122107
@@ -150,15 +135,13 @@ Object.assign(AeSdkMethods.prototype, mapObject<Function, Function>(
150135 function methodWrapper ( this : AeSdkMethods , ...args : any [ ] ) {
151136 args . length = handler . length ;
152137 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 ) ;
158139 return handler ( ...args ) ;
159140 } ,
160141 ] ,
161142) ) ;
162143
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