Compare commits

...

2 Commits

Author SHA1 Message Date
5e9b66fe7f wip 2025-04-25 10:01:30 +03:00
eb32dcfe55 wip 2025-04-24 10:30:44 +03:00
15 changed files with 102 additions and 76 deletions

View File

@ -81,7 +81,7 @@ export default tseslint.config(
caughtErrorsIgnorePattern: '^_', caughtErrorsIgnorePattern: '^_',
}, },
], ],
'@typescript-eslint/no-explicit-any': 'error', '@typescript-eslint/no-explicit-any': ['error', { ignoreRestArgs: true }],
'@typescript-eslint/ban-ts-comment': 'off', '@typescript-eslint/ban-ts-comment': 'off',
'@typescript-eslint/no-misused-promises': ['error', { checksVoidReturn: false }], '@typescript-eslint/no-misused-promises': ['error', { checksVoidReturn: false }],
'@typescript-eslint/no-empty-object-type': 'off', '@typescript-eslint/no-empty-object-type': 'off',

View File

@ -9,7 +9,6 @@ export const constructAddWordUiCommand = ({ word, contractId }: { word: string;
methodName: PUBLIC_METHODS.ADD_WORD, methodName: PUBLIC_METHODS.ADD_WORD,
methodArgs: [[word] satisfies AddWordArgs], methodArgs: [[word] satisfies AddWordArgs],
cost: FEE.ADD_WORD, cost: FEE.ADD_WORD,
withQueue: false,
}), }),
]); ]);
}; };

View File

