From 8d9bd844a92ddc730f7101cabb416950c4e63b6a Mon Sep 17 00:00:00 2001 From: J M Rossy Date: Fri, 26 Apr 2024 20:31:59 -0400 Subject: [PATCH] Loosen auto stake activation validation --- src/app/staking/api/route.ts | 28 ++++++++++++++------------ src/features/staking/StakeForm.tsx | 6 +++++- src/features/staking/autoActivation.ts | 1 + 3 files changed, 21 insertions(+), 14 deletions(-) diff --git a/src/app/staking/api/route.ts b/src/app/staking/api/route.ts index e57eec6..cf74ef3 100644 --- a/src/app/staking/api/route.ts +++ b/src/app/staking/api/route.ts @@ -8,7 +8,7 @@ import { import { eqAddress } from 'src/utils/addresses'; import { logger } from 'src/utils/logger'; import { errorToString } from 'src/utils/strings'; -import { createPublicClient, createWalletClient, decodeEventLog, http } from 'viem'; +import { createPublicClient, createWalletClient, http } from 'viem'; import { privateKeyToAccount } from 'viem/accounts'; import { celo } from 'viem/chains'; @@ -45,11 +45,12 @@ export async function POST(request: Request) { async function activateStake(request: StakeActivationRequest) { const address = request.address as HexString; + const group = request.group as HexString; const transactionHash = request.transactionHash as HexString; const client = createPublicClient({ chain: celo, transport: http(fornoRpcUrl) }); - const transaction = await client.getTransactionReceipt({ hash: transactionHash }); + const transaction = await client.getTransaction({ hash: transactionHash }); if (!eqAddress(transaction.from, address)) throw new Error('Tx sender and request address do not match'); if (!transaction.to || !eqAddress(transaction.to, Addresses.Election)) @@ -59,18 +60,19 @@ async function activateStake(request: StakeActivationRequest) { const timePassed = Date.now() - Number(block.timestamp) * 1000; if (timePassed > 3 * 24 * 60 * 60 * 1000) throw new Error('Transaction is too old'); - const log = transaction.logs[0]; - const { eventName, args } = decodeEventLog({ - abi: electionABI, - data: log.data, - topics: log.topics, - strict: true, - }); - if (eventName !== 'ValidatorGroupVoteCast') throw new Error('Transaction is not a stake vote'); - if (!eqAddress(args.account, address)) - throw new Error('Transaction staker does not match request'); + // Used to validate tx logs but that's not strictly needed + // and fetching them for old txs was causing problems + // const log = transaction.logs[0]; + // const { eventName, args } = decodeEventLog({ + // abi: electionABI, + // data: log.data, + // topics: log.topics, + // strict: true, + // }); + // if (eventName !== 'ValidatorGroupVoteCast') throw new Error('Transaction is not a stake vote'); + // if (!eqAddress(args.account, address)) + // throw new Error('Transaction staker does not match request'); - const group = args.group; const hasActivatable = await client.readContract({ address: Addresses.Election, abi: electionABI, diff --git a/src/features/staking/StakeForm.tsx b/src/features/staking/StakeForm.tsx index b209d0b..cb8c60f 100644 --- a/src/features/staking/StakeForm.tsx +++ b/src/features/staking/StakeForm.tsx @@ -64,7 +64,11 @@ export function StakeForm({ const onPlanSuccess = (v: StakeFormValues, r: TransactionReceipt) => { if (v.action === StakeActionType.Stake) { - submitStakeActivationRequest({ address: address!, transactionHash: r.transactionHash }); + submitStakeActivationRequest({ + address: address!, + group: v.group, + transactionHash: r.transactionHash, + }); } onConfirmed({ message: `${v.action} successful`, diff --git a/src/features/staking/autoActivation.ts b/src/features/staking/autoActivation.ts index 0e99d71..e8f0ef2 100644 --- a/src/features/staking/autoActivation.ts +++ b/src/features/staking/autoActivation.ts @@ -7,6 +7,7 @@ import { z } from 'zod'; export const StakeActivationRequestSchema = z.object({ address: z.string().regex(ADDRESS_REGEX), + group: z.string().regex(ADDRESS_REGEX), transactionHash: z.string().regex(TX_HASH_REGEX), });