import type { AbiItem } from 'web3-utils' ; import * as ABICoder from 'web3-eth-abi' ; import BulkCheckout from '@masknet/web3-contracts/abis/BulkCheckout.json' ; import ITO2 from '@masknet/web3-contracts/abis/ITO2.json' ; import NftHappyRedPacket from '@masknet/web3-contracts/abis/NftHappyRedPacket.json' ; import HappyRedPacketV4 from '@masknet/web3-contracts/abis/HappyRedPacketV4.json' ; import ERC20 from '@masknet/web3-contracts/abis/ERC20.json' ; import RouterV2ABI from '@masknet/web3-contracts/abis/RouterV2.json' ; import SwapRouter from '@masknet/web3-contracts/abis/SwapRouter.json' ; import MaskBox from '@masknet/web3-contracts/abis/MaskBox.json' ; const coder = ABICoder as unknown as ABICoder . AbiCoder ; type InternalItem = { name : string ; parameters : { name : string ; type : string ; } [ ] ; } ; const ABI_MAP = new Map ( ) ; export function readABI ( sig ? ) { if ( ! sig ) return ; return ABI_MAP . get ( sig ) ; } function constructABI ( abi ) { abi . forEach ( ( x ) => { if ( x . type !== 'function' ) return ; if ( x . stateMutability === 'pure' || x . stateMutability === 'view' ) return ; const { name , inputs = [ ] } = x ; if ( ! name ) return ; try { const sig = coder . encodeFunctionSignature ( `${ x . name }(${ inputs . map ( ( y ) => y . type ) . join ( ',' ) })` ) ; if ( ABI_MAP . has ( sig ) ) console . warn ( `The signature of ${ `${ x . name }(${ inputs . map ( ( y ) => y . type ) . join ( ',' ) })` } already exists.` ) ; ABI_MAP . set ( sig , { name , parameters : inputs . map ( ( y ) => ( { name : y . name , type : y . type , } ) ) ?? [ ] , } ) ; } catch ( error ) { console . log ( 'Failed to encode function signature from below ABI:' ) ; console . log ( x ) ; } } ) ; } constructABI ( BulkCheckout as AbiItem [ ] ) ; constructABI ( ITO2 as AbiItem [ ] ) ; constructABI ( NftHappyRedPacket as AbiItem [ ] ) ; constructABI ( HappyRedPacketV4 as AbiItem [ ] ) ; constructABI ( MaskBox as AbiItem [ ] ) ; constructABI ( ERC20 as AbiItem [ ] ) ; constructABI ( RouterV2ABI as AbiItem [ ] ) ; constructABI ( SwapRouter as AbiItem [ ] ) ;