import Transaction from 'arweave/node/lib/transaction' ; import Arweave from 'arweave' ; import { GQLEdgeInterface } from './interfaces/gqlResult' ; interface UnformattedTag { name : string ; value : string ; } export function getTag ( tx , name ) { const tags = tx . get ( 'tags' ) as any ; for ( const tag of tags ) { try { if ( tag . get ( 'name' , { decode : true , string : true } ) === name ) { return tag . get ( 'value' , { decode : true , string : true } ) ; } } catch ( e ) { } } return false ; } param tx export function unpackTags ( tx ) { const tags = tx . get ( 'tags' ) as any ; const result = { } ; for ( const tag of tags ) { try { const name = tag . get ( 'name' , { decode : true , string : true } ) as string ; const value = tag . get ( 'value' , { decode : true , string : true } ) as string ; if ( ! result . hasOwnProperty ( name ) ) { result [ name ] = value ; continue ; } result [ name ] = [ ... result [ name ] , value ] ; } catch ( e ) { } } return result ; } export function formatTags ( tags ) { const result = { } ; for ( const tag of tags ) { const { name , value } = tag ; if ( ! result . hasOwnProperty ( name ) ) { result [ name ] = value ; continue ; } result [ name ] = [ ... result [ name ] , value ] ; } return result ; } export function arrayToHex ( arr ) { let str = '' ; for ( const a of arr ) { str += ( '0' + a . toString ( 16 ) ) . slice ( - 2 ) ; } return str ; } export function log ( arweave ? , ... str ) { if ( ! arweave || ! arweave . getConfig ( ) . api . logging ) return ; typeof arweave . getConfig ( ) . api . logger === 'function' ? arweave . getConfig ( ) . api . logger ( ... str ) : console . log ( ... str ) ; } export function normalizeContractSource ( contractSrc ) { contractSrc = contractSrc . replace ( /export\s+async\s+function\s+handle/gmu , 'async function handle' ) . replace ( /export\s+function\s+handle/gmu , 'function handle' ) . replace ( /\(\s*\(\)\s*=>\s*{/g , '' ) . replace ( /\s*\(\s*function\s*\(\)\s*{/g , '' ) . replace ( /}\s*\)\s*\(\)\s*;/g , '' ) ; return `
    const [SmartWeave, BigNumber, clarity] = arguments;
    clarity.SmartWeave = SmartWeave;
    class ContractError extends Error { constructor(message) { super(message); this.name = \'ContractError\' } };
    function ContractAssert(cond, message) { if (!cond) throw new ContractError(message) };
    ${ contractSrc };
    return handle;
  ` ; } param state returns { Map } export function evalSettings ( state ) { let settings = new Map < string , any > ( ) ; if ( state . settings ) { if ( isIterable ( state . settings ) ) { settings = new Map < string , any > ( state . settings ) ; } else if ( isObject ( state . settings ) ) { settings = new Map < string , any > ( Object . entries ( state . settings ) ) ; } } return settings ; } param obj returns function isIterable ( obj ) { if ( obj == null ) { return false ; } return typeof obj [ Symbol . iterator ] === 'function' ; } param obj returns function isObject ( obj ) { return typeof obj === 'object' && obj !== null && ! Array . isArray ( obj ) ; } param gqlResult returns { boolean } export function hasMultipleinteractions ( gqlResult ) { return gqlResult . node . tags . filter ( ( tag ) => tag . name === 'Contract' ) . length > 1 ; }