@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,440 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Credential,
|
|
3
|
+
fromText,
|
|
4
|
+
LucidEvolution,
|
|
5
|
+
ScriptHash,
|
|
6
|
+
toHex,
|
|
7
|
+
toText,
|
|
8
|
+
UTxO,
|
|
9
|
+
} from '@lucid-evolution/lucid';
|
|
10
|
+
import {
|
|
11
|
+
addrDetails,
|
|
12
|
+
adjustPriceToDecimals,
|
|
13
|
+
cdpCollateralRatioPercentage,
|
|
14
|
+
createScriptAddress,
|
|
15
|
+
fromSystemParamsAsset,
|
|
16
|
+
getRandomElement,
|
|
17
|
+
matchSingle,
|
|
18
|
+
SystemParams,
|
|
19
|
+
} from '../../src';
|
|
20
|
+
import { option as O, array as A, function as F } from 'fp-ts';
|
|
21
|
+
import {
|
|
22
|
+
CDPContent,
|
|
23
|
+
parseCdpDatum,
|
|
24
|
+
parseStableswapPoolDatum,
|
|
25
|
+
StableswapPoolContent,
|
|
26
|
+
} from '../../src/contracts/cdp/types-new';
|
|
27
|
+
import { match, P } from 'ts-pattern';
|
|
28
|
+
import {
|
|
29
|
+
AssetClass,
|
|
30
|
+
assetClassToUnit,
|
|
31
|
+
getInlineDatumOrThrow,
|
|
32
|
+
} from '@3rd-eye-labs/cardano-offchain-common';
|
|
33
|
+
import {
|
|
34
|
+
InterestOracleDatum,
|
|
35
|
+
parseInterestOracleDatum,
|
|
36
|
+
} from '../../src/contracts/interest-oracle/types-new';
|
|
37
|
+
import { findCollateralAsset, findIAsset } from '../queries/iasset-queries';
|
|
38
|
+
import { findStabilityPool } from '../queries/stability-pool-queries';
|
|
39
|
+
import { findPriceOracle } from '../price-oracle/price-oracle-queries';
|
|
40
|
+
import { findInterestOracle } from '../queries/interest-oracle-queries';
|
|
41
|
+
import { findRandomCollector } from '../queries/collector-queries';
|
|
42
|
+
import { findGov } from '../gov/governance-queries';
|
|
43
|
+
import { findRandomTreasuryUtxoWithOnlyAda } from '../queries/treasury-queries';
|
|
44
|
+
import { parsePriceOracleDatum } from '../../src/contracts/price-oracle/types-new';
|
|
45
|
+
import {
|
|
46
|
+
CollateralAssetOutput,
|
|
47
|
+
IAssetOutput,
|
|
48
|
+
} from '../../src/contracts/iasset/types';
|
|
49
|
+
import { findRandomNonAdminInterestCollector } from '../interest-collection/interest-collector-queries';
|
|
50
|
+
import { LucidContext } from '../test-helpers';
|
|
51
|
+
import { Rational } from '../../src/types/rational';
|
|
52
|
+
|
|
53
|
+
export async function findAllActiveCdps(
|
|
54
|
+
lucid: LucidEvolution,
|
|
55
|
+
sysParams: SystemParams,
|
|
56
|
+
assetAscii: string,
|
|
57
|
+
stakeCred?: Credential,
|
|
58
|
+
): Promise<{ utxo: UTxO; datum: CDPContent }[]> {
|
|
59
|
+
const cdpUtxos = await lucid.utxosAtWithUnit(
|
|
60
|
+
createScriptAddress(
|
|
61
|
+
lucid.config().network!,
|
|
62
|
+
sysParams.validatorHashes.cdpHash,
|
|
63
|
+
stakeCred,
|
|
64
|
+
),
|
|
65
|
+
assetClassToUnit(fromSystemParamsAsset(sysParams.cdpParams.cdpAuthToken)),
|
|
66
|
+
);
|
|
67
|
+
|
|
68
|
+
return F.pipe(
|
|
69
|
+
cdpUtxos.map((utxo) =>
|
|
70
|
+
F.pipe(
|
|
71
|
+
O.fromNullable(utxo.datum),
|
|
72
|
+
O.flatMap(parseCdpDatum),
|
|
73
|
+
O.flatMap((datum) => {
|
|
74
|
+
if (toHex(datum.iasset) === fromText(assetAscii) && datum.cdpOwner) {
|
|
75
|
+
return O.some({ utxo, datum: datum });
|
|
76
|
+
} else {
|
|
77
|
+
return O.none;
|
|
78
|
+
}
|
|
79
|
+
}),
|
|
80
|
+
),
|
|
81
|
+
),
|
|
82
|
+
A.compact,
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export async function findCdp(
|
|
87
|
+
lucid: LucidEvolution,
|
|
88
|
+
cdpScriptHash: ScriptHash,
|
|
89
|
+
cdpNft: AssetClass,
|
|
90
|
+
ownerPkh: string,
|
|
91
|
+
stakeCred?: Credential,
|
|
92
|
+
): Promise<{ utxo: UTxO; datum: CDPContent }> {
|
|
93
|
+
const cdpUtxos = await lucid.utxosAtWithUnit(
|
|
94
|
+
createScriptAddress(lucid.config().network!, cdpScriptHash, stakeCred),
|
|
95
|
+
assetClassToUnit(cdpNft),
|
|
96
|
+
);
|
|
97
|
+
|
|
98
|
+
return matchSingle(
|
|
99
|
+
F.pipe(
|
|
100
|
+
cdpUtxos.map((utxo) =>
|
|
101
|
+
F.pipe(
|
|
102
|
+
O.fromNullable(utxo.datum),
|
|
103
|
+
O.flatMap(parseCdpDatum),
|
|
104
|
+
O.flatMap((datum) => {
|
|
105
|
+
if (datum.cdpOwner && toHex(datum.cdpOwner) === ownerPkh) {
|
|
106
|
+
return O.some({ utxo, datum: datum });
|
|
107
|
+
} else {
|
|
108
|
+
return O.none;
|
|
109
|
+
}
|
|
110
|
+
}),
|
|
111
|
+
),
|
|
112
|
+
),
|
|
113
|
+
A.compact,
|
|
114
|
+
),
|
|
115
|
+
(res) => new Error('Expected a single CDP UTXO.: ' + JSON.stringify(res)),
|
|
116
|
+
);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// TODO: use the new variant defined below.
|
|
120
|
+
export async function findOwnCdp(
|
|
121
|
+
lucid: LucidEvolution,
|
|
122
|
+
cdpScriptHash: ScriptHash,
|
|
123
|
+
cdpNft: AssetClass,
|
|
124
|
+
): Promise<{ utxo: UTxO; datum: CDPContent }> {
|
|
125
|
+
const [pkh, skh] = await addrDetails(lucid);
|
|
126
|
+
return findCdp(lucid, cdpScriptHash, cdpNft, pkh.hash, skh);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
export async function findOwnCdpNew(
|
|
130
|
+
lucid: LucidEvolution,
|
|
131
|
+
sysParams: SystemParams,
|
|
132
|
+
): Promise<{ utxo: UTxO; datum: CDPContent }> {
|
|
133
|
+
const [pkh, skh] = await addrDetails(lucid);
|
|
134
|
+
return findCdp(
|
|
135
|
+
lucid,
|
|
136
|
+
sysParams.validatorHashes.cdpHash,
|
|
137
|
+
fromSystemParamsAsset(sysParams.cdpParams.cdpAuthToken),
|
|
138
|
+
pkh.hash,
|
|
139
|
+
skh,
|
|
140
|
+
);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
export async function findFrozenCDPs(
|
|
144
|
+
lucid: LucidEvolution,
|
|
145
|
+
cdpScriptHash: ScriptHash,
|
|
146
|
+
cdpNft: AssetClass,
|
|
147
|
+
assetAscii: string,
|
|
148
|
+
): Promise<{ utxo: UTxO; datum: CDPContent }[]> {
|
|
149
|
+
const cdpUtxos = await lucid.utxosAtWithUnit(
|
|
150
|
+
createScriptAddress(lucid.config().network!, cdpScriptHash),
|
|
151
|
+
assetClassToUnit(cdpNft),
|
|
152
|
+
);
|
|
153
|
+
|
|
154
|
+
return F.pipe(
|
|
155
|
+
cdpUtxos.map((utxo) =>
|
|
156
|
+
F.pipe(
|
|
157
|
+
O.fromNullable(utxo.datum),
|
|
158
|
+
O.flatMap(parseCdpDatum),
|
|
159
|
+
O.flatMap((datum) => {
|
|
160
|
+
if (
|
|
161
|
+
datum.cdpOwner == null &&
|
|
162
|
+
toHex(datum.iasset) === fromText(assetAscii)
|
|
163
|
+
) {
|
|
164
|
+
return O.some({ utxo, datum: datum });
|
|
165
|
+
} else {
|
|
166
|
+
return O.none;
|
|
167
|
+
}
|
|
168
|
+
}),
|
|
169
|
+
),
|
|
170
|
+
),
|
|
171
|
+
A.compact,
|
|
172
|
+
);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
export async function findStableswapPool(
|
|
176
|
+
lucid: LucidEvolution,
|
|
177
|
+
cdpScriptHash: ScriptHash,
|
|
178
|
+
cdpNft: AssetClass,
|
|
179
|
+
iassetName: string,
|
|
180
|
+
collateralAsset: AssetClass,
|
|
181
|
+
): Promise<{ utxo: UTxO; datum: StableswapPoolContent }> {
|
|
182
|
+
const cdpUtxos = await lucid.utxosAtWithUnit(
|
|
183
|
+
createScriptAddress(lucid.config().network!, cdpScriptHash),
|
|
184
|
+
assetClassToUnit(cdpNft),
|
|
185
|
+
);
|
|
186
|
+
|
|
187
|
+
return matchSingle(
|
|
188
|
+
F.pipe(
|
|
189
|
+
cdpUtxos.map((utxo) =>
|
|
190
|
+
F.pipe(
|
|
191
|
+
O.fromNullable(utxo.datum),
|
|
192
|
+
O.flatMap(parseStableswapPoolDatum),
|
|
193
|
+
O.flatMap((datum) => {
|
|
194
|
+
if (
|
|
195
|
+
toHex(datum.iasset) === iassetName &&
|
|
196
|
+
toHex(datum.collateralAsset.currencySymbol) ===
|
|
197
|
+
toHex(collateralAsset.currencySymbol) &&
|
|
198
|
+
toHex(datum.collateralAsset.tokenName) ===
|
|
199
|
+
toHex(collateralAsset.tokenName)
|
|
200
|
+
) {
|
|
201
|
+
return O.some({ utxo, datum: datum });
|
|
202
|
+
} else {
|
|
203
|
+
return O.none;
|
|
204
|
+
}
|
|
205
|
+
}),
|
|
206
|
+
),
|
|
207
|
+
),
|
|
208
|
+
A.compact,
|
|
209
|
+
),
|
|
210
|
+
(res) =>
|
|
211
|
+
new Error(
|
|
212
|
+
'Expected a single stableswap pool UTXO.: ' + JSON.stringify(res),
|
|
213
|
+
),
|
|
214
|
+
);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
export async function findAllCdpCreators(
|
|
218
|
+
lucid: LucidEvolution,
|
|
219
|
+
cdpCreatorScriptHash: string,
|
|
220
|
+
cdpCreatorNft: AssetClass,
|
|
221
|
+
): Promise<UTxO[]> {
|
|
222
|
+
return lucid.utxosAtWithUnit(
|
|
223
|
+
createScriptAddress(lucid.config().network!, cdpCreatorScriptHash),
|
|
224
|
+
assetClassToUnit(cdpCreatorNft),
|
|
225
|
+
);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
// TODO: replace by the new variant defined below
|
|
229
|
+
export async function findRandomCdpCreator(
|
|
230
|
+
lucid: LucidEvolution,
|
|
231
|
+
cdpCreatorScriptHash: string,
|
|
232
|
+
cdpCreatorNft: AssetClass,
|
|
233
|
+
): Promise<UTxO> {
|
|
234
|
+
const cdpCreatorUtxos = await findAllCdpCreators(
|
|
235
|
+
lucid,
|
|
236
|
+
cdpCreatorScriptHash,
|
|
237
|
+
cdpCreatorNft,
|
|
238
|
+
);
|
|
239
|
+
|
|
240
|
+
return F.pipe(
|
|
241
|
+
O.fromNullable(getRandomElement(cdpCreatorUtxos)),
|
|
242
|
+
O.match(() => {
|
|
243
|
+
throw new Error('Expected some cdp creator UTXOs.');
|
|
244
|
+
}, F.identity),
|
|
245
|
+
);
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
export async function findRandomCdpCreatorNew(
|
|
249
|
+
context: LucidContext,
|
|
250
|
+
sysParams: SystemParams,
|
|
251
|
+
): Promise<UTxO> {
|
|
252
|
+
const cdpCreatorUtxos = await findAllCdpCreators(
|
|
253
|
+
context.lucid,
|
|
254
|
+
sysParams.validatorHashes.cdpCreatorHash,
|
|
255
|
+
fromSystemParamsAsset(sysParams.cdpCreatorParams.cdpCreatorNft),
|
|
256
|
+
);
|
|
257
|
+
|
|
258
|
+
return F.pipe(
|
|
259
|
+
O.fromNullable(getRandomElement(cdpCreatorUtxos)),
|
|
260
|
+
O.match(() => {
|
|
261
|
+
throw new Error('Expected some cdp creator UTXOs.');
|
|
262
|
+
}, F.identity),
|
|
263
|
+
);
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
export function findPriceOracleFromCollateralAsset(
|
|
267
|
+
lucid: LucidEvolution,
|
|
268
|
+
collateralAsset: CollateralAssetOutput,
|
|
269
|
+
): Promise<UTxO | undefined> {
|
|
270
|
+
return match(collateralAsset.datum.priceInfo)
|
|
271
|
+
.with({ OracleNft: P.select() }, (oracleNft) =>
|
|
272
|
+
findPriceOracle(lucid, oracleNft),
|
|
273
|
+
)
|
|
274
|
+
.with({ Delisted: P.any }, () => {
|
|
275
|
+
throw new Error('Cannot find price oracle as iAsset is delisted');
|
|
276
|
+
})
|
|
277
|
+
.otherwise(() => Promise.resolve(undefined));
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
export async function findAllNecessaryOrefs(
|
|
281
|
+
lucid: LucidEvolution,
|
|
282
|
+
sysParams: SystemParams,
|
|
283
|
+
iasset: string,
|
|
284
|
+
collateralAsset: AssetClass,
|
|
285
|
+
): Promise<{
|
|
286
|
+
stabilityPoolUtxo: UTxO;
|
|
287
|
+
iasset: IAssetOutput;
|
|
288
|
+
collateralAsset: CollateralAssetOutput;
|
|
289
|
+
cdpCreatorUtxo: UTxO;
|
|
290
|
+
interestOracleUtxo: UTxO;
|
|
291
|
+
collectorUtxo: UTxO;
|
|
292
|
+
interestCollectorUtxo: UTxO;
|
|
293
|
+
govUtxo: UTxO;
|
|
294
|
+
treasuryUtxo: UTxO;
|
|
295
|
+
}> {
|
|
296
|
+
const iassetOut = await findIAsset(
|
|
297
|
+
lucid,
|
|
298
|
+
sysParams.validatorHashes.iassetHash,
|
|
299
|
+
fromSystemParamsAsset(sysParams.cdpParams.iAssetAuthToken),
|
|
300
|
+
iasset,
|
|
301
|
+
);
|
|
302
|
+
|
|
303
|
+
const collateralAssetOut = await findCollateralAsset(
|
|
304
|
+
lucid,
|
|
305
|
+
sysParams,
|
|
306
|
+
fromSystemParamsAsset(sysParams.cdpCreatorParams.collateralAssetAuthTk),
|
|
307
|
+
iasset,
|
|
308
|
+
collateralAsset,
|
|
309
|
+
);
|
|
310
|
+
|
|
311
|
+
const stabilityPool = await findStabilityPool(lucid, sysParams, iasset);
|
|
312
|
+
|
|
313
|
+
return {
|
|
314
|
+
stabilityPoolUtxo: stabilityPool.utxo,
|
|
315
|
+
iasset: iassetOut,
|
|
316
|
+
collateralAsset: collateralAssetOut,
|
|
317
|
+
cdpCreatorUtxo: await findRandomCdpCreator(
|
|
318
|
+
lucid,
|
|
319
|
+
sysParams.validatorHashes.cdpCreatorHash,
|
|
320
|
+
fromSystemParamsAsset(sysParams.cdpCreatorParams.cdpCreatorNft),
|
|
321
|
+
),
|
|
322
|
+
interestOracleUtxo: await findInterestOracle(
|
|
323
|
+
lucid,
|
|
324
|
+
collateralAssetOut.datum.interestOracleNft,
|
|
325
|
+
),
|
|
326
|
+
collectorUtxo: await findRandomCollector(
|
|
327
|
+
lucid,
|
|
328
|
+
sysParams.validatorHashes.collectorHash,
|
|
329
|
+
),
|
|
330
|
+
interestCollectorUtxo: await findRandomNonAdminInterestCollector(
|
|
331
|
+
lucid,
|
|
332
|
+
sysParams.validatorHashes.interestCollectionHash,
|
|
333
|
+
fromSystemParamsAsset(sysParams.interestCollectionParams.multisigUtxoNft),
|
|
334
|
+
),
|
|
335
|
+
govUtxo: (
|
|
336
|
+
await findGov(
|
|
337
|
+
lucid,
|
|
338
|
+
sysParams.validatorHashes.govHash,
|
|
339
|
+
fromSystemParamsAsset(sysParams.govParams.govNFT),
|
|
340
|
+
)
|
|
341
|
+
).utxo,
|
|
342
|
+
treasuryUtxo: await findRandomTreasuryUtxoWithOnlyAda(lucid, sysParams),
|
|
343
|
+
};
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
export async function findPrice(
|
|
347
|
+
lucid: LucidEvolution,
|
|
348
|
+
sysParams: SystemParams,
|
|
349
|
+
asset: string,
|
|
350
|
+
collateralAsset: AssetClass,
|
|
351
|
+
): Promise<Rational> {
|
|
352
|
+
const orefs = await findAllNecessaryOrefs(
|
|
353
|
+
lucid,
|
|
354
|
+
sysParams,
|
|
355
|
+
asset,
|
|
356
|
+
collateralAsset,
|
|
357
|
+
);
|
|
358
|
+
|
|
359
|
+
const priceIasset = await match(orefs.collateralAsset.datum.priceInfo)
|
|
360
|
+
.with({ OracleNft: P.select() }, async (oracleNft) => {
|
|
361
|
+
const priceOracleUtxo = matchSingle(
|
|
362
|
+
await lucid.utxosByOutRef([await findPriceOracle(lucid, oracleNft)]),
|
|
363
|
+
(_) => new Error('Expected a single price oracle UTXO'),
|
|
364
|
+
);
|
|
365
|
+
return parsePriceOracleDatum(getInlineDatumOrThrow(priceOracleUtxo))
|
|
366
|
+
.price;
|
|
367
|
+
})
|
|
368
|
+
.with({ Delisted: P.select() }, (price) => {
|
|
369
|
+
return price.price;
|
|
370
|
+
})
|
|
371
|
+
.with({ DeferredValidation: P.select() }, (_) => {
|
|
372
|
+
throw new Error('Pyth price not implemented');
|
|
373
|
+
})
|
|
374
|
+
.exhaustive();
|
|
375
|
+
|
|
376
|
+
return priceIasset;
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
export async function findInterestDatum(
|
|
380
|
+
lucid: LucidEvolution,
|
|
381
|
+
sysParams: SystemParams,
|
|
382
|
+
asset: string,
|
|
383
|
+
collateralAsset: AssetClass,
|
|
384
|
+
): Promise<InterestOracleDatum> {
|
|
385
|
+
const orefs = await findAllNecessaryOrefs(
|
|
386
|
+
lucid,
|
|
387
|
+
sysParams,
|
|
388
|
+
asset,
|
|
389
|
+
collateralAsset,
|
|
390
|
+
);
|
|
391
|
+
|
|
392
|
+
const interestOracleUtxo = matchSingle(
|
|
393
|
+
await lucid.utxosByOutRef([orefs.interestOracleUtxo]),
|
|
394
|
+
(_) => new Error('Expected a single interest oracle UTXO'),
|
|
395
|
+
);
|
|
396
|
+
return parseInterestOracleDatum(getInlineDatumOrThrow(interestOracleUtxo));
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
export async function findCdpCR(
|
|
400
|
+
lucid: LucidEvolution,
|
|
401
|
+
sysParams: SystemParams,
|
|
402
|
+
cdp: { utxo: UTxO; datum: CDPContent },
|
|
403
|
+
slot: number,
|
|
404
|
+
): Promise<number> {
|
|
405
|
+
const iassetAscii = toText(toHex(cdp.datum.iasset));
|
|
406
|
+
|
|
407
|
+
const collateralAsset = await findCollateralAsset(
|
|
408
|
+
lucid,
|
|
409
|
+
sysParams,
|
|
410
|
+
fromSystemParamsAsset(sysParams.cdpParams.collateralAssetAuthToken),
|
|
411
|
+
iassetAscii,
|
|
412
|
+
cdp.datum.collateralAsset,
|
|
413
|
+
);
|
|
414
|
+
|
|
415
|
+
const price = await findPrice(
|
|
416
|
+
lucid,
|
|
417
|
+
sysParams,
|
|
418
|
+
iassetAscii,
|
|
419
|
+
cdp.datum.collateralAsset,
|
|
420
|
+
);
|
|
421
|
+
|
|
422
|
+
const adjustedPrice = adjustPriceToDecimals(
|
|
423
|
+
collateralAsset.datum.extraDecimals,
|
|
424
|
+
price,
|
|
425
|
+
);
|
|
426
|
+
|
|
427
|
+
return cdpCollateralRatioPercentage(
|
|
428
|
+
slot,
|
|
429
|
+
adjustedPrice,
|
|
430
|
+
cdp.utxo,
|
|
431
|
+
cdp.datum,
|
|
432
|
+
await findInterestDatum(
|
|
433
|
+
lucid,
|
|
434
|
+
sysParams,
|
|
435
|
+
iassetAscii,
|
|
436
|
+
cdp.datum.collateralAsset,
|
|
437
|
+
),
|
|
438
|
+
lucid.config().network!,
|
|
439
|
+
);
|
|
440
|
+
}
|