@lifi/compose-spec 0.15.2 → 0.16.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +25 -14
- package/dist/index.d.cts +24 -12
- package/dist/index.d.ts +24 -12
- package/dist/index.js +21 -13
- package/package.json +1 -1
- package/src/flashloan.ts +61 -29
- package/src/index.ts +5 -2
- package/src/resource.ts +7 -0
package/dist/index.cjs
CHANGED
|
@@ -484,30 +484,38 @@ const resourceKey = (r) => foldResource(r, {
|
|
|
484
484
|
native: ({ chainId }) => `native:${chainId}`,
|
|
485
485
|
erc20: ({ token, chainId }) => `erc20:${token.toLowerCase()}:${chainId}`
|
|
486
486
|
});
|
|
487
|
+
const describeResource = (r) => foldResource(r, {
|
|
488
|
+
native: ({ chainId }) => `native coin on chain ${chainId}`,
|
|
489
|
+
erc20: ({ token, chainId }) => `${token} on chain ${chainId}`
|
|
490
|
+
});
|
|
487
491
|
const erc20Token = (r) => {
|
|
488
492
|
if (r.kind !== "erc20") throw new Error(`Expected erc20 resource, got ${r.kind}`);
|
|
489
493
|
return r.token;
|
|
490
494
|
};
|
|
491
495
|
//#endregion
|
|
492
496
|
//#region src/flashloan.ts
|
|
493
|
-
const PROVIDER_KIND = {
|
|
494
|
-
AAVE_V3: 0,
|
|
495
|
-
ERC3156: 1,
|
|
496
|
-
BALANCER_V2: 2,
|
|
497
|
-
MORPHO_BLUE: 3
|
|
498
|
-
};
|
|
499
497
|
const providerKindNames = [
|
|
500
498
|
"aave-v3",
|
|
501
|
-
"erc3156",
|
|
502
499
|
"balancer-v2",
|
|
503
500
|
"morpho-blue"
|
|
504
501
|
];
|
|
505
|
-
const
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
502
|
+
const FlashloanConfigSchema = effect.Schema.Struct({
|
|
503
|
+
providerKind: effect.Schema.Literal(...providerKindNames),
|
|
504
|
+
amount: IntegerString,
|
|
505
|
+
maxFee: effect.Schema.optional(IntegerString)
|
|
506
|
+
});
|
|
507
|
+
const FlashloanMaterialiserInputSchema = effect.Schema.Struct({
|
|
508
|
+
kind: effect.Schema.Literal("flashloan"),
|
|
509
|
+
...FlashloanConfigSchema.fields
|
|
510
|
+
});
|
|
511
|
+
const isFlashloanMaterialiserInput = effect.Schema.is(FlashloanMaterialiserInputSchema);
|
|
512
|
+
const FLASHLOAN_FEE_BPS = {
|
|
513
|
+
"aave-v3": 5n,
|
|
514
|
+
"balancer-v2": 0n,
|
|
515
|
+
"morpho-blue": 0n
|
|
510
516
|
};
|
|
517
|
+
const BPS_DENOMINATOR = 10000n;
|
|
518
|
+
const flashloanMaxFee = ({ providerKind, amount, maxFee }) => maxFee !== void 0 ? BigInt(maxFee) : (BigInt(amount) * FLASHLOAN_FEE_BPS[providerKind] + BPS_DENOMINATOR - 1n) / BPS_DENOMINATOR;
|
|
511
519
|
//#endregion
|
|
512
520
|
//#region src/constructors.ts
|
|
513
521
|
/** Returns `{ [key]: value }` when `value` is not nullish, else `{}`. */
|
|
@@ -1001,6 +1009,8 @@ exports.EVM_NATIVE_SENTINEL = EVM_NATIVE_SENTINEL;
|
|
|
1001
1009
|
exports.EpilogueForwardSchema = EpilogueForwardSchema;
|
|
1002
1010
|
exports.EpilogueSchema = EpilogueSchema;
|
|
1003
1011
|
exports.Erc20BalanceGteSpecSchema = Erc20BalanceGteSpecSchema;
|
|
1012
|
+
exports.FlashloanConfigSchema = FlashloanConfigSchema;
|
|
1013
|
+
exports.FlashloanMaterialiserInputSchema = FlashloanMaterialiserInputSchema;
|
|
1004
1014
|
exports.FlowInputSchema = FlowInputSchema;
|
|
1005
1015
|
exports.FlowSchema = FlowSchema;
|
|
1006
1016
|
exports.ForwardToSchema = ForwardToSchema;
|
|
@@ -1022,7 +1032,6 @@ exports.ManifestPreconditionZod = ManifestPreconditionZod;
|
|
|
1022
1032
|
exports.ManifestRoutingZod = ManifestRoutingZod;
|
|
1023
1033
|
exports.NativeBalanceGteSpecSchema = NativeBalanceGteSpecSchema;
|
|
1024
1034
|
exports.OutputPortZod = OutputPortZod;
|
|
1025
|
-
exports.PROVIDER_KIND = PROVIDER_KIND;
|
|
1026
1035
|
exports.Phase1ArtifactSchema = Phase1ArtifactSchema;
|
|
1027
1036
|
exports.RESERVED_REF_SCOPES = RESERVED_REF_SCOPES;
|
|
1028
1037
|
exports.RefSchema = RefSchema;
|
|
@@ -1046,6 +1055,7 @@ exports.canonicalAssertedPredicate = canonicalAssertedPredicate;
|
|
|
1046
1055
|
exports.canonicalNativeAddress = canonicalNativeAddress;
|
|
1047
1056
|
exports.checkCompatibility = checkCompatibility;
|
|
1048
1057
|
exports.copy = copy;
|
|
1058
|
+
exports.describeResource = describeResource;
|
|
1049
1059
|
exports.erc20 = erc20;
|
|
1050
1060
|
exports.erc20Allowance = erc20Allowance;
|
|
1051
1061
|
exports.erc20Balance = erc20Balance;
|
|
@@ -1053,6 +1063,7 @@ exports.erc20Resource = erc20Resource;
|
|
|
1053
1063
|
exports.erc20Token = erc20Token;
|
|
1054
1064
|
exports.expandOutputPorts = expandOutputPorts;
|
|
1055
1065
|
exports.familySize = familySize;
|
|
1066
|
+
exports.flashloanMaxFee = flashloanMaxFee;
|
|
1056
1067
|
exports.flowInputMode = flowInputMode;
|
|
1057
1068
|
exports.flowInputResource = flowInputResource;
|
|
1058
1069
|
exports.flowInputType = flowInputType;
|
|
@@ -1069,6 +1080,7 @@ exports.isContextKey = isContextKey;
|
|
|
1069
1080
|
exports.isERC20Resource = isERC20Resource;
|
|
1070
1081
|
exports.isErc20AllowancePrecondition = isErc20AllowancePrecondition;
|
|
1071
1082
|
exports.isErc20BalancePrecondition = isErc20BalancePrecondition;
|
|
1083
|
+
exports.isFlashloanMaterialiserInput = isFlashloanMaterialiserInput;
|
|
1072
1084
|
exports.isFutureResourceOutput = isFutureResourceOutput;
|
|
1073
1085
|
exports.isHandlePort = isHandlePort;
|
|
1074
1086
|
exports.isMaterialiserInput = isMaterialiserInput;
|
|
@@ -1092,7 +1104,6 @@ exports.normalizeResource = normalizeResource;
|
|
|
1092
1104
|
exports.opHandle = opHandle;
|
|
1093
1105
|
exports.parseRef = parseRef;
|
|
1094
1106
|
exports.parseVersion = parseVersion;
|
|
1095
|
-
exports.providerKindByName = providerKindByName;
|
|
1096
1107
|
exports.providerKindNames = providerKindNames;
|
|
1097
1108
|
exports.readResource = readResource;
|
|
1098
1109
|
exports.ref = ref;
|
package/dist/index.d.cts
CHANGED
|
@@ -720,6 +720,7 @@ export declare const foldResource: <R>(r: Resource, cases: {
|
|
|
720
720
|
}) => R;
|
|
721
721
|
export declare const resourceToken: (r: Resource) => string;
|
|
722
722
|
export declare const resourceKey: (r: Resource) => string;
|
|
723
|
+
export declare const describeResource: (r: Resource) => string;
|
|
723
724
|
export declare const erc20Token: (r: Resource) => string;
|
|
724
725
|
//#endregion
|
|
725
726
|
//#region src/output.d.ts
|
|
@@ -1717,20 +1718,31 @@ type PreconditionOf<C extends object> = PreconditionBase & C;
|
|
|
1717
1718
|
type PreconditionConfigOf<C extends object> = Omit<PreconditionBase, 'type'> & C;
|
|
1718
1719
|
//#endregion
|
|
1719
1720
|
//#region src/flashloan.d.ts
|
|
1720
|
-
export declare const
|
|
1721
|
-
readonly AAVE_V3: 0;
|
|
1722
|
-
readonly ERC3156: 1;
|
|
1723
|
-
readonly BALANCER_V2: 2;
|
|
1724
|
-
readonly MORPHO_BLUE: 3;
|
|
1725
|
-
};
|
|
1726
|
-
export declare const providerKindNames: readonly ['aave-v3', 'erc3156', 'balancer-v2', 'morpho-blue'];
|
|
1721
|
+
export declare const providerKindNames: readonly ['aave-v3', 'balancer-v2', 'morpho-blue'];
|
|
1727
1722
|
type ProviderKindName = (typeof providerKindNames)[number];
|
|
1728
|
-
export declare const
|
|
1729
|
-
|
|
1730
|
-
|
|
1731
|
-
|
|
1723
|
+
export declare const FlashloanConfigSchema: Schema.Struct<{
|
|
1724
|
+
providerKind: Schema.Literal<["aave-v3", "balancer-v2", "morpho-blue"]>;
|
|
1725
|
+
amount: Schema.filter<typeof Schema.String>;
|
|
1726
|
+
maxFee: Schema.optional<Schema.filter<typeof Schema.String>>;
|
|
1727
|
+
}>;
|
|
1728
|
+
export declare const FlashloanMaterialiserInputSchema: Schema.Struct<{
|
|
1729
|
+
providerKind: Schema.Literal<["aave-v3", "balancer-v2", "morpho-blue"]>;
|
|
1730
|
+
amount: Schema.filter<typeof Schema.String>;
|
|
1731
|
+
maxFee: Schema.optional<Schema.filter<typeof Schema.String>>;
|
|
1732
|
+
kind: Schema.Literal<["flashloan"]>;
|
|
1733
|
+
}>;
|
|
1734
|
+
type FlashloanMaterialiserInput = typeof FlashloanMaterialiserInputSchema.Type;
|
|
1735
|
+
export declare const isFlashloanMaterialiserInput: (u: unknown, overrideOptions?: import("effect/SchemaAST").ParseOptions | number) => u is {
|
|
1736
|
+
readonly providerKind: "aave-v3" | "balancer-v2" | "morpho-blue";
|
|
1732
1737
|
readonly amount: string;
|
|
1733
|
-
|
|
1738
|
+
readonly maxFee?: string | undefined;
|
|
1739
|
+
readonly kind: "flashloan";
|
|
1740
|
+
};
|
|
1741
|
+
export declare const flashloanMaxFee: ({ providerKind, amount, maxFee }: {
|
|
1742
|
+
readonly providerKind: ProviderKindName;
|
|
1743
|
+
readonly amount: string | bigint;
|
|
1744
|
+
readonly maxFee?: string | bigint;
|
|
1745
|
+
}) => bigint;
|
|
1734
1746
|
//#endregion
|
|
1735
1747
|
//#region src/constructors.d.ts
|
|
1736
1748
|
interface OpHandleOptions<Opt extends boolean = boolean> {
|
package/dist/index.d.ts
CHANGED
|
@@ -720,6 +720,7 @@ export declare const foldResource: <R>(r: Resource, cases: {
|
|
|
720
720
|
}) => R;
|
|
721
721
|
export declare const resourceToken: (r: Resource) => string;
|
|
722
722
|
export declare const resourceKey: (r: Resource) => string;
|
|
723
|
+
export declare const describeResource: (r: Resource) => string;
|
|
723
724
|
export declare const erc20Token: (r: Resource) => string;
|
|
724
725
|
//#endregion
|
|
725
726
|
//#region src/output.d.ts
|
|
@@ -1717,20 +1718,31 @@ type PreconditionOf<C extends object> = PreconditionBase & C;
|
|
|
1717
1718
|
type PreconditionConfigOf<C extends object> = Omit<PreconditionBase, 'type'> & C;
|
|
1718
1719
|
//#endregion
|
|
1719
1720
|
//#region src/flashloan.d.ts
|
|
1720
|
-
export declare const
|
|
1721
|
-
readonly AAVE_V3: 0;
|
|
1722
|
-
readonly ERC3156: 1;
|
|
1723
|
-
readonly BALANCER_V2: 2;
|
|
1724
|
-
readonly MORPHO_BLUE: 3;
|
|
1725
|
-
};
|
|
1726
|
-
export declare const providerKindNames: readonly ['aave-v3', 'erc3156', 'balancer-v2', 'morpho-blue'];
|
|
1721
|
+
export declare const providerKindNames: readonly ['aave-v3', 'balancer-v2', 'morpho-blue'];
|
|
1727
1722
|
type ProviderKindName = (typeof providerKindNames)[number];
|
|
1728
|
-
export declare const
|
|
1729
|
-
|
|
1730
|
-
|
|
1731
|
-
|
|
1723
|
+
export declare const FlashloanConfigSchema: Schema.Struct<{
|
|
1724
|
+
providerKind: Schema.Literal<["aave-v3", "balancer-v2", "morpho-blue"]>;
|
|
1725
|
+
amount: Schema.filter<typeof Schema.String>;
|
|
1726
|
+
maxFee: Schema.optional<Schema.filter<typeof Schema.String>>;
|
|
1727
|
+
}>;
|
|
1728
|
+
export declare const FlashloanMaterialiserInputSchema: Schema.Struct<{
|
|
1729
|
+
providerKind: Schema.Literal<["aave-v3", "balancer-v2", "morpho-blue"]>;
|
|
1730
|
+
amount: Schema.filter<typeof Schema.String>;
|
|
1731
|
+
maxFee: Schema.optional<Schema.filter<typeof Schema.String>>;
|
|
1732
|
+
kind: Schema.Literal<["flashloan"]>;
|
|
1733
|
+
}>;
|
|
1734
|
+
type FlashloanMaterialiserInput = typeof FlashloanMaterialiserInputSchema.Type;
|
|
1735
|
+
export declare const isFlashloanMaterialiserInput: (u: unknown, overrideOptions?: import("effect/SchemaAST").ParseOptions | number) => u is {
|
|
1736
|
+
readonly providerKind: "aave-v3" | "balancer-v2" | "morpho-blue";
|
|
1732
1737
|
readonly amount: string;
|
|
1733
|
-
|
|
1738
|
+
readonly maxFee?: string | undefined;
|
|
1739
|
+
readonly kind: "flashloan";
|
|
1740
|
+
};
|
|
1741
|
+
export declare const flashloanMaxFee: ({ providerKind, amount, maxFee }: {
|
|
1742
|
+
readonly providerKind: ProviderKindName;
|
|
1743
|
+
readonly amount: string | bigint;
|
|
1744
|
+
readonly maxFee?: string | bigint;
|
|
1745
|
+
}) => bigint;
|
|
1734
1746
|
//#endregion
|
|
1735
1747
|
//#region src/constructors.d.ts
|
|
1736
1748
|
interface OpHandleOptions<Opt extends boolean = boolean> {
|
package/dist/index.js
CHANGED
|
@@ -460,30 +460,38 @@ const resourceKey = (r) => foldResource(r, {
|
|
|
460
460
|
native: ({ chainId }) => `native:${chainId}`,
|
|
461
461
|
erc20: ({ token, chainId }) => `erc20:${token.toLowerCase()}:${chainId}`
|
|
462
462
|
});
|
|
463
|
+
const describeResource = (r) => foldResource(r, {
|
|
464
|
+
native: ({ chainId }) => `native coin on chain ${chainId}`,
|
|
465
|
+
erc20: ({ token, chainId }) => `${token} on chain ${chainId}`
|
|
466
|
+
});
|
|
463
467
|
const erc20Token = (r) => {
|
|
464
468
|
if (r.kind !== "erc20") throw new Error(`Expected erc20 resource, got ${r.kind}`);
|
|
465
469
|
return r.token;
|
|
466
470
|
};
|
|
467
471
|
//#endregion
|
|
468
472
|
//#region src/flashloan.ts
|
|
469
|
-
const PROVIDER_KIND = {
|
|
470
|
-
AAVE_V3: 0,
|
|
471
|
-
ERC3156: 1,
|
|
472
|
-
BALANCER_V2: 2,
|
|
473
|
-
MORPHO_BLUE: 3
|
|
474
|
-
};
|
|
475
473
|
const providerKindNames = [
|
|
476
474
|
"aave-v3",
|
|
477
|
-
"erc3156",
|
|
478
475
|
"balancer-v2",
|
|
479
476
|
"morpho-blue"
|
|
480
477
|
];
|
|
481
|
-
const
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
478
|
+
const FlashloanConfigSchema = Schema.Struct({
|
|
479
|
+
providerKind: Schema.Literal(...providerKindNames),
|
|
480
|
+
amount: IntegerString,
|
|
481
|
+
maxFee: Schema.optional(IntegerString)
|
|
482
|
+
});
|
|
483
|
+
const FlashloanMaterialiserInputSchema = Schema.Struct({
|
|
484
|
+
kind: Schema.Literal("flashloan"),
|
|
485
|
+
...FlashloanConfigSchema.fields
|
|
486
|
+
});
|
|
487
|
+
const isFlashloanMaterialiserInput = Schema.is(FlashloanMaterialiserInputSchema);
|
|
488
|
+
const FLASHLOAN_FEE_BPS = {
|
|
489
|
+
"aave-v3": 5n,
|
|
490
|
+
"balancer-v2": 0n,
|
|
491
|
+
"morpho-blue": 0n
|
|
486
492
|
};
|
|
493
|
+
const BPS_DENOMINATOR = 10000n;
|
|
494
|
+
const flashloanMaxFee = ({ providerKind, amount, maxFee }) => maxFee !== void 0 ? BigInt(maxFee) : (BigInt(amount) * FLASHLOAN_FEE_BPS[providerKind] + BPS_DENOMINATOR - 1n) / BPS_DENOMINATOR;
|
|
487
495
|
//#endregion
|
|
488
496
|
//#region src/constructors.ts
|
|
489
497
|
/** Returns `{ [key]: value }` when `value` is not nullish, else `{}`. */
|
|
@@ -953,4 +961,4 @@ const ShortfallZod = z.object({
|
|
|
953
961
|
available: z.string()
|
|
954
962
|
});
|
|
955
963
|
//#endregion
|
|
956
|
-
export { ARC_CHAIN_ID, ARC_NATIVE_ERC20, ARC_NATIVE_SCALE, AppliedGuardSchema, AssertedPredicateSchema, AssertionOwnerSchema, AssertionSpecSchema, AssertionSpecZod, BindValueSchema, Bytes32HexSchema, COMPOSER_SDK_VERSION_HEADER, COMPOSE_VERSION_HEADER, CONTINUATION_COMMITMENT_OP_ID, CallCheckSpecSchema, CallSchema, CommittedOutcomeSchema, ComposeManifestZod, ContinuationIntentSchema, ContinuationOutcomeSchema, EVMAddressString, EVM_NATIVE_SENTINEL, EpilogueForwardSchema, EpilogueSchema, Erc20BalanceGteSpecSchema, FlowInputSchema, FlowSchema, ForwardToSchema, GuardCompatibilityZod, GuardSelectorZod, HANDLE_UNITS, HandleInputSchema, HandlePortZod, HandleUnitsZod, HexStringSchema, InputPortZod, IntegerString, LineageSchema, LiteralBindingSchema, ManifestGuardZod, ManifestMaterialiserZod, ManifestOperationZod, ManifestPreconditionZod, ManifestRoutingZod, NativeBalanceGteSpecSchema, OutputPortZod,
|
|
964
|
+
export { ARC_CHAIN_ID, ARC_NATIVE_ERC20, ARC_NATIVE_SCALE, AppliedGuardSchema, AssertedPredicateSchema, AssertionOwnerSchema, AssertionSpecSchema, AssertionSpecZod, BindValueSchema, Bytes32HexSchema, COMPOSER_SDK_VERSION_HEADER, COMPOSE_VERSION_HEADER, CONTINUATION_COMMITMENT_OP_ID, CallCheckSpecSchema, CallSchema, CommittedOutcomeSchema, ComposeManifestZod, ContinuationIntentSchema, ContinuationOutcomeSchema, EVMAddressString, EVM_NATIVE_SENTINEL, EpilogueForwardSchema, EpilogueSchema, Erc20BalanceGteSpecSchema, FlashloanConfigSchema, FlashloanMaterialiserInputSchema, FlowInputSchema, FlowSchema, ForwardToSchema, GuardCompatibilityZod, GuardSelectorZod, HANDLE_UNITS, HandleInputSchema, HandlePortZod, HandleUnitsZod, HexStringSchema, InputPortZod, IntegerString, LineageSchema, LiteralBindingSchema, ManifestGuardZod, ManifestMaterialiserZod, ManifestOperationZod, ManifestPreconditionZod, ManifestRoutingZod, NativeBalanceGteSpecSchema, OutputPortZod, Phase1ArtifactSchema, RESERVED_REF_SCOPES, RefSchema, ResourceInputSchema, ResourceOutputPortZod, ResourcePortZod, ResourceSchema, SIMULATE_MAX_REQUIREMENTS, SIMULATE_MAX_TRACKED_BALANCES, STATIC_SOL_TYPES, SettlementZod, ShortfallZod, SolTypeSchema, StaticSolTypeZod, TEMPO_CHAIN_ID, TEMPO_NATIVE_ERC20, UuidSchema, VerificationDescriptorSchema, VerificationDescriptorZod, canonicalAssertedPredicate, canonicalNativeAddress, checkCompatibility, copy, describeResource, erc20, erc20Allowance, erc20Balance, erc20Resource, erc20Token, expandOutputPorts, familySize, flashloanMaxFee, flowInputMode, flowInputResource, flowInputType, foldChainCapabilities, foldMaterialiserMetadata, foldRef, foldResource, getChainCapabilities, handle, hasErc20CanonicalNative, instancePortName, isAsErc20Chain, isContextKey, isERC20Resource, isErc20AllowancePrecondition, isErc20BalancePrecondition, isFlashloanMaterialiserInput, isFutureResourceOutput, isHandlePort, isMaterialiserInput, isMsgValueNative, isNativeAddressForChain, isNativeBalancePrecondition, isNativeResource, isOutputFamily, isRef, isResourceInput, isResourceOutputPort, isResourcePort, isStaticSolType, linear, native, nativeBalance, nativeCheckPolicy, nativeResource, normalizeNativeTokenAddress, normalizeResource, opHandle, parseRef, parseVersion, providerKindNames, readResource, ref, refKey, resource, resourceKey, resourceOutput, resourceToken, resourcesEqual, settledOutcome };
|
package/package.json
CHANGED
package/src/flashloan.ts
CHANGED
|
@@ -1,41 +1,73 @@
|
|
|
1
|
-
|
|
2
|
-
// `Yggdrasil-VirtualMachine/src/flashloan/FlashloanLeg.sol`. The numeric
|
|
3
|
-
// values are wire format; the Rust EVM backend consumes them in
|
|
4
|
-
// `executeWithFlashloan(adapter, legs, programBytes)`. Drift between this
|
|
5
|
-
// file and the Solidity source is a bug.
|
|
6
|
-
|
|
7
|
-
export const PROVIDER_KIND = {
|
|
8
|
-
AAVE_V3: 0,
|
|
9
|
-
ERC3156: 1,
|
|
10
|
-
BALANCER_V2: 2,
|
|
11
|
-
MORPHO_BLUE: 3,
|
|
12
|
-
} as const;
|
|
1
|
+
import { Schema } from 'effect';
|
|
13
2
|
|
|
3
|
+
import { IntegerString } from './flowSchema.js';
|
|
4
|
+
|
|
5
|
+
// The lenders a `flashloan` input can borrow from. Each name selects one
|
|
6
|
+
// lender adapter of the v1.3 flashloan runtime (one adapter contract per
|
|
7
|
+
// kind) and that lender's per-chain address.
|
|
14
8
|
export const providerKindNames = [
|
|
15
9
|
'aave-v3',
|
|
16
|
-
'erc3156',
|
|
17
10
|
'balancer-v2',
|
|
18
11
|
'morpho-blue',
|
|
19
12
|
] as const;
|
|
20
13
|
|
|
21
14
|
export type ProviderKindName = (typeof providerKindNames)[number];
|
|
22
15
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
16
|
+
// The one definition of a `flashloan` input's fields, in the wire form
|
|
17
|
+
// (decimal-integer strings). The materialiser's `configSchema`
|
|
18
|
+
// (compose-builtins), the input type and its guard all derive from it.
|
|
19
|
+
// - `amount`: what the leg borrows, in the token's smallest unit.
|
|
20
|
+
// - `maxFee`: the most the lender may charge for the leg; the runtime reverts
|
|
21
|
+
// `FlashloanFeeTooHigh` above it. Defaults to `flashloanMaxFee`'s rate.
|
|
22
|
+
export const FlashloanConfigSchema = Schema.Struct({
|
|
23
|
+
providerKind: Schema.Literal(...providerKindNames),
|
|
24
|
+
amount: IntegerString,
|
|
25
|
+
maxFee: Schema.optional(IntegerString),
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
// A `flashloan` input as authored: the config under the literal
|
|
29
|
+
// `kind: "flashloan"` that discriminates inside the `MaterialiserInput` union.
|
|
30
|
+
export const FlashloanMaterialiserInputSchema = Schema.Struct({
|
|
31
|
+
kind: Schema.Literal('flashloan'),
|
|
32
|
+
...FlashloanConfigSchema.fields,
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
export type FlashloanMaterialiserInput =
|
|
36
|
+
typeof FlashloanMaterialiserInputSchema.Type;
|
|
37
|
+
|
|
38
|
+
export const isFlashloanMaterialiserInput = Schema.is(
|
|
39
|
+
FlashloanMaterialiserInputSchema,
|
|
40
|
+
);
|
|
41
|
+
|
|
42
|
+
// Reference fee rates, in basis points of the borrowed amount, that size a
|
|
43
|
+
// leg's default `maxFee`. Aave v3: `FLASHLOAN_PREMIUM_TOTAL` is 5 bps on the
|
|
44
|
+
// markets in the registry.
|
|
45
|
+
const FLASHLOAN_FEE_BPS: Readonly<Record<ProviderKindName, bigint>> = {
|
|
46
|
+
'aave-v3': 5n,
|
|
47
|
+
'balancer-v2': 0n,
|
|
48
|
+
'morpho-blue': 0n,
|
|
28
49
|
};
|
|
29
50
|
|
|
30
|
-
//
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
//
|
|
34
|
-
//
|
|
35
|
-
//
|
|
36
|
-
//
|
|
37
|
-
|
|
38
|
-
|
|
51
|
+
// Local rather than imported: compose-spec may depend on no @lifi/* package.
|
|
52
|
+
const BPS_DENOMINATOR = 10_000n;
|
|
53
|
+
|
|
54
|
+
// The fee cap of one leg: the input's own `maxFee`, else the reference rate
|
|
55
|
+
// rounded up. Rounding up matters: Aave rounds its premium half-up
|
|
56
|
+
// (`percentMul`), so a rounded-down cap can sit one unit below the fee it
|
|
57
|
+
// charges and revert the flight. Amounts are wire strings or bigints, so an
|
|
58
|
+
// author who holds a bigint does not format it first.
|
|
59
|
+
export const flashloanMaxFee = ({
|
|
60
|
+
providerKind,
|
|
61
|
+
amount,
|
|
62
|
+
maxFee,
|
|
63
|
+
}: {
|
|
39
64
|
readonly providerKind: ProviderKindName;
|
|
40
|
-
readonly amount: string;
|
|
41
|
-
|
|
65
|
+
readonly amount: string | bigint;
|
|
66
|
+
readonly maxFee?: string | bigint;
|
|
67
|
+
}): bigint =>
|
|
68
|
+
maxFee !== undefined
|
|
69
|
+
? BigInt(maxFee)
|
|
70
|
+
: (BigInt(amount) * FLASHLOAN_FEE_BPS[providerKind] +
|
|
71
|
+
BPS_DENOMINATOR -
|
|
72
|
+
1n) /
|
|
73
|
+
BPS_DENOMINATOR;
|
package/src/index.ts
CHANGED
|
@@ -118,6 +118,7 @@ export type {
|
|
|
118
118
|
Resource,
|
|
119
119
|
} from './resource.js';
|
|
120
120
|
export {
|
|
121
|
+
describeResource,
|
|
121
122
|
erc20Resource,
|
|
122
123
|
erc20Token,
|
|
123
124
|
foldResource,
|
|
@@ -147,8 +148,10 @@ export type {
|
|
|
147
148
|
FlashloanMaterialiserInput,
|
|
148
149
|
} from './flashloan.js';
|
|
149
150
|
export {
|
|
150
|
-
|
|
151
|
-
|
|
151
|
+
FlashloanConfigSchema,
|
|
152
|
+
FlashloanMaterialiserInputSchema,
|
|
153
|
+
flashloanMaxFee,
|
|
154
|
+
isFlashloanMaterialiserInput,
|
|
152
155
|
providerKindNames,
|
|
153
156
|
} from './flashloan.js';
|
|
154
157
|
|
package/src/resource.ts
CHANGED
|
@@ -84,6 +84,13 @@ export const resourceKey = (r: Resource): string =>
|
|
|
84
84
|
erc20: ({ token, chainId }) => `erc20:${token.toLowerCase()}:${chainId}`,
|
|
85
85
|
});
|
|
86
86
|
|
|
87
|
+
// A resource as an error message names it.
|
|
88
|
+
export const describeResource = (r: Resource): string =>
|
|
89
|
+
foldResource(r, {
|
|
90
|
+
native: ({ chainId }) => `native coin on chain ${chainId}`,
|
|
91
|
+
erc20: ({ token, chainId }) => `${token} on chain ${chainId}`,
|
|
92
|
+
});
|
|
93
|
+
|
|
87
94
|
export const erc20Token = (r: Resource): string => {
|
|
88
95
|
if (r.kind !== 'erc20')
|
|
89
96
|
throw new Error(`Expected erc20 resource, got ${r.kind}`);
|