@indigo-labs/indigo-sdk 0.2.41 → 0.3.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/.github/workflows/ci.yml +4 -2
- package/dist/index.d.mts +3008 -2194
- package/dist/index.d.ts +3008 -2194
- package/dist/index.js +9827 -6194
- package/dist/index.mjs +8591 -4809
- package/package.json +14 -3
- package/src/contracts/cdp/helpers.ts +68 -72
- package/src/contracts/cdp/scripts.ts +50 -13
- package/src/contracts/cdp/transactions.ts +831 -545
- package/src/contracts/cdp/types-new.ts +256 -0
- package/src/contracts/cdp/types.ts +26 -144
- package/src/contracts/cdp-creator/scripts.ts +15 -9
- package/src/contracts/cdp-creator/types-new.ts +50 -0
- package/src/contracts/cdp-creator/types.ts +5 -31
- package/src/contracts/collector/scripts.ts +1 -1
- package/src/contracts/collector/transactions.ts +23 -13
- package/src/contracts/collector/types-new.ts +17 -0
- package/src/contracts/execute/scripts.ts +19 -10
- package/src/contracts/execute/types-new.ts +44 -0
- package/src/contracts/execute/types.ts +5 -38
- package/src/contracts/gov/helpers.ts +187 -51
- package/src/contracts/gov/scripts.ts +17 -10
- package/src/contracts/gov/transactions.ts +599 -271
- package/src/contracts/gov/types-new.ts +253 -100
- package/src/contracts/gov/types.ts +4 -71
- package/src/contracts/iasset/helpers.ts +172 -0
- package/src/contracts/iasset/scripts.ts +38 -0
- package/src/contracts/iasset/types.ts +154 -0
- package/src/contracts/initialize/actions.ts +768 -0
- package/src/contracts/initialize/helpers.ts +611 -36
- package/src/contracts/initialize/types.ts +102 -28
- package/src/contracts/interest-collection/helpers.ts +19 -0
- package/src/contracts/interest-collection/scripts.ts +44 -0
- package/src/contracts/interest-collection/transactions.ts +436 -0
- package/src/contracts/interest-collection/types-new.ts +50 -0
- package/src/contracts/interest-collection/types.ts +26 -0
- package/src/contracts/interest-oracle/helpers.ts +2 -30
- package/src/contracts/interest-oracle/scripts.ts +1 -1
- package/src/contracts/interest-oracle/transactions.ts +21 -16
- package/src/contracts/interest-oracle/types-new.ts +32 -0
- package/src/contracts/interest-oracle/types.ts +1 -40
- package/src/contracts/one-shot/transactions.ts +1 -2
- package/src/contracts/poll/helpers.ts +5 -23
- package/src/contracts/poll/scripts.ts +12 -13
- package/src/contracts/poll/types-poll-manager.ts +1 -19
- package/src/contracts/poll/types-poll-new.ts +170 -0
- package/src/contracts/poll/types-poll-shard.ts +2 -24
- package/src/contracts/price-oracle/helpers.ts +1 -4
- package/src/contracts/price-oracle/scripts.ts +3 -8
- package/src/contracts/price-oracle/transactions.ts +32 -25
- package/src/contracts/price-oracle/types-new.ts +50 -0
- package/src/contracts/price-oracle/types.ts +2 -36
- package/src/contracts/pyth-feed/helpers.ts +58 -0
- package/src/contracts/pyth-feed/scripts.ts +15 -0
- package/src/contracts/pyth-feed/types.ts +181 -0
- package/src/contracts/rob/helpers.ts +405 -0
- package/src/contracts/rob/scripts.ts +35 -0
- package/src/contracts/rob/transactions.ts +410 -0
- package/src/contracts/rob/types-new.ts +128 -0
- package/src/contracts/rob/types.ts +16 -0
- package/src/contracts/rob-leverage/helpers.ts +424 -0
- package/src/contracts/{leverage → rob-leverage}/transactions.ts +68 -48
- package/src/contracts/stability-pool/helpers.ts +714 -230
- package/src/contracts/stability-pool/scripts.ts +20 -15
- package/src/contracts/stability-pool/transactions.ts +625 -495
- package/src/contracts/stability-pool/types-new.ts +237 -100
- package/src/contracts/stability-pool/types.ts +5 -22
- package/src/contracts/stableswap/helpers.ts +22 -0
- package/src/contracts/stableswap/scripts.ts +37 -0
- package/src/contracts/stableswap/transactions.ts +647 -0
- package/src/contracts/stableswap/types-new.ts +131 -0
- package/src/contracts/stableswap/types.ts +17 -0
- package/src/contracts/staking/helpers.ts +49 -34
- package/src/contracts/staking/scripts.ts +1 -1
- package/src/contracts/staking/transactions.ts +85 -130
- package/src/contracts/staking/types-new.ts +60 -28
- package/src/contracts/staking/types.ts +1 -28
- package/src/contracts/treasury/helpers.ts +21 -0
- package/src/contracts/treasury/scripts.ts +16 -26
- package/src/contracts/treasury/transactions.ts +256 -27
- package/src/contracts/treasury/types-new.ts +69 -0
- package/src/contracts/treasury/types.ts +2 -43
- package/src/contracts/version-registry/scripts.ts +2 -2
- package/src/contracts/version-registry/types-new.ts +6 -7
- package/src/index.ts +37 -20
- package/src/scripts/auth-token-policy.ts +3 -2
- package/src/scripts/iasset-policy.ts +3 -2
- package/src/types/evolution-schema-options.ts +3 -3
- package/src/types/generic.ts +17 -89
- package/src/types/multisig.ts +48 -0
- package/src/types/on-chain-decimal.ts +14 -7
- package/src/types/rational.ts +61 -0
- package/src/types/system-params.ts +237 -41
- package/src/utils/array-utils.ts +70 -1
- package/src/utils/bigint-utils.ts +12 -0
- package/src/utils/indigo-helpers.ts +8 -10
- package/src/utils/lucid-utils.ts +47 -40
- package/src/utils/oracle-helpers.ts +62 -0
- package/src/utils/pyth/decode.ts +223 -0
- package/src/utils/pyth/encode.ts +262 -0
- package/src/utils/pyth/index.ts +14 -0
- package/src/utils/pyth/types.ts +87 -0
- package/src/validators/always-succeed-validator.ts +6 -0
- package/src/validators/cdp-creator-validator.ts +2 -2
- package/src/validators/cdp-redeem-validator.ts +7 -0
- package/src/validators/cdp-validator.ts +2 -2
- package/src/validators/collector-validator.ts +2 -2
- package/src/validators/execute-validator.ts +2 -2
- package/src/validators/governance-validator.ts +2 -2
- package/src/validators/iasset-validator.ts +7 -0
- package/src/validators/interest-collection-validator.ts +7 -0
- package/src/validators/interest-oracle-validator.ts +2 -2
- package/src/validators/poll-manager-validator.ts +2 -2
- package/src/validators/poll-shard-validator.ts +2 -2
- package/src/validators/price-oracle-validator.ts +7 -0
- package/src/validators/pyth-feed-validator.ts +7 -0
- package/src/validators/rob-validator.ts +7 -0
- package/src/validators/stability-pool-validator.ts +2 -2
- package/src/validators/stableswap-validator.ts +7 -0
- package/src/validators/staking-validator.ts +2 -2
- package/src/validators/treasury-validator.ts +2 -2
- package/src/validators/version-record-policy.ts +2 -2
- package/src/validators/version-registry-validator.ts +2 -2
- package/tests/always-succeed/script.ts +7 -0
- package/tests/bigint-utils.test.ts +41 -0
- package/tests/cdp/actions.ts +611 -0
- package/tests/cdp/cdp-helpers.ts +55 -0
- package/tests/cdp/cdp-queries.ts +440 -0
- package/tests/cdp/cdp.test.ts +6087 -0
- package/tests/cdp/transactions-mutated.ts +1729 -0
- package/tests/data/system-params.json +177 -34
- package/tests/datums.test.ts +209 -210
- package/tests/endpoints/initialize.ts +68 -0
- package/tests/endpoints/interest-collector.ts +37 -0
- package/tests/endpoints/treasury.ts +70 -0
- package/tests/gov/actions.ts +406 -0
- package/tests/gov/gov.test.ts +4450 -0
- package/tests/{queries → gov}/governance-queries.ts +6 -3
- package/tests/hash-checks.test.ts +38 -11
- package/tests/indigo-test-helpers.ts +100 -0
- package/tests/initialize.test.ts +61 -9
- package/tests/interest-collection/interest-collection.test.ts +892 -0
- package/tests/interest-collection/interest-collector-queries.ts +49 -0
- package/tests/interest-collection/transactions-mutated.ts +260 -0
- package/tests/interest-oracle.test.ts +43 -35
- package/tests/mock/assets-mock.ts +234 -23
- package/tests/mock/protocol-params-mock.ts +21 -0
- package/tests/price-oracle/actions.ts +163 -0
- package/tests/price-oracle/price-oracle-queries.ts +12 -0
- package/tests/price-oracle/price-oracle.test.ts +240 -0
- package/tests/price-oracle/transactions-mutated.ts +62 -0
- package/tests/pyth/endpoints.ts +96 -0
- package/tests/pyth/helpers.ts +37 -0
- package/tests/pyth/pyth-encoding.test.ts +376 -0
- package/tests/pyth/pyth-indigo.test.ts +509 -0
- package/tests/pyth/pyth.test.ts +300 -0
- package/tests/queries/execute-queries.ts +6 -5
- package/tests/queries/iasset-queries.ts +175 -5
- package/tests/queries/interest-oracle-queries.ts +4 -2
- package/tests/queries/poll-queries.ts +8 -9
- package/tests/queries/stability-pool-queries.ts +95 -48
- package/tests/queries/staking-queries.ts +4 -2
- package/tests/queries/treasury-queries.ts +80 -5
- package/tests/rob/actions.ts +58 -0
- package/tests/{lrp-leverage.test.ts → rob/rob-leverage.test.ts} +393 -296
- package/tests/rob/rob-queries.ts +95 -0
- package/tests/rob/rob.test.ts +3762 -0
- package/tests/rob/transactions-mutated.ts +853 -0
- package/tests/script-size.test.ts +240 -0
- package/tests/setup.ts +135 -0
- package/tests/stability-pool/actions.ts +210 -0
- package/tests/stability-pool.test.ts +5469 -666
- package/tests/stableswap/stableswap-actions.ts +84 -0
- package/tests/stableswap/stableswap-queries.ts +89 -0
- package/tests/stableswap/stableswap.test.ts +3891 -0
- package/tests/stableswap/transactions-mutated.ts +348 -0
- package/tests/staking.test.ts +82 -99
- package/tests/test-helpers.ts +58 -11
- package/tests/treasury.test.ts +242 -0
- package/tests/utils/asserts.ts +74 -0
- package/tests/utils/benchmark-utils.ts +81 -0
- package/tests/utils/index.ts +122 -4
- package/tsconfig.json +9 -1
- package/vitest.config.ts +3 -1
- package/src/contracts/collector/types.ts +0 -16
- package/src/contracts/initialize/transactions.ts +0 -891
- package/src/contracts/leverage/helpers.ts +0 -424
- package/src/contracts/lrp/helpers.ts +0 -294
- package/src/contracts/lrp/scripts.ts +0 -27
- package/src/contracts/lrp/transactions.ts +0 -250
- package/src/contracts/lrp/types.ts +0 -131
- package/src/contracts/poll/types-poll.ts +0 -88
- package/src/contracts/vesting/helpers.ts +0 -218
- package/src/utils/value-helpers.ts +0 -37
- package/src/validators/lrp-validator.ts +0 -7
- package/tests/cdp.test.ts +0 -1528
- package/tests/gov.test.ts +0 -2011
- package/tests/lrp.test.ts +0 -673
- package/tests/queries/cdp-queries.ts +0 -220
- package/tests/queries/lrp-queries.ts +0 -76
- package/tests/queries/price-oracle-queries.ts +0 -10
|
@@ -0,0 +1,1729 @@
|
|
|
1
|
+
import {
|
|
2
|
+
addAssets,
|
|
3
|
+
Data,
|
|
4
|
+
fromHex,
|
|
5
|
+
LucidEvolution,
|
|
6
|
+
Credential,
|
|
7
|
+
OutRef,
|
|
8
|
+
slotToUnixTime,
|
|
9
|
+
toHex,
|
|
10
|
+
TxBuilder,
|
|
11
|
+
UTxO,
|
|
12
|
+
getInputIndices,
|
|
13
|
+
credentialToAddress,
|
|
14
|
+
validatorToScriptHash,
|
|
15
|
+
credentialToRewardAddress,
|
|
16
|
+
scriptHashToCredential,
|
|
17
|
+
} from '@lucid-evolution/lucid';
|
|
18
|
+
import {
|
|
19
|
+
fromSystemParamsAsset,
|
|
20
|
+
fromSystemParamsScriptRef,
|
|
21
|
+
SystemParams,
|
|
22
|
+
} from '../../src/types/system-params';
|
|
23
|
+
import {
|
|
24
|
+
addrDetails,
|
|
25
|
+
calculateMinCollateralCappedIAssetRedemptionAmt,
|
|
26
|
+
createScriptAddress,
|
|
27
|
+
getInlineDatumOrThrow,
|
|
28
|
+
matchSingle,
|
|
29
|
+
mkPriceOracleValidator,
|
|
30
|
+
mkTreasuryValidatorFromSP,
|
|
31
|
+
ONE_SECOND,
|
|
32
|
+
PriceOracleParams,
|
|
33
|
+
treasuryFeeTx,
|
|
34
|
+
} from '../../src';
|
|
35
|
+
import {
|
|
36
|
+
parseCdpDatumOrThrow,
|
|
37
|
+
serialiseCdpDatum,
|
|
38
|
+
serialiseCdpRedeemer,
|
|
39
|
+
serialiseRedeemCdpWithdrawalRedeemer,
|
|
40
|
+
} from '../../src/contracts/cdp/types-new';
|
|
41
|
+
import {
|
|
42
|
+
parseCollateralAssetDatumOrThrow,
|
|
43
|
+
parseIAssetDatumOrThrow,
|
|
44
|
+
} from '../../src/contracts/iasset/types';
|
|
45
|
+
import {
|
|
46
|
+
parsePriceOracleDatum,
|
|
47
|
+
serialisePriceOracleDatum,
|
|
48
|
+
serialisePriceOracleRedeemer,
|
|
49
|
+
} from '../../src/contracts/price-oracle/types-new';
|
|
50
|
+
import { parseInterestOracleDatum } from '../../src/contracts/interest-oracle/types-new';
|
|
51
|
+
import { parseGovDatumOrThrow } from '../../src/contracts/gov/types-new';
|
|
52
|
+
import { match, P } from 'ts-pattern';
|
|
53
|
+
import {
|
|
54
|
+
calculateAccruedInterest,
|
|
55
|
+
calculateUnitaryInterestSinceOracleLastUpdated,
|
|
56
|
+
} from '../../src/contracts/interest-oracle/helpers';
|
|
57
|
+
import { calculateFeeFromRatio } from '../../src/utils/indigo-helpers';
|
|
58
|
+
import {
|
|
59
|
+
AssetClass,
|
|
60
|
+
assetClassValueOf,
|
|
61
|
+
mkAssetsOf,
|
|
62
|
+
mkLovelacesOf,
|
|
63
|
+
} from '@3rd-eye-labs/cardano-offchain-common';
|
|
64
|
+
import { bigintMin } from '../../src/utils/bigint-utils';
|
|
65
|
+
import { oracleExpirationAwareValidity } from '../../src/contracts/price-oracle/helpers';
|
|
66
|
+
import {
|
|
67
|
+
findAllNecessaryOrefs,
|
|
68
|
+
findCdp,
|
|
69
|
+
findPriceOracleFromCollateralAsset,
|
|
70
|
+
} from './cdp-queries';
|
|
71
|
+
import { collectInterestTx } from '../../src/contracts/interest-collection/transactions';
|
|
72
|
+
import { findCollateralAsset, findIAsset } from '../queries/iasset-queries';
|
|
73
|
+
import { findInterestOracle } from '../queries/interest-oracle-queries';
|
|
74
|
+
import { AssetInfo } from '../endpoints/initialize';
|
|
75
|
+
import { LucidContext } from '../test-helpers';
|
|
76
|
+
import { option as O, function as F } from 'fp-ts';
|
|
77
|
+
import { findPriceOracle } from '../price-oracle/price-oracle-queries';
|
|
78
|
+
import { findRandomNonAdminInterestCollector } from '../interest-collection/interest-collector-queries';
|
|
79
|
+
import {
|
|
80
|
+
serialiseCDPCreatorDatum,
|
|
81
|
+
serialiseCDPCreatorRedeemer,
|
|
82
|
+
} from '../../src/contracts/cdp-creator/types-new';
|
|
83
|
+
import {
|
|
84
|
+
CollectInterestVariation,
|
|
85
|
+
testCollectInterest,
|
|
86
|
+
} from '../interest-collection/transactions-mutated';
|
|
87
|
+
import { findRandomTreasuryUtxoWithOnlyAda } from '../queries/treasury-queries';
|
|
88
|
+
import {
|
|
89
|
+
Rational,
|
|
90
|
+
rationalFloor,
|
|
91
|
+
rationalFromInt,
|
|
92
|
+
rationalMul,
|
|
93
|
+
} from '../../src/types/rational';
|
|
94
|
+
import { retrieveAdjustedPrice } from '../../src/utils/oracle-helpers';
|
|
95
|
+
import * as Core from '@evolution-sdk/evolution';
|
|
96
|
+
|
|
97
|
+
export async function mutatedRedeemCdp(
|
|
98
|
+
/**
|
|
99
|
+
* When the goal is to redeem the maximum possible, just pass in the total minted amount of the CDP.
|
|
100
|
+
* The logic will automatically cap the amount to the max.
|
|
101
|
+
*/
|
|
102
|
+
attemptedRedemptionIAssetAmt: bigint,
|
|
103
|
+
cdpOref: OutRef,
|
|
104
|
+
iassetOref: OutRef,
|
|
105
|
+
collateralAssetOref: OutRef,
|
|
106
|
+
priceOracleOref: OutRef | undefined,
|
|
107
|
+
interestOracleOref: OutRef,
|
|
108
|
+
interestCollectorOref: OutRef,
|
|
109
|
+
govOref: OutRef,
|
|
110
|
+
sysParams: SystemParams,
|
|
111
|
+
lucid: LucidEvolution,
|
|
112
|
+
currentSlot: number,
|
|
113
|
+
pythMessage?: string,
|
|
114
|
+
): Promise<TxBuilder> {
|
|
115
|
+
const network = lucid.config().network!;
|
|
116
|
+
const currentTime = BigInt(slotToUnixTime(network, currentSlot));
|
|
117
|
+
|
|
118
|
+
const cdpRefScriptUtxo = matchSingle(
|
|
119
|
+
await lucid.utxosByOutRef([
|
|
120
|
+
fromSystemParamsScriptRef(sysParams.scriptReferences.cdpValidatorRef),
|
|
121
|
+
]),
|
|
122
|
+
(_) => new Error('Expected a single cdp Ref Script UTXO'),
|
|
123
|
+
);
|
|
124
|
+
|
|
125
|
+
const cdpRedeemRefScriptUtxo = matchSingle(
|
|
126
|
+
await lucid.utxosByOutRef([
|
|
127
|
+
fromSystemParamsScriptRef(
|
|
128
|
+
sysParams.scriptReferences.cdpRedeemValidatorRef,
|
|
129
|
+
),
|
|
130
|
+
]),
|
|
131
|
+
(_) => new Error('Expected a single cdp redeem Ref Script UTXO'),
|
|
132
|
+
);
|
|
133
|
+
|
|
134
|
+
const iAssetTokenPolicyRefScriptUtxo = matchSingle(
|
|
135
|
+
await lucid.utxosByOutRef([
|
|
136
|
+
fromSystemParamsScriptRef(
|
|
137
|
+
sysParams.scriptReferences.iAssetTokenPolicyRef,
|
|
138
|
+
),
|
|
139
|
+
]),
|
|
140
|
+
(_) => new Error('Expected a single iasset token policy Ref Script UTXO'),
|
|
141
|
+
);
|
|
142
|
+
|
|
143
|
+
const cdpUtxo = matchSingle(
|
|
144
|
+
await lucid.utxosByOutRef([cdpOref]),
|
|
145
|
+
(_) => new Error('Expected a single cdp UTXO'),
|
|
146
|
+
);
|
|
147
|
+
const cdpDatum = parseCdpDatumOrThrow(getInlineDatumOrThrow(cdpUtxo));
|
|
148
|
+
|
|
149
|
+
const iassetUtxo = matchSingle(
|
|
150
|
+
await lucid.utxosByOutRef([iassetOref]),
|
|
151
|
+
(_) => new Error('Expected a single iasset UTXO'),
|
|
152
|
+
);
|
|
153
|
+
const iassetDatum = parseIAssetDatumOrThrow(
|
|
154
|
+
getInlineDatumOrThrow(iassetUtxo),
|
|
155
|
+
);
|
|
156
|
+
|
|
157
|
+
const collateralAssetUtxo = matchSingle(
|
|
158
|
+
await lucid.utxosByOutRef([collateralAssetOref]),
|
|
159
|
+
(_) => new Error('Expected a single iasset UTXO'),
|
|
160
|
+
);
|
|
161
|
+
const collateralAssetDatum = parseCollateralAssetDatumOrThrow(
|
|
162
|
+
getInlineDatumOrThrow(collateralAssetUtxo),
|
|
163
|
+
);
|
|
164
|
+
|
|
165
|
+
const isDelisted = match(collateralAssetDatum.priceInfo)
|
|
166
|
+
.with({ Delisted: P.any }, () => true)
|
|
167
|
+
.otherwise(() => false);
|
|
168
|
+
|
|
169
|
+
if (!isDelisted && priceOracleOref === undefined) {
|
|
170
|
+
throw new Error('Missing price oracle');
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
const [adjustedPrice, priceOracleUtxo] = await retrieveAdjustedPrice(
|
|
174
|
+
iassetDatum.assetName,
|
|
175
|
+
collateralAssetDatum.collateralAsset,
|
|
176
|
+
collateralAssetDatum.priceInfo,
|
|
177
|
+
collateralAssetDatum.extraDecimals,
|
|
178
|
+
priceOracleOref,
|
|
179
|
+
pythMessage,
|
|
180
|
+
sysParams.pythConfig,
|
|
181
|
+
lucid,
|
|
182
|
+
);
|
|
183
|
+
|
|
184
|
+
const interestOracleUtxo = matchSingle(
|
|
185
|
+
await lucid.utxosByOutRef([interestOracleOref]),
|
|
186
|
+
(_) => new Error('Expected a single interest oracle UTXO'),
|
|
187
|
+
);
|
|
188
|
+
const interestOracleDatum = parseInterestOracleDatum(
|
|
189
|
+
getInlineDatumOrThrow(interestOracleUtxo),
|
|
190
|
+
);
|
|
191
|
+
|
|
192
|
+
const govUtxo = matchSingle(
|
|
193
|
+
await lucid.utxosByOutRef([govOref]),
|
|
194
|
+
(_) => new Error('Expected a single gov UTXO'),
|
|
195
|
+
);
|
|
196
|
+
const govDatum = parseGovDatumOrThrow(getInlineDatumOrThrow(govUtxo));
|
|
197
|
+
|
|
198
|
+
const interestAmt = match(cdpDatum.cdpFees)
|
|
199
|
+
.with({ FrozenCDPAccumulatedFees: P.any }, () => {
|
|
200
|
+
throw new Error('CDP fees wrong');
|
|
201
|
+
})
|
|
202
|
+
.with({ ActiveCDPInterestTracking: P.select() }, (interest) => {
|
|
203
|
+
return calculateAccruedInterest(
|
|
204
|
+
currentTime,
|
|
205
|
+
interest.unitaryInterestSnapshot,
|
|
206
|
+
cdpDatum.mintedAmt,
|
|
207
|
+
interest.lastSettled,
|
|
208
|
+
interestOracleDatum,
|
|
209
|
+
);
|
|
210
|
+
})
|
|
211
|
+
.exhaustive();
|
|
212
|
+
|
|
213
|
+
const collateralAmt = assetClassValueOf(
|
|
214
|
+
cdpUtxo.assets,
|
|
215
|
+
cdpDatum.collateralAsset,
|
|
216
|
+
);
|
|
217
|
+
|
|
218
|
+
const totalCdpDebt = cdpDatum.mintedAmt + interestAmt;
|
|
219
|
+
|
|
220
|
+
const [isPartial, redemptionIAssetAmt] = (() => {
|
|
221
|
+
const res = calculateMinCollateralCappedIAssetRedemptionAmt(
|
|
222
|
+
collateralAmt,
|
|
223
|
+
totalCdpDebt,
|
|
224
|
+
adjustedPrice,
|
|
225
|
+
collateralAssetDatum.redemptionRatio,
|
|
226
|
+
iassetDatum.redemptionReimbursementRatio,
|
|
227
|
+
BigInt(collateralAssetDatum.minCollateralAmt),
|
|
228
|
+
);
|
|
229
|
+
|
|
230
|
+
const redemptionAmt = bigintMin(
|
|
231
|
+
attemptedRedemptionIAssetAmt,
|
|
232
|
+
res.cappedIAssetRedemptionAmt,
|
|
233
|
+
);
|
|
234
|
+
|
|
235
|
+
return [redemptionAmt < res.cappedIAssetRedemptionAmt, redemptionAmt];
|
|
236
|
+
})();
|
|
237
|
+
|
|
238
|
+
if (redemptionIAssetAmt <= 0) {
|
|
239
|
+
throw new Error("There's no iAssets available for redemption.");
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
const redemptionCollateralAmt = rationalFloor(
|
|
243
|
+
rationalMul(adjustedPrice, rationalFromInt(redemptionIAssetAmt)),
|
|
244
|
+
);
|
|
245
|
+
|
|
246
|
+
const isPublicRedemption =
|
|
247
|
+
!govDatum.protocolParams.cdpRedemptionRequiredSignature;
|
|
248
|
+
|
|
249
|
+
const partialRedemptionFee =
|
|
250
|
+
isPartial && isPublicRedemption
|
|
251
|
+
? BigInt(sysParams.cdpRedeemParams.partialRedemptionExtraFeeLovelace)
|
|
252
|
+
: 0n;
|
|
253
|
+
|
|
254
|
+
const processingFee = calculateFeeFromRatio(
|
|
255
|
+
iassetDatum.redemptionProcessingFeeRatio,
|
|
256
|
+
redemptionIAssetAmt,
|
|
257
|
+
);
|
|
258
|
+
|
|
259
|
+
const reimburstmentFee = calculateFeeFromRatio(
|
|
260
|
+
iassetDatum.redemptionReimbursementRatio,
|
|
261
|
+
redemptionCollateralAmt,
|
|
262
|
+
);
|
|
263
|
+
|
|
264
|
+
const referenceScripts = [
|
|
265
|
+
cdpRefScriptUtxo,
|
|
266
|
+
iAssetTokenPolicyRefScriptUtxo,
|
|
267
|
+
cdpRedeemRefScriptUtxo,
|
|
268
|
+
];
|
|
269
|
+
|
|
270
|
+
const referenceInputs = [
|
|
271
|
+
iassetUtxo,
|
|
272
|
+
collateralAssetUtxo,
|
|
273
|
+
interestOracleUtxo,
|
|
274
|
+
govUtxo,
|
|
275
|
+
];
|
|
276
|
+
|
|
277
|
+
const tx = lucid
|
|
278
|
+
.newTx()
|
|
279
|
+
// Ref Script
|
|
280
|
+
.readFrom(referenceScripts)
|
|
281
|
+
// Ref inputs
|
|
282
|
+
.readFrom(referenceInputs)
|
|
283
|
+
.collectFrom([cdpUtxo], serialiseCdpRedeemer('RedeemCdp'))
|
|
284
|
+
.mintAssets(
|
|
285
|
+
mkAssetsOf(
|
|
286
|
+
{
|
|
287
|
+
currencySymbol: fromHex(
|
|
288
|
+
sysParams.cdpParams.cdpAssetSymbol.unCurrencySymbol,
|
|
289
|
+
),
|
|
290
|
+
tokenName: iassetDatum.assetName,
|
|
291
|
+
},
|
|
292
|
+
interestAmt - redemptionIAssetAmt,
|
|
293
|
+
),
|
|
294
|
+
Data.void(),
|
|
295
|
+
)
|
|
296
|
+
.pay.ToContract(
|
|
297
|
+
cdpUtxo.address,
|
|
298
|
+
{
|
|
299
|
+
kind: 'inline',
|
|
300
|
+
value: serialiseCdpDatum({
|
|
301
|
+
...cdpDatum,
|
|
302
|
+
mintedAmt: totalCdpDebt - redemptionIAssetAmt,
|
|
303
|
+
cdpFees: {
|
|
304
|
+
ActiveCDPInterestTracking: {
|
|
305
|
+
lastSettled: currentTime,
|
|
306
|
+
unitaryInterestSnapshot:
|
|
307
|
+
interestOracleDatum.unitaryInterest +
|
|
308
|
+
calculateUnitaryInterestSinceOracleLastUpdated(
|
|
309
|
+
currentTime,
|
|
310
|
+
interestOracleDatum,
|
|
311
|
+
),
|
|
312
|
+
},
|
|
313
|
+
},
|
|
314
|
+
}),
|
|
315
|
+
},
|
|
316
|
+
addAssets(
|
|
317
|
+
cdpUtxo.assets,
|
|
318
|
+
mkLovelacesOf(-redemptionCollateralAmt),
|
|
319
|
+
mkLovelacesOf(reimburstmentFee),
|
|
320
|
+
),
|
|
321
|
+
);
|
|
322
|
+
|
|
323
|
+
if (priceOracleUtxo !== undefined) {
|
|
324
|
+
const priceOracleDatum = parsePriceOracleDatum(
|
|
325
|
+
getInlineDatumOrThrow(priceOracleUtxo),
|
|
326
|
+
);
|
|
327
|
+
const txValidity = oracleExpirationAwareValidity(
|
|
328
|
+
currentSlot,
|
|
329
|
+
Number(sysParams.cdpCreatorParams.biasTime),
|
|
330
|
+
Number(priceOracleDatum.expirationTime),
|
|
331
|
+
network,
|
|
332
|
+
);
|
|
333
|
+
|
|
334
|
+
referenceInputs.push(priceOracleUtxo);
|
|
335
|
+
|
|
336
|
+
tx.validFrom(txValidity.validFrom)
|
|
337
|
+
.validTo(txValidity.validTo)
|
|
338
|
+
.readFrom([priceOracleUtxo]);
|
|
339
|
+
} else {
|
|
340
|
+
const validateFrom = slotToUnixTime(network, currentSlot - 1);
|
|
341
|
+
const validateTo =
|
|
342
|
+
validateFrom + Number(sysParams.cdpCreatorParams.biasTime);
|
|
343
|
+
|
|
344
|
+
tx.validFrom(validateFrom).validTo(validateTo);
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
// Trigger CDP Redeem Withdrawal validator
|
|
348
|
+
tx.withdraw(
|
|
349
|
+
credentialToRewardAddress(
|
|
350
|
+
lucid.config().network!,
|
|
351
|
+
scriptHashToCredential(sysParams.cdpParams.cdpRedeemValHash),
|
|
352
|
+
),
|
|
353
|
+
0n,
|
|
354
|
+
serialiseRedeemCdpWithdrawalRedeemer({
|
|
355
|
+
cdpOutReference: {
|
|
356
|
+
txHash: fromHex(cdpUtxo.txHash),
|
|
357
|
+
outputIndex: BigInt(cdpUtxo.outputIndex),
|
|
358
|
+
},
|
|
359
|
+
currentTime: currentTime,
|
|
360
|
+
priceOracleIdx: priceOracleUtxo
|
|
361
|
+
? {
|
|
362
|
+
OracleRefInputIdx: getInputIndices(
|
|
363
|
+
[priceOracleUtxo],
|
|
364
|
+
[...referenceInputs, ...referenceScripts],
|
|
365
|
+
)[0],
|
|
366
|
+
}
|
|
367
|
+
: 'OracleVoid',
|
|
368
|
+
}),
|
|
369
|
+
);
|
|
370
|
+
|
|
371
|
+
//TODO: Use a treasury input to save on ADA.
|
|
372
|
+
tx.pay.ToContract(
|
|
373
|
+
credentialToAddress(lucid.config().network!, {
|
|
374
|
+
hash: validatorToScriptHash(
|
|
375
|
+
mkTreasuryValidatorFromSP(sysParams.treasuryParams),
|
|
376
|
+
),
|
|
377
|
+
type: 'Script',
|
|
378
|
+
}),
|
|
379
|
+
{ kind: 'inline', value: Data.void() },
|
|
380
|
+
addAssets(
|
|
381
|
+
mkAssetsOf(cdpDatum.collateralAsset, processingFee),
|
|
382
|
+
mkLovelacesOf(partialRedemptionFee),
|
|
383
|
+
),
|
|
384
|
+
);
|
|
385
|
+
if (interestAmt > 0n) {
|
|
386
|
+
await collectInterestTx(
|
|
387
|
+
mkAssetsOf(
|
|
388
|
+
{
|
|
389
|
+
currencySymbol: fromHex(
|
|
390
|
+
sysParams.cdpParams.cdpAssetSymbol.unCurrencySymbol,
|
|
391
|
+
),
|
|
392
|
+
tokenName: iassetDatum.assetName,
|
|
393
|
+
},
|
|
394
|
+
interestAmt,
|
|
395
|
+
),
|
|
396
|
+
lucid,
|
|
397
|
+
sysParams,
|
|
398
|
+
tx,
|
|
399
|
+
interestCollectorOref,
|
|
400
|
+
);
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
return tx;
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
export async function runOpenCdpDelisted(
|
|
407
|
+
context: LucidContext,
|
|
408
|
+
sysParams: SystemParams,
|
|
409
|
+
asset: string,
|
|
410
|
+
collateralAsset: AssetClass,
|
|
411
|
+
initialCollateral: bigint,
|
|
412
|
+
initialMint: bigint,
|
|
413
|
+
): Promise<TxBuilder> {
|
|
414
|
+
const orefs = await findAllNecessaryOrefs(
|
|
415
|
+
context.lucid,
|
|
416
|
+
sysParams,
|
|
417
|
+
asset,
|
|
418
|
+
collateralAsset,
|
|
419
|
+
);
|
|
420
|
+
|
|
421
|
+
const network = context.lucid.config().network!;
|
|
422
|
+
const currentTime = BigInt(slotToUnixTime(network, context.emulator.slot));
|
|
423
|
+
|
|
424
|
+
const [pkh, skh] = await addrDetails(context.lucid);
|
|
425
|
+
|
|
426
|
+
const cdpCreatorRefScriptUtxo = matchSingle(
|
|
427
|
+
await context.lucid.utxosByOutRef([
|
|
428
|
+
fromSystemParamsScriptRef(
|
|
429
|
+
sysParams.scriptReferences.cdpCreatorValidatorRef,
|
|
430
|
+
),
|
|
431
|
+
]),
|
|
432
|
+
(_) => new Error('Expected a single cdp creator Ref Script UTXO'),
|
|
433
|
+
);
|
|
434
|
+
const cdpAuthTokenPolicyRefScriptUtxo = matchSingle(
|
|
435
|
+
await context.lucid.utxosByOutRef([
|
|
436
|
+
fromSystemParamsScriptRef(
|
|
437
|
+
sysParams.scriptReferences.authTokenPolicies.cdpAuthTokenRef,
|
|
438
|
+
),
|
|
439
|
+
]),
|
|
440
|
+
(_) => new Error('Expected a single cdp auth token policy Ref Script UTXO'),
|
|
441
|
+
);
|
|
442
|
+
const iAssetTokenPolicyRefScriptUtxo = matchSingle(
|
|
443
|
+
await context.lucid.utxosByOutRef([
|
|
444
|
+
fromSystemParamsScriptRef(
|
|
445
|
+
sysParams.scriptReferences.iAssetTokenPolicyRef,
|
|
446
|
+
),
|
|
447
|
+
]),
|
|
448
|
+
(_) => new Error('Expected a single iasset token policy Ref Script UTXO'),
|
|
449
|
+
);
|
|
450
|
+
|
|
451
|
+
const iassetUtxo = matchSingle(
|
|
452
|
+
await context.lucid.utxosByOutRef([orefs.iasset.utxo]),
|
|
453
|
+
(_) => new Error('Expected a single iasset UTXO'),
|
|
454
|
+
);
|
|
455
|
+
const iassetDatum = parseIAssetDatumOrThrow(
|
|
456
|
+
getInlineDatumOrThrow(iassetUtxo),
|
|
457
|
+
);
|
|
458
|
+
|
|
459
|
+
const collateralAssetUtxo = matchSingle(
|
|
460
|
+
await context.lucid.utxosByOutRef([orefs.collateralAsset.utxo]),
|
|
461
|
+
(_) => new Error('Expected a single iasset UTXO'),
|
|
462
|
+
);
|
|
463
|
+
const collateralAssetDatum = parseCollateralAssetDatumOrThrow(
|
|
464
|
+
getInlineDatumOrThrow(collateralAssetUtxo),
|
|
465
|
+
);
|
|
466
|
+
|
|
467
|
+
const interestOracleUtxo = matchSingle(
|
|
468
|
+
await context.lucid.utxosByOutRef([orefs.interestOracleUtxo]),
|
|
469
|
+
(_) => new Error('Expected a single interest oracle UTXO'),
|
|
470
|
+
);
|
|
471
|
+
const interestOracleDatum = parseInterestOracleDatum(
|
|
472
|
+
getInlineDatumOrThrow(interestOracleUtxo),
|
|
473
|
+
);
|
|
474
|
+
|
|
475
|
+
const cdpCreatorUtxo = matchSingle(
|
|
476
|
+
await context.lucid.utxosByOutRef([orefs.cdpCreatorUtxo]),
|
|
477
|
+
(_) => new Error('Expected a single CDP creator UTXO'),
|
|
478
|
+
);
|
|
479
|
+
|
|
480
|
+
const cdpNftVal = mkAssetsOf(
|
|
481
|
+
fromSystemParamsAsset(sysParams.cdpParams.cdpAuthToken),
|
|
482
|
+
1n,
|
|
483
|
+
);
|
|
484
|
+
|
|
485
|
+
const iassetClass = {
|
|
486
|
+
currencySymbol: fromHex(
|
|
487
|
+
sysParams.cdpParams.cdpAssetSymbol.unCurrencySymbol,
|
|
488
|
+
),
|
|
489
|
+
tokenName: iassetDatum.assetName,
|
|
490
|
+
};
|
|
491
|
+
|
|
492
|
+
const iassetTokensVal = mkAssetsOf(iassetClass, initialMint);
|
|
493
|
+
|
|
494
|
+
const refScripts: UTxO[] = [
|
|
495
|
+
cdpCreatorRefScriptUtxo,
|
|
496
|
+
cdpAuthTokenPolicyRefScriptUtxo,
|
|
497
|
+
iAssetTokenPolicyRefScriptUtxo,
|
|
498
|
+
];
|
|
499
|
+
|
|
500
|
+
const referenceInputs: UTxO[] = [
|
|
501
|
+
interestOracleUtxo,
|
|
502
|
+
iassetUtxo,
|
|
503
|
+
collateralAssetUtxo,
|
|
504
|
+
];
|
|
505
|
+
|
|
506
|
+
const tx = context.lucid
|
|
507
|
+
.newTx()
|
|
508
|
+
.readFrom(refScripts)
|
|
509
|
+
.readFrom(referenceInputs)
|
|
510
|
+
.mintAssets(cdpNftVal, Data.void())
|
|
511
|
+
.mintAssets(iassetTokensVal, Data.void())
|
|
512
|
+
.pay.ToContract(
|
|
513
|
+
createScriptAddress(network, sysParams.validatorHashes.cdpHash, skh),
|
|
514
|
+
{
|
|
515
|
+
kind: 'inline',
|
|
516
|
+
value: serialiseCdpDatum({
|
|
517
|
+
cdpOwner: fromHex(pkh.hash),
|
|
518
|
+
iasset: iassetDatum.assetName,
|
|
519
|
+
collateralAsset: collateralAssetDatum.collateralAsset,
|
|
520
|
+
mintedAmt: initialMint,
|
|
521
|
+
cdpFees: {
|
|
522
|
+
ActiveCDPInterestTracking: {
|
|
523
|
+
lastSettled: currentTime,
|
|
524
|
+
unitaryInterestSnapshot:
|
|
525
|
+
calculateUnitaryInterestSinceOracleLastUpdated(
|
|
526
|
+
currentTime,
|
|
527
|
+
interestOracleDatum,
|
|
528
|
+
) + interestOracleDatum.unitaryInterest,
|
|
529
|
+
},
|
|
530
|
+
},
|
|
531
|
+
}),
|
|
532
|
+
},
|
|
533
|
+
addAssets(
|
|
534
|
+
cdpNftVal,
|
|
535
|
+
mkAssetsOf(collateralAssetDatum.collateralAsset, initialCollateral),
|
|
536
|
+
),
|
|
537
|
+
)
|
|
538
|
+
.pay.ToContract(
|
|
539
|
+
cdpCreatorUtxo.address,
|
|
540
|
+
{
|
|
541
|
+
kind: 'inline',
|
|
542
|
+
value: serialiseCDPCreatorDatum({
|
|
543
|
+
creatorInputOref: {
|
|
544
|
+
outputIndex: BigInt(cdpCreatorUtxo.outputIndex),
|
|
545
|
+
txHash: fromHex(cdpCreatorUtxo.txHash),
|
|
546
|
+
},
|
|
547
|
+
}),
|
|
548
|
+
},
|
|
549
|
+
cdpCreatorUtxo.assets,
|
|
550
|
+
);
|
|
551
|
+
|
|
552
|
+
const debtMintingFee = calculateFeeFromRatio(
|
|
553
|
+
iassetDatum.debtMintingFeeRatio,
|
|
554
|
+
initialMint,
|
|
555
|
+
);
|
|
556
|
+
|
|
557
|
+
const treasuryRefScriptUtxo =
|
|
558
|
+
debtMintingFee > 0
|
|
559
|
+
? await treasuryFeeTx(
|
|
560
|
+
iassetClass,
|
|
561
|
+
debtMintingFee,
|
|
562
|
+
0n,
|
|
563
|
+
context.lucid,
|
|
564
|
+
sysParams,
|
|
565
|
+
tx,
|
|
566
|
+
cdpCreatorUtxo,
|
|
567
|
+
orefs.treasuryUtxo,
|
|
568
|
+
)
|
|
569
|
+
: undefined;
|
|
570
|
+
|
|
571
|
+
const validFrom = slotToUnixTime(network, context.emulator.slot - 1);
|
|
572
|
+
const validTo = validFrom + Number(sysParams.cdpCreatorParams.biasTime);
|
|
573
|
+
|
|
574
|
+
tx.validFrom(validFrom).validTo(validTo);
|
|
575
|
+
|
|
576
|
+
// We need to take into account the treasury ref script.
|
|
577
|
+
const refInputsIndices = getInputIndices(referenceInputs, [
|
|
578
|
+
...referenceInputs,
|
|
579
|
+
...refScripts,
|
|
580
|
+
...(treasuryRefScriptUtxo != null ? [treasuryRefScriptUtxo] : []),
|
|
581
|
+
]);
|
|
582
|
+
|
|
583
|
+
tx.collectFrom([cdpCreatorUtxo], {
|
|
584
|
+
kind: 'self',
|
|
585
|
+
makeRedeemer: (inputIdx) => {
|
|
586
|
+
return serialiseCDPCreatorRedeemer({
|
|
587
|
+
CreateCDP: {
|
|
588
|
+
cdpOwner: fromHex(pkh.hash),
|
|
589
|
+
minted: initialMint,
|
|
590
|
+
collateralAmt: initialCollateral,
|
|
591
|
+
currentTime: currentTime,
|
|
592
|
+
creatorInputIdx: inputIdx,
|
|
593
|
+
creatorOutputIdx: 1n,
|
|
594
|
+
cdpOutputIdx: 0n,
|
|
595
|
+
iassetRefInputIdx: refInputsIndices[1],
|
|
596
|
+
collateralAssetRefInputIdx: refInputsIndices[2],
|
|
597
|
+
interestOracleRefInputIdx: refInputsIndices[0],
|
|
598
|
+
priceOracleIdx: 'OracleVoid',
|
|
599
|
+
},
|
|
600
|
+
});
|
|
601
|
+
},
|
|
602
|
+
});
|
|
603
|
+
|
|
604
|
+
return tx;
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
export async function runOpenCdpAndUpdateOracle(
|
|
608
|
+
context: LucidContext,
|
|
609
|
+
sysParams: SystemParams,
|
|
610
|
+
asset: string,
|
|
611
|
+
collateralAsset: AssetClass,
|
|
612
|
+
initialCollateral: bigint,
|
|
613
|
+
initialMint: bigint,
|
|
614
|
+
oracleParams: PriceOracleParams,
|
|
615
|
+
newPrice: Rational,
|
|
616
|
+
): Promise<TxBuilder> {
|
|
617
|
+
const orefs = await findAllNecessaryOrefs(
|
|
618
|
+
context.lucid,
|
|
619
|
+
sysParams,
|
|
620
|
+
asset,
|
|
621
|
+
collateralAsset,
|
|
622
|
+
);
|
|
623
|
+
|
|
624
|
+
const network = context.lucid.config().network!;
|
|
625
|
+
const currentTime = BigInt(slotToUnixTime(network, context.emulator.slot));
|
|
626
|
+
|
|
627
|
+
const [pkh, skh] = await addrDetails(context.lucid);
|
|
628
|
+
|
|
629
|
+
const oracleValidator = mkPriceOracleValidator(oracleParams);
|
|
630
|
+
|
|
631
|
+
const cdpCreatorRefScriptUtxo = matchSingle(
|
|
632
|
+
await context.lucid.utxosByOutRef([
|
|
633
|
+
fromSystemParamsScriptRef(
|
|
634
|
+
sysParams.scriptReferences.cdpCreatorValidatorRef,
|
|
635
|
+
),
|
|
636
|
+
]),
|
|
637
|
+
(_) => new Error('Expected a single cdp creator Ref Script UTXO'),
|
|
638
|
+
);
|
|
639
|
+
const cdpAuthTokenPolicyRefScriptUtxo = matchSingle(
|
|
640
|
+
await context.lucid.utxosByOutRef([
|
|
641
|
+
fromSystemParamsScriptRef(
|
|
642
|
+
sysParams.scriptReferences.authTokenPolicies.cdpAuthTokenRef,
|
|
643
|
+
),
|
|
644
|
+
]),
|
|
645
|
+
(_) => new Error('Expected a single cdp auth token policy Ref Script UTXO'),
|
|
646
|
+
);
|
|
647
|
+
const iAssetTokenPolicyRefScriptUtxo = matchSingle(
|
|
648
|
+
await context.lucid.utxosByOutRef([
|
|
649
|
+
fromSystemParamsScriptRef(
|
|
650
|
+
sysParams.scriptReferences.iAssetTokenPolicyRef,
|
|
651
|
+
),
|
|
652
|
+
]),
|
|
653
|
+
(_) => new Error('Expected a single iasset token policy Ref Script UTXO'),
|
|
654
|
+
);
|
|
655
|
+
|
|
656
|
+
const iassetUtxo = matchSingle(
|
|
657
|
+
await context.lucid.utxosByOutRef([orefs.iasset.utxo]),
|
|
658
|
+
(_) => new Error('Expected a single iasset UTXO'),
|
|
659
|
+
);
|
|
660
|
+
const iassetDatum = parseIAssetDatumOrThrow(
|
|
661
|
+
getInlineDatumOrThrow(iassetUtxo),
|
|
662
|
+
);
|
|
663
|
+
|
|
664
|
+
const collateralAssetUtxo = matchSingle(
|
|
665
|
+
await context.lucid.utxosByOutRef([orefs.collateralAsset.utxo]),
|
|
666
|
+
(_) => new Error('Expected a single iasset UTXO'),
|
|
667
|
+
);
|
|
668
|
+
const collateralAssetDatum = parseCollateralAssetDatumOrThrow(
|
|
669
|
+
getInlineDatumOrThrow(collateralAssetUtxo),
|
|
670
|
+
);
|
|
671
|
+
|
|
672
|
+
const priceOracleUtxo = await findPriceOracleFromCollateralAsset(
|
|
673
|
+
context.lucid,
|
|
674
|
+
orefs.collateralAsset,
|
|
675
|
+
);
|
|
676
|
+
|
|
677
|
+
if (!priceOracleUtxo) throw new Error('Expected a price oracle');
|
|
678
|
+
|
|
679
|
+
const interestOracleUtxo = matchSingle(
|
|
680
|
+
await context.lucid.utxosByOutRef([orefs.interestOracleUtxo]),
|
|
681
|
+
(_) => new Error('Expected a single interest oracle UTXO'),
|
|
682
|
+
);
|
|
683
|
+
const interestOracleDatum = parseInterestOracleDatum(
|
|
684
|
+
getInlineDatumOrThrow(interestOracleUtxo),
|
|
685
|
+
);
|
|
686
|
+
|
|
687
|
+
const cdpCreatorUtxo = matchSingle(
|
|
688
|
+
await context.lucid.utxosByOutRef([orefs.cdpCreatorUtxo]),
|
|
689
|
+
(_) => new Error('Expected a single CDP creator UTXO'),
|
|
690
|
+
);
|
|
691
|
+
|
|
692
|
+
const cdpNftVal = mkAssetsOf(
|
|
693
|
+
fromSystemParamsAsset(sysParams.cdpParams.cdpAuthToken),
|
|
694
|
+
1n,
|
|
695
|
+
);
|
|
696
|
+
|
|
697
|
+
const iassetClass = {
|
|
698
|
+
currencySymbol: fromHex(
|
|
699
|
+
sysParams.cdpParams.cdpAssetSymbol.unCurrencySymbol,
|
|
700
|
+
),
|
|
701
|
+
tokenName: iassetDatum.assetName,
|
|
702
|
+
};
|
|
703
|
+
|
|
704
|
+
const iassetTokensVal = mkAssetsOf(iassetClass, initialMint);
|
|
705
|
+
|
|
706
|
+
const refScripts: UTxO[] = [
|
|
707
|
+
cdpCreatorRefScriptUtxo,
|
|
708
|
+
cdpAuthTokenPolicyRefScriptUtxo,
|
|
709
|
+
iAssetTokenPolicyRefScriptUtxo,
|
|
710
|
+
];
|
|
711
|
+
|
|
712
|
+
const referenceInputs: UTxO[] = [
|
|
713
|
+
interestOracleUtxo,
|
|
714
|
+
iassetUtxo,
|
|
715
|
+
collateralAssetUtxo,
|
|
716
|
+
];
|
|
717
|
+
|
|
718
|
+
const tx = context.lucid
|
|
719
|
+
.newTx()
|
|
720
|
+
.validFrom(Number(currentTime - oracleParams.biasTime) + ONE_SECOND)
|
|
721
|
+
.validTo(Number(currentTime + oracleParams.biasTime) - ONE_SECOND)
|
|
722
|
+
.attach.SpendingValidator(oracleValidator)
|
|
723
|
+
.readFrom(refScripts)
|
|
724
|
+
.readFrom(referenceInputs)
|
|
725
|
+
.mintAssets(cdpNftVal, Data.void())
|
|
726
|
+
.mintAssets(iassetTokensVal, Data.void())
|
|
727
|
+
.collectFrom(
|
|
728
|
+
[priceOracleUtxo],
|
|
729
|
+
serialisePriceOracleRedeemer({
|
|
730
|
+
currentTime: currentTime,
|
|
731
|
+
newPrice: newPrice,
|
|
732
|
+
}),
|
|
733
|
+
)
|
|
734
|
+
.pay.ToContract(
|
|
735
|
+
priceOracleUtxo.address,
|
|
736
|
+
{
|
|
737
|
+
kind: 'inline',
|
|
738
|
+
value: serialisePriceOracleDatum({
|
|
739
|
+
price: newPrice,
|
|
740
|
+
expirationTime: currentTime + oracleParams.expirationPeriod,
|
|
741
|
+
auxiliaryData: Core.Data.fromCBORHex(Data.void()),
|
|
742
|
+
}),
|
|
743
|
+
},
|
|
744
|
+
priceOracleUtxo.assets,
|
|
745
|
+
)
|
|
746
|
+
.pay.ToContract(
|
|
747
|
+
createScriptAddress(network, sysParams.validatorHashes.cdpHash, skh),
|
|
748
|
+
{
|
|
749
|
+
kind: 'inline',
|
|
750
|
+
value: serialiseCdpDatum({
|
|
751
|
+
cdpOwner: fromHex(pkh.hash),
|
|
752
|
+
iasset: iassetDatum.assetName,
|
|
753
|
+
collateralAsset: collateralAssetDatum.collateralAsset,
|
|
754
|
+
mintedAmt: initialMint,
|
|
755
|
+
cdpFees: {
|
|
756
|
+
ActiveCDPInterestTracking: {
|
|
757
|
+
lastSettled: currentTime,
|
|
758
|
+
unitaryInterestSnapshot:
|
|
759
|
+
calculateUnitaryInterestSinceOracleLastUpdated(
|
|
760
|
+
currentTime,
|
|
761
|
+
interestOracleDatum,
|
|
762
|
+
) + interestOracleDatum.unitaryInterest,
|
|
763
|
+
},
|
|
764
|
+
},
|
|
765
|
+
}),
|
|
766
|
+
},
|
|
767
|
+
addAssets(
|
|
768
|
+
cdpNftVal,
|
|
769
|
+
mkAssetsOf(collateralAssetDatum.collateralAsset, initialCollateral),
|
|
770
|
+
),
|
|
771
|
+
)
|
|
772
|
+
.pay.ToContract(
|
|
773
|
+
cdpCreatorUtxo.address,
|
|
774
|
+
{
|
|
775
|
+
kind: 'inline',
|
|
776
|
+
value: serialiseCDPCreatorDatum({
|
|
777
|
+
creatorInputOref: {
|
|
778
|
+
outputIndex: BigInt(cdpCreatorUtxo.outputIndex),
|
|
779
|
+
txHash: fromHex(cdpCreatorUtxo.txHash),
|
|
780
|
+
},
|
|
781
|
+
}),
|
|
782
|
+
},
|
|
783
|
+
cdpCreatorUtxo.assets,
|
|
784
|
+
)
|
|
785
|
+
.addSignerKey(pkh.hash);
|
|
786
|
+
|
|
787
|
+
const debtMintingFee = calculateFeeFromRatio(
|
|
788
|
+
iassetDatum.debtMintingFeeRatio,
|
|
789
|
+
initialMint,
|
|
790
|
+
);
|
|
791
|
+
|
|
792
|
+
const treasuryRefScriptUtxo =
|
|
793
|
+
debtMintingFee > 0
|
|
794
|
+
? await treasuryFeeTx(
|
|
795
|
+
iassetClass,
|
|
796
|
+
debtMintingFee,
|
|
797
|
+
0n,
|
|
798
|
+
context.lucid,
|
|
799
|
+
sysParams,
|
|
800
|
+
tx,
|
|
801
|
+
cdpCreatorUtxo,
|
|
802
|
+
orefs.treasuryUtxo,
|
|
803
|
+
)
|
|
804
|
+
: undefined;
|
|
805
|
+
|
|
806
|
+
// We need to take into account the treasury ref script as well.
|
|
807
|
+
const refInputsIndices = getInputIndices(referenceInputs, [
|
|
808
|
+
...referenceInputs,
|
|
809
|
+
...refScripts,
|
|
810
|
+
...(treasuryRefScriptUtxo != null ? [treasuryRefScriptUtxo] : []),
|
|
811
|
+
]);
|
|
812
|
+
|
|
813
|
+
tx.collectFrom([cdpCreatorUtxo], {
|
|
814
|
+
kind: 'self',
|
|
815
|
+
makeRedeemer: (inputIdx) => {
|
|
816
|
+
return serialiseCDPCreatorRedeemer({
|
|
817
|
+
CreateCDP: {
|
|
818
|
+
cdpOwner: fromHex(pkh.hash),
|
|
819
|
+
minted: initialMint,
|
|
820
|
+
collateralAmt: initialCollateral,
|
|
821
|
+
currentTime: currentTime,
|
|
822
|
+
creatorInputIdx: inputIdx,
|
|
823
|
+
creatorOutputIdx: 2n,
|
|
824
|
+
cdpOutputIdx: 1n,
|
|
825
|
+
iassetRefInputIdx: refInputsIndices[1],
|
|
826
|
+
collateralAssetRefInputIdx: refInputsIndices[2],
|
|
827
|
+
interestOracleRefInputIdx: refInputsIndices[0],
|
|
828
|
+
priceOracleIdx: { OracleOutputIdx: 0n },
|
|
829
|
+
},
|
|
830
|
+
});
|
|
831
|
+
},
|
|
832
|
+
});
|
|
833
|
+
|
|
834
|
+
return tx;
|
|
835
|
+
}
|
|
836
|
+
|
|
837
|
+
export async function runTestAdjustCdpDelisted(
|
|
838
|
+
context: LucidContext,
|
|
839
|
+
sysParams: SystemParams,
|
|
840
|
+
asset: string,
|
|
841
|
+
collateralAsset: AssetClass,
|
|
842
|
+
collateralAdjustment: bigint,
|
|
843
|
+
debtAdjustment: bigint,
|
|
844
|
+
): Promise<TxBuilder> {
|
|
845
|
+
const network = context.lucid.config().network!;
|
|
846
|
+
const currentTime = BigInt(slotToUnixTime(network, context.emulator.slot));
|
|
847
|
+
const [pkh, skh] = await addrDetails(context.lucid);
|
|
848
|
+
|
|
849
|
+
const cdp = await findCdp(
|
|
850
|
+
context.lucid,
|
|
851
|
+
sysParams.validatorHashes.cdpHash,
|
|
852
|
+
fromSystemParamsAsset(sysParams.cdpParams.cdpAuthToken),
|
|
853
|
+
pkh.hash,
|
|
854
|
+
skh,
|
|
855
|
+
);
|
|
856
|
+
|
|
857
|
+
const cdpRefScriptUtxo = matchSingle(
|
|
858
|
+
await context.lucid.utxosByOutRef([
|
|
859
|
+
fromSystemParamsScriptRef(sysParams.scriptReferences.cdpValidatorRef),
|
|
860
|
+
]),
|
|
861
|
+
(_) => new Error('Expected a single cdp Ref Script UTXO'),
|
|
862
|
+
);
|
|
863
|
+
|
|
864
|
+
const cdpUtxo = matchSingle(
|
|
865
|
+
await context.lucid.utxosByOutRef([cdp.utxo]),
|
|
866
|
+
(_) => new Error('Expected a single cdp UTXO'),
|
|
867
|
+
);
|
|
868
|
+
const cdpDatum = parseCdpDatumOrThrow(getInlineDatumOrThrow(cdpUtxo));
|
|
869
|
+
|
|
870
|
+
const iassetOutput = await findIAsset(
|
|
871
|
+
context.lucid,
|
|
872
|
+
sysParams.validatorHashes.iassetHash,
|
|
873
|
+
fromSystemParamsAsset(sysParams.cdpParams.iAssetAuthToken),
|
|
874
|
+
asset,
|
|
875
|
+
);
|
|
876
|
+
|
|
877
|
+
const iassetUtxo = matchSingle(
|
|
878
|
+
await context.lucid.utxosByOutRef([iassetOutput.utxo]),
|
|
879
|
+
(_) => new Error('Expected a single iasset UTXO'),
|
|
880
|
+
);
|
|
881
|
+
const iassetDatum = parseIAssetDatumOrThrow(
|
|
882
|
+
getInlineDatumOrThrow(iassetUtxo),
|
|
883
|
+
);
|
|
884
|
+
|
|
885
|
+
const collateralAssetOutput = await findCollateralAsset(
|
|
886
|
+
context.lucid,
|
|
887
|
+
sysParams,
|
|
888
|
+
fromSystemParamsAsset(sysParams.cdpParams.collateralAssetAuthToken),
|
|
889
|
+
asset,
|
|
890
|
+
collateralAsset,
|
|
891
|
+
);
|
|
892
|
+
|
|
893
|
+
const collateralAssetUtxo = matchSingle(
|
|
894
|
+
await context.lucid.utxosByOutRef([collateralAssetOutput.utxo]),
|
|
895
|
+
(_) => new Error('Expected a single collateral asset UTXO'),
|
|
896
|
+
);
|
|
897
|
+
const collateralAssetDatum = parseCollateralAssetDatumOrThrow(
|
|
898
|
+
getInlineDatumOrThrow(collateralAssetUtxo),
|
|
899
|
+
);
|
|
900
|
+
|
|
901
|
+
const interestOracleUtxo = await findInterestOracle(
|
|
902
|
+
context.lucid,
|
|
903
|
+
collateralAssetDatum.interestOracleNft,
|
|
904
|
+
);
|
|
905
|
+
|
|
906
|
+
const interestOracleDatum = parseInterestOracleDatum(
|
|
907
|
+
getInlineDatumOrThrow(interestOracleUtxo),
|
|
908
|
+
);
|
|
909
|
+
|
|
910
|
+
const interestCollectorUtxo = await findRandomNonAdminInterestCollector(
|
|
911
|
+
context.lucid,
|
|
912
|
+
sysParams.validatorHashes.interestCollectionHash,
|
|
913
|
+
fromSystemParamsAsset(sysParams.interestCollectionParams.multisigUtxoNft),
|
|
914
|
+
);
|
|
915
|
+
|
|
916
|
+
const validateFrom = slotToUnixTime(network, context.emulator.slot - 1);
|
|
917
|
+
const validateTo =
|
|
918
|
+
validateFrom + Number(sysParams.cdpParams.biasTime) - 60_000;
|
|
919
|
+
|
|
920
|
+
const interestAmt = match(cdpDatum.cdpFees)
|
|
921
|
+
.with({ FrozenCDPAccumulatedFees: P.any }, () => {
|
|
922
|
+
throw new Error('CDP fees wrong');
|
|
923
|
+
})
|
|
924
|
+
.with({ ActiveCDPInterestTracking: P.select() }, (interest) => {
|
|
925
|
+
return calculateAccruedInterest(
|
|
926
|
+
currentTime,
|
|
927
|
+
interest.unitaryInterestSnapshot,
|
|
928
|
+
cdpDatum.mintedAmt,
|
|
929
|
+
interest.lastSettled,
|
|
930
|
+
interestOracleDatum,
|
|
931
|
+
);
|
|
932
|
+
})
|
|
933
|
+
.exhaustive();
|
|
934
|
+
|
|
935
|
+
const mintedAmountChange = debtAdjustment + interestAmt;
|
|
936
|
+
|
|
937
|
+
const tx = context.lucid
|
|
938
|
+
.newTx()
|
|
939
|
+
.validFrom(validateFrom)
|
|
940
|
+
.validTo(validateTo)
|
|
941
|
+
.collectFrom(
|
|
942
|
+
[cdpUtxo],
|
|
943
|
+
serialiseCdpRedeemer({
|
|
944
|
+
AdjustCdp: {
|
|
945
|
+
currentTime: currentTime,
|
|
946
|
+
debtAdjustment,
|
|
947
|
+
collateralAdjustment,
|
|
948
|
+
priceOracleIdx: 'OracleVoid',
|
|
949
|
+
},
|
|
950
|
+
}),
|
|
951
|
+
)
|
|
952
|
+
.readFrom([cdpRefScriptUtxo])
|
|
953
|
+
.readFrom([iassetUtxo, collateralAssetUtxo, interestOracleUtxo])
|
|
954
|
+
.pay.ToContract(
|
|
955
|
+
cdpUtxo.address,
|
|
956
|
+
{
|
|
957
|
+
kind: 'inline',
|
|
958
|
+
value: serialiseCdpDatum({
|
|
959
|
+
...cdpDatum,
|
|
960
|
+
mintedAmt: cdpDatum.mintedAmt + mintedAmountChange,
|
|
961
|
+
cdpFees: {
|
|
962
|
+
ActiveCDPInterestTracking: {
|
|
963
|
+
lastSettled: currentTime,
|
|
964
|
+
unitaryInterestSnapshot:
|
|
965
|
+
calculateUnitaryInterestSinceOracleLastUpdated(
|
|
966
|
+
currentTime,
|
|
967
|
+
interestOracleDatum,
|
|
968
|
+
) + interestOracleDatum.unitaryInterest,
|
|
969
|
+
},
|
|
970
|
+
},
|
|
971
|
+
}),
|
|
972
|
+
},
|
|
973
|
+
addAssets(
|
|
974
|
+
cdpUtxo.assets,
|
|
975
|
+
mkAssetsOf(cdpDatum.collateralAsset, collateralAdjustment),
|
|
976
|
+
),
|
|
977
|
+
);
|
|
978
|
+
|
|
979
|
+
if (!cdpDatum.cdpOwner) {
|
|
980
|
+
throw new Error('Expected active CDP');
|
|
981
|
+
}
|
|
982
|
+
|
|
983
|
+
tx.addSignerKey(toHex(cdpDatum.cdpOwner));
|
|
984
|
+
|
|
985
|
+
if (mintedAmountChange !== 0n) {
|
|
986
|
+
const iAssetTokenPolicyRefScriptUtxo = matchSingle(
|
|
987
|
+
await context.lucid.utxosByOutRef([
|
|
988
|
+
fromSystemParamsScriptRef(
|
|
989
|
+
sysParams.scriptReferences.iAssetTokenPolicyRef,
|
|
990
|
+
),
|
|
991
|
+
]),
|
|
992
|
+
(_) => new Error('Expected a single iasset token policy Ref Script UTXO'),
|
|
993
|
+
);
|
|
994
|
+
|
|
995
|
+
const iassetTokensVal = mkAssetsOf(
|
|
996
|
+
{
|
|
997
|
+
currencySymbol: fromHex(
|
|
998
|
+
sysParams.cdpParams.cdpAssetSymbol.unCurrencySymbol,
|
|
999
|
+
),
|
|
1000
|
+
tokenName: iassetDatum.assetName,
|
|
1001
|
+
},
|
|
1002
|
+
mintedAmountChange,
|
|
1003
|
+
);
|
|
1004
|
+
|
|
1005
|
+
tx.readFrom([iAssetTokenPolicyRefScriptUtxo]).mintAssets(
|
|
1006
|
+
iassetTokensVal,
|
|
1007
|
+
Data.void(),
|
|
1008
|
+
);
|
|
1009
|
+
}
|
|
1010
|
+
|
|
1011
|
+
const iAssetAc = {
|
|
1012
|
+
currencySymbol: fromHex(
|
|
1013
|
+
sysParams.cdpParams.cdpAssetSymbol.unCurrencySymbol,
|
|
1014
|
+
),
|
|
1015
|
+
tokenName: iassetDatum.assetName,
|
|
1016
|
+
};
|
|
1017
|
+
|
|
1018
|
+
if (interestAmt > 0n) {
|
|
1019
|
+
await collectInterestTx(
|
|
1020
|
+
mkAssetsOf(iAssetAc, interestAmt),
|
|
1021
|
+
context.lucid,
|
|
1022
|
+
sysParams,
|
|
1023
|
+
tx,
|
|
1024
|
+
interestCollectorUtxo,
|
|
1025
|
+
);
|
|
1026
|
+
}
|
|
1027
|
+
|
|
1028
|
+
let treasuryFee = 0n;
|
|
1029
|
+
|
|
1030
|
+
if (debtAdjustment > 0n) {
|
|
1031
|
+
const treasuryUtxo = await findRandomTreasuryUtxoWithOnlyAda(
|
|
1032
|
+
context.lucid,
|
|
1033
|
+
sysParams,
|
|
1034
|
+
);
|
|
1035
|
+
|
|
1036
|
+
treasuryFee += calculateFeeFromRatio(
|
|
1037
|
+
iassetDatum.debtMintingFeeRatio,
|
|
1038
|
+
debtAdjustment,
|
|
1039
|
+
);
|
|
1040
|
+
|
|
1041
|
+
if (treasuryFee > 0n) {
|
|
1042
|
+
await treasuryFeeTx(
|
|
1043
|
+
iAssetAc,
|
|
1044
|
+
treasuryFee,
|
|
1045
|
+
0n,
|
|
1046
|
+
context.lucid,
|
|
1047
|
+
sysParams,
|
|
1048
|
+
tx,
|
|
1049
|
+
cdpUtxo,
|
|
1050
|
+
treasuryUtxo,
|
|
1051
|
+
);
|
|
1052
|
+
}
|
|
1053
|
+
}
|
|
1054
|
+
|
|
1055
|
+
return tx;
|
|
1056
|
+
}
|
|
1057
|
+
|
|
1058
|
+
export async function runTestDepositCdpWithInterestVar(
|
|
1059
|
+
context: LucidContext,
|
|
1060
|
+
sysParams: SystemParams,
|
|
1061
|
+
iasset: string,
|
|
1062
|
+
collateralAsset: AssetClass,
|
|
1063
|
+
interestCollectorUtxo: UTxO,
|
|
1064
|
+
interestVariation: CollectInterestVariation,
|
|
1065
|
+
|
|
1066
|
+
amount: bigint = 1_000_000n,
|
|
1067
|
+
): Promise<TxBuilder> {
|
|
1068
|
+
const network = context.lucid.config().network!;
|
|
1069
|
+
const currentTime = BigInt(slotToUnixTime(network, context.emulator.slot));
|
|
1070
|
+
const [pkh, skh] = await addrDetails(context.lucid);
|
|
1071
|
+
|
|
1072
|
+
const cdp = await findCdp(
|
|
1073
|
+
context.lucid,
|
|
1074
|
+
sysParams.validatorHashes.cdpHash,
|
|
1075
|
+
fromSystemParamsAsset(sysParams.cdpParams.cdpAuthToken),
|
|
1076
|
+
pkh.hash,
|
|
1077
|
+
skh,
|
|
1078
|
+
);
|
|
1079
|
+
|
|
1080
|
+
const cdpRefScriptUtxo = matchSingle(
|
|
1081
|
+
await context.lucid.utxosByOutRef([
|
|
1082
|
+
fromSystemParamsScriptRef(sysParams.scriptReferences.cdpValidatorRef),
|
|
1083
|
+
]),
|
|
1084
|
+
(_) => new Error('Expected a single cdp Ref Script UTXO'),
|
|
1085
|
+
);
|
|
1086
|
+
|
|
1087
|
+
const cdpUtxo = matchSingle(
|
|
1088
|
+
await context.lucid.utxosByOutRef([cdp.utxo]),
|
|
1089
|
+
(_) => new Error('Expected a single cdp UTXO'),
|
|
1090
|
+
);
|
|
1091
|
+
const cdpDatum = parseCdpDatumOrThrow(getInlineDatumOrThrow(cdpUtxo));
|
|
1092
|
+
|
|
1093
|
+
const iassetOutput = await findIAsset(
|
|
1094
|
+
context.lucid,
|
|
1095
|
+
sysParams.validatorHashes.iassetHash,
|
|
1096
|
+
fromSystemParamsAsset(sysParams.cdpParams.iAssetAuthToken),
|
|
1097
|
+
iasset,
|
|
1098
|
+
);
|
|
1099
|
+
|
|
1100
|
+
const iassetUtxo = matchSingle(
|
|
1101
|
+
await context.lucid.utxosByOutRef([iassetOutput.utxo]),
|
|
1102
|
+
(_) => new Error('Expected a single iasset UTXO'),
|
|
1103
|
+
);
|
|
1104
|
+
const iassetDatum = parseIAssetDatumOrThrow(
|
|
1105
|
+
getInlineDatumOrThrow(iassetUtxo),
|
|
1106
|
+
);
|
|
1107
|
+
|
|
1108
|
+
const collateralAssetOutput = await findCollateralAsset(
|
|
1109
|
+
context.lucid,
|
|
1110
|
+
sysParams,
|
|
1111
|
+
fromSystemParamsAsset(sysParams.cdpParams.collateralAssetAuthToken),
|
|
1112
|
+
iasset,
|
|
1113
|
+
collateralAsset,
|
|
1114
|
+
);
|
|
1115
|
+
|
|
1116
|
+
const collateralAssetUtxo = matchSingle(
|
|
1117
|
+
await context.lucid.utxosByOutRef([collateralAssetOutput.utxo]),
|
|
1118
|
+
(_) => new Error('Expected a single iasset UTXO'),
|
|
1119
|
+
);
|
|
1120
|
+
|
|
1121
|
+
const interestOracleUtxo = await findInterestOracle(
|
|
1122
|
+
context.lucid,
|
|
1123
|
+
collateralAssetOutput.datum.interestOracleNft,
|
|
1124
|
+
);
|
|
1125
|
+
const interestOracleDatum = parseInterestOracleDatum(
|
|
1126
|
+
getInlineDatumOrThrow(interestOracleUtxo),
|
|
1127
|
+
);
|
|
1128
|
+
|
|
1129
|
+
const validateFrom = slotToUnixTime(network, context.emulator.slot - 1);
|
|
1130
|
+
const validateTo =
|
|
1131
|
+
validateFrom + Number(sysParams.cdpParams.biasTime) - 60_000;
|
|
1132
|
+
|
|
1133
|
+
const interestAmt = match(cdpDatum.cdpFees)
|
|
1134
|
+
.with({ FrozenCDPAccumulatedFees: P.any }, () => {
|
|
1135
|
+
throw new Error('CDP fees wrong');
|
|
1136
|
+
})
|
|
1137
|
+
.with({ ActiveCDPInterestTracking: P.select() }, (interest) => {
|
|
1138
|
+
return calculateAccruedInterest(
|
|
1139
|
+
currentTime,
|
|
1140
|
+
interest.unitaryInterestSnapshot,
|
|
1141
|
+
cdpDatum.mintedAmt,
|
|
1142
|
+
interest.lastSettled,
|
|
1143
|
+
interestOracleDatum,
|
|
1144
|
+
);
|
|
1145
|
+
})
|
|
1146
|
+
.exhaustive();
|
|
1147
|
+
|
|
1148
|
+
const mintedAmountChange = interestAmt;
|
|
1149
|
+
|
|
1150
|
+
const tx = context.lucid
|
|
1151
|
+
.newTx()
|
|
1152
|
+
.validFrom(validateFrom)
|
|
1153
|
+
.validTo(validateTo)
|
|
1154
|
+
.collectFrom(
|
|
1155
|
+
[cdpUtxo],
|
|
1156
|
+
serialiseCdpRedeemer({
|
|
1157
|
+
AdjustCdp: {
|
|
1158
|
+
currentTime: currentTime,
|
|
1159
|
+
debtAdjustment: 0n,
|
|
1160
|
+
collateralAdjustment: amount,
|
|
1161
|
+
priceOracleIdx: 'OracleVoid',
|
|
1162
|
+
},
|
|
1163
|
+
}),
|
|
1164
|
+
)
|
|
1165
|
+
.readFrom([cdpRefScriptUtxo])
|
|
1166
|
+
.readFrom([iassetUtxo, collateralAssetUtxo, interestOracleUtxo])
|
|
1167
|
+
.pay.ToContract(
|
|
1168
|
+
cdpUtxo.address,
|
|
1169
|
+
{
|
|
1170
|
+
kind: 'inline',
|
|
1171
|
+
value: serialiseCdpDatum({
|
|
1172
|
+
...cdpDatum,
|
|
1173
|
+
mintedAmt: cdpDatum.mintedAmt + mintedAmountChange,
|
|
1174
|
+
cdpFees: {
|
|
1175
|
+
ActiveCDPInterestTracking: {
|
|
1176
|
+
lastSettled: currentTime,
|
|
1177
|
+
unitaryInterestSnapshot:
|
|
1178
|
+
calculateUnitaryInterestSinceOracleLastUpdated(
|
|
1179
|
+
currentTime,
|
|
1180
|
+
interestOracleDatum,
|
|
1181
|
+
) + interestOracleDatum.unitaryInterest,
|
|
1182
|
+
},
|
|
1183
|
+
},
|
|
1184
|
+
}),
|
|
1185
|
+
},
|
|
1186
|
+
addAssets(cdpUtxo.assets, mkLovelacesOf(amount)),
|
|
1187
|
+
);
|
|
1188
|
+
|
|
1189
|
+
if (!cdpDatum.cdpOwner) {
|
|
1190
|
+
throw new Error('Expected active CDP');
|
|
1191
|
+
}
|
|
1192
|
+
|
|
1193
|
+
tx.addSignerKey(toHex(cdpDatum.cdpOwner));
|
|
1194
|
+
|
|
1195
|
+
if (mintedAmountChange !== 0n) {
|
|
1196
|
+
const iAssetTokenPolicyRefScriptUtxo = matchSingle(
|
|
1197
|
+
await context.lucid.utxosByOutRef([
|
|
1198
|
+
fromSystemParamsScriptRef(
|
|
1199
|
+
sysParams.scriptReferences.iAssetTokenPolicyRef,
|
|
1200
|
+
),
|
|
1201
|
+
]),
|
|
1202
|
+
(_) => new Error('Expected a single iasset token policy Ref Script UTXO'),
|
|
1203
|
+
);
|
|
1204
|
+
|
|
1205
|
+
const iassetTokensVal = mkAssetsOf(
|
|
1206
|
+
{
|
|
1207
|
+
currencySymbol: fromHex(
|
|
1208
|
+
sysParams.cdpParams.cdpAssetSymbol.unCurrencySymbol,
|
|
1209
|
+
),
|
|
1210
|
+
tokenName: iassetDatum.assetName,
|
|
1211
|
+
},
|
|
1212
|
+
mintedAmountChange,
|
|
1213
|
+
);
|
|
1214
|
+
|
|
1215
|
+
tx.readFrom([iAssetTokenPolicyRefScriptUtxo]).mintAssets(
|
|
1216
|
+
iassetTokensVal,
|
|
1217
|
+
Data.void(),
|
|
1218
|
+
);
|
|
1219
|
+
}
|
|
1220
|
+
|
|
1221
|
+
if (interestAmt > 0n) {
|
|
1222
|
+
await testCollectInterest(
|
|
1223
|
+
mkAssetsOf(
|
|
1224
|
+
{
|
|
1225
|
+
currencySymbol: fromHex(
|
|
1226
|
+
sysParams.cdpParams.cdpAssetSymbol.unCurrencySymbol,
|
|
1227
|
+
),
|
|
1228
|
+
tokenName: iassetDatum.assetName,
|
|
1229
|
+
},
|
|
1230
|
+
interestAmt,
|
|
1231
|
+
),
|
|
1232
|
+
context.lucid,
|
|
1233
|
+
sysParams,
|
|
1234
|
+
tx,
|
|
1235
|
+
interestCollectorUtxo,
|
|
1236
|
+
interestVariation,
|
|
1237
|
+
);
|
|
1238
|
+
}
|
|
1239
|
+
|
|
1240
|
+
return tx;
|
|
1241
|
+
}
|
|
1242
|
+
|
|
1243
|
+
export async function runCloseCdpWrongOracle(
|
|
1244
|
+
lucid: LucidEvolution,
|
|
1245
|
+
currentSlot: number,
|
|
1246
|
+
sysParams: SystemParams,
|
|
1247
|
+
iasset: string,
|
|
1248
|
+
wrongAsset: string,
|
|
1249
|
+
collateralAsset: AssetClass,
|
|
1250
|
+
): Promise<TxBuilder> {
|
|
1251
|
+
const [pkh, skh] = await addrDetails(lucid);
|
|
1252
|
+
|
|
1253
|
+
const cdp = await findCdp(
|
|
1254
|
+
lucid,
|
|
1255
|
+
sysParams.validatorHashes.cdpHash,
|
|
1256
|
+
fromSystemParamsAsset(sysParams.cdpParams.cdpAuthToken),
|
|
1257
|
+
pkh.hash,
|
|
1258
|
+
skh,
|
|
1259
|
+
);
|
|
1260
|
+
const orefs = await findAllNecessaryOrefs(
|
|
1261
|
+
lucid,
|
|
1262
|
+
sysParams,
|
|
1263
|
+
iasset,
|
|
1264
|
+
collateralAsset,
|
|
1265
|
+
);
|
|
1266
|
+
|
|
1267
|
+
const priceOracleOref = await findPriceOracleFromCollateralAsset(
|
|
1268
|
+
lucid,
|
|
1269
|
+
orefs.collateralAsset,
|
|
1270
|
+
);
|
|
1271
|
+
|
|
1272
|
+
const network = lucid.config().network!;
|
|
1273
|
+
const currentTime = BigInt(slotToUnixTime(network, currentSlot));
|
|
1274
|
+
|
|
1275
|
+
const cdpRefScriptUtxo = matchSingle(
|
|
1276
|
+
await lucid.utxosByOutRef([
|
|
1277
|
+
fromSystemParamsScriptRef(sysParams.scriptReferences.cdpValidatorRef),
|
|
1278
|
+
]),
|
|
1279
|
+
(_) => new Error('Expected a single cdp Ref Script UTXO'),
|
|
1280
|
+
);
|
|
1281
|
+
|
|
1282
|
+
const cdpAuthTokenPolicyRefScriptUtxo = matchSingle(
|
|
1283
|
+
await lucid.utxosByOutRef([
|
|
1284
|
+
fromSystemParamsScriptRef(
|
|
1285
|
+
sysParams.scriptReferences.authTokenPolicies.cdpAuthTokenRef,
|
|
1286
|
+
),
|
|
1287
|
+
]),
|
|
1288
|
+
(_) => new Error('Expected a single cdp auth token policy Ref Script UTXO'),
|
|
1289
|
+
);
|
|
1290
|
+
|
|
1291
|
+
const iAssetTokenPolicyRefScriptUtxo = matchSingle(
|
|
1292
|
+
await lucid.utxosByOutRef([
|
|
1293
|
+
fromSystemParamsScriptRef(
|
|
1294
|
+
sysParams.scriptReferences.iAssetTokenPolicyRef,
|
|
1295
|
+
),
|
|
1296
|
+
]),
|
|
1297
|
+
(_) => new Error('Expected a single iasset token policy Ref Script UTXO'),
|
|
1298
|
+
);
|
|
1299
|
+
|
|
1300
|
+
const cdpUtxo = matchSingle(
|
|
1301
|
+
await lucid.utxosByOutRef([cdp.utxo]),
|
|
1302
|
+
(_) => new Error('Expected a single cdp UTXO'),
|
|
1303
|
+
);
|
|
1304
|
+
const cdpDatum = parseCdpDatumOrThrow(getInlineDatumOrThrow(cdpUtxo));
|
|
1305
|
+
|
|
1306
|
+
const iassetUtxo = matchSingle(
|
|
1307
|
+
await lucid.utxosByOutRef([orefs.iasset.utxo]),
|
|
1308
|
+
(_) => new Error('Expected a single iasset UTXO'),
|
|
1309
|
+
);
|
|
1310
|
+
const iassetDatum = parseIAssetDatumOrThrow(
|
|
1311
|
+
getInlineDatumOrThrow(iassetUtxo),
|
|
1312
|
+
);
|
|
1313
|
+
|
|
1314
|
+
const priceOracleUtxo = matchSingle(
|
|
1315
|
+
await lucid.utxosByOutRef([priceOracleOref!]),
|
|
1316
|
+
(_) => new Error('Expected a single price oracle UTXO'),
|
|
1317
|
+
);
|
|
1318
|
+
const priceOracleDatum = parsePriceOracleDatum(
|
|
1319
|
+
getInlineDatumOrThrow(priceOracleUtxo),
|
|
1320
|
+
);
|
|
1321
|
+
|
|
1322
|
+
const wrongCollateral = await findCollateralAsset(
|
|
1323
|
+
lucid,
|
|
1324
|
+
sysParams,
|
|
1325
|
+
fromSystemParamsAsset(sysParams.cdpParams.collateralAssetAuthToken),
|
|
1326
|
+
wrongAsset,
|
|
1327
|
+
collateralAsset,
|
|
1328
|
+
);
|
|
1329
|
+
|
|
1330
|
+
const wrongInterestOracleUtxo = await findInterestOracle(
|
|
1331
|
+
lucid,
|
|
1332
|
+
wrongCollateral.datum.interestOracleNft,
|
|
1333
|
+
);
|
|
1334
|
+
|
|
1335
|
+
const wrongInterestOracleDatum = parseInterestOracleDatum(
|
|
1336
|
+
getInlineDatumOrThrow(wrongInterestOracleUtxo),
|
|
1337
|
+
);
|
|
1338
|
+
|
|
1339
|
+
const txValidity = oracleExpirationAwareValidity(
|
|
1340
|
+
currentSlot,
|
|
1341
|
+
Number(sysParams.cdpCreatorParams.biasTime),
|
|
1342
|
+
Number(priceOracleDatum.expirationTime),
|
|
1343
|
+
network,
|
|
1344
|
+
);
|
|
1345
|
+
|
|
1346
|
+
const tx = lucid
|
|
1347
|
+
.newTx()
|
|
1348
|
+
.readFrom([
|
|
1349
|
+
cdpRefScriptUtxo,
|
|
1350
|
+
iAssetTokenPolicyRefScriptUtxo,
|
|
1351
|
+
cdpAuthTokenPolicyRefScriptUtxo,
|
|
1352
|
+
])
|
|
1353
|
+
.readFrom([wrongCollateral.utxo, wrongInterestOracleUtxo])
|
|
1354
|
+
.validFrom(txValidity.validFrom)
|
|
1355
|
+
.validTo(txValidity.validTo)
|
|
1356
|
+
.mintAssets(
|
|
1357
|
+
mkAssetsOf(
|
|
1358
|
+
{
|
|
1359
|
+
currencySymbol: fromHex(
|
|
1360
|
+
sysParams.cdpParams.cdpAssetSymbol.unCurrencySymbol,
|
|
1361
|
+
),
|
|
1362
|
+
tokenName: iassetDatum.assetName,
|
|
1363
|
+
},
|
|
1364
|
+
-cdpDatum.mintedAmt,
|
|
1365
|
+
),
|
|
1366
|
+
Data.void(),
|
|
1367
|
+
)
|
|
1368
|
+
.mintAssets(
|
|
1369
|
+
mkAssetsOf(fromSystemParamsAsset(sysParams.cdpParams.cdpAuthToken), -1n),
|
|
1370
|
+
Data.void(),
|
|
1371
|
+
)
|
|
1372
|
+
.collectFrom(
|
|
1373
|
+
[cdpUtxo],
|
|
1374
|
+
serialiseCdpRedeemer({ CloseCdp: { currentTime: currentTime } }),
|
|
1375
|
+
);
|
|
1376
|
+
|
|
1377
|
+
if (!cdpDatum.cdpOwner) {
|
|
1378
|
+
throw new Error('Expected active CDP');
|
|
1379
|
+
}
|
|
1380
|
+
|
|
1381
|
+
tx.addSignerKey(toHex(cdpDatum.cdpOwner));
|
|
1382
|
+
|
|
1383
|
+
const interestAmt = match(cdpDatum.cdpFees)
|
|
1384
|
+
.with({ FrozenCDPAccumulatedFees: P.any }, () => {
|
|
1385
|
+
throw new Error('CDP fees wrong');
|
|
1386
|
+
})
|
|
1387
|
+
.with({ ActiveCDPInterestTracking: P.select() }, (interest) => {
|
|
1388
|
+
return calculateAccruedInterest(
|
|
1389
|
+
currentTime,
|
|
1390
|
+
interest.unitaryInterestSnapshot,
|
|
1391
|
+
cdpDatum.mintedAmt,
|
|
1392
|
+
interest.lastSettled,
|
|
1393
|
+
wrongInterestOracleDatum,
|
|
1394
|
+
);
|
|
1395
|
+
})
|
|
1396
|
+
.exhaustive();
|
|
1397
|
+
|
|
1398
|
+
if (interestAmt > 0n) {
|
|
1399
|
+
await collectInterestTx(
|
|
1400
|
+
mkAssetsOf(
|
|
1401
|
+
{
|
|
1402
|
+
currencySymbol: fromHex(
|
|
1403
|
+
sysParams.cdpParams.cdpAssetSymbol.unCurrencySymbol,
|
|
1404
|
+
),
|
|
1405
|
+
tokenName: cdpDatum.iasset,
|
|
1406
|
+
},
|
|
1407
|
+
interestAmt,
|
|
1408
|
+
),
|
|
1409
|
+
lucid,
|
|
1410
|
+
sysParams,
|
|
1411
|
+
tx,
|
|
1412
|
+
orefs.interestCollectorUtxo,
|
|
1413
|
+
);
|
|
1414
|
+
}
|
|
1415
|
+
|
|
1416
|
+
return tx;
|
|
1417
|
+
}
|
|
1418
|
+
|
|
1419
|
+
export async function runRedeemCdpWrongOracle(
|
|
1420
|
+
context: LucidContext,
|
|
1421
|
+
sysParams: SystemParams,
|
|
1422
|
+
assetInfo: AssetInfo,
|
|
1423
|
+
wrongAssetInfo: AssetInfo,
|
|
1424
|
+
collateralAsset: AssetClass,
|
|
1425
|
+
pkh: string,
|
|
1426
|
+
skh: Credential | undefined,
|
|
1427
|
+
): Promise<TxBuilder> {
|
|
1428
|
+
const cdp = await findCdp(
|
|
1429
|
+
context.lucid,
|
|
1430
|
+
sysParams.validatorHashes.cdpHash,
|
|
1431
|
+
fromSystemParamsAsset(sysParams.cdpParams.cdpAuthToken),
|
|
1432
|
+
pkh,
|
|
1433
|
+
skh,
|
|
1434
|
+
);
|
|
1435
|
+
|
|
1436
|
+
const cdpRedeemRefScriptUtxo = matchSingle(
|
|
1437
|
+
await context.lucid.utxosByOutRef([
|
|
1438
|
+
fromSystemParamsScriptRef(
|
|
1439
|
+
sysParams.scriptReferences.cdpRedeemValidatorRef,
|
|
1440
|
+
),
|
|
1441
|
+
]),
|
|
1442
|
+
(_) => new Error('Expected a single cdp redeem Ref Script UTXO'),
|
|
1443
|
+
);
|
|
1444
|
+
|
|
1445
|
+
const orefs = await findAllNecessaryOrefs(
|
|
1446
|
+
context.lucid,
|
|
1447
|
+
sysParams,
|
|
1448
|
+
assetInfo.iassetTokenNameAscii,
|
|
1449
|
+
collateralAsset,
|
|
1450
|
+
);
|
|
1451
|
+
|
|
1452
|
+
const wrongIasset = await findIAsset(
|
|
1453
|
+
context.lucid,
|
|
1454
|
+
sysParams.validatorHashes.iassetHash,
|
|
1455
|
+
fromSystemParamsAsset(sysParams.cdpParams.iAssetAuthToken),
|
|
1456
|
+
wrongAssetInfo.iassetTokenNameAscii,
|
|
1457
|
+
);
|
|
1458
|
+
|
|
1459
|
+
const wrongCollateral = await findCollateralAsset(
|
|
1460
|
+
context.lucid,
|
|
1461
|
+
sysParams,
|
|
1462
|
+
fromSystemParamsAsset(sysParams.cdpParams.collateralAssetAuthToken),
|
|
1463
|
+
wrongAssetInfo.iassetTokenNameAscii,
|
|
1464
|
+
collateralAsset,
|
|
1465
|
+
);
|
|
1466
|
+
|
|
1467
|
+
const wrongInterestOracleUtxo = await findInterestOracle(
|
|
1468
|
+
context.lucid,
|
|
1469
|
+
wrongCollateral.datum.interestOracleNft,
|
|
1470
|
+
);
|
|
1471
|
+
|
|
1472
|
+
const wrongInterestOracleDatum = parseInterestOracleDatum(
|
|
1473
|
+
getInlineDatumOrThrow(wrongInterestOracleUtxo),
|
|
1474
|
+
);
|
|
1475
|
+
|
|
1476
|
+
const wrongPriceOracleUtxo = await findPriceOracle(
|
|
1477
|
+
context.lucid,
|
|
1478
|
+
match(wrongCollateral.datum.priceInfo)
|
|
1479
|
+
.with({ OracleNft: P.select() }, (oracleNft) => oracleNft)
|
|
1480
|
+
.otherwise(() => {
|
|
1481
|
+
throw new Error('Expected active oracle');
|
|
1482
|
+
}),
|
|
1483
|
+
);
|
|
1484
|
+
|
|
1485
|
+
const wrongPriceOracleDatum = parsePriceOracleDatum(
|
|
1486
|
+
getInlineDatumOrThrow(wrongPriceOracleUtxo),
|
|
1487
|
+
);
|
|
1488
|
+
|
|
1489
|
+
const network = context.lucid.config().network!;
|
|
1490
|
+
const currentTime = BigInt(slotToUnixTime(network, context.emulator.slot));
|
|
1491
|
+
|
|
1492
|
+
const cdpRefScriptUtxo = matchSingle(
|
|
1493
|
+
await context.lucid.utxosByOutRef([
|
|
1494
|
+
fromSystemParamsScriptRef(sysParams.scriptReferences.cdpValidatorRef),
|
|
1495
|
+
]),
|
|
1496
|
+
(_) => new Error('Expected a single cdp Ref Script UTXO'),
|
|
1497
|
+
);
|
|
1498
|
+
|
|
1499
|
+
const iAssetTokenPolicyRefScriptUtxo = matchSingle(
|
|
1500
|
+
await context.lucid.utxosByOutRef([
|
|
1501
|
+
fromSystemParamsScriptRef(
|
|
1502
|
+
sysParams.scriptReferences.iAssetTokenPolicyRef,
|
|
1503
|
+
),
|
|
1504
|
+
]),
|
|
1505
|
+
(_) => new Error('Expected a single iasset token policy Ref Script UTXO'),
|
|
1506
|
+
);
|
|
1507
|
+
|
|
1508
|
+
const cdpUtxo = matchSingle(
|
|
1509
|
+
await context.lucid.utxosByOutRef([cdp.utxo]),
|
|
1510
|
+
(_) => new Error('Expected a single cdp UTXO'),
|
|
1511
|
+
);
|
|
1512
|
+
const cdpDatum = parseCdpDatumOrThrow(getInlineDatumOrThrow(cdpUtxo));
|
|
1513
|
+
|
|
1514
|
+
const govUtxo = matchSingle(
|
|
1515
|
+
await context.lucid.utxosByOutRef([orefs.govUtxo]),
|
|
1516
|
+
(_) => new Error('Expected a single gov UTXO'),
|
|
1517
|
+
);
|
|
1518
|
+
const govDatum = parseGovDatumOrThrow(getInlineDatumOrThrow(govUtxo));
|
|
1519
|
+
|
|
1520
|
+
const interestAmt = match(cdpDatum.cdpFees)
|
|
1521
|
+
.with({ FrozenCDPAccumulatedFees: P.any }, () => {
|
|
1522
|
+
throw new Error('CDP fees wrong');
|
|
1523
|
+
})
|
|
1524
|
+
.with({ ActiveCDPInterestTracking: P.select() }, (interest) => {
|
|
1525
|
+
return calculateAccruedInterest(
|
|
1526
|
+
currentTime,
|
|
1527
|
+
interest.unitaryInterestSnapshot,
|
|
1528
|
+
cdpDatum.mintedAmt,
|
|
1529
|
+
interest.lastSettled,
|
|
1530
|
+
wrongInterestOracleDatum,
|
|
1531
|
+
);
|
|
1532
|
+
})
|
|
1533
|
+
.exhaustive();
|
|
1534
|
+
|
|
1535
|
+
const collateralAmt = assetClassValueOf(
|
|
1536
|
+
cdpUtxo.assets,
|
|
1537
|
+
cdpDatum.collateralAsset,
|
|
1538
|
+
);
|
|
1539
|
+
|
|
1540
|
+
const totalCdpDebt = cdpDatum.mintedAmt + interestAmt;
|
|
1541
|
+
|
|
1542
|
+
const [isPartial, redemptionIAssetAmt] = (() => {
|
|
1543
|
+
const res = calculateMinCollateralCappedIAssetRedemptionAmt(
|
|
1544
|
+
collateralAmt,
|
|
1545
|
+
totalCdpDebt,
|
|
1546
|
+
wrongPriceOracleDatum.price,
|
|
1547
|
+
wrongCollateral.datum.redemptionRatio,
|
|
1548
|
+
wrongIasset.datum.redemptionReimbursementRatio,
|
|
1549
|
+
BigInt(wrongCollateral.datum.minCollateralAmt),
|
|
1550
|
+
);
|
|
1551
|
+
|
|
1552
|
+
const redemptionAmt = bigintMin(
|
|
1553
|
+
cdp.datum.mintedAmt,
|
|
1554
|
+
res.cappedIAssetRedemptionAmt,
|
|
1555
|
+
);
|
|
1556
|
+
|
|
1557
|
+
return [redemptionAmt < res.cappedIAssetRedemptionAmt, redemptionAmt];
|
|
1558
|
+
})();
|
|
1559
|
+
|
|
1560
|
+
if (redemptionIAssetAmt <= 0) {
|
|
1561
|
+
throw new Error("There's no iAssets available for redemption.");
|
|
1562
|
+
}
|
|
1563
|
+
|
|
1564
|
+
const redemptionCollateralAmt = rationalFloor(
|
|
1565
|
+
rationalMul(
|
|
1566
|
+
wrongPriceOracleDatum.price,
|
|
1567
|
+
rationalFromInt(redemptionIAssetAmt),
|
|
1568
|
+
),
|
|
1569
|
+
);
|
|
1570
|
+
|
|
1571
|
+
const processingFee = calculateFeeFromRatio(
|
|
1572
|
+
wrongIasset.datum.redemptionProcessingFeeRatio,
|
|
1573
|
+
redemptionCollateralAmt,
|
|
1574
|
+
);
|
|
1575
|
+
const reimburstmentFee = calculateFeeFromRatio(
|
|
1576
|
+
wrongIasset.datum.redemptionReimbursementRatio,
|
|
1577
|
+
redemptionCollateralAmt,
|
|
1578
|
+
);
|
|
1579
|
+
|
|
1580
|
+
const txValidity = oracleExpirationAwareValidity(
|
|
1581
|
+
context.emulator.slot,
|
|
1582
|
+
Number(sysParams.cdpCreatorParams.biasTime),
|
|
1583
|
+
Number(wrongPriceOracleDatum.expirationTime),
|
|
1584
|
+
network,
|
|
1585
|
+
);
|
|
1586
|
+
|
|
1587
|
+
const referenceInputs = [
|
|
1588
|
+
wrongIasset.utxo,
|
|
1589
|
+
wrongCollateral.utxo,
|
|
1590
|
+
wrongPriceOracleUtxo,
|
|
1591
|
+
wrongInterestOracleUtxo,
|
|
1592
|
+
govUtxo,
|
|
1593
|
+
];
|
|
1594
|
+
|
|
1595
|
+
const referenceScripts = [
|
|
1596
|
+
cdpRefScriptUtxo,
|
|
1597
|
+
iAssetTokenPolicyRefScriptUtxo,
|
|
1598
|
+
cdpRedeemRefScriptUtxo,
|
|
1599
|
+
];
|
|
1600
|
+
|
|
1601
|
+
const tx = context.lucid.newTx().readFrom(referenceScripts);
|
|
1602
|
+
|
|
1603
|
+
const interestCollectorRefScriptUtxo =
|
|
1604
|
+
interestAmt > 0n
|
|
1605
|
+
? await collectInterestTx(
|
|
1606
|
+
mkAssetsOf(
|
|
1607
|
+
{
|
|
1608
|
+
currencySymbol: fromHex(
|
|
1609
|
+
sysParams.cdpParams.cdpAssetSymbol.unCurrencySymbol,
|
|
1610
|
+
),
|
|
1611
|
+
tokenName: cdpDatum.iasset,
|
|
1612
|
+
},
|
|
1613
|
+
interestAmt,
|
|
1614
|
+
),
|
|
1615
|
+
context.lucid,
|
|
1616
|
+
sysParams,
|
|
1617
|
+
tx,
|
|
1618
|
+
orefs.interestCollectorUtxo,
|
|
1619
|
+
)
|
|
1620
|
+
: undefined;
|
|
1621
|
+
|
|
1622
|
+
// We need to take into account the interest collector ref script as well.
|
|
1623
|
+
const priceOracleIdx = getInputIndices(
|
|
1624
|
+
[wrongPriceOracleUtxo],
|
|
1625
|
+
[
|
|
1626
|
+
...referenceInputs,
|
|
1627
|
+
...referenceScripts,
|
|
1628
|
+
...(interestCollectorRefScriptUtxo != null
|
|
1629
|
+
? [interestCollectorRefScriptUtxo]
|
|
1630
|
+
: []),
|
|
1631
|
+
],
|
|
1632
|
+
)[0];
|
|
1633
|
+
|
|
1634
|
+
tx.readFrom(referenceInputs)
|
|
1635
|
+
// Trigger CDP Redeem Withdrawal validator
|
|
1636
|
+
.withdraw(
|
|
1637
|
+
credentialToRewardAddress(
|
|
1638
|
+
context.lucid.config().network!,
|
|
1639
|
+
scriptHashToCredential(sysParams.cdpParams.cdpRedeemValHash),
|
|
1640
|
+
),
|
|
1641
|
+
0n,
|
|
1642
|
+
serialiseRedeemCdpWithdrawalRedeemer({
|
|
1643
|
+
cdpOutReference: {
|
|
1644
|
+
txHash: fromHex(cdpUtxo.txHash),
|
|
1645
|
+
outputIndex: BigInt(cdpUtxo.outputIndex),
|
|
1646
|
+
},
|
|
1647
|
+
currentTime: currentTime,
|
|
1648
|
+
priceOracleIdx: { OracleRefInputIdx: priceOracleIdx },
|
|
1649
|
+
}),
|
|
1650
|
+
)
|
|
1651
|
+
.validFrom(txValidity.validFrom)
|
|
1652
|
+
.validTo(txValidity.validTo)
|
|
1653
|
+
.collectFrom([cdpUtxo], serialiseCdpRedeemer('RedeemCdp'))
|
|
1654
|
+
.mintAssets(
|
|
1655
|
+
mkAssetsOf(
|
|
1656
|
+
{
|
|
1657
|
+
currencySymbol: fromHex(
|
|
1658
|
+
sysParams.cdpParams.cdpAssetSymbol.unCurrencySymbol,
|
|
1659
|
+
),
|
|
1660
|
+
tokenName: cdpDatum.iasset,
|
|
1661
|
+
},
|
|
1662
|
+
interestAmt - redemptionIAssetAmt,
|
|
1663
|
+
),
|
|
1664
|
+
Data.void(),
|
|
1665
|
+
)
|
|
1666
|
+
.pay.ToContract(
|
|
1667
|
+
cdpUtxo.address,
|
|
1668
|
+
{
|
|
1669
|
+
kind: 'inline',
|
|
1670
|
+
value: serialiseCdpDatum({
|
|
1671
|
+
...cdpDatum,
|
|
1672
|
+
mintedAmt: totalCdpDebt - redemptionIAssetAmt,
|
|
1673
|
+
cdpFees: {
|
|
1674
|
+
ActiveCDPInterestTracking: {
|
|
1675
|
+
lastSettled: currentTime,
|
|
1676
|
+
unitaryInterestSnapshot:
|
|
1677
|
+
wrongInterestOracleDatum.unitaryInterest +
|
|
1678
|
+
calculateUnitaryInterestSinceOracleLastUpdated(
|
|
1679
|
+
currentTime,
|
|
1680
|
+
wrongInterestOracleDatum,
|
|
1681
|
+
),
|
|
1682
|
+
},
|
|
1683
|
+
},
|
|
1684
|
+
}),
|
|
1685
|
+
},
|
|
1686
|
+
addAssets(
|
|
1687
|
+
cdpUtxo.assets,
|
|
1688
|
+
mkAssetsOf(
|
|
1689
|
+
cdpDatum.collateralAsset,
|
|
1690
|
+
-redemptionCollateralAmt + reimburstmentFee,
|
|
1691
|
+
),
|
|
1692
|
+
),
|
|
1693
|
+
);
|
|
1694
|
+
|
|
1695
|
+
const partialRedemptionFee = F.pipe(
|
|
1696
|
+
govDatum.protocolParams.cdpRedemptionRequiredSignature,
|
|
1697
|
+
O.fromNullable,
|
|
1698
|
+
O.match(
|
|
1699
|
+
// When public redemptions
|
|
1700
|
+
() => {
|
|
1701
|
+
return isPartial
|
|
1702
|
+
? BigInt(sysParams.cdpRedeemParams.partialRedemptionExtraFeeLovelace)
|
|
1703
|
+
: 0n;
|
|
1704
|
+
},
|
|
1705
|
+
// When private redemptions
|
|
1706
|
+
(requiredSignature) => {
|
|
1707
|
+
tx.addSignerKey(toHex(requiredSignature));
|
|
1708
|
+
return 0n;
|
|
1709
|
+
},
|
|
1710
|
+
),
|
|
1711
|
+
);
|
|
1712
|
+
|
|
1713
|
+
//TODO: Use a treasury input to save on ADA.
|
|
1714
|
+
tx.pay.ToContract(
|
|
1715
|
+
credentialToAddress(context.lucid.config().network!, {
|
|
1716
|
+
hash: validatorToScriptHash(
|
|
1717
|
+
mkTreasuryValidatorFromSP(sysParams.treasuryParams),
|
|
1718
|
+
),
|
|
1719
|
+
type: 'Script',
|
|
1720
|
+
}),
|
|
1721
|
+
{ kind: 'inline', value: Data.void() },
|
|
1722
|
+
addAssets(
|
|
1723
|
+
mkAssetsOf(cdpDatum.collateralAsset, processingFee),
|
|
1724
|
+
mkLovelacesOf(partialRedemptionFee),
|
|
1725
|
+
),
|
|
1726
|
+
);
|
|
1727
|
+
|
|
1728
|
+
return tx;
|
|
1729
|
+
}
|