From 90c7367809737d1da253a1e6a3c561de93fef700 Mon Sep 17 00:00:00 2001 From: dtfiedler Date: Mon, 17 Jun 2024 08:00:57 -0700 Subject: [PATCH 1/2] chore: add ts-ignore for not all code paths return value warning --- src/contract.ts | 152 ++++++++++++++++++++++++++---------------------- 1 file changed, 82 insertions(+), 70 deletions(-) diff --git a/src/contract.ts b/src/contract.ts index d566f191..0e6db0c3 100644 --- a/src/contract.ts +++ b/src/contract.ts @@ -6,27 +6,27 @@ import { getGateway, getGateways } from './actions/read/gateways'; import { getEpoch, getPrescribedObservers } from './actions/read/observers'; import { getPriceForInteraction } from './actions/read/price'; import { getRecord } from './actions/read/record'; -import { buyRecord } from './actions/write/buyRecord'; -import { createReservedName } from './actions/write/createReservedName'; -import { createVault } from './actions/write/createVault'; -import { decreaseDelegateStake } from './actions/write/decreaseDelegateStake'; -import { decreaseOperatorStake } from './actions/write/decreaseOperatorStake'; -import { delegateStake } from './actions/write/delegateStake'; +// import { buyRecord } from './actions/write/buyRecord'; +// import { createReservedName } from './actions/write/createReservedName'; +// import { createVault } from './actions/write/createVault'; +// import { decreaseDelegateStake } from './actions/write/decreaseDelegateStake'; +// import { decreaseOperatorStake } from './actions/write/decreaseOperatorStake'; +// import { delegateStake } from './actions/write/delegateStake'; import { evolve } from './actions/write/evolve'; import { evolveState } from './actions/write/evolveState'; -import { extendRecord } from './actions/write/extendRecord'; -import { extendVault } from './actions/write/extendVault'; -import { increaseOperatorStake } from './actions/write/increaseOperatorStake'; -import { increaseUndernameCount } from './actions/write/increaseUndernameCount'; -import { increaseVault } from './actions/write/increaseVault'; -import { joinNetwork } from './actions/write/joinNetwork'; -import { leaveNetwork } from './actions/write/leaveNetwork'; -import { saveObservations } from './actions/write/saveObservations'; -import { submitAuctionBid } from './actions/write/submitAuctionBid'; +// import { extendRecord } from './actions/write/extendRecord'; +// import { extendVault } from './actions/write/extendVault'; +// import { increaseOperatorStake } from './actions/write/increaseOperatorStake'; +// import { increaseUndernameCount } from './actions/write/increaseUndernameCount'; +// import { increaseVault } from './actions/write/increaseVault'; +// import { joinNetwork } from './actions/write/joinNetwork'; +// import { leaveNetwork } from './actions/write/leaveNetwork'; +// import { saveObservations } from './actions/write/saveObservations'; +// import { submitAuctionBid } from './actions/write/submitAuctionBid'; import { tick } from './actions/write/tick'; -import { transferTokens } from './actions/write/transferTokens'; -import { updateGatewaySettings } from './actions/write/updateGatewaySettings'; -import { vaultedTransfer } from './actions/write/vaultedTransfer'; +// import { transferTokens } from './actions/write/transferTokens'; +// import { updateGatewaySettings } from './actions/write/updateGatewaySettings'; +// import { vaultedTransfer } from './actions/write/vaultedTransfer'; import { ContractReadResult, ContractWriteResult, @@ -35,9 +35,21 @@ import { PstAction, } from './types'; +export const FORKED_AO_BLOCK_HEIGHT = 144900; // approximately July 21, 2024 + +export const throwForkedError = (blockHeight: number): void => { + if (blockHeight >= FORKED_AO_BLOCK_HEIGHT) { + throw new ContractError( + `This contract is being forked to AO. Write interactions are disabled. Follow along for migration updates: https://twitter.com/ar_io_network`, + ); + } +}; + export async function handle( state: IOState, action: PstAction, + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-ignore ): Promise { const input = action.input; @@ -58,11 +70,9 @@ export async function handle( // all the remaining interactions require a ticked state, even when reading, so users get the most recent evaluation const { state: tickedState } = await tick(state); - switch (input.function as IOContractFunctions) { - // owner wallet functions - case 'createReservedName': - return createReservedName(tickedState, action); + const blockHeight = SmartWeave.block.height; + switch (input.function as IOContractFunctions) { // registry read interactions case 'gateway': return getGateway(tickedState, action); @@ -70,34 +80,10 @@ export async function handle( return getGateways(tickedState); case 'prescribedObservers': return getPrescribedObservers(tickedState); - case 'delegateStake': - return delegateStake(tickedState, action); - case 'decreaseDelegateStake': - return decreaseDelegateStake(tickedState, action); - - // registry write interactions - case 'joinNetwork': - return joinNetwork(tickedState, action); - case 'leaveNetwork': - return leaveNetwork(tickedState, action); - case 'increaseOperatorStake': - return increaseOperatorStake(tickedState, action); - case 'decreaseOperatorStake': - return decreaseOperatorStake(tickedState, action); - case 'updateGatewaySettings': - return updateGatewaySettings(tickedState, action); - case 'saveObservations': - return saveObservations(tickedState, action); - - // arns write interactions - case 'submitAuctionBid': - return submitAuctionBid(tickedState, action); - case 'buyRecord': - return buyRecord(tickedState, action); - case 'extendRecord': - return extendRecord(tickedState, action); - case 'increaseUndernameCount': - return increaseUndernameCount(tickedState, action); + // case 'delegateStake': + // return delegateStake(tickedState, action); + // case 'decreaseDelegateStake': + // return decreaseDelegateStake(tickedState, action); // arns read interactions case 'record': @@ -105,35 +91,61 @@ export async function handle( case 'auction': return getAuction(tickedState, action); - // balance write interactions - case 'transfer': - return transferTokens(tickedState, action); - case 'vaultedTransfer': - return vaultedTransfer(tickedState, action); - case 'createVault': - return createVault(tickedState, action); - case 'extendVault': - return extendVault(tickedState, action); - case 'increaseVault': - return increaseVault(tickedState, action); - // balance read interactions case 'balance': return balance(tickedState, action); - // utility write interactions - case 'tick': - // we already ticked, so just return the state - return { state: tickedState }; - // utility read interactions case 'epoch': return getEpoch(tickedState, action); case 'priceForInteraction': return getPriceForInteraction(tickedState, action); + + // owner wallet functions + // case 'createReservedName': + // return createReservedName(tickedState, action); + + // registry write interactions + // case 'joinNetwork': + // throwForkedError(blockHeight); + // return joinNetwork(tickedState, action); + // case 'leaveNetwork': + // throwForkedError(blockHeight); + // return leaveNetwork(tickedState, action); + // case 'increaseOperatorStake': + // throwForkedError(blockHeight); + // return increaseOperatorStake(tickedState, action); + // case 'decreaseOperatorStake': + // throwForkedError(blockHeight); + // return decreaseOperatorStake(tickedState, action); + // case 'updateGatewaySettings': + // return updateGatewaySettings(tickedState, action); + // case 'saveObservations': + // return saveObservations(tickedState, action); + + // arns write interactions + // case 'submitAuctionBid': + // return submitAuctionBid(tickedState, action); + // case 'buyRecord': + // return buyRecord(tickedState, action); + // case 'extendRecord': + // return extendRecord(tickedState, action); + // case 'increaseUndernameCount': + // return increaseUndernameCount(tickedState, action); + + // balance write interactions + // case 'transfer': + // return transferTokens(tickedState, action); + // case 'vaultedTransfer': + // return vaultedTransfer(tickedState, action); + // case 'createVault': + // return createVault(tickedState, action); + // case 'extendVault': + // return extendVault(tickedState, action); + // case 'increaseVault': + // return increaseVault(tickedState, action); + default: - throw new ContractError( - `No function supplied or function not recognized: "${input.function}"`, - ); + throwForkedError(blockHeight); } } From 2e3d315c03f41678cde087d26bb67953d1f9a6b6 Mon Sep 17 00:00:00 2001 From: dtfiedler Date: Mon, 17 Jun 2024 08:05:40 -0700 Subject: [PATCH 2/2] fix(contract): move write interaction block before switch --- src/contract.ts | 176 +++++++++++++++++++++++++++--------------------- 1 file changed, 98 insertions(+), 78 deletions(-) diff --git a/src/contract.ts b/src/contract.ts index 0e6db0c3..efc14930 100644 --- a/src/contract.ts +++ b/src/contract.ts @@ -6,27 +6,27 @@ import { getGateway, getGateways } from './actions/read/gateways'; import { getEpoch, getPrescribedObservers } from './actions/read/observers'; import { getPriceForInteraction } from './actions/read/price'; import { getRecord } from './actions/read/record'; -// import { buyRecord } from './actions/write/buyRecord'; -// import { createReservedName } from './actions/write/createReservedName'; -// import { createVault } from './actions/write/createVault'; -// import { decreaseDelegateStake } from './actions/write/decreaseDelegateStake'; -// import { decreaseOperatorStake } from './actions/write/decreaseOperatorStake'; -// import { delegateStake } from './actions/write/delegateStake'; +import { buyRecord } from './actions/write/buyRecord'; +import { createReservedName } from './actions/write/createReservedName'; +import { createVault } from './actions/write/createVault'; +import { decreaseDelegateStake } from './actions/write/decreaseDelegateStake'; +import { decreaseOperatorStake } from './actions/write/decreaseOperatorStake'; +import { delegateStake } from './actions/write/delegateStake'; import { evolve } from './actions/write/evolve'; import { evolveState } from './actions/write/evolveState'; -// import { extendRecord } from './actions/write/extendRecord'; -// import { extendVault } from './actions/write/extendVault'; -// import { increaseOperatorStake } from './actions/write/increaseOperatorStake'; -// import { increaseUndernameCount } from './actions/write/increaseUndernameCount'; -// import { increaseVault } from './actions/write/increaseVault'; -// import { joinNetwork } from './actions/write/joinNetwork'; -// import { leaveNetwork } from './actions/write/leaveNetwork'; -// import { saveObservations } from './actions/write/saveObservations'; -// import { submitAuctionBid } from './actions/write/submitAuctionBid'; +import { extendRecord } from './actions/write/extendRecord'; +import { extendVault } from './actions/write/extendVault'; +import { increaseOperatorStake } from './actions/write/increaseOperatorStake'; +import { increaseUndernameCount } from './actions/write/increaseUndernameCount'; +import { increaseVault } from './actions/write/increaseVault'; +import { joinNetwork } from './actions/write/joinNetwork'; +import { leaveNetwork } from './actions/write/leaveNetwork'; +import { saveObservations } from './actions/write/saveObservations'; +import { submitAuctionBid } from './actions/write/submitAuctionBid'; import { tick } from './actions/write/tick'; -// import { transferTokens } from './actions/write/transferTokens'; -// import { updateGatewaySettings } from './actions/write/updateGatewaySettings'; -// import { vaultedTransfer } from './actions/write/vaultedTransfer'; +import { transferTokens } from './actions/write/transferTokens'; +import { updateGatewaySettings } from './actions/write/updateGatewaySettings'; +import { vaultedTransfer } from './actions/write/vaultedTransfer'; import { ContractReadResult, ContractWriteResult, @@ -37,19 +37,27 @@ import { export const FORKED_AO_BLOCK_HEIGHT = 144900; // approximately July 21, 2024 -export const throwForkedError = (blockHeight: number): void => { - if (blockHeight >= FORKED_AO_BLOCK_HEIGHT) { - throw new ContractError( - `This contract is being forked to AO. Write interactions are disabled. Follow along for migration updates: https://twitter.com/ar_io_network`, - ); - } -}; +const writeInteractions = [ + 'joinNetwork', + 'leaveNetwork', + 'increaseOperatorStake', + 'decreaseOperatorStake', + 'updateGatewaySettings', + 'saveObservations', + 'submitAuctionBid', + 'buyRecord', + 'extendRecord', + 'increaseUndernameCount', + 'transfer', + 'vaultedTransfer', + 'createVault', + 'extendVault', + 'increaseVault', +]; export async function handle( state: IOState, action: PstAction, - // eslint-disable-next-line @typescript-eslint/ban-ts-comment - // @ts-ignore ): Promise { const input = action.input; @@ -70,9 +78,23 @@ export async function handle( // all the remaining interactions require a ticked state, even when reading, so users get the most recent evaluation const { state: tickedState } = await tick(state); - const blockHeight = SmartWeave.block.height; + const blockHeight = +SmartWeave.block.height; + + // check if it's a write interaction and if it's being called after the forked block height + if ( + writeInteractions.includes(input.function) && + blockHeight >= FORKED_AO_BLOCK_HEIGHT + ) { + throw new ContractError( + `This contract is being forked to AO. Write interactions are disabled. Follow along for migration updates: https://twitter.com/ar_io_network`, + ); + } switch (input.function as IOContractFunctions) { + // owner wallet functions + case 'createReservedName': + return createReservedName(tickedState, action); + // registry read interactions case 'gateway': return getGateway(tickedState, action); @@ -80,10 +102,34 @@ export async function handle( return getGateways(tickedState); case 'prescribedObservers': return getPrescribedObservers(tickedState); - // case 'delegateStake': - // return delegateStake(tickedState, action); - // case 'decreaseDelegateStake': - // return decreaseDelegateStake(tickedState, action); + case 'delegateStake': + return delegateStake(tickedState, action); + case 'decreaseDelegateStake': + return decreaseDelegateStake(tickedState, action); + + // registry write interactions + case 'joinNetwork': + return joinNetwork(tickedState, action); + case 'leaveNetwork': + return leaveNetwork(tickedState, action); + case 'increaseOperatorStake': + return increaseOperatorStake(tickedState, action); + case 'decreaseOperatorStake': + return decreaseOperatorStake(tickedState, action); + case 'updateGatewaySettings': + return updateGatewaySettings(tickedState, action); + case 'saveObservations': + return saveObservations(tickedState, action); + + // arns write interactions + case 'submitAuctionBid': + return submitAuctionBid(tickedState, action); + case 'buyRecord': + return buyRecord(tickedState, action); + case 'extendRecord': + return extendRecord(tickedState, action); + case 'increaseUndernameCount': + return increaseUndernameCount(tickedState, action); // arns read interactions case 'record': @@ -91,61 +137,35 @@ export async function handle( case 'auction': return getAuction(tickedState, action); + // balance write interactions + case 'transfer': + return transferTokens(tickedState, action); + case 'vaultedTransfer': + return vaultedTransfer(tickedState, action); + case 'createVault': + return createVault(tickedState, action); + case 'extendVault': + return extendVault(tickedState, action); + case 'increaseVault': + return increaseVault(tickedState, action); + // balance read interactions case 'balance': return balance(tickedState, action); + // utility write interactions + case 'tick': + // we already ticked, so just return the state + return { state: tickedState }; + // utility read interactions case 'epoch': return getEpoch(tickedState, action); case 'priceForInteraction': return getPriceForInteraction(tickedState, action); - - // owner wallet functions - // case 'createReservedName': - // return createReservedName(tickedState, action); - - // registry write interactions - // case 'joinNetwork': - // throwForkedError(blockHeight); - // return joinNetwork(tickedState, action); - // case 'leaveNetwork': - // throwForkedError(blockHeight); - // return leaveNetwork(tickedState, action); - // case 'increaseOperatorStake': - // throwForkedError(blockHeight); - // return increaseOperatorStake(tickedState, action); - // case 'decreaseOperatorStake': - // throwForkedError(blockHeight); - // return decreaseOperatorStake(tickedState, action); - // case 'updateGatewaySettings': - // return updateGatewaySettings(tickedState, action); - // case 'saveObservations': - // return saveObservations(tickedState, action); - - // arns write interactions - // case 'submitAuctionBid': - // return submitAuctionBid(tickedState, action); - // case 'buyRecord': - // return buyRecord(tickedState, action); - // case 'extendRecord': - // return extendRecord(tickedState, action); - // case 'increaseUndernameCount': - // return increaseUndernameCount(tickedState, action); - - // balance write interactions - // case 'transfer': - // return transferTokens(tickedState, action); - // case 'vaultedTransfer': - // return vaultedTransfer(tickedState, action); - // case 'createVault': - // return createVault(tickedState, action); - // case 'extendVault': - // return extendVault(tickedState, action); - // case 'increaseVault': - // return increaseVault(tickedState, action); - default: - throwForkedError(blockHeight); + throw new ContractError( + `No function supplied or function not recognized: "${input.function}"`, + ); } }