@stable-io/cctp-sdk-definitions
Version:
Definitions for the CCTP SDK
67 lines • 2.39 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 { Amount } from "@stable-io/amount";
import { UniversalAddress } from "./address.js";
import { domainOf, domainIdOf } from "./constants/chains/index.js";
export const uint256Item = {
binary: "uint", size: 32,
};
export const hashItem = {
binary: "bytes", size: 32,
};
export const universalAddressItem = {
binary: "bytes",
size: UniversalAddress.byteSize,
custom: {
to: (encoded) => new UniversalAddress(encoded),
from: (addr) => addr.toUint8Array(),
},
};
export const signatureItem = {
binary: "bytes", size: 65,
};
export const rawDomainItem = {
binary: "uint", size: 4,
};
// ----
export const domainItem = (domainTuple) => ({
...rawDomainItem,
custom: {
to: (val) => {
const domain = domainOf.get(val);
if (domain === undefined)
throw new Error(`unknown domainId ${val}`);
if (domainTuple && !domainTuple.includes(domain))
throw new Error(`Domain ${domain} not in domains ${domainTuple}`);
return domain;
},
from: (val) => domainIdOf(val),
},
});
export const fixedDomainItem = (domain) => ({
...rawDomainItem,
custom: {
to: domain,
from: domainIdOf(domain),
},
});
export function amountItem(size, kind, unitSymbol, transform) {
const numMaxSize = 6;
if (unitSymbol === undefined || unitSymbol === "atomic")
unitSymbol = kind.atomic;
const to = transform === undefined
? (val) => Amount.from(val, kind, unitSymbol)
: (val) => Amount.from(transform.to(val), kind, unitSymbol);
//sacrificing DRYness for directness
const from = (transform === undefined
? size > numMaxSize
? (amount) => amount.toUnit(unitSymbol).floor()
: (amount) => Number(amount.toUnit(unitSymbol).floor())
: size > numMaxSize
? (amount) => BigInt(transform.from(amount.toUnit(unitSymbol)))
: (amount) => Number(transform.from(amount.toUnit(unitSymbol))));
return { binary: "uint", size, custom: { to, from } };
}
//# sourceMappingURL=layoutItems.js.map