uatom. string

This commit is contained in:
zramsay 2025-05-02 14:14:09 -04:00
parent a8be189229
commit 429bb62cf1
3 changed files with 16 additions and 11 deletions

View File

@ -33,7 +33,7 @@ export async function verifyAtomPayment(
): Promise<{ ): Promise<{
valid: boolean, valid: boolean,
reason?: string, reason?: string,
amount?: number, amount?: string,
sender?: string, sender?: string,
alreadyUsed?: boolean alreadyUsed?: boolean
}> { }> {
@ -72,7 +72,7 @@ export async function verifyAtomPayment(
// Return cached verification result // Return cached verification result
return { return {
valid: true, valid: true,
amount: txData.amount, amount: `${txData.amount}uatom`,
sender: txData.sender, sender: txData.sender,
alreadyUsed: txData.used alreadyUsed: txData.used
}; };
@ -103,7 +103,7 @@ export async function verifyAtomPayment(
// Extract the payment details // Extract the payment details
const tx = response.data.tx; const tx = response.data.tx;
let foundValidPayment = false; let foundValidPayment = false;
let paymentAmount = 0; let paymentAmountUAtom = '';
let sender = ''; let sender = '';
// Get the sender address from the first signer // Get the sender address from the first signer
@ -116,9 +116,14 @@ export async function verifyAtomPayment(
if (msg['@type'] === '/cosmos.bank.v1beta1.MsgSend') { if (msg['@type'] === '/cosmos.bank.v1beta1.MsgSend') {
if (msg.to_address === Config.ATOM_PAYMENT_ADDRESS) { if (msg.to_address === Config.ATOM_PAYMENT_ADDRESS) {
for (const coin of msg.amount) { for (const coin of msg.amount) {
if (coin.denom === 'uatom') { // uatom is the micro ATOM denomination (1 ATOM = 1,000,000 uatom) if (coin.denom === 'uatom') {
paymentAmount = parseInt(coin.amount) / 1000000; // Convert from uatom to ATOM // Get the amount in uatom
if (paymentAmount >= requiredAmount) { paymentAmountUAtom = coin.amount;
// Extract the required amount as an integer from the format like "10uatom"
const requiredAmountUAtom = parseInt(requiredAmount.replace(/[^0-9]/g, ''));
if (parseInt(paymentAmountUAtom) >= requiredAmountUAtom) {
foundValidPayment = true; foundValidPayment = true;
} }
break; break;
@ -131,21 +136,21 @@ export async function verifyAtomPayment(
if (!foundValidPayment) { if (!foundValidPayment) {
return { return {
valid: false, valid: false,
reason: `Payment amount (${paymentAmount} ATOM) is less than required (${requiredAmount} ATOM) or not sent to the correct address` reason: `Payment amount (${paymentAmountUAtom || '0'}uatom) is less than required (${requiredAmount}) or not sent to the correct address`
}; };
} }
// Cache the verification result // Cache the verification result
verifiedTransactions.set(txHash, { verifiedTransactions.set(txHash, {
timestamp: Date.now(), timestamp: Date.now(),
amount: paymentAmount, amount: parseInt(paymentAmountUAtom),
sender, sender,
used: markAsUsed used: markAsUsed
}); });
return { return {
valid: true, valid: true,
amount: paymentAmount, amount: `${paymentAmountUAtom}uatom`,
sender, sender,
alreadyUsed: markAsUsed alreadyUsed: markAsUsed
}; };

View File

@ -24,7 +24,7 @@ export const Config = {
OPENPGP_PRIVATE_KEY_FILE: process.env.OPENPGP_PRIVATE_KEY_FILE, OPENPGP_PRIVATE_KEY_FILE: process.env.OPENPGP_PRIVATE_KEY_FILE,
// Cosmos ATOM payment configuration // Cosmos ATOM payment configuration
ATOM_PAYMENT_ADDRESS: process.env.ATOM_PAYMENT_ADDRESS || '', ATOM_PAYMENT_ADDRESS: process.env.ATOM_PAYMENT_ADDRESS || '',
MIN_ATOM_PAYMENT: parseInt(process.env.MIN_ATOM_PAYMENT || '1'), MIN_ATOM_PAYMENT: process.env.MIN_ATOM_PAYMENT || '1000000uatom',
COSMOS_RPC_ENDPOINT: process.env.COSMOS_RPC_ENDPOINT || '', COSMOS_RPC_ENDPOINT: process.env.COSMOS_RPC_ENDPOINT || '',
}; };

View File

@ -137,7 +137,7 @@ export class RegHelper {
const verification = await verifyAtomPayment(request.attributes.payment, minAtomPayment); const verification = await verifyAtomPayment(request.attributes.payment, minAtomPayment);
if (verification.valid) { if (verification.valid) {
console.log(`ATOM payment verified for request ${request.id} - Amount: ${verification.amount} ATOM`); console.log(`ATOM payment verified for request ${request.id} - Amount: ${verification.amount}`);
return true; return true;
} else { } else {
console.log(`ATOM payment verification failed for request ${request.id}: ${verification.reason}`); console.log(`ATOM payment verification failed for request ${request.id}: ${verification.reason}`);