Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow to pass custom readContract function #88

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/contract-load.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

from my understanding - after this change different "readContract" functions might be used inside SWC code and from the SDK itself (SDK always uses built-in readContract version). Not sure if that's a good solution - shouldn't this be consistent - ie. both SDK and version exposed within SWC must the same?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

Copy link
Author

@balthazar balthazar Jul 2, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already have our own custom readContract (we do not use the SDK one), we have to use that one during contract execution.

const getContractFunction = new Function(returningSrc); // eslint-disable-line

// console.log(returningSrc);

return {
handler: getContractFunction(swGlobal, BigNumber, clarity) as ContractHandler,
swGlobal,
Expand Down
1 change: 0 additions & 1 deletion src/contract-step.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
14 changes: 12 additions & 2 deletions src/smartweave-global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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),
Expand Down