fix: check self-call
This commit is contained in:
parent
d9670b314a
commit
0a753b36d0
@ -9,6 +9,7 @@ export const constructAddWordUiCommand = ({ word, contractId }: { word: string;
|
||||
methodName: PUBLIC_METHODS.ADD_WORD,
|
||||
methodArgs: [[word] satisfies AddWordArgs],
|
||||
cost: FEE.ADD_WORD,
|
||||
withQueue: false,
|
||||
}),
|
||||
]);
|
||||
};
|
||||
|
||||
@ -4,6 +4,8 @@ import { readOp, storeOp } from 'cwait';
|
||||
import { TypedClaim } from '../../../lib/dist/shared/types';
|
||||
import { AddWordArgs, createWordKey, WordClaimBody } from '../offchain/shared';
|
||||
|
||||
import { extraLogic } from './extraLogic';
|
||||
|
||||
function hashCode(str: string): string {
|
||||
let hash = 0;
|
||||
for (let i = 0, len = str.length; i < len; i++) {
|
||||
@ -21,7 +23,7 @@ export const addWord = async (...[word]: AddWordArgs) => {
|
||||
|
||||
await storeOp(constructClaim(createWordKey(id), { word }, '0x0'));
|
||||
|
||||
const wordClaim = await readOp<TypedClaim<WordClaimBody>>(createWordKey(id));
|
||||
const wordClaim = await extraLogic(id);
|
||||
|
||||
const newWord1 = (wordClaim?.body.word ?? '') + '!';
|
||||
const newId1 = hashCode(newWord1);
|
||||
|
||||
@ -1,20 +1,16 @@
|
||||
import { SELF_REGISTER_HANDLER_NAME, ContractHandlers as CKContractHandlers } from '@coinweb/contract-kit';
|
||||
import { selfRegisterHandler } from '@coinweb/self-register';
|
||||
import { addMethodHandler, ContractHandlers, executeHandler, executor, MethodCallback, selfCallWrapper } from 'cwait';
|
||||
import { addMethodHandler, ContractHandlers, executeHandler, executor } from 'cwait';
|
||||
import { queue } from 'lib/onchain';
|
||||
|
||||
import { PUBLIC_METHODS } from '../offchain/shared';
|
||||
|
||||
import { addWord } from './addWord';
|
||||
|
||||
const addWrappers = (method: MethodCallback): MethodCallback => {
|
||||
return selfCallWrapper(method);
|
||||
};
|
||||
|
||||
const createModule = (): ContractHandlers => {
|
||||
const module: ContractHandlers = { handlers: {} };
|
||||
|
||||
addMethodHandler(module, PUBLIC_METHODS.ADD_WORD, addWrappers(executor(addWord)));
|
||||
addMethodHandler(module, PUBLIC_METHODS.ADD_WORD, executor(addWord));
|
||||
|
||||
addMethodHandler(module, SELF_REGISTER_HANDLER_NAME, selfRegisterHandler);
|
||||
|
||||
|
||||
9
packages/contract.cm/src/onchain/extraLogic.ts
Normal file
9
packages/contract.cm/src/onchain/extraLogic.ts
Normal file
@ -0,0 +1,9 @@
|
||||
import { readOp, TypedClaim } from 'cwait';
|
||||
|
||||
import { createWordKey, WordClaimBody } from '../offchain';
|
||||
|
||||
export const extraLogic = async (id: string) => {
|
||||
const result = await readOp<TypedClaim<WordClaimBody>>(createWordKey(id));
|
||||
|
||||
return result;
|
||||
};
|
||||
@ -6,14 +6,15 @@ import {
|
||||
constructContinueTx,
|
||||
constructContractRef,
|
||||
constructDataUnverified,
|
||||
isSelfCall,
|
||||
} from '@coinweb/contract-kit';
|
||||
import { getCallParameters, queue } from 'lib/onchain';
|
||||
|
||||
import { ResolvedOp, Task } from '../types';
|
||||
|
||||
import { context, getRawContext, setRawContext } from './context';
|
||||
import { getAwaitedOps } from './promisifedOps/awaited';
|
||||
import { pushResolvedOp } from './promisifedOps/resolved';
|
||||
import { getAwaitedOps } from './promisifiedOps/awaited';
|
||||
import { pushResolvedOp } from './promisifiedOps/resolved';
|
||||
|
||||
let abortExecution: ((result: boolean) => void) | null = null;
|
||||
|
||||
@ -29,7 +30,12 @@ const handleState = () => {
|
||||
|
||||
pushResolvedOp(allResolvedOps);
|
||||
|
||||
return { args: initialArgs, methodName: methodArgs[0] as string, ops: allResolvedOps };
|
||||
return {
|
||||
args: initialArgs,
|
||||
methodName: methodArgs[0] as string,
|
||||
ops: allResolvedOps,
|
||||
isChildCall: previousOps.length > 0,
|
||||
};
|
||||
};
|
||||
|
||||
export const executor =
|
||||
@ -39,7 +45,11 @@ export const executor =
|
||||
console.log('executor-start');
|
||||
setRawContext(ctx);
|
||||
|
||||
const { args, methodName, ops } = handleState();
|
||||
const { args, methodName, ops, isChildCall } = handleState();
|
||||
|
||||
if (isChildCall && !isSelfCall(ctx)) {
|
||||
throw new Error('Only contract itself can call it');
|
||||
}
|
||||
|
||||
const execution = new Promise<boolean>((resolve, reject) => {
|
||||
abortExecution = resolve;
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
import { markOpBatch } from './promisifedOps/awaited';
|
||||
import { markOpBatch } from './promisifiedOps/awaited';
|
||||
|
||||
export const opMarker = Symbol('opMarker');
|
||||
|
||||
|
||||
@ -1,3 +1,3 @@
|
||||
export * from './context';
|
||||
export * from './executor';
|
||||
export * from './promisifedOps';
|
||||
export * from './promisifiedOps';
|
||||
|
||||
2
packages/cwait/src/onchain/promisifiedTxs/index.ts
Normal file
2
packages/cwait/src/onchain/promisifiedTxs/index.ts
Normal file
@ -0,0 +1,2 @@
|
||||
export * from './jumpTx';
|
||||
export * from './tx';
|
||||
1
packages/cwait/src/onchain/promisifiedTxs/jumpTx.ts
Normal file
1
packages/cwait/src/onchain/promisifiedTxs/jumpTx.ts
Normal file
@ -0,0 +1 @@
|
||||
export const jumpTx = () => null;
|
||||
1
packages/cwait/src/onchain/promisifiedTxs/tx.ts
Normal file
1
packages/cwait/src/onchain/promisifiedTxs/tx.ts
Normal file
@ -0,0 +1 @@
|
||||
export const tx = () => null;
|
||||
@ -1,4 +1,4 @@
|
||||
VITE_API_URL='https://api-cloud.coinweb.io/wallet'
|
||||
VITE_EXPLORER_URL='https://explorer.coinweb.io'
|
||||
|
||||
VITE_CONTRACT_ADDRESS="0x049eedf15331c4c39155b286aa6be8ce50a0bcab15eac345913fd084a1f32bdd"
|
||||
VITE_CONTRACT_ADDRESS="0xd0b364db47a35710320a815fbcae80435ae83e0a1730e70529f1b0b2a6b7bfd5"
|
||||
Loading…
x
Reference in New Issue
Block a user