-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #696 from subspace/main
Sync prod with main
- Loading branch information
Showing
29 changed files
with
905 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
import { AuthProvider } from '@/constants' | ||
import { balance, transfer } from '@autonomys/auto-consensus' | ||
import { ActivateWalletInput, activateWallet } from '@autonomys/auto-utils' | ||
import type { ApiPromise } from '@polkadot/api' | ||
import { stringToU8a } from '@polkadot/util' | ||
import { decodeAddress, signatureVerify } from '@polkadot/util-crypto' | ||
import { chains } from 'constants/chains' | ||
import { CLAIM_TYPES } from 'constants/routes' | ||
import { NextRequest, NextResponse } from 'next/server' | ||
import { verifyToken } from 'utils/auth/verifyToken' | ||
import { | ||
findClaim, | ||
findClaimStats, | ||
findUserByID, | ||
saveClaim, | ||
saveClaimStats, | ||
updateClaimStats, | ||
updateUser, | ||
} from 'utils/fauna' | ||
import { formatUnitsToNumber } from 'utils/number' | ||
import { sendSlackStatsMessage, walletBalanceLowSlackMessage } from 'utils/slack' | ||
|
||
export const POST = async (req: NextRequest) => { | ||
try { | ||
if (!process.env.WALLET_CLAIM_OPERATOR_DISBURSEMENT_URI) | ||
throw new Error('Missing WALLET_CLAIM_OPERATOR_DISBURSEMENT_URI') | ||
if ( | ||
!process.env.CLAIM_OPERATOR_DISBURSEMENT_AMOUNT && | ||
process.env.CLAIM_OPERATOR_DISBURSEMENT_AMOUNT !== '0' | ||
) | ||
throw new Error('Missing CLAIM_OPERATOR_DISBURSEMENT_AMOUNT') | ||
|
||
const session = verifyToken() | ||
console.log('session', session) | ||
|
||
const dbSession = await findUserByID(session.id) | ||
console.log('dbSession', dbSession) | ||
|
||
if (!dbSession) return NextResponse.json({ error: 'User not found' }, { status: 404 }) | ||
|
||
const pathname = req.nextUrl.pathname | ||
const chain = pathname.split('/').slice(3)[0] | ||
const claimType = pathname.split('/').slice(4)[0] | ||
if (claimType !== CLAIM_TYPES.OperatorDisbursement) | ||
return NextResponse.json({ error: 'Invalid claim type' }, { status: 400 }) | ||
|
||
const chainMatch = chains.find((c) => c.urls.page === chain) | ||
if (!chainMatch) return NextResponse.json({ error: 'Invalid chain' }, { status: 400 }) | ||
|
||
const previousClaim = await findClaim(session.id, chainMatch.urls.page, claimType) | ||
if (previousClaim) return NextResponse.json({ error: 'Already claimed' }, { status: 400 }) | ||
|
||
const claim = await req.json() | ||
const { message, signature, address } = claim | ||
|
||
// Verify the signature | ||
const publicKey = decodeAddress(address) | ||
const isValid = signatureVerify(stringToU8a(message), signature, publicKey).isValid | ||
if (!isValid) return NextResponse.json({ error: 'Invalid signature' }, { status: 400 }) | ||
|
||
const claimStats = await findClaimStats(chainMatch.urls.page, claimType) | ||
|
||
const { | ||
api, | ||
accounts: [wallet], | ||
} = await activateWallet({ | ||
uri: process.env.WALLET_CLAIM_OPERATOR_DISBURSEMENT_URI, | ||
networkId: 'autonomys-' + chainMatch.urls.page, | ||
} as ActivateWalletInput) | ||
|
||
// Get wallet free balance | ||
const { free } = await balance(api as unknown as ApiPromise, wallet.address) | ||
if (BigInt(free) < BigInt(process.env.CLAIM_WALLET_LOW_FUND_WARNING || 1000 * 10 ** 18)) | ||
await walletBalanceLowSlackMessage( | ||
formatUnitsToNumber(free.toString()).toString(), | ||
wallet.address, | ||
) | ||
|
||
if (BigInt(free) <= BigInt(process.env.CLAIM_OPERATOR_DISBURSEMENT_AMOUNT)) | ||
return NextResponse.json({ error: 'Insufficient funds' }, { status: 400 }) | ||
|
||
// Create and sign the transfer transaction | ||
const block = await api.rpc.chain.getBlock() | ||
const tx = await transfer( | ||
api as unknown as ApiPromise, | ||
address, | ||
process.env.CLAIM_OPERATOR_DISBURSEMENT_AMOUNT, | ||
) | ||
const hash = await tx.signAndSend(wallet) | ||
const txReceipt = { | ||
ownerAccount: wallet.address, | ||
status: 'pending', | ||
submittedAtBlockHash: block.block.header.hash.toHex(), | ||
submittedAtBlockNumber: block.block.header.number.toNumber(), | ||
call: 'balances.transferKeepAlive', | ||
txHash: hash.hash.toHex(), | ||
blockHash: '', | ||
} | ||
|
||
await saveClaim(session, chainMatch.urls.page, claimType, claim, txReceipt) | ||
|
||
if (!claimStats) { | ||
const slackMessage = await sendSlackStatsMessage(1) | ||
if (slackMessage) await saveClaimStats(session, chainMatch.urls.page, claimType, slackMessage) | ||
} else { | ||
await sendSlackStatsMessage( | ||
claimStats[0].data.totalClaims + 1, | ||
claimStats[0].data.slackMessageId, | ||
) | ||
await updateClaimStats(claimStats[0].ref, claimStats[0].data, session) | ||
} | ||
|
||
await api.disconnect() | ||
|
||
await updateUser(dbSession[0].ref, dbSession[0].data, AuthProvider.subspace, { | ||
...dbSession[0].data.subspace, | ||
disbursements: | ||
dbSession[0].data.subspace && dbSession[0].data.subspace.disbursements | ||
? { | ||
...dbSession[0].data.subspace.disbursements, | ||
stakeWars2: true, | ||
} | ||
: { | ||
stakeWars2: true, | ||
}, | ||
}) | ||
|
||
return NextResponse.json({ | ||
message: 'Disbursement claimed successfully', | ||
hash: hash.hash.toHex(), | ||
}) | ||
} catch (error) { | ||
console.error('Error processing disbursement:', error) | ||
return NextResponse.json({ error: 'Failed to claim disbursement' }, { status: 500 }) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.