import Arweave from 'arweave' ; import * as clarity from '@weavery/clarity' ; import { getTag , normalizeContractSource } from './utils' ; import { ContractHandler } from './contract-step' ; import { SmartWeaveGlobal } from './smartweave-global' ; import BigNumber from 'bignumber.js' ; param arweave param contractID export async function loadContract ( arweave , contractID , contractSrcTXID ? ) { const contractTX = await arweave . transactions . get ( contractID ) ; const contractOwner = await arweave . wallets . ownerToAddress ( contractTX . owner ) ; contractSrcTXID = contractSrcTXID || getTag ( contractTX , 'Contract-Src' ) ; const minFee = getTag ( contractTX , 'Min-Fee' ) ; const contractSrcTX = await arweave . transactions . get ( contractSrcTXID ) ; const contractSrc = contractSrcTX . get ( 'data' , { decode : true , string : true } ) ; let state ; if ( getTag ( contractTX , 'Init-State' ) ) { state = getTag ( contractTX , 'Init-State' ) ; } else if ( getTag ( contractTX , 'Init-State-TX' ) ) { const stateTX = await arweave . transactions . get ( getTag ( contractTX , 'Init-State-TX' ) ) ; state = stateTX . get ( 'data' , { decode : true , string : true } ) ; } else { state = contractTX . get ( 'data' , { decode : true , string : true } ) ; } const { handler , swGlobal } = createContractExecutionEnvironment ( arweave , contractSrc , contractID , contractOwner ) ; return { id : contractID , contractSrcTXID , contractSrc , initState : state , minFee , contractTX , handler , swGlobal , } ; } param contractSrc export function createContractExecutionEnvironment ( arweave , contractSrc , contractId , contractOwner ) { const returningSrc = normalizeContractSource ( contractSrc ) ; const swGlobal = new SmartWeaveGlobal ( arweave , { id : contractId , owner : contractOwner } ) ; const getContractFunction = new Function ( returningSrc ) ; return { handler : getContractFunction ( swGlobal , BigNumber , clarity ) as ContractHandler , swGlobal , } ; }