atom-deploy/src/services/registry.ts
2025-05-02 16:44:39 -04:00

107 lines
3.4 KiB
TypeScript

import axios from 'axios';
import { CreateRecordResponse } from '../types';
export const createApplicationDeploymentRequest = async (
url: string,
txHash: string
): Promise<CreateRecordResponse> => {
try {
console.log(`Creating deployment request for URL: ${url} with transaction: ${txHash}`);
// Call our serverless API endpoint to handle the registry interaction
const response = await fetch('/api/registry', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ url, txHash }),
});
const result = await response.json();
console.log('API response:', result);
if (response.ok && result.status === 'success') {
return {
id: result.id,
applicationRecordId: result.applicationRecordId,
lrn: result.lrn,
status: 'success',
};
} else {
console.error('API error:', result);
return {
id: '',
status: 'error',
message: result.message || 'Failed to create record in Laconic Registry',
};
}
} catch (error) {
console.error('Failed to create application deployment request:', error);
return {
id: '',
status: 'error',
message: error instanceof Error ? error.message : 'Unknown error',
};
}
};
export const verifyTransaction = async (txHash: string): Promise<boolean> => {
try {
// Use the public Cosmos RPC URL for verification
const rpcEndpoint = process.env.NEXT_PUBLIC_COSMOS_RPC_URL;
if (!rpcEndpoint) {
console.error('NEXT_PUBLIC_COSMOS_RPC_URL environment variable not set');
return false;
}
// Use Axios to directly query the Cosmos transaction
const response = await axios.get(`${rpcEndpoint}/cosmos/tx/v1beta1/txs/${txHash}`);
// Check if transaction exists and was successful
// The Cosmos API returns a tx_response object with a code field - 0 means success
if (response.data &&
response.data.tx_response &&
response.data.tx_response.code === 0) {
return true;
}
// Also check for successful transactions with code === undefined (some nodes report it this way)
if (response.data &&
response.data.tx_response &&
response.data.tx_response.code === undefined &&
response.data.tx_response.height) {
return true;
}
// Also fallback to checking if the transaction has a height (was included in a block)
if (response.data &&
response.data.tx_response &&
response.data.tx_response.height &&
!response.data.tx_response.code) {
return true;
}
return false;
} catch (error) {
console.error('Failed to verify transaction:', error);
// If the API call fails, try checking a public explorer API as fallback
try {
// Try a different URL format that some RPC nodes might use
const rpcEndpoint = process.env.NEXT_PUBLIC_COSMOS_RPC_URL;
const fallbackResponse = await axios.get(`${rpcEndpoint}/tx?hash=0x${txHash}`);
if (fallbackResponse.data &&
fallbackResponse.data.result &&
(fallbackResponse.data.result.height ||
(fallbackResponse.data.result.tx_result &&
fallbackResponse.data.result.tx_result.code === 0))) {
return true;
}
} catch (fallbackError) {
console.error('Fallback verification also failed:', fallbackError);
}
return false;
}
};