import Arweave from 'arweave' ; import Transaction from 'arweave/node/lib/transaction' ; import { JWKInterface } from 'arweave/node/lib/wallet' ; param arweave param wallet param initState param contractSrc export async function simulateCreateContractFromSource ( arweave , wallet , initState , contractSrc , reward ? ) { const srcTx = await arweave . createTransaction ( { data : contractSrc , reward } , wallet ) ; srcTx . addTag ( 'App-Name' , 'SmartWeaveContractSource' ) ; srcTx . addTag ( 'App-Version' , '0.3.0' ) ; srcTx . addTag ( 'Content-Type' , 'application/javascript' ) ; await arweave . transactions . sign ( srcTx , wallet ) ; const deployInitStateTx = await simulateCreateContractFromTx ( arweave , wallet , srcTx . id , initState ) ; const initStateReward = deployInitStateTx . reward ; srcTx . reward = ( parseFloat ( srcTx . reward ) + parseFloat ( initStateReward ) ) . toString ( ) ; return srcTx ; } param arweave param wallet param srcTxId param state param tags param target param winstonQty export async function simulateCreateContractFromTx ( arweave , wallet , srcTxId , state , tags = [ ] , target = '' , winstonQty = '' , reward ? ) { let contractTX = await arweave . createTransaction ( { data : state , reward } , wallet ) ; if ( target && winstonQty && target . length && + winstonQty > 0 ) { contractTX = await arweave . createTransaction ( { data : state , target : target . toString ( ) , quantity : winstonQty . toString ( ) , reward , } , wallet ) ; } if ( tags && tags . length ) { for ( const tag of tags ) { contractTX . addTag ( tag . name . toString ( ) , tag . value . toString ( ) ) ; } } contractTX . addTag ( 'App-Name' , 'SmartWeaveContract' ) ; contractTX . addTag ( 'App-Version' , '0.3.0' ) ; contractTX . addTag ( 'Contract-Src' , srcTxId ) ; contractTX . addTag ( 'Content-Type' , 'application/json' ) ; await arweave . transactions . sign ( contractTX , wallet ) ; return contractTX ; } param arweave param wallet param contractSrc param initState export async function createContract ( arweave , wallet , contractSrc , initState , reward ? ) { const srcTx = await arweave . createTransaction ( { data : contractSrc , reward } , wallet ) ; srcTx . addTag ( 'App-Name' , 'SmartWeaveContractSource' ) ; srcTx . addTag ( 'App-Version' , '0.3.0' ) ; srcTx . addTag ( 'Content-Type' , 'application/javascript' ) ; await arweave . transactions . sign ( srcTx , wallet ) ; const response = await arweave . transactions . post ( srcTx ) ; if ( response . status === 200 || response . status === 208 ) { return await createContractFromTx ( arweave , wallet , srcTx . id , initState ) ; } else { throw new Error ( 'Unable to write Contract Source.' ) ; } } param arweave param wallet param srcTxId param state param tags param target param winstonQty export async function createContractFromTx ( arweave , wallet , srcTxId , state , tags = [ ] , target = '' , winstonQty = '' , reward ? ) { let contractTX = await arweave . createTransaction ( { data : state , reward } , wallet ) ; if ( target && winstonQty && target . length && + winstonQty > 0 ) { contractTX = await arweave . createTransaction ( { data : state , target : target . toString ( ) , quantity : winstonQty . toString ( ) , reward , } , wallet ) ; } if ( tags && tags . length ) { for ( const tag of tags ) { contractTX . addTag ( tag . name . toString ( ) , tag . value . toString ( ) ) ; } } contractTX . addTag ( 'App-Name' , 'SmartWeaveContract' ) ; contractTX . addTag ( 'App-Version' , '0.3.0' ) ; contractTX . addTag ( 'Contract-Src' , srcTxId ) ; contractTX . addTag ( 'Content-Type' , 'application/json' ) ; await arweave . transactions . sign ( contractTX , wallet ) ; const response = await arweave . transactions . post ( contractTX ) ; if ( response . status === 200 || response . status === 208 ) { return contractTX . id ; } else { throw new Error ( 'Unable to write Contract Initial State' ) ; } }