import { ChainInfo } from '@gnosis.pm/safe-apps-sdk' ; import getAbi from './getAbi' ; export interface ContractInput { internalType : string ; name : string ; type : string ; } interface ContractMethod { inputs : ContractInput [ ] ; name : string ; payable : boolean ; } export interface ContractInterface { methods : ContractMethod [ ] ; } class InterfaceRepository { chainInfo : ChainInfo ; web3 : any ; constructor ( chainInfo , web3 ) { this . chainInfo = chainInfo ; this . web3 = web3 ; } private async _loadAbiFromBlockExplorer ( address ) { return await getAbi ( address , this . chainInfo ) ; } private _isMethodPayable = ( m ) => m . payable || m . stateMutability === 'payable' ; async loadAbi ( addressOrAbi ) { const abi = this . web3 . utils . isAddress ( addressOrAbi ) ? await this . _loadAbiFromBlockExplorer ( addressOrAbi ) : JSON . parse ( addressOrAbi ) ; const methods = abi . filter ( ( e ) => { if ( [ 'pure' , 'view' ] . includes ( e . stateMutability ) ) { return false ; } if ( e ?. type . toLowerCase ( ) === 'event' ) { return false ; } return ! e . constant ; } ) . filter ( ( m ) => m . type !== 'constructor' ) . map ( ( m ) => { return { inputs : m . inputs || [ ] , name : m . name || ( m . type === 'fallback' ? 'fallback' : 'receive' ) , payable : this . _isMethodPayable ( m ) , } ; } ) ; return { methods } ; } } export type InterfaceRepo = InstanceType < typeof InterfaceRepository > ; export default InterfaceRepository ;