import Arweave from 'arweave' ; import { CreateTransactionInterface } from 'arweave/node/common' ; import Transaction from 'arweave/node/lib/transaction' ; import { JWKInterface } from 'arweave/node/lib/wallet' ; import { loadContract } from './contract-load' ; import { readContract } from './contract-read' ; import { execute , ContractInteraction , ContractInteractionResult , ContractHandler } from './contract-step' ; import { evalSettings , unpackTags } from './utils' ; import { BlockData } from 'arweave/node/blocks' ; import SmartWeaveError , { SmartWeaveErrorType } from './errors' ; import { SmartWeaveGlobal } from './smartweave-global' ; param arweave param wallet param contractId param input param tags param target param winstonQty param reward export async function interactWrite ( arweave , wallet , contractId , input , tags = [ ] , target = '' , winstonQty = '' , reward ? ) { const interactionTx = await createTx ( arweave , wallet , contractId , input , tags , target , winstonQty , reward ) ; const response = await arweave . transactions . post ( interactionTx ) ; if ( response . status !== 200 ) return null ; return interactionTx . id ; } param arweave param wallet param contractId param input param tags param target param winstonQty export async function simulateInteractWrite ( arweave , wallet , contractId , input , tags = [ ] , target = '' , winstonQty = '' ) { const interactionTx = await createTx ( arweave , wallet , contractId , input , tags , target , winstonQty ) ; return interactionTx ; } param arweave param wallet param contractId param input param tags param target param winstonQty param myState param fromParam param contractInfoParam export async function interactWriteDryRun ( arweave , wallet , contractId , input , tags = [ ] , target = '' , winstonQty = '' , myState ? , fromParam ? , contractInfoParam ? ) { let { handler , swGlobal , contractSrcTXID } = contractInfoParam || ( await loadContract ( arweave , contractId ) ) ; const latestState = myState || ( await readContract ( arweave , contractId ) ) ; const from = fromParam || ( await arweave . wallets . getAddress ( wallet ) ) ; const settings = evalSettings ( latestState ) ; const evolve = latestState . evolve || settings . get ( 'evolve' ) ; let canEvolve = latestState . canEvolve || settings . get ( 'canEvolve' ) ; if ( canEvolve === undefined || canEvolve === null ) { canEvolve = true ; } if ( evolve && /[a-z0-9_-]{43}/i . test ( evolve ) && canEvolve ) { if ( contractSrcTXID !== evolve ) { try { const contractInfo = await loadContract ( arweave , contractId , evolve ) ; swGlobal = contractInfo . swGlobal ; handler = contractInfo . handler ; contractSrcTXID = evolve ; } catch ( e ) { const error = new SmartWeaveError ( SmartWeaveErrorType . CONTRACT_NOT_FOUND , { message : `Contract having txId: ${ contractId } not found` , requestedTxId : contractId , } ) ; console . log ( error ) ; } } } const interaction = { input , caller : from , } ; const tx = await createTx ( arweave , wallet , contractId , input , tags , target , winstonQty ) ; const ts = unpackTags ( tx ) ; const currentBlock = await arweave . blocks . getCurrent ( ) ; swGlobal . _activeTx = createDummyTx ( tx , from , ts , currentBlock ) ; return await execute ( handler , interaction , latestState ) ; } param arweave param tx param contractId param input param myState param fromParam param contractInfoParam export async function interactWriteDryRunCustom ( arweave , tx , contractId , input , myState , fromParam = { } , contractInfoParam ) { let { handler , swGlobal , contractSrcTXID } = contractInfoParam || ( await loadContract ( arweave , contractId ) ) ; const latestState = myState || ( await readContract ( arweave , contractId ) ) ; const from = fromParam ; const settings = evalSettings ( latestState ) ; const evolve = latestState . evolve || settings . get ( 'evolve' ) ; let canEvolve = latestState . canEvolve || settings . get ( 'canEvolve' ) ; if ( canEvolve === undefined || canEvolve === null ) { canEvolve = true ; } if ( evolve && /[a-z0-9_-]{43}/i . test ( evolve ) && canEvolve ) { if ( contractSrcTXID !== evolve ) { try { const contractInfo = await loadContract ( arweave , contractId , evolve ) ; swGlobal = contractInfo . swGlobal ; handler = contractInfo . handler ; contractSrcTXID = evolve ; } catch ( e ) { const error = new SmartWeaveError ( SmartWeaveErrorType . CONTRACT_NOT_FOUND , { message : `Contract having txId: ${ contractId } not found` , requestedTxId : contractId , } ) ; console . log ( error ) ; } } } const interaction = { input , caller : from , } ; const ts = unpackTags ( tx ) ; const currentBlock = await arweave . blocks . getCurrent ( ) ; swGlobal . _activeTx = createDummyTx ( tx , from , ts , currentBlock ) ; return await execute ( handler , interaction , latestState ) ; } param arweave param wallet param contractId param input param tags param target param winstonQty export async function interactRead ( arweave , wallet , contractId , input , tags = [ ] , target = '' , winstonQty = '' ) { let { handler , swGlobal , contractSrcTXID } = await loadContract ( arweave , contractId ) ; const latestState = await readContract ( arweave , contractId ) ; const from = wallet ? await arweave . wallets . getAddress ( wallet ) : '' ; const settings = evalSettings ( latestState ) ; const evolve = latestState . evolve || settings . get ( 'evolve' ) ; let canEvolve = latestState . canEvolve || settings . get ( 'canEvolve' ) ; if ( canEvolve === undefined || canEvolve === null ) { canEvolve = true ; } if ( evolve && /[a-z0-9_-]{43}/i . test ( evolve ) && canEvolve ) { if ( contractSrcTXID !== evolve ) { try { const contractInfo = await loadContract ( arweave , contractId , evolve ) ; swGlobal = contractInfo . swGlobal ; handler = contractInfo . handler ; contractSrcTXID = evolve ; } catch ( e ) { const error = new SmartWeaveError ( SmartWeaveErrorType . CONTRACT_NOT_FOUND , { message : `Contract having txId: ${ contractId } not found` , requestedTxId : contractId , } ) ; console . log ( error ) ; } } } const interaction = { input , caller : from , } ; const tx = await createTx ( arweave , wallet , contractId , input , tags , target , winstonQty ) ; const ts = unpackTags ( tx ) ; const currentBlock = await arweave . blocks . getCurrent ( ) ; swGlobal . _activeTx = createDummyTx ( tx , from , ts , currentBlock ) ; const result = await execute ( handler , interaction , latestState ) ; return result . result ; } async function createTx ( arweave , wallet , contractId , input , tags , target = '' , winstonQty = '0' , reward ? ) { const options = { data : Math . random ( ) . toString ( ) . slice ( - 4 ) , reward , } ; if ( target && target . length ) { options . target = target . toString ( ) ; if ( winstonQty && + winstonQty > 0 ) { options . quantity = winstonQty . toString ( ) ; } } const interactionTx = await arweave . createTransaction ( options , wallet ) ; if ( ! input ) { throw new Error ( `Input should be a truthy value: ${ JSON . stringify ( input ) }` ) ; } if ( tags && tags . length ) { for ( const tag of tags ) { interactionTx . addTag ( tag . name . toString ( ) , tag . value . toString ( ) ) ; } } interactionTx . addTag ( 'App-Name' , 'SmartWeaveAction' ) ; interactionTx . addTag ( 'App-Version' , '0.3.0' ) ; interactionTx . addTag ( 'Contract' , contractId ) ; interactionTx . addTag ( 'Input' , JSON . stringify ( input ) ) ; await arweave . transactions . sign ( interactionTx , wallet ) ; return interactionTx ; } function createDummyTx ( tx , from , tags , block ) { return { id : tx . id , owner : { address : from , } , recipient : tx . target , tags , fee : { winston : tx . reward , } , quantity : { winston : tx . quantity , } , block : { id : block . indep_hash , height : block . height , timestamp : block . timestamp , } , } ; }