@hyperlane-xyz/sdk
Version:
The official SDK for the Hyperlane Network
107 lines • 4.1 kB
JavaScript
import { z } from 'zod';
import { ZBigNumberish, ZChainName, ZHash, } from '../metadata/customZodTypes.js';
import { convertToBps } from './utils.js';
// Matches the enum in BaseFee.sol
export var OnchainTokenFeeType;
(function (OnchainTokenFeeType) {
OnchainTokenFeeType[OnchainTokenFeeType["LinearFee"] = 1] = "LinearFee";
OnchainTokenFeeType[OnchainTokenFeeType["RegressiveFee"] = 2] = "RegressiveFee";
OnchainTokenFeeType[OnchainTokenFeeType["ProgressiveFee"] = 3] = "ProgressiveFee";
OnchainTokenFeeType[OnchainTokenFeeType["RoutingFee"] = 4] = "RoutingFee";
})(OnchainTokenFeeType || (OnchainTokenFeeType = {}));
export var TokenFeeType;
(function (TokenFeeType) {
TokenFeeType["LinearFee"] = "LinearFee";
TokenFeeType["ProgressiveFee"] = "ProgressiveFee";
TokenFeeType["RegressiveFee"] = "RegressiveFee";
TokenFeeType["RoutingFee"] = "RoutingFee";
})(TokenFeeType || (TokenFeeType = {}));
export const ImmutableTokenFeeType = [
TokenFeeType.LinearFee,
TokenFeeType.RegressiveFee,
TokenFeeType.ProgressiveFee,
];
// Mapping between the on-chain token fee type (uint) and the token fee type (string)
export const onChainTypeToTokenFeeTypeMap = {
[OnchainTokenFeeType.LinearFee]: TokenFeeType.LinearFee,
[OnchainTokenFeeType.RegressiveFee]: TokenFeeType.RegressiveFee,
[OnchainTokenFeeType.ProgressiveFee]: TokenFeeType.ProgressiveFee,
[OnchainTokenFeeType.RoutingFee]: TokenFeeType.RoutingFee,
};
// ====== SHARED SCHEMAS ======
export const BaseFeeConfigSchema = z.object({
token: ZHash,
owner: ZHash,
});
export const FeeParametersSchema = z.object({
maxFee: ZBigNumberish,
halfAmount: ZBigNumberish,
});
const StandardFeeConfigBaseSchema = BaseFeeConfigSchema.merge(FeeParametersSchema);
// ====== INDIVIDUAL FEE SCHEMAS ======
export const LinearFeeConfigSchema = StandardFeeConfigBaseSchema.extend({
type: z.literal(TokenFeeType.LinearFee),
bps: ZBigNumberish,
});
// Linear Fee Input - only requires bps & type
export const LinearFeeInputConfigSchema = BaseFeeConfigSchema.extend({
type: z.literal(TokenFeeType.LinearFee),
bps: ZBigNumberish.optional(),
...FeeParametersSchema.partial().shape,
})
.superRefine((v, ctx) => {
const hasBps = v.bps !== undefined;
const hasFeeParams = v.maxFee !== undefined && v.halfAmount !== undefined;
if (!hasBps && !hasFeeParams) {
ctx.addIssue({
code: 'custom',
path: ['bps'],
message: 'Provide bps or both maxFee and halfAmount',
});
}
if (v.halfAmount === 0n) {
// Prevents divide by 0
ctx.addIssue({
code: 'custom',
path: ['halfAmount'],
message: 'halfAmount must be > 0',
});
}
})
.transform((v) => ({
...v,
bps: v.bps ?? convertToBps(v.maxFee, v.halfAmount),
}));
export const ProgressiveFeeConfigSchema = StandardFeeConfigBaseSchema.extend({
type: z.literal(TokenFeeType.ProgressiveFee),
});
export const RegressiveFeeConfigSchema = StandardFeeConfigBaseSchema.extend({
type: z.literal(TokenFeeType.RegressiveFee),
});
export const RoutingFeeConfigSchema = BaseFeeConfigSchema.extend({
type: z.literal(TokenFeeType.RoutingFee),
feeContracts: z
.record(ZChainName, z.lazy(() => TokenFeeConfigSchema))
.optional(), // Destination -> Fee
maxFee: ZBigNumberish.optional(),
halfAmount: ZBigNumberish.optional(),
});
export const RoutingFeeInputConfigSchema = RoutingFeeConfigSchema.extend({
feeContracts: z
.record(ZChainName, z.lazy(() => TokenFeeConfigInputSchema))
.optional(), // Destination -> Fee
});
// ====== UNION SCHEMAS ======
export const TokenFeeConfigSchema = z.discriminatedUnion('type', [
LinearFeeConfigSchema,
ProgressiveFeeConfigSchema,
RegressiveFeeConfigSchema,
RoutingFeeConfigSchema,
]);
export const TokenFeeConfigInputSchema = z.union([
LinearFeeInputConfigSchema,
ProgressiveFeeConfigSchema,
RegressiveFeeConfigSchema,
RoutingFeeInputConfigSchema,
]);
//# sourceMappingURL=types.js.map