@@ -14,12 +14,10 @@ GNU Lesser General Public License for more details.
14
14
You should have received a copy of the GNU Lesser General Public License
15
15
along with web3.js. If not, see <http://www.gnu.org/licenses/>.
16
16
*/
17
-
18
17
import { AbiError } from 'web3-errors' ;
19
- import { ParamType , Result } from '@ethersproject/abi' ;
20
- import { HexString , AbiInput , DecodedParams } from 'web3-types' ;
21
- import ethersAbiCoder from '../ethers_abi_coder.js' ;
22
- import { formatParam , isAbiFragment , mapTypes , modifyParams } from '../utils.js' ;
18
+ import { AbiInput , HexString } from 'web3-types' ;
19
+ import { decodeParameters as decodeParametersInternal } from '../coders/decode.js' ;
20
+ import { encodeParameters as encodeParametersInternal } from '../coders/encode.js' ;
23
21
24
22
/**
25
23
* Encodes a parameter based on its type to its ABI representation.
@@ -37,40 +35,8 @@ import { formatParam, isAbiFragment, mapTypes, modifyParams } from '../utils.js'
37
35
* > 0x000000000000000000000000000000000000000000000000000000008bd02b7b0000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000748656c6c6f212500000000000000000000000000000000000000000000000000
38
36
* ```
39
37
*/
40
- export const encodeParameters = ( abi : ReadonlyArray < AbiInput > , params : unknown [ ] ) : string => {
41
- try {
42
- const modifiedTypes = mapTypes (
43
- Array . isArray ( abi ) ? ( abi as AbiInput [ ] ) : ( [ abi ] as unknown as AbiInput [ ] ) ,
44
- ) ;
45
- const modifiedParams : Array < unknown > = [ ] ;
46
- for ( const [ index , param ] of params . entries ( ) ) {
47
- const item = modifiedTypes [ index ] ;
48
- let type : string ;
49
-
50
- if ( isAbiFragment ( item ) && item . type ) {
51
- // We may get a named type of shape {name, type}
52
- type = item . type ;
53
- } else {
54
- type = item as unknown as string ;
55
- }
56
-
57
- const newParam = formatParam ( type , param ) ;
58
-
59
- if ( typeof type === 'string' && type . includes ( 'tuple' ) ) {
60
- const coder = ethersAbiCoder . _getCoder ( ParamType . from ( type ) ) ;
61
- modifyParams ( coder , [ newParam ] ) ;
62
- }
63
-
64
- modifiedParams . push ( newParam ) ;
65
- }
66
- return ethersAbiCoder . encode (
67
- modifiedTypes . map ( p => ParamType . from ( p ) ) ,
68
- modifiedParams ,
69
- ) ;
70
- } catch ( err ) {
71
- throw new AbiError ( `Parameter encoding error` , err as Error ) ;
72
- }
73
- } ;
38
+ export const encodeParameters = ( abi : ReadonlyArray < AbiInput > , params : unknown [ ] ) : string =>
39
+ encodeParametersInternal ( abi , params ) ;
74
40
75
41
/**
76
42
* Encodes a parameter based on its type to its ABI representation.
@@ -130,30 +96,6 @@ export const encodeParameters = (abi: ReadonlyArray<AbiInput>, params: unknown[]
130
96
*/
131
97
export const encodeParameter = ( abi : AbiInput , param : unknown ) : string =>
132
98
encodeParameters ( [ abi ] , [ param ] ) ;
133
-
134
- // If encoded param is an array and there are mixed on integer and string keys
135
- const isParamRequiredToConvert = ( data : Result ) : boolean =>
136
- Array . isArray ( data ) &&
137
- Object . keys ( data ) . filter ( k => Number . isInteger ( + k ) ) . length !== Object . keys ( data ) . length ;
138
-
139
- // Ethers-Encoder return the decoded result as an array with additional string indexes for named params
140
- // We want these to be converted to an object with named keys
141
- const formatArrayResToObject = ( data : Result ) : DecodedParams => {
142
- const returnValue : DecodedParams = {
143
- __length__ : 0 ,
144
- } ;
145
-
146
- for ( const key of Object . keys ( data ) ) {
147
- returnValue [ key ] =
148
- Array . isArray ( data [ key ] ) && isParamRequiredToConvert ( data [ key ] as Result )
149
- ? formatArrayResToObject ( data [ key ] as Result )
150
- : data [ key ] ;
151
-
152
- returnValue . __length__ += Number . isInteger ( + key ) ? 1 : 0 ;
153
- }
154
- return returnValue ;
155
- } ;
156
-
157
99
/**
158
100
* Should be used to decode list of params
159
101
*/
@@ -172,14 +114,11 @@ export const decodeParametersWith = (
172
114
'or querying a node which is not fully synced.' ,
173
115
) ;
174
116
}
175
- const res = ethersAbiCoder . decode (
176
- mapTypes ( abis ) . map ( p => ParamType . from ( p ) ) ,
177
- `0x${ bytes . replace ( / 0 x / i, '' ) } ` ,
178
- loose ,
179
- ) ;
180
- return formatArrayResToObject ( res ) ;
117
+ return decodeParametersInternal ( abis , `0x${ bytes . replace ( / 0 x / i, '' ) } ` , loose ) ;
181
118
} catch ( err ) {
182
- throw new AbiError ( `Parameter decoding error: ${ ( err as Error ) . message } ` ) ;
119
+ throw new AbiError ( `Parameter decoding error: ${ ( err as Error ) . message } ` , {
120
+ internalErr : err ,
121
+ } ) ;
183
122
}
184
123
} ;
185
124
0 commit comments