@stable-io/cctp-sdk-definitions
Version:
Definitions for the CCTP SDK
54 lines • 2.64 kB
JavaScript
// Copyright (c) 2025 Stable Technologies Inc
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
import { usdc, domainIdOf, percentage } from "../../constants/index.js";
import { apiEndpoint, apiEndpointWithQuery } from "../../constants/apis.js";
import { fetchApiResponse } from "../../api.js";
import { encoding } from "@stable-io/utils";
export const fetchFastBurnAllowanceFactory = (network) => async (ttl) => {
const endpoint = apiEndpoint(network)(2, "fastBurn", "USDC", "allowance");
const response = await fetchApiResponse(endpoint, ttl);
return {
allowance: usdc(response.value.allowance),
lastUpdated: new Date(response.value.lastUpdated),
};
};
export const fetchFastBurnFeeFactory = (network) => async (sourceDomain, destinationDomain, ttl) => {
const sourceDomainId = domainIdOf(sourceDomain);
const destinationDomainId = domainIdOf(destinationDomain);
const endpoint = apiEndpoint(network)(2, "fastBurn", "USDC", "fees", sourceDomainId.toString(10), destinationDomainId.toString(10));
const response = await fetchApiResponse(endpoint, ttl);
return { minimumFee: percentage(response.value.minimumFee, "bp") };
};
export const fetchMessagesFactory = (network) => async (sourceDomain, txHashOrNonce, ttl) => {
const sourceDomainId = domainIdOf(sourceDomain);
const endpoint = apiEndpointWithQuery(network)(2, txHashOrNonce, "messages", sourceDomainId.toString(10));
const response = await fetchApiResponse(endpoint, ttl);
if (response.status === 400) {
throw new Error("The GetMessages request was malformed");
}
if (response.status === 404) {
return { status: "not_found", ...response.value };
}
const message = response.value.messages[0];
if (message.status !== "complete")
return { status: "pending" };
return {
status: "success",
messages: [{
message: encoding.hex.decode(message.message),
eventNonce: message.eventNonce,
attestation: encoding.hex.decode(message.attestation),
decodedMessage: message.decodedMessage,
cctpVersion: message.cctpVersion,
status: message.status,
}],
};
};
export const init = (network) => ({
fetchFastBurnAllowance: fetchFastBurnAllowanceFactory(network),
fetchFastBurnFee: fetchFastBurnFeeFactory(network),
fetchMessages: fetchMessagesFactory(network),
});
//# sourceMappingURL=api.js.map