@ -26,6 +26,14 @@ export const addWord = cwait(async (...[word]: AddWordArgs) => {
console.log('await storeOp'); console.log('await storeOp');
await storeOp(constructClaim(createWordKey(id), { word }, '0x0')); await storeOp(constructClaim(createWordKey(id), { word }, '0x0'));
console.log('await readOp');
const res = await readOp<TypedClaim<WordClaimBody>>(createWordKey(id));
console.log('await storeOp2');
await storeOp(
constructClaim(createWordKey(id + id), { word: (res?.body.word ?? '') + (res?.body.word ?? '') }, '0x0')
);
console.log('await extraLogic'); console.log('await extraLogic');
const wordClaim = await extraLogic(id); const wordClaim = await extraLogic(id);

View File

@ -1,25 +1,9 @@
import { SELF_REGISTER_HANDLER_NAME } from '@coinweb/contract-kit'; import { constructCwebMain } from 'cwait';
import { selfRegisterHandler } from '@coinweb/self-register';
import { addMethodHandler, ContractHandlers, executeHandler, executor } from 'cwait';
import { PUBLIC_METHODS } from '../offchain/shared'; import { PUBLIC_METHODS } from '../offchain/shared';
import { addWord } from './addWord'; import { addWord } from './addWord';
const createModule = (): ContractHandlers => { export const cwebMain = constructCwebMain({
const module: ContractHandlers = { handlers: {} }; [PUBLIC_METHODS.ADD_WORD]: addWord,
});
addMethodHandler(module, PUBLIC_METHODS.ADD_WORD, executor(addWord));
addMethodHandler(module, SELF_REGISTER_HANDLER_NAME, selfRegisterHandler);
return module;
};
export const cwebMain = () => {
return (async () => {
const module = createModule();
await executeHandler(module);
})();
};

View File

@ -0,0 +1,22 @@
import { ContractHandlers as ContractHandlersOrig, SELF_REGISTER_HANDLER_NAME } from '@coinweb/contract-kit';
import { selfRegisterHandler } from '@coinweb/self-register';
import { queue } from 'lib/onchain';
import { addMethodHandler, ContractHandlers, executeHandler } from '../contract-kit';
import { executor } from './executor';
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const constructCwebMain = (methods: Record<string, (...args: any[]) => Promise<void>>) => async () => {
const module: ContractHandlers = { handlers: {} };
Object.entries(methods).forEach(([name, handler]) => {
addMethodHandler(module, name, executor(handler));
});
addMethodHandler(module, SELF_REGISTER_HANDLER_NAME, selfRegisterHandler);
queue.applyQueue(module as ContractHandlersOrig, []);
await executeHandler(module);
};

View File

@ -1,18 +1,15 @@
import { constructStore } from '@coinweb/contract-kit/dist/esm/operations/store'; import { constructStore } from '@coinweb/contract-kit/dist/esm/operations/store';
import { constructResultClaim } from '../claims/result'; import { constructResultClaim } from './claims';
import { context } from '../context'; import { context } from './context';
import { stopExecution } from '../executor'; import { stopExecution } from './executor';
import { opMarker } from '../global'; import { opMarker } from './global';
import { isResolvedChildOp, isResolvedExecOp, isResolvedSlotOp } from '../utils'; import { getAwaitedTasks, pushAwaitedTask } from './promisifiedOps/awaited';
import { uuid } from '../utils'; import { getUsedOps, saveUsedOps, shiftResolvedOp } from './promisifiedOps/resolved';
import { isResolvedChildOp, isResolvedExecOp, isResolvedSlotOp, uuid } from './utils';
import { getAwaitedTasks, pushAwaitedTask } from './awaited';
import { getUsedOps, saveUsedOps, shiftResolvedOp } from './resolved';
let isRootDetected = false; let isRootDetected = false;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const cwait = <TAsyncCallback extends (...args: any[]) => Promise<unknown>>(asyncCallback: TAsyncCallback) => { export const cwait = <TAsyncCallback extends (...args: any[]) => Promise<unknown>>(asyncCallback: TAsyncCallback) => {
console.log('cwait: ', asyncCallback.name); console.log('cwait: ', asyncCallback.name);
let isRoot = false; let isRoot = false;

View File

@ -1,3 +1,5 @@
export * from './context'; export * from './context';
export * from './executor'; export * from './executor';
export * from './promisifiedOps'; export * from './promisifiedOps';
export * from './createContract';
export * from './cwait';

View File

@ -1,4 +1,3 @@
export * from './awaited'; export * from './awaited';
export * from './cwait';
export * from './ops'; export * from './ops';
export * from './resolved'; export * from './resolved';

View File

@ -1,2 +0,0 @@
export * from './jumpTx';
export * from './tx';

View File

@ -1 +0,0 @@
export const jumpTx = () => null;

View File

@ -1 +0,0 @@
export const tx = () => null;

View File

@ -5,11 +5,14 @@ import {
FullCallInfo, FullCallInfo,
NewTx, NewTx,
passCwebFrom, passCwebFrom,
PreparedCallInfo,
PreparedOperation, PreparedOperation,
prepareQueueCall,
sendCwebInterface, sendCwebInterface,
} from '@coinweb/contract-kit'; } from '@coinweb/contract-kit';
import { CwebBlock } from '@coinweb/contract-kit/dist/esm/operations/block'; import { CwebBlock } from '@coinweb/contract-kit/dist/esm/operations/block';
import { GBlock } from '@coinweb/contract-kit/dist/types/operations/generics'; import { GBlock } from '@coinweb/contract-kit/dist/types/operations/generics';
import { queue, queueCostFee, queueUnlockFee } from 'lib/onchain';
import { ExecutorMethodArgs, ResolvedOp, ResolvedSlotOp, Task } from '../../../types'; import { ExecutorMethodArgs, ResolvedOp, ResolvedSlotOp, Task } from '../../../types';
import { constructFundsClaimRangRead, constructFundsClaimStore } from '../../claims/funds'; import { constructFundsClaimRangRead, constructFundsClaimStore } from '../../claims/funds';
@ -38,7 +41,7 @@ export const prepareInThreadTxs = ({
const restOfAvailableCweb = availableCweb - outThreadFee; const restOfAvailableCweb = availableCweb - outThreadFee;
const restOfCweb = restOfAvailableCweb + storedCweb - BigInt(takeOps.length) * 100n - 3000n; const restOfCweb = restOfAvailableCweb + storedCweb - BigInt(takeOps.length) * 100n - 3000n - queueCostFee;
const txs = const txs =
restOfCweb > 0n restOfCweb > 0n
@ -48,8 +51,9 @@ export const prepareInThreadTxs = ({
...takeOps, ...takeOps,
...constructSendCweb(restOfCweb, user, null), ...constructSendCweb(restOfCweb, user, null),
]), ]),
...queue.gateway.unlock(getRawContext()),
] ]
: []; : queue.gateway.unlock(getRawContext());
return { txs, calls: 0 }; return { txs, calls: 0 };
} }
@ -80,8 +84,7 @@ export const prepareInThreadTxs = ({
txFee += 200n; txFee += 200n;
childCalls.push({ childCalls.push({
callInfo: { callInfo: prepareQueueCall(context.issuer, {
ref: constructContractRef(context.issuer, []),
methodInfo: { methodInfo: {
methodName: context.methodName, methodName: context.methodName,
methodArgs: [ methodArgs: [
@ -91,14 +94,15 @@ export const prepareInThreadTxs = ({
id, id,
context.thisId, context.thisId,
true, true,
[],
] satisfies ExecutorMethodArgs, ] satisfies ExecutorMethodArgs,
}, },
contractInfo: { contractInfo: {
providedCweb: cwebPerCall - 700n, providedCweb: cwebPerCall - 700n - queueCostFee,
authenticated: null, authenticated: null,
}, },
contractArgs: [], contractArgs: [],
}, }),
}); });
callsPrepared++; callsPrepared++;
@ -150,32 +154,46 @@ export const prepareInThreadTxs = ({
txFee += 800n + outThreadFee + BigInt(takeOps.length) * 100n; txFee += 800n + outThreadFee + BigInt(takeOps.length) * 100n;
callsPrepared++; callsPrepared++;
const isNeedToResetQueue = !!childCalls.length;
if (isNeedToResetQueue) {
console.log('isNeedToResetQueue');
txFee += queueUnlockFee;
txFee += queueCostFee;
}
let callInfo: PreparedCallInfo = {
ref: constructContractRef(context.issuer, []),
methodInfo: {
methodName: context.methodName,
methodArgs: [
context.initialArgs,
[...context.ops, ...resolvedSlotOps],
context.user,
context.thisId,
context.parentId,
context.needSaveResult,
takeOps.map((op) => (op.TakeOp.key.second_part as [string])[0]),
] satisfies ExecutorMethodArgs,
},
contractInfo: {
providedCweb: cwebPerCall - txFee + storedCweb,
authenticated: null,
},
contractArgs: [constructFundsClaimRangRead(context.thisId), ...callArgs],
};
if (isNeedToResetQueue) {
callInfo = prepareQueueCall(context.issuer, callInfo);
}
returnTxs.push( returnTxs.push(
constructContinueTx( constructContinueTx(
getRawContext(), getRawContext(),
[passCwebFrom(context.issuer, cwebPerCall - outThreadFee), ...takeOps], [passCwebFrom(context.issuer, cwebPerCall - outThreadFee), ...takeOps],
[ [
{ {
callInfo: { callInfo,
ref: constructContractRef(context.issuer, []),
methodInfo: {
methodName: context.methodName,
methodArgs: [
context.initialArgs,
[...context.ops, ...resolvedSlotOps],
context.user,
context.thisId,
context.parentId,
context.needSaveResult,
takeOps.map((op) => (op.TakeOp.key.second_part as [string])[0]),
] satisfies ExecutorMethodArgs,
},
contractInfo: {
providedCweb: cwebPerCall - txFee + storedCweb,
authenticated: null,
},
contractArgs: [constructFundsClaimRangRead(context.thisId), ...callArgs],
},
}, },
] ]
) )
@ -184,6 +202,10 @@ export const prepareInThreadTxs = ({
if (childCalls.length) { if (childCalls.length) {
returnTxs.push(constructContinueTx(getRawContext(), childBlocks, childCalls)); returnTxs.push(constructContinueTx(getRawContext(), childBlocks, childCalls));
} }
if (isNeedToResetQueue) {
returnTxs.push(...queue.gateway.unlock(getRawContext()));
}
} }
} }

View File

@ -1,10 +1,5 @@
import { import { constructContinueTx, FullCallInfo, NewTx, PreparedOperation, prepareQueueCall } from '@coinweb/contract-kit';
constructContinueTx, import { queueCostFee } from 'lib/onchain';
constructContractRef,
FullCallInfo,
NewTx,
PreparedOperation,
} from '@coinweb/contract-kit';
import { ExecutorMethodArgs, Task } from '../../../types'; import { ExecutorMethodArgs, Task } from '../../../types';
import { context, getRawContext } from '../../context'; import { context, getRawContext } from '../../context';
@ -40,8 +35,7 @@ export const prepareOutThreadTxs = ({
const id = task.op.ExecOp.id; const id = task.op.ExecOp.id;
const callInfo = { const callInfo = {
callInfo: { callInfo: prepareQueueCall(context.issuer, {
ref: constructContractRef(context.issuer, []),
methodInfo: { methodInfo: {
methodName: context.methodName, methodName: context.methodName,
methodArgs: [ methodArgs: [
@ -51,14 +45,15 @@ export const prepareOutThreadTxs = ({
id, id,
context.parentId ?? context.thisId, context.parentId ?? context.thisId,
false, false,
[],
] satisfies ExecutorMethodArgs, ] satisfies ExecutorMethodArgs,
}, },
contractInfo: { contractInfo: {
providedCweb: cwebPerCall - 700n, providedCweb: cwebPerCall - 700n - queueCostFee,
authenticated: null, authenticated: null,
}, },
contractArgs: [], contractArgs: [],
}, }),
}; };
if (siblingTxInfo[task.batchId]) { if (siblingTxInfo[task.batchId]) {

View File

@ -1,5 +1,7 @@
import { Queue } from '@coinweb/contract-kit'; import { Queue } from '@coinweb/contract-kit';
export const queue = new Queue({ name: 'queue', executionDepth: 2 }); export const queue = new Queue({ name: 'queue', executionDepth: 4 });
export const queueUnlockFee = 3000n; export const queueUnlockFee = 3000n;
export const queueCostFee = Queue.queueCostFee();

View File

@ -1,4 +1,4 @@
VITE_API_URL='https://api-cloud.coinweb.io/wallet' VITE_API_URL='https://api-cloud.coinweb.io/wallet'
VITE_EXPLORER_URL='https://explorer.coinweb.io' VITE_EXPLORER_URL='https://explorer.coinweb.io'
VITE_CONTRACT_ADDRESS="0x8c89cb42634cfe892290845d907af8ec2d482cead42afaef3ccc3cea4446fce5" VITE_CONTRACT_ADDRESS="0xa1bd5281d39b1182db65565d90919aff15adf65ac8ba43d984048485df5a6d8d"