@indigo-labs/indigo-sdk 0.1.18 → 0.1.20
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/.github/workflows/ci.yml +8 -3
- package/dist/index.d.mts +1004 -681
- package/dist/index.d.ts +1004 -681
- package/dist/index.js +4653 -2270
- package/dist/index.mjs +4590 -2222
- package/eslint.config.mjs +7 -1
- package/package.json +9 -4
- package/src/contracts/cdp/helpers.ts +167 -0
- package/src/contracts/cdp/scripts.ts +33 -0
- package/src/contracts/cdp/transactions.ts +1310 -0
- package/src/contracts/cdp/types.ts +161 -0
- package/src/contracts/cdp-creator/scripts.ts +39 -0
- package/src/{types/indigo/cdp-creator.ts → contracts/cdp-creator/types.ts} +6 -4
- package/src/contracts/collector/scripts.ts +32 -0
- package/src/contracts/collector/transactions.ts +44 -0
- package/src/contracts/execute/scripts.ts +48 -0
- package/src/contracts/execute/types.ts +57 -0
- package/src/contracts/gov/helpers.ts +157 -0
- package/src/contracts/gov/scripts.ts +34 -0
- package/src/contracts/gov/transactions.ts +1224 -0
- package/src/contracts/gov/types-new.ts +115 -0
- package/src/contracts/gov/types.ts +89 -0
- package/src/{helpers/interest-oracle.ts → contracts/interest-oracle/helpers.ts} +37 -9
- package/src/contracts/interest-oracle/scripts.ts +18 -0
- package/src/contracts/interest-oracle/transactions.ts +149 -0
- package/src/{types/indigo/interest-oracle.ts → contracts/interest-oracle/types.ts} +1 -1
- package/src/contracts/lrp/scripts.ts +27 -0
- package/src/contracts/{lrp.ts → lrp/transactions.ts} +24 -38
- package/src/{types/indigo/lrp.ts → contracts/lrp/types.ts} +3 -3
- package/src/{scripts/one-shot-policy.ts → contracts/one-shot/scripts.ts} +1 -1
- package/src/contracts/{one-shot.ts → one-shot/transactions.ts} +3 -3
- package/src/contracts/poll/helpers.ts +55 -0
- package/src/contracts/poll/scripts.ts +72 -0
- package/src/contracts/poll/types-poll-manager.ts +38 -0
- package/src/contracts/poll/types-poll-shard.ts +38 -0
- package/src/contracts/poll/types-poll.ts +88 -0
- package/src/{scripts/price-oracle-validator.ts → contracts/price-oracle/scripts.ts} +1 -4
- package/src/contracts/price-oracle/transactions.ts +112 -0
- package/src/{types/indigo/price-oracle.ts → contracts/price-oracle/types.ts} +16 -4
- package/src/{helpers/stability-pool-helpers.ts → contracts/stability-pool/helpers.ts} +110 -6
- package/src/contracts/stability-pool/scripts.ts +46 -0
- package/src/contracts/stability-pool/transactions.ts +660 -0
- package/src/contracts/stability-pool/types-new.ts +208 -0
- package/src/contracts/stability-pool/types.ts +42 -0
- package/src/contracts/staking/helpers.ts +116 -0
- package/src/contracts/staking/scripts.ts +41 -0
- package/src/contracts/staking/transactions.ts +268 -0
- package/src/contracts/staking/types-new.ts +81 -0
- package/src/contracts/staking/types.ts +41 -0
- package/src/contracts/treasury/scripts.ts +37 -0
- package/src/contracts/treasury/transactions.ts +44 -0
- package/src/contracts/treasury/types.ts +55 -0
- package/src/contracts/version-registry/scripts.ts +29 -0
- package/src/contracts/version-registry/types-new.ts +19 -0
- package/src/{types/indigo/version-record.ts → contracts/version-registry/types.ts} +1 -1
- package/src/contracts/vesting/helpers.ts +267 -0
- package/src/index.ts +37 -33
- package/src/types/evolution-schema-options.ts +16 -0
- package/src/types/generic.ts +78 -60
- package/src/types/on-chain-decimal.ts +22 -0
- package/src/types/system-params.ts +22 -11
- package/src/utils/bigint-utils.ts +7 -0
- package/src/{helpers → utils}/helper-txs.ts +1 -0
- package/src/utils/time-helpers.ts +4 -0
- package/src/{helpers/helpers.ts → utils/utils.ts} +0 -10
- package/src/{helpers → utils}/value-helpers.ts +10 -0
- package/src/{scripts → validators}/cdp-creator-validator.ts +4 -50
- package/src/{scripts → validators}/cdp-validator.ts +3 -5
- package/src/{scripts → validators}/collector-validator.ts +2 -3
- package/src/{scripts/version-registry.ts → validators/execute-validator.ts} +3 -11
- package/src/{scripts/gov-validator.ts → validators/governance-validator.ts} +3 -40
- package/src/{scripts → validators}/interest-oracle-validator.ts +4 -20
- package/src/validators/lrp-validator.ts +7 -0
- package/src/validators/poll-manager-validator.ts +7 -0
- package/src/{scripts → validators}/poll-shard-validator.ts +3 -43
- package/src/{scripts → validators}/stability-pool-validator.ts +4 -57
- package/src/{scripts → validators}/staking-validator.ts +2 -3
- package/src/{scripts → validators}/treasury-validator.ts +2 -3
- package/src/{scripts → validators}/version-record-policy.ts +4 -23
- package/src/validators/version-registry-validator.ts +7 -0
- package/tests/cdp.test.ts +1565 -0
- package/tests/datums.test.ts +125 -108
- package/tests/endpoints/initialize.ts +240 -338
- package/tests/gov.test.ts +1874 -0
- package/tests/hash-checks.test.ts +26 -21
- package/tests/indigo-test-helpers.ts +1 -55
- package/tests/initialize.test.ts +10 -5
- package/tests/interest-calculations.test.ts +18 -18
- package/tests/interest-oracle.test.ts +20 -18
- package/tests/lrp.test.ts +180 -78
- package/tests/mock/assets-mock.ts +59 -0
- package/tests/queries/cdp-queries.ts +144 -0
- package/tests/queries/collector-queries.ts +26 -0
- package/tests/queries/execute-queries.ts +46 -0
- package/tests/queries/governance-queries.ts +19 -16
- package/tests/queries/iasset-queries.ts +46 -23
- package/tests/queries/interest-oracle-queries.ts +3 -6
- package/tests/queries/lrp-queries.ts +2 -2
- package/tests/queries/poll-queries.ts +97 -0
- package/tests/queries/price-oracle-queries.ts +5 -22
- package/tests/queries/stability-pool-queries.ts +10 -8
- package/tests/queries/staking-queries.ts +28 -19
- package/tests/queries/treasury-queries.ts +19 -0
- package/tests/stability-pool.test.ts +186 -71
- package/tests/staking.test.ts +30 -23
- package/tests/test-helpers.ts +11 -2
- package/tests/utils/asserts.ts +13 -0
- package/tests/utils/index.ts +50 -0
- package/tsconfig.json +3 -1
- package/vitest.config.ts +1 -1
- package/.github/workflows/test.yml +0 -44
- package/src/contracts/cdp.ts +0 -748
- package/src/contracts/collector.ts +0 -98
- package/src/contracts/gov.ts +0 -1
- package/src/contracts/interest-oracle.ts +0 -149
- package/src/contracts/stability-pool.ts +0 -692
- package/src/contracts/staking.ts +0 -348
- package/src/contracts/treasury.ts +0 -112
- package/src/helpers/asset-helpers.ts +0 -57
- package/src/helpers/staking-helpers.ts +0 -94
- package/src/helpers/time-helpers.ts +0 -4
- package/src/scripts/execute-validator.ts +0 -52
- package/src/scripts/lrp-validator.ts +0 -40
- package/src/scripts/poll-manager-validator.ts +0 -52
- package/src/types/indigo/cdp.ts +0 -88
- package/src/types/indigo/execute.ts +0 -21
- package/src/types/indigo/gov.ts +0 -51
- package/src/types/indigo/poll-manager.ts +0 -21
- package/src/types/indigo/poll-shard.ts +0 -16
- package/src/types/indigo/stability-pool.ts +0 -233
- package/src/types/indigo/staking.ts +0 -99
- /package/src/{types/one-shot.ts → contracts/one-shot/types.ts} +0 -0
- /package/src/{helpers/price-oracle-helpers.ts → contracts/price-oracle/helpers.ts} +0 -0
- /package/src/{helpers → utils}/indigo-helpers.ts +0 -0
- /package/src/{helpers → utils}/lucid-utils.ts +0 -0
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { Core as EvoCore } from '@evolution-sdk/evolution';
|
|
2
|
+
import { Data } from '@lucid-evolution/lucid';
|
|
3
|
+
import { OracleAssetNftSchema } from '../price-oracle/types';
|
|
4
|
+
import { AssetClassSchema } from '../../types/generic';
|
|
5
|
+
import { OnChainDecimalSchema } from '../../types/on-chain-decimal';
|
|
6
|
+
import { IAssetPriceInfoSchema } from '../cdp/types';
|
|
7
|
+
import { DEFAULT_SCHEMA_OPTIONS } from '../../types/evolution-schema-options';
|
|
8
|
+
|
|
9
|
+
export const ProtocolParamsSchema = Data.Object({
|
|
10
|
+
proposalDeposit: Data.Integer(),
|
|
11
|
+
votingPeriod: Data.Integer(),
|
|
12
|
+
effectiveDelay: Data.Integer(),
|
|
13
|
+
expirationPeriod: Data.Integer(),
|
|
14
|
+
collateralFeePercentage: OnChainDecimalSchema,
|
|
15
|
+
proposingPeriod: Data.Integer(),
|
|
16
|
+
/// Total numer of shards used for voting.
|
|
17
|
+
totalShards: Data.Integer(),
|
|
18
|
+
/// The minimum number of votes (yes + no votes) for a proposal to be possible to pass.
|
|
19
|
+
minimumQuorum: Data.Integer(),
|
|
20
|
+
/// Maximum amount of lovelaces that can be spent at once from the treasury.
|
|
21
|
+
maxTreasuryLovelaceSpend: Data.Integer(),
|
|
22
|
+
/// Maximum amount of INDY that can be spent at once from the treasury.
|
|
23
|
+
maxTreasuryIndySpend: Data.Integer(),
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
export type ProtocolParams = Data.Static<typeof ProtocolParamsSchema>;
|
|
27
|
+
export const ProtocolParams = ProtocolParamsSchema as unknown as ProtocolParams;
|
|
28
|
+
|
|
29
|
+
const ProposeAssetContentSchema = Data.Object({
|
|
30
|
+
asset: Data.Bytes(),
|
|
31
|
+
priceOracleNft: OracleAssetNftSchema,
|
|
32
|
+
interestOracleNft: AssetClassSchema,
|
|
33
|
+
redemptionRatioPercentage: OnChainDecimalSchema,
|
|
34
|
+
maintenanceRatioPercentage: OnChainDecimalSchema,
|
|
35
|
+
liquidationRatioPercentage: OnChainDecimalSchema,
|
|
36
|
+
debtMintingFeePercentage: OnChainDecimalSchema,
|
|
37
|
+
liquidationProcessingFeePercentage: OnChainDecimalSchema,
|
|
38
|
+
stabilityPoolWithdrawalFeePercentage: OnChainDecimalSchema,
|
|
39
|
+
redemptionReimbursementPercentage: OnChainDecimalSchema,
|
|
40
|
+
redemptionProcessingFeePercentage: OnChainDecimalSchema,
|
|
41
|
+
interestCollectorPortionPercentage: OnChainDecimalSchema,
|
|
42
|
+
});
|
|
43
|
+
export type ProposeAssetContent = Data.Static<typeof ProposeAssetContentSchema>;
|
|
44
|
+
|
|
45
|
+
export const ProposalContentSchema = Data.Enum([
|
|
46
|
+
Data.Object({
|
|
47
|
+
ProposeAsset: ProposeAssetContentSchema,
|
|
48
|
+
}),
|
|
49
|
+
Data.Object({
|
|
50
|
+
ModifyAsset: Data.Object({
|
|
51
|
+
asset: Data.Bytes(),
|
|
52
|
+
newAssetPriceInfo: IAssetPriceInfoSchema,
|
|
53
|
+
newInterestOracleNft: AssetClassSchema,
|
|
54
|
+
newRedemptionRatioPercentage: OnChainDecimalSchema,
|
|
55
|
+
newMaintenanceRatioPercentage: OnChainDecimalSchema,
|
|
56
|
+
newLiquidationRatioPercentage: OnChainDecimalSchema,
|
|
57
|
+
newDebtMintingFeePercentage: OnChainDecimalSchema,
|
|
58
|
+
newLiquidationProcessingFeePercentage: OnChainDecimalSchema,
|
|
59
|
+
newStabilityPoolWithdrawalFeePercentage: OnChainDecimalSchema,
|
|
60
|
+
newRedemptionReimbursementPercentage: OnChainDecimalSchema,
|
|
61
|
+
newRedemptionProcessingFeePercentage: OnChainDecimalSchema,
|
|
62
|
+
newInterestCollectorPortionPercentage: OnChainDecimalSchema,
|
|
63
|
+
}),
|
|
64
|
+
}),
|
|
65
|
+
Data.Object({
|
|
66
|
+
ModifyProtocolParams: Data.Object({
|
|
67
|
+
newParams: ProtocolParamsSchema,
|
|
68
|
+
}),
|
|
69
|
+
}),
|
|
70
|
+
Data.Object({
|
|
71
|
+
UpgradeProtocol: Data.Object({
|
|
72
|
+
content:
|
|
73
|
+
// NOTICE: This is replaced by evolution-sdk encoding of `UpgradePathsSchema` defined in this file.
|
|
74
|
+
Data.Any(),
|
|
75
|
+
}),
|
|
76
|
+
}),
|
|
77
|
+
Data.Object({
|
|
78
|
+
TextProposal: Data.Object({
|
|
79
|
+
bytes: Data.Bytes(),
|
|
80
|
+
}),
|
|
81
|
+
}),
|
|
82
|
+
]);
|
|
83
|
+
export type ProposalContent = Data.Static<typeof ProposalContentSchema>;
|
|
84
|
+
export const ProposalContent =
|
|
85
|
+
ProposalContentSchema as unknown as ProposalContent;
|
|
86
|
+
|
|
87
|
+
const UpgradePathSchema = EvoCore.TSchema.Struct({
|
|
88
|
+
upgradeSymbol: EvoCore.TSchema.ByteArray,
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
const UpgradePathsSchema = EvoCore.TSchema.Struct({
|
|
92
|
+
upgradeId: EvoCore.TSchema.Integer,
|
|
93
|
+
/// Underlying representation of the following mapping: ValidatorHash -> UpgradePath
|
|
94
|
+
upgradePaths: EvoCore.TSchema.Map(
|
|
95
|
+
EvoCore.TSchema.ByteArray,
|
|
96
|
+
UpgradePathSchema,
|
|
97
|
+
),
|
|
98
|
+
});
|
|
99
|
+
export type UpgradePaths = typeof UpgradePathsSchema.Type;
|
|
100
|
+
|
|
101
|
+
export function serialiseUpgradePaths(d: UpgradePaths): Data {
|
|
102
|
+
return Data.from(
|
|
103
|
+
EvoCore.Data.withSchema(
|
|
104
|
+
UpgradePathsSchema,
|
|
105
|
+
DEFAULT_SCHEMA_OPTIONS,
|
|
106
|
+
).toCBORHex(d),
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export function parseUpgradePaths(d: Data): UpgradePaths {
|
|
111
|
+
return EvoCore.Data.withSchema(
|
|
112
|
+
UpgradePathsSchema,
|
|
113
|
+
DEFAULT_SCHEMA_OPTIONS,
|
|
114
|
+
).fromCBORHex(Data.to(d));
|
|
115
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { Data, Datum, Redeemer } from '@lucid-evolution/lucid';
|
|
2
|
+
import { AddressSchema, AssetClassSchema } from '../../types/generic';
|
|
3
|
+
import { option as O, function as F } from 'fp-ts';
|
|
4
|
+
import { ProposalContentSchema, ProtocolParamsSchema } from './types-new';
|
|
5
|
+
|
|
6
|
+
const GovDatumSchema = Data.Object({
|
|
7
|
+
currentProposal: Data.Integer(),
|
|
8
|
+
protocolParams: ProtocolParamsSchema,
|
|
9
|
+
currentVersion: Data.Integer(),
|
|
10
|
+
iassetsCount: Data.Integer(),
|
|
11
|
+
activeProposals: Data.Integer(),
|
|
12
|
+
treasuryIndyWithdrawnAmt: Data.Integer(),
|
|
13
|
+
});
|
|
14
|
+
export type GovDatum = Data.Static<typeof GovDatumSchema>;
|
|
15
|
+
const GovDatum = GovDatumSchema as unknown as GovDatum;
|
|
16
|
+
|
|
17
|
+
const GovParamsSchema = Data.Object({
|
|
18
|
+
govNFT: AssetClassSchema,
|
|
19
|
+
pollToken: AssetClassSchema,
|
|
20
|
+
upgradeToken: AssetClassSchema,
|
|
21
|
+
indyAsset: AssetClassSchema,
|
|
22
|
+
versionRecordToken: AssetClassSchema,
|
|
23
|
+
pollManagerValHash: Data.Bytes(),
|
|
24
|
+
gBiasTime: Data.Integer(),
|
|
25
|
+
daoIdentityToken: AssetClassSchema,
|
|
26
|
+
iAssetAuthToken: AssetClassSchema,
|
|
27
|
+
});
|
|
28
|
+
export type GovParams = Data.Static<typeof GovParamsSchema>;
|
|
29
|
+
export const GovParams = GovParamsSchema as unknown as GovParams;
|
|
30
|
+
|
|
31
|
+
const ValueWithdrawalItemSchema = Data.Tuple(
|
|
32
|
+
[Data.Bytes(), Data.Bytes(), Data.Integer()],
|
|
33
|
+
{ hasConstr: true },
|
|
34
|
+
);
|
|
35
|
+
export type TreasuryWithdrawalItem = Data.Static<
|
|
36
|
+
typeof ValueWithdrawalItemSchema
|
|
37
|
+
>;
|
|
38
|
+
|
|
39
|
+
export const TreasuryWithdrawalSchema = Data.Object({
|
|
40
|
+
destination: AddressSchema,
|
|
41
|
+
value: Data.Array(ValueWithdrawalItemSchema),
|
|
42
|
+
});
|
|
43
|
+
export type TreasuryWithdrawal = Data.Static<typeof TreasuryWithdrawalSchema>;
|
|
44
|
+
|
|
45
|
+
const GovRedeemerSchema = Data.Enum([
|
|
46
|
+
Data.Object({
|
|
47
|
+
CreatePoll: Data.Object({
|
|
48
|
+
currentTime: Data.Integer(),
|
|
49
|
+
proposalOwner: Data.Bytes(),
|
|
50
|
+
content: ProposalContentSchema,
|
|
51
|
+
treasuryWithdrawal: Data.Nullable(TreasuryWithdrawalSchema),
|
|
52
|
+
}),
|
|
53
|
+
}),
|
|
54
|
+
Data.Object({ WitnessEndPoll: Data.Object({ currentTime: Data.Integer() }) }),
|
|
55
|
+
Data.Literal('UpgradeGov'),
|
|
56
|
+
Data.Literal('UpgradeVersion'),
|
|
57
|
+
]);
|
|
58
|
+
|
|
59
|
+
export type GovRedeemer = Data.Static<typeof GovRedeemerSchema>;
|
|
60
|
+
export const GovRedeemer = GovRedeemerSchema as unknown as GovRedeemer;
|
|
61
|
+
|
|
62
|
+
export function parseGovDatum(datum: Datum): O.Option<GovDatum> {
|
|
63
|
+
try {
|
|
64
|
+
return O.some(Data.from<GovDatum>(datum, GovDatum));
|
|
65
|
+
} catch (_) {
|
|
66
|
+
return O.none;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function parseGovDatumOrThrow(datum: Datum): GovDatum {
|
|
71
|
+
return F.pipe(
|
|
72
|
+
parseGovDatum(datum),
|
|
73
|
+
O.match(() => {
|
|
74
|
+
throw new Error('Expected a Gov datum.');
|
|
75
|
+
}, F.identity),
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export function serialiseGovDatum(d: GovDatum): Datum {
|
|
80
|
+
return Data.to<GovDatum>(d, GovDatum);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export function serialiseGovRedeemer(redeemer: GovRedeemer): Redeemer {
|
|
84
|
+
return Data.to<GovRedeemer>(redeemer, GovRedeemer);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export function castGovParams(params: GovParams): Data {
|
|
88
|
+
return Data.castTo(params, GovParams);
|
|
89
|
+
}
|
|
@@ -1,8 +1,14 @@
|
|
|
1
|
-
import { InterestOracleDatum } from '
|
|
2
|
-
import {
|
|
1
|
+
import { InterestOracleDatum } from './types';
|
|
2
|
+
import { ONE_YEAR } from '../../utils/time-helpers';
|
|
3
|
+
import {
|
|
4
|
+
OCD_DECIMAL_UNIT,
|
|
5
|
+
ocdAdd,
|
|
6
|
+
ocdCeil,
|
|
7
|
+
ocdMul,
|
|
8
|
+
OnChainDecimal,
|
|
9
|
+
} from '../../types/on-chain-decimal';
|
|
3
10
|
|
|
4
11
|
const unitaryInterestPrecision = 1_000_000_000_000_000_000n;
|
|
5
|
-
const decimalUnit = 1_000_000n;
|
|
6
12
|
|
|
7
13
|
export function calculateUnitaryInterest(
|
|
8
14
|
timePeriod: bigint,
|
|
@@ -10,8 +16,8 @@ export function calculateUnitaryInterest(
|
|
|
10
16
|
): bigint {
|
|
11
17
|
return (
|
|
12
18
|
(timePeriod * interestRate * unitaryInterestPrecision) /
|
|
13
|
-
|
|
14
|
-
|
|
19
|
+
ONE_YEAR /
|
|
20
|
+
OCD_DECIMAL_UNIT
|
|
15
21
|
);
|
|
16
22
|
}
|
|
17
23
|
|
|
@@ -41,8 +47,8 @@ export function calculateAccruedInterest(
|
|
|
41
47
|
((now - interestOracleDatum.lastUpdated) *
|
|
42
48
|
interestOracleDatum.interestRate.getOnChainInt *
|
|
43
49
|
mintedAmount) /
|
|
44
|
-
|
|
45
|
-
|
|
50
|
+
ONE_YEAR /
|
|
51
|
+
OCD_DECIMAL_UNIT;
|
|
46
52
|
|
|
47
53
|
return interestFromPreviousRates + lastRateInterest;
|
|
48
54
|
} else {
|
|
@@ -50,8 +56,30 @@ export function calculateAccruedInterest(
|
|
|
50
56
|
((now - interestLastSettled) *
|
|
51
57
|
interestOracleDatum.interestRate.getOnChainInt *
|
|
52
58
|
mintedAmount) /
|
|
53
|
-
|
|
54
|
-
|
|
59
|
+
ONE_YEAR /
|
|
60
|
+
OCD_DECIMAL_UNIT
|
|
55
61
|
);
|
|
56
62
|
}
|
|
57
63
|
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Calculate the amount of interest needed to achieve 100% collateral ratio.
|
|
67
|
+
*/
|
|
68
|
+
export function computeInterestLovelacesFor100PercentCR(
|
|
69
|
+
collateral: bigint,
|
|
70
|
+
mintedAmt: bigint,
|
|
71
|
+
price: OnChainDecimal,
|
|
72
|
+
): bigint {
|
|
73
|
+
const amt = ocdCeil(
|
|
74
|
+
ocdAdd(
|
|
75
|
+
{ getOnChainInt: collateral * OCD_DECIMAL_UNIT },
|
|
76
|
+
ocdMul({ getOnChainInt: -mintedAmt * OCD_DECIMAL_UNIT }, price),
|
|
77
|
+
),
|
|
78
|
+
);
|
|
79
|
+
|
|
80
|
+
if (amt <= 0) {
|
|
81
|
+
return 0n;
|
|
82
|
+
} else {
|
|
83
|
+
return amt;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { applyParamsToScript } from '@lucid-evolution/lucid';
|
|
2
|
+
import { SpendingValidator } from '@lucid-evolution/lucid';
|
|
3
|
+
import {
|
|
4
|
+
castInterestOracleParams,
|
|
5
|
+
InterestOracleParams,
|
|
6
|
+
} from '../interest-oracle/types';
|
|
7
|
+
import { _interestOracleValidator } from '../../validators/interest-oracle-validator';
|
|
8
|
+
|
|
9
|
+
export function mkInterestOracleValidator(
|
|
10
|
+
params: InterestOracleParams,
|
|
11
|
+
): SpendingValidator {
|
|
12
|
+
return {
|
|
13
|
+
type: 'PlutusV2',
|
|
14
|
+
script: applyParamsToScript(_interestOracleValidator.cborHex, [
|
|
15
|
+
castInterestOracleParams(params),
|
|
16
|
+
]),
|
|
17
|
+
};
|
|
18
|
+
}
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import {
|
|
2
|
+
fromText,
|
|
3
|
+
LucidEvolution,
|
|
4
|
+
OutRef,
|
|
5
|
+
toUnit,
|
|
6
|
+
TxBuilder,
|
|
7
|
+
UTxO,
|
|
8
|
+
validatorToAddress,
|
|
9
|
+
} from '@lucid-evolution/lucid';
|
|
10
|
+
import { AssetClass } from '../../types/generic';
|
|
11
|
+
import {
|
|
12
|
+
InterestOracleParams,
|
|
13
|
+
parseInterestOracleDatum,
|
|
14
|
+
serialiseFeedInterestOracleRedeemer,
|
|
15
|
+
serialiseInterestOracleDatum,
|
|
16
|
+
} from './types';
|
|
17
|
+
import { oneShotMintTx } from '../one-shot/transactions';
|
|
18
|
+
import { mkInterestOracleValidator } from './scripts';
|
|
19
|
+
import { findInterestOracle } from '../../../tests/queries/interest-oracle-queries';
|
|
20
|
+
import { ONE_SECOND } from '../../utils/time-helpers';
|
|
21
|
+
import { calculateUnitaryInterestSinceOracleLastUpdated } from '../interest-oracle/helpers';
|
|
22
|
+
import { getInlineDatumOrThrow } from '../../utils/lucid-utils';
|
|
23
|
+
|
|
24
|
+
export async function startInterestOracle(
|
|
25
|
+
initialUnitaryInterest: bigint,
|
|
26
|
+
initialInterestRate: bigint,
|
|
27
|
+
initialLastInterestUpdate: bigint,
|
|
28
|
+
oracleParams: InterestOracleParams,
|
|
29
|
+
lucid: LucidEvolution,
|
|
30
|
+
interestTokenName?: string,
|
|
31
|
+
withScriptRef: boolean = false,
|
|
32
|
+
refOutRef?: OutRef,
|
|
33
|
+
): Promise<[TxBuilder, AssetClass]> {
|
|
34
|
+
const network = lucid.config().network!;
|
|
35
|
+
|
|
36
|
+
const tokenName = interestTokenName ?? 'INTEREST_ORACLE';
|
|
37
|
+
if (!refOutRef) {
|
|
38
|
+
refOutRef = (await lucid.wallet().getUtxos())[0];
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const [tx, policyId] = await oneShotMintTx(lucid, {
|
|
42
|
+
referenceOutRef: {
|
|
43
|
+
txHash: refOutRef.txHash,
|
|
44
|
+
outputIdx: BigInt(refOutRef.outputIndex),
|
|
45
|
+
},
|
|
46
|
+
mintAmounts: [
|
|
47
|
+
{
|
|
48
|
+
tokenName: fromText(tokenName),
|
|
49
|
+
amount: 1n,
|
|
50
|
+
},
|
|
51
|
+
],
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
const validator = mkInterestOracleValidator(oracleParams);
|
|
55
|
+
|
|
56
|
+
tx.pay.ToContract(
|
|
57
|
+
validatorToAddress(network, validator),
|
|
58
|
+
{
|
|
59
|
+
kind: 'inline',
|
|
60
|
+
value: serialiseInterestOracleDatum({
|
|
61
|
+
unitaryInterest: initialUnitaryInterest,
|
|
62
|
+
interestRate: {
|
|
63
|
+
getOnChainInt: initialInterestRate,
|
|
64
|
+
},
|
|
65
|
+
lastUpdated: initialLastInterestUpdate,
|
|
66
|
+
}),
|
|
67
|
+
},
|
|
68
|
+
{
|
|
69
|
+
lovelace: 2_500_000n,
|
|
70
|
+
[toUnit(policyId, fromText(tokenName))]: 1n,
|
|
71
|
+
},
|
|
72
|
+
);
|
|
73
|
+
|
|
74
|
+
if (withScriptRef) {
|
|
75
|
+
tx.pay.ToAddressWithData(
|
|
76
|
+
validatorToAddress(network, validator),
|
|
77
|
+
undefined,
|
|
78
|
+
undefined,
|
|
79
|
+
validator,
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
return [
|
|
84
|
+
tx,
|
|
85
|
+
{
|
|
86
|
+
currencySymbol: policyId,
|
|
87
|
+
tokenName: fromText(tokenName),
|
|
88
|
+
},
|
|
89
|
+
];
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export async function feedInterestOracle(
|
|
93
|
+
params: InterestOracleParams,
|
|
94
|
+
newInterestRate: bigint,
|
|
95
|
+
lucid: LucidEvolution,
|
|
96
|
+
assetClass?: AssetClass,
|
|
97
|
+
utxo?: UTxO,
|
|
98
|
+
scriptRef?: UTxO,
|
|
99
|
+
): Promise<TxBuilder> {
|
|
100
|
+
if (!assetClass && !utxo)
|
|
101
|
+
throw new Error('Either interest oracle nft or utxo must be provided');
|
|
102
|
+
if (assetClass && !utxo) {
|
|
103
|
+
utxo = await findInterestOracle(lucid, assetClass);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const now = BigInt(Date.now());
|
|
107
|
+
const tx = lucid.newTx();
|
|
108
|
+
const datum = parseInterestOracleDatum(getInlineDatumOrThrow(utxo!));
|
|
109
|
+
|
|
110
|
+
if (scriptRef) {
|
|
111
|
+
tx.readFrom([scriptRef]);
|
|
112
|
+
} else {
|
|
113
|
+
tx.attach.Script(mkInterestOracleValidator(params));
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
tx.collectFrom(
|
|
117
|
+
[utxo!],
|
|
118
|
+
serialiseFeedInterestOracleRedeemer({
|
|
119
|
+
newInterestRate: {
|
|
120
|
+
getOnChainInt: newInterestRate,
|
|
121
|
+
},
|
|
122
|
+
currentTime: now,
|
|
123
|
+
}),
|
|
124
|
+
);
|
|
125
|
+
|
|
126
|
+
tx.pay.ToContract(
|
|
127
|
+
utxo!.address,
|
|
128
|
+
{
|
|
129
|
+
kind: 'inline',
|
|
130
|
+
value: serialiseInterestOracleDatum({
|
|
131
|
+
unitaryInterest:
|
|
132
|
+
datum.unitaryInterest +
|
|
133
|
+
calculateUnitaryInterestSinceOracleLastUpdated(now, datum),
|
|
134
|
+
interestRate: {
|
|
135
|
+
getOnChainInt: newInterestRate,
|
|
136
|
+
},
|
|
137
|
+
lastUpdated: now,
|
|
138
|
+
}),
|
|
139
|
+
},
|
|
140
|
+
utxo!.assets,
|
|
141
|
+
);
|
|
142
|
+
|
|
143
|
+
tx.validFrom(Number(now) - ONE_SECOND);
|
|
144
|
+
tx.validTo(Number(now + params.biasTime) - ONE_SECOND);
|
|
145
|
+
|
|
146
|
+
tx.addSignerKey(params.owner);
|
|
147
|
+
|
|
148
|
+
return tx;
|
|
149
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Data, Datum } from '@lucid-evolution/lucid';
|
|
2
|
-
import { OnChainDecimalSchema } from '
|
|
2
|
+
import { OnChainDecimalSchema } from '../../types/on-chain-decimal';
|
|
3
3
|
|
|
4
4
|
export const InterestOracleDatumSchema = Data.Object({
|
|
5
5
|
unitaryInterest: Data.Integer(),
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { applyParamsToScript, SpendingValidator } from '@lucid-evolution/lucid';
|
|
2
|
+
import { castLrpParams, LRPParams } from './types';
|
|
3
|
+
import { fromSystemParamsAsset, LrpParamsSP } from '../../types/system-params';
|
|
4
|
+
import { _lrpValidator } from '../../validators/lrp-validator';
|
|
5
|
+
|
|
6
|
+
export const mkLrpValidator = (params: LRPParams): SpendingValidator => {
|
|
7
|
+
return {
|
|
8
|
+
type: 'PlutusV2',
|
|
9
|
+
script: applyParamsToScript(_lrpValidator.cborHex, [castLrpParams(params)]),
|
|
10
|
+
};
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export const mkLrpValidatorFromSP = (
|
|
14
|
+
params: LrpParamsSP,
|
|
15
|
+
): SpendingValidator => {
|
|
16
|
+
return {
|
|
17
|
+
type: 'PlutusV2',
|
|
18
|
+
script: applyParamsToScript(_lrpValidator.cborHex, [
|
|
19
|
+
castLrpParams({
|
|
20
|
+
versionRecordToken: fromSystemParamsAsset(params.versionRecordToken),
|
|
21
|
+
iassetNft: fromSystemParamsAsset(params.iassetNft),
|
|
22
|
+
minRedemptionLovelacesAmt: BigInt(params.minRedemptionLovelacesAmt),
|
|
23
|
+
iassetPolicyId: params.iassetPolicyId.unCurrencySymbol,
|
|
24
|
+
}),
|
|
25
|
+
]),
|
|
26
|
+
};
|
|
27
|
+
};
|
|
@@ -9,13 +9,12 @@ import {
|
|
|
9
9
|
addAssets,
|
|
10
10
|
unixTimeToSlot,
|
|
11
11
|
slotToUnixTime,
|
|
12
|
-
toUnit,
|
|
13
12
|
} from '@lucid-evolution/lucid';
|
|
14
13
|
import {
|
|
15
14
|
addrDetails,
|
|
16
15
|
createScriptAddress,
|
|
17
16
|
getInlineDatumOrThrow,
|
|
18
|
-
} from '
|
|
17
|
+
} from '../../utils/lucid-utils';
|
|
19
18
|
import { match, P } from 'ts-pattern';
|
|
20
19
|
import { unzip, zip } from 'fp-ts/lib/Array';
|
|
21
20
|
import { reduceWithIndex } from 'fp-ts/lib/Array';
|
|
@@ -25,18 +24,18 @@ import {
|
|
|
25
24
|
parseLrpDatum,
|
|
26
25
|
serialiseLrpDatum,
|
|
27
26
|
serialiseLrpRedeemer,
|
|
28
|
-
} from '
|
|
29
|
-
import { parsePriceOracleDatum } from '../
|
|
30
|
-
import { ocdMul, OnChainDecimal } from '
|
|
31
|
-
import {
|
|
27
|
+
} from './types';
|
|
28
|
+
import { parsePriceOracleDatum } from '../price-oracle/types';
|
|
29
|
+
import { ocdMul, OnChainDecimal } from '../../types/on-chain-decimal';
|
|
30
|
+
import { parseIAssetDatumOrThrow } from '../cdp/types';
|
|
32
31
|
import {
|
|
33
32
|
assetClassValueOf,
|
|
34
33
|
mkAssetsOf,
|
|
35
34
|
mkLovelacesOf,
|
|
36
|
-
} from '
|
|
37
|
-
import { calculateFeeFromPercentage } from '
|
|
38
|
-
import { matchSingle } from '
|
|
39
|
-
import { AssetClass } from '
|
|
35
|
+
} from '../../utils/value-helpers';
|
|
36
|
+
import { calculateFeeFromPercentage } from '../../utils/indigo-helpers';
|
|
37
|
+
import { matchSingle } from '../../utils/utils';
|
|
38
|
+
import { AssetClass } from '../../types/generic';
|
|
40
39
|
|
|
41
40
|
const MIN_UTXO_COLLATERAL_AMT = 2_000_000n;
|
|
42
41
|
|
|
@@ -117,7 +116,9 @@ export async function redeemLrp(
|
|
|
117
116
|
(_) => new Error('Expected a single IAsset UTXO'),
|
|
118
117
|
);
|
|
119
118
|
|
|
120
|
-
const iassetDatum =
|
|
119
|
+
const iassetDatum = parseIAssetDatumOrThrow(
|
|
120
|
+
getInlineDatumOrThrow(iassetUtxo),
|
|
121
|
+
);
|
|
121
122
|
|
|
122
123
|
const [lrpsToRedeemOutRefs, lrpRedemptionIAssetAmt] =
|
|
123
124
|
unzip(redemptionLrpsData);
|
|
@@ -153,32 +154,12 @@ export async function redeemLrp(
|
|
|
153
154
|
},
|
|
154
155
|
priceOracleDatum.price,
|
|
155
156
|
).getOnChainInt;
|
|
156
|
-
|
|
157
|
-
console.log(lovelacesForRedemption, redeemIAssetAmt, priceOracleDatum.price);
|
|
158
157
|
const reimburstmentLovelaces = calculateFeeFromPercentage(
|
|
159
158
|
iassetDatum.redemptionReimbursementPercentage,
|
|
160
159
|
lovelacesForRedemption,
|
|
161
160
|
);
|
|
162
161
|
|
|
163
|
-
console.log(reimburstmentLovelaces, iassetDatum.redemptionReimbursementPercentage, lovelacesForRedemption);
|
|
164
|
-
|
|
165
162
|
const lrpDatum = parseLrpDatum(getInlineDatumOrThrow(lrpUtxo));
|
|
166
|
-
const outputLovelacesAmt = lrpUtxo.assets.lovelace - lovelacesForRedemption + reimburstmentLovelaces;
|
|
167
|
-
const assetUnit = toUnit(lrpParams.iassetPolicyId, mainLrpDatum.iasset);
|
|
168
|
-
|
|
169
|
-
console.log(lrpUtxo.address,
|
|
170
|
-
{
|
|
171
|
-
kind: 'inline',
|
|
172
|
-
value: serialiseLrpDatum({
|
|
173
|
-
...lrpDatum,
|
|
174
|
-
lovelacesToSpend:
|
|
175
|
-
lrpDatum.lovelacesToSpend - lovelacesForRedemption,
|
|
176
|
-
}),
|
|
177
|
-
},
|
|
178
|
-
{
|
|
179
|
-
lovelace: outputLovelacesAmt,
|
|
180
|
-
[assetUnit]: redeemIAssetAmt + (lrpUtxo.assets[assetUnit] ?? 0n),
|
|
181
|
-
});
|
|
182
163
|
|
|
183
164
|
return acc
|
|
184
165
|
.collectFrom(
|
|
@@ -211,10 +192,17 @@ export async function redeemLrp(
|
|
|
211
192
|
lrpDatum.lovelacesToSpend - lovelacesForRedemption,
|
|
212
193
|
}),
|
|
213
194
|
},
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
195
|
+
addAssets(
|
|
196
|
+
lrpUtxo.assets,
|
|
197
|
+
mkLovelacesOf(-lovelacesForRedemption + reimburstmentLovelaces),
|
|
198
|
+
mkAssetsOf(
|
|
199
|
+
{
|
|
200
|
+
currencySymbol: lrpParams.iassetPolicyId,
|
|
201
|
+
tokenName: mainLrpDatum.iasset,
|
|
202
|
+
},
|
|
203
|
+
redeemIAssetAmt,
|
|
204
|
+
),
|
|
205
|
+
),
|
|
218
206
|
);
|
|
219
207
|
},
|
|
220
208
|
)(redemptionLrps);
|
|
@@ -250,8 +238,6 @@ export async function adjustLrp(
|
|
|
250
238
|
lrpRefScriptOutRef: OutRef,
|
|
251
239
|
lrpParams: LRPParams,
|
|
252
240
|
): Promise<TxBuilder> {
|
|
253
|
-
const ownAddr = await lucid.wallet().address();
|
|
254
|
-
|
|
255
241
|
const lrpScriptRefUtxo = matchSingle(
|
|
256
242
|
await lucid.utxosByOutRef([lrpRefScriptOutRef]),
|
|
257
243
|
(_) => new Error('Expected a single LRP Ref Script UTXO'),
|
|
@@ -306,7 +292,7 @@ export async function adjustLrp(
|
|
|
306
292
|
mkLovelacesOf(lovelacesAdjustAmt),
|
|
307
293
|
),
|
|
308
294
|
)
|
|
309
|
-
.
|
|
295
|
+
.addSignerKey(lrpDatum.owner);
|
|
310
296
|
}
|
|
311
297
|
|
|
312
298
|
/**
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Data, Datum, Redeemer } from '@lucid-evolution/lucid';
|
|
2
|
-
import { AssetClassSchema, OutputReferenceSchema } from '
|
|
3
|
-
import { OnChainDecimalSchema } from '
|
|
2
|
+
import { AssetClassSchema, OutputReferenceSchema } from '../../types/generic';
|
|
3
|
+
import { OnChainDecimalSchema } from '../../types/on-chain-decimal';
|
|
4
4
|
|
|
5
5
|
export const LRPParamsSchema = Data.Object({
|
|
6
6
|
versionRecordToken: AssetClassSchema,
|
|
@@ -51,7 +51,7 @@ export function serialiseLrpDatum(datum: LRPDatum): Datum {
|
|
|
51
51
|
}
|
|
52
52
|
|
|
53
53
|
export function serialiseLrpRedeemer(redeemer: LRPRedeemer): Redeemer {
|
|
54
|
-
return Data.to<LRPRedeemer>(redeemer, LRPRedeemer
|
|
54
|
+
return Data.to<LRPRedeemer>(redeemer, LRPRedeemer);
|
|
55
55
|
}
|
|
56
56
|
|
|
57
57
|
export function castLrpParams(params: LRPParams): Data {
|
|
@@ -9,9 +9,9 @@ import {
|
|
|
9
9
|
toUnit,
|
|
10
10
|
TxBuilder,
|
|
11
11
|
} from '@lucid-evolution/lucid';
|
|
12
|
-
import { OneShotParams } from '
|
|
13
|
-
import { matchSingle } from '
|
|
14
|
-
import { mkOneShotPolicy } from '
|
|
12
|
+
import { OneShotParams } from './types';
|
|
13
|
+
import { matchSingle } from '../../utils/utils';
|
|
14
|
+
import { mkOneShotPolicy } from './scripts';
|
|
15
15
|
import { reduce } from 'fp-ts/lib/Array';
|
|
16
16
|
|
|
17
17
|
export async function oneShotMintTx(
|