import ITO_ABI from '@masknet/web3-contracts/abis/ITO.json' ; import ITO2_ABI from '@masknet/web3-contracts/abis/ITO2.json' ; import type { ChainId } from '@masknet/web3-shared-evm' ; import { Interface } from '@ethersproject/abi' ; import type { Availability } from '../../types' ; import Services from '../../../../extension/service' ; const interFaceV1 = new Interface ( ITO_ABI ) ; const interFaceV2 = new Interface ( ITO2_ABI ) ; export async function checkAvailability ( pid , from , to , chainId , isV1 = false ) { const callData = ( isV1 ? interFaceV1 : interFaceV2 ) . encodeFunctionData ( 'check_availability' , [ pid ] ) ; const data = await Services . Ethereum . call ( { to , from , data : callData } , { chainId } ) ; return decodeResult ( data , isV1 ) ; } function decodeResult ( data , isV1 ) { const results = ( isV1 ? interFaceV1 : interFaceV2 ) . decodeFunctionResult ( 'check_availability' , data ) ; return { exchange_addrs : results [ 0 ] , remaining : + parseHexToInt ( results [ 1 ] ) , started : results [ 2 ] , expired : results [ 3 ] , destructed : results [ 4 ] , unlock_time : parseHexToInt ( results [ 5 ] ) , swapped : parseHexToInt ( results [ 6 ] ) , exchanged_tokens : parse ( results [ 7 ] ) . map ( parseHexToInt ) , ... ( isV1 ? { } : { claimed : results [ 8 ] , start_time : parseHexToInt ( results [ 9 ] ) , end_time : parseHexToInt ( results [ 10 ] ) , qualification_addr : results [ 11 ] , } ) , } ; } function parse ( input ) { return JSON . parse ( JSON . stringify ( input ) ) ; } function parseHexToInt ( input ) { return Number . parseInt ( parse ( input ) . hex , 16 ) . toString ( ) ; }