From 9aa8bc5d504b12d7b39c107bfceb0d7837e1c91c Mon Sep 17 00:00:00 2001 From: Balthazar Gronon Date: Wed, 30 Jun 2021 15:54:31 -0700 Subject: [PATCH 1/2] feat: custom readContract for contract execution --- src/contract-load.ts | 5 ++--- src/smartweave-global.ts | 14 ++++++++++++-- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/contract-load.ts b/src/contract-load.ts index 9168fa5..1a7440b 100644 --- a/src/contract-load.ts +++ b/src/contract-load.ts @@ -63,13 +63,12 @@ export function createContractExecutionEnvironment( contractSrc: string, contractId: string, contractOwner: string, + customReadContract?: any, ) { const returningSrc = normalizeContractSource(contractSrc); - const swGlobal = new SmartWeaveGlobal(arweave, { id: contractId, owner: contractOwner }); + const swGlobal = new SmartWeaveGlobal(arweave, { id: contractId, owner: contractOwner, customReadContract }); const getContractFunction = new Function(returningSrc); // eslint-disable-line - // console.log(returningSrc); - return { handler: getContractFunction(swGlobal, BigNumber, clarity) as ContractHandler, swGlobal, diff --git a/src/smartweave-global.ts b/src/smartweave-global.ts index 7ce2f0c..07258eb 100644 --- a/src/smartweave-global.ts +++ b/src/smartweave-global.ts @@ -46,7 +46,14 @@ export class SmartWeaveGlobal { return !this._activeTx; } - constructor(arweave: Arweave, contract: { id: string; owner: string }) { + constructor( + arweave: Arweave, + contract: { + id: string; + owner: string; + customReadContract: (arweave: Arweave, contractId: string, height?: number, returnValidity?: boolean) => any; + }, + ) { this.unsafeClient = arweave; this.arweave = { ar: arweave.ar, @@ -57,9 +64,12 @@ export class SmartWeaveGlobal { this.contract = contract; this.transaction = new Transaction(this); this.block = new Block(this); + + const readContractFn = contract.customReadContract || readContract; + this.contracts = { readContractState: (contractId: string, height?: number, returnValidity?: boolean) => - readContract( + readContractFn( arweave, contractId, height || (this._isDryRunning ? Number.POSITIVE_INFINITY : this.block.height), From a3b2b7176e119b6cbdc83793b470b473b57f19da Mon Sep 17 00:00:00 2001 From: Balthazar Gronon Date: Thu, 1 Jul 2021 12:52:25 -0700 Subject: [PATCH 2/2] chore: remove old param in comment for execute --- src/contract-step.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/contract-step.ts b/src/contract-step.ts index 8f042ea..331a56d 100644 --- a/src/contract-step.ts +++ b/src/contract-step.ts @@ -45,7 +45,6 @@ export interface ContractInteractionResult { * @param contractSrc the source code of the contract * @param input the input interaction, should be a plain Js object * @param state the current state of the contract - * @param caller the wallet address of the caller who is interacting with the contract */ export async function execute( handler: ContractHandler,