Skip to content

Commit

Permalink
Loosen auto stake activation validation
Browse files Browse the repository at this point in the history
  • Loading branch information
jmrossy committed Apr 27, 2024
1 parent 1ce6a85 commit 8d9bd84
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 14 deletions.
28 changes: 15 additions & 13 deletions src/app/staking/api/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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))
Expand All @@ -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,
Expand Down
6 changes: 5 additions & 1 deletion src/features/staking/StakeForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
Expand Down
1 change: 1 addition & 0 deletions src/features/staking/autoActivation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
});

Expand Down

0 comments on commit 8d9bd84

Please sign in to comment.