// Operations for managing blockchain chains
import { z } from "zod";
import { getChainsApi } from "../common/client";
import {
CurvegridResourceNotFoundError,
CurvegridError,
} from "../common/errors";
import { log } from "../common/logging";
// Schema for getting chain status
export const GetChainSchema = z.object({});
// Schema for transferring ETH
export const TransferEthSchema = z.object({
from: z.string().describe("Sender address"),
to: z.string().describe("Recipient address"),
value: z.string().describe("Amount of ETH to transfer in wei"),
gasPrice: z.number().optional().describe("Gas price in wei"),
gasLimit: z.number().optional().describe("Gas limit"),
nonce: z.number().optional().describe("Transaction nonce"),
});
// Schema for submitting a signed transaction
export const SubmitSignedTransactionSchema = z.object({
signedTx: z.string().describe("Signed transaction data (hex format)"),
});
// Schema for getting transaction details
export const GetTransactionSchema = z.object({
txHash: z.string().describe("Transaction hash"),
});
// Schema for getting transaction receipt
export const GetTransactionReceiptSchema = z.object({
txHash: z.string().describe("Transaction hash"),
});
// Schema for getting block details
export const GetBlockSchema = z.object({
blockNumber: z.string().describe('Block number or "latest"'),
});
// Get chain status
export async function getChain(options: z.infer<typeof GetChainSchema>) {
try {
const chainsApi = getChainsApi();
const response = await chainsApi.getChainStatus();
return response.data.result;
} catch (error: any) {
throw new CurvegridError(`Failed to get chain status: ${error.message}`);
}
}
// Transfer ETH
export async function transferEth(options: z.infer<typeof TransferEthSchema>) {
try {
const chainsApi = getChainsApi();
const response = await chainsApi.transferEth({
from: options.from,
to: options.to,
value: options.value,
gasPrice: options.gasPrice,
gas: options.gasLimit,
nonce: options.nonce,
});
return response.data.result;
} catch (error: any) {
throw new CurvegridError(`Failed to transfer ETH: ${error.message}`);
}
}
// Submit signed transaction
export async function submitSignedTransaction(
options: z.infer<typeof SubmitSignedTransactionSchema>,
) {
try {
const chainsApi = getChainsApi();
const response = await chainsApi.submitSignedTransaction({
signedTx: options.signedTx,
});
return response.data.result;
} catch (error: any) {
throw new CurvegridError(
`Failed to submit signed transaction: ${error.message}`,
);
}
}
// Get transaction details
export async function getTransaction(
options: z.infer<typeof GetTransactionSchema>,
) {
try {
const chainsApi = getChainsApi();
const response = await chainsApi.getTransaction(options.txHash);
return response.data.result;
} catch (error: any) {
if (error.response?.status === 404) {
throw new CurvegridResourceNotFoundError(
`Transaction ${options.txHash} not found`,
);
}
throw new CurvegridError(
`Failed to get transaction details: ${error.message}`,
);
}
}
// Get transaction receipt
export async function getTransactionReceipt(
options: z.infer<typeof GetTransactionReceiptSchema>,
) {
try {
const chainsApi = getChainsApi();
const response = await chainsApi.getTransactionReceipt(options.txHash);
return response.data.result;
} catch (error: any) {
if (error.response?.status === 404) {
throw new CurvegridResourceNotFoundError(
`Transaction receipt for ${options.txHash} not found`,
);
}
throw new CurvegridError(
`Failed to get transaction receipt: ${error.message}`,
);
}
}
// Get block details
export async function getBlock(options: z.infer<typeof GetBlockSchema>) {
try {
const chainsApi = getChainsApi();
const response = await chainsApi.getBlock(options.blockNumber);
return response.data.result;
} catch (error: any) {
if (error.response?.status === 404) {
const identifier = `number ${options.blockNumber}`;
throw new CurvegridResourceNotFoundError(
`Block with ${identifier} not found`,
);
}
throw new CurvegridError(`Failed to get block details: ${error.message}`);
}
}