@indigo-labs/indigo-sdk 0.1.3 → 0.1.4
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/package.json +4 -1
- package/.github/workflows/ci.yml +0 -62
- package/.github/workflows/test.yml +0 -44
- package/.husky/pre-commit +0 -1
- package/.nvmrc +0 -1
- package/.prettierrc +0 -6
- package/eslint.config.mjs +0 -35
- package/src/contracts/cdp.ts +0 -741
- package/src/contracts/collector.ts +0 -98
- package/src/contracts/gov.ts +0 -1
- package/src/contracts/interest-oracle.ts +0 -149
- package/src/contracts/lrp.ts +0 -223
- package/src/contracts/one-shot.ts +0 -67
- package/src/contracts/stability-pool.ts +0 -684
- package/src/contracts/staking.ts +0 -348
- package/src/contracts/treasury.ts +0 -112
- package/src/helpers/asset-helpers.ts +0 -57
- package/src/helpers/helper-txs.ts +0 -30
- package/src/helpers/helpers.ts +0 -38
- package/src/helpers/indigo-helpers.ts +0 -19
- package/src/helpers/interest-oracle.ts +0 -57
- package/src/helpers/lucid-utils.ts +0 -70
- package/src/helpers/price-oracle-helpers.ts +0 -36
- package/src/helpers/stability-pool-helpers.ts +0 -207
- package/src/helpers/staking-helpers.ts +0 -94
- package/src/helpers/time-helpers.ts +0 -4
- package/src/helpers/value-helpers.ts +0 -16
- package/src/index.ts +0 -34
- package/src/scripts/always-fail-validator.ts +0 -7
- package/src/scripts/auth-token-policy.ts +0 -23
- package/src/scripts/cdp-creator-validator.ts +0 -53
- package/src/scripts/cdp-validator.ts +0 -9
- package/src/scripts/collector-validator.ts +0 -8
- package/src/scripts/execute-validator.ts +0 -52
- package/src/scripts/gov-validator.ts +0 -44
- package/src/scripts/iasset-policy.ts +0 -22
- package/src/scripts/interest-oracle-validator.ts +0 -23
- package/src/scripts/lrp-validator.ts +0 -23
- package/src/scripts/one-shot-policy.ts +0 -62
- package/src/scripts/poll-manager-validator.ts +0 -52
- package/src/scripts/poll-shard-validator.ts +0 -47
- package/src/scripts/price-oracle-validator.ts +0 -26
- package/src/scripts/stability-pool-validator.ts +0 -60
- package/src/scripts/staking-validator.ts +0 -8
- package/src/scripts/treasury-validator.ts +0 -8
- package/src/scripts/version-record-policy.ts +0 -26
- package/src/scripts/version-registry.ts +0 -15
- package/src/types/generic.ts +0 -106
- package/src/types/indigo/cdp-creator.ts +0 -46
- package/src/types/indigo/cdp.ts +0 -88
- package/src/types/indigo/execute.ts +0 -21
- package/src/types/indigo/gov.ts +0 -51
- package/src/types/indigo/interest-oracle.ts +0 -55
- package/src/types/indigo/lrp.ts +0 -54
- package/src/types/indigo/poll-manager.ts +0 -21
- package/src/types/indigo/poll-shard.ts +0 -16
- package/src/types/indigo/price-oracle.ts +0 -38
- package/src/types/indigo/stability-pool.ts +0 -233
- package/src/types/indigo/staking.ts +0 -99
- package/src/types/indigo/version-record.ts +0 -17
- package/src/types/on-chain-decimal.ts +0 -23
- package/src/types/one-shot.ts +0 -22
- package/src/types/system-params.ts +0 -246
- package/tests/data/system-params.json +0 -989
- package/tests/datums.test.ts +0 -286
- package/tests/endpoints/initialize.ts +0 -1013
- package/tests/hash-checks.test.ts +0 -73
- package/tests/indigo-test-helpers.ts +0 -115
- package/tests/initialize.test.ts +0 -27
- package/tests/interest-calculations.test.ts +0 -120
- package/tests/interest-oracle.test.ts +0 -90
- package/tests/lrp.test.ts +0 -149
- package/tests/queries/governance-queries.ts +0 -31
- package/tests/queries/iasset-queries.ts +0 -39
- package/tests/queries/interest-oracle-queries.ts +0 -13
- package/tests/queries/lrp-queries.ts +0 -39
- package/tests/queries/price-oracle-queries.ts +0 -27
- package/tests/queries/stability-pool-queries.ts +0 -75
- package/tests/queries/staking-queries.ts +0 -43
- package/tests/stability-pool.test.ts +0 -364
- package/tests/staking.test.ts +0 -105
- package/tests/test-helpers.ts +0 -38
- package/tsconfig.build.json +0 -4
- package/tsconfig.json +0 -16
- package/vitest.config.ts +0 -9
package/src/contracts/staking.ts
DELETED
|
@@ -1,348 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
Address,
|
|
3
|
-
applyParamsToScript,
|
|
4
|
-
Constr,
|
|
5
|
-
Data,
|
|
6
|
-
fromText,
|
|
7
|
-
LucidEvolution,
|
|
8
|
-
OutRef,
|
|
9
|
-
SpendingValidator,
|
|
10
|
-
TxBuilder,
|
|
11
|
-
UTxO,
|
|
12
|
-
validatorToAddress,
|
|
13
|
-
validatorToScriptHash,
|
|
14
|
-
} from '@lucid-evolution/lucid';
|
|
15
|
-
import {
|
|
16
|
-
ScriptReferences,
|
|
17
|
-
StakingParams,
|
|
18
|
-
SystemParams,
|
|
19
|
-
} from '../types/system-params';
|
|
20
|
-
import { addrDetails, scriptRef } from '../helpers/lucid-utils';
|
|
21
|
-
import { _stakingValidator } from '../scripts/staking-validator';
|
|
22
|
-
import {
|
|
23
|
-
serialiseStakingDatum,
|
|
24
|
-
StakingManagerContent,
|
|
25
|
-
StakingPositionContent,
|
|
26
|
-
} from '../types/indigo/staking';
|
|
27
|
-
import { StakingHelpers } from '../helpers/staking-helpers';
|
|
28
|
-
|
|
29
|
-
export class StakingContract {
|
|
30
|
-
static async openPosition(
|
|
31
|
-
amount: bigint,
|
|
32
|
-
params: SystemParams,
|
|
33
|
-
lucid: LucidEvolution,
|
|
34
|
-
stakingManagerRef?: OutRef,
|
|
35
|
-
): Promise<TxBuilder> {
|
|
36
|
-
const [pkh, _] = await addrDetails(lucid);
|
|
37
|
-
|
|
38
|
-
const stakingManagerOut = stakingManagerRef
|
|
39
|
-
? await StakingHelpers.findStakingManagerByOutRef(
|
|
40
|
-
stakingManagerRef,
|
|
41
|
-
lucid,
|
|
42
|
-
)
|
|
43
|
-
: await StakingHelpers.findStakingManager(params, lucid);
|
|
44
|
-
const stakingScriptRef = await StakingContract.scriptRef(
|
|
45
|
-
params.scriptReferences,
|
|
46
|
-
lucid,
|
|
47
|
-
);
|
|
48
|
-
const stakingTokenScriptRefUtxo =
|
|
49
|
-
await StakingContract.stakingTokenScriptRef(
|
|
50
|
-
params.scriptReferences,
|
|
51
|
-
lucid,
|
|
52
|
-
);
|
|
53
|
-
|
|
54
|
-
const newStakingManagerDatum: StakingManagerContent = {
|
|
55
|
-
totalStake: stakingManagerOut.datum.totalStake + amount,
|
|
56
|
-
managerSnapshot: {
|
|
57
|
-
snapshotAda: stakingManagerOut.datum.managerSnapshot.snapshotAda,
|
|
58
|
-
},
|
|
59
|
-
};
|
|
60
|
-
|
|
61
|
-
const stakingPositionDatum: StakingPositionContent = {
|
|
62
|
-
owner: pkh.hash,
|
|
63
|
-
lockedAmount: new Map([]),
|
|
64
|
-
positionSnapshot: {
|
|
65
|
-
snapshotAda: stakingManagerOut.datum.managerSnapshot.snapshotAda,
|
|
66
|
-
},
|
|
67
|
-
};
|
|
68
|
-
|
|
69
|
-
const stakingToken =
|
|
70
|
-
params.stakingParams.stakingToken[0].unCurrencySymbol +
|
|
71
|
-
fromText(params.stakingParams.stakingToken[1].unTokenName);
|
|
72
|
-
const indyToken =
|
|
73
|
-
params.stakingParams.indyToken[0].unCurrencySymbol +
|
|
74
|
-
fromText(params.stakingParams.indyToken[1].unTokenName);
|
|
75
|
-
return lucid
|
|
76
|
-
.newTx()
|
|
77
|
-
.collectFrom([stakingManagerOut.utxo], Data.to(new Constr(0, [pkh.hash])))
|
|
78
|
-
.readFrom([stakingScriptRef])
|
|
79
|
-
.pay.ToContract(
|
|
80
|
-
stakingManagerOut.utxo.address,
|
|
81
|
-
{
|
|
82
|
-
kind: 'inline',
|
|
83
|
-
value: serialiseStakingDatum({
|
|
84
|
-
StakingManager: { content: newStakingManagerDatum },
|
|
85
|
-
}),
|
|
86
|
-
},
|
|
87
|
-
stakingManagerOut.utxo.assets,
|
|
88
|
-
)
|
|
89
|
-
.readFrom([stakingTokenScriptRefUtxo])
|
|
90
|
-
.mintAssets(
|
|
91
|
-
{
|
|
92
|
-
[stakingToken]: 1n,
|
|
93
|
-
},
|
|
94
|
-
Data.void(),
|
|
95
|
-
)
|
|
96
|
-
.pay.ToContract(
|
|
97
|
-
StakingContract.address(params.stakingParams, lucid),
|
|
98
|
-
{
|
|
99
|
-
kind: 'inline',
|
|
100
|
-
value: serialiseStakingDatum({
|
|
101
|
-
StakingPosition: { content: stakingPositionDatum },
|
|
102
|
-
}),
|
|
103
|
-
},
|
|
104
|
-
{
|
|
105
|
-
[stakingToken]: 1n,
|
|
106
|
-
[indyToken]: amount,
|
|
107
|
-
},
|
|
108
|
-
)
|
|
109
|
-
.addSignerKey(pkh.hash);
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
static async adjustPosition(
|
|
113
|
-
stakingPositionRef: OutRef,
|
|
114
|
-
amount: bigint,
|
|
115
|
-
params: SystemParams,
|
|
116
|
-
lucid: LucidEvolution,
|
|
117
|
-
stakingManagerRef?: OutRef,
|
|
118
|
-
): Promise<TxBuilder> {
|
|
119
|
-
const [pkh, _] = await addrDetails(lucid);
|
|
120
|
-
const now = Date.now();
|
|
121
|
-
|
|
122
|
-
const stakingPositionOut = await StakingHelpers.findStakingPositionByOutRef(
|
|
123
|
-
stakingPositionRef,
|
|
124
|
-
lucid,
|
|
125
|
-
);
|
|
126
|
-
const stakingManagerOut = stakingManagerRef
|
|
127
|
-
? await StakingHelpers.findStakingManagerByOutRef(
|
|
128
|
-
stakingManagerRef,
|
|
129
|
-
lucid,
|
|
130
|
-
)
|
|
131
|
-
: await StakingHelpers.findStakingManager(params, lucid);
|
|
132
|
-
|
|
133
|
-
const stakingScriptRef = await StakingContract.scriptRef(
|
|
134
|
-
params.scriptReferences,
|
|
135
|
-
lucid,
|
|
136
|
-
);
|
|
137
|
-
|
|
138
|
-
const stakingToken =
|
|
139
|
-
params.stakingParams.stakingToken[0].unCurrencySymbol +
|
|
140
|
-
fromText(params.stakingParams.stakingToken[1].unTokenName);
|
|
141
|
-
const stakingManagerToken =
|
|
142
|
-
params.stakingParams.stakingManagerNFT[0].unCurrencySymbol +
|
|
143
|
-
fromText(params.stakingParams.stakingManagerNFT[1].unTokenName);
|
|
144
|
-
const indyToken =
|
|
145
|
-
params.stakingParams.indyToken[0].unCurrencySymbol +
|
|
146
|
-
fromText(params.stakingParams.indyToken[1].unTokenName);
|
|
147
|
-
|
|
148
|
-
const existingIndyAmount = stakingPositionOut.utxo.assets[indyToken] ?? 0n;
|
|
149
|
-
const currentSnapshotAda =
|
|
150
|
-
stakingManagerOut.datum.managerSnapshot.snapshotAda;
|
|
151
|
-
const oldSnapshotAda =
|
|
152
|
-
stakingPositionOut.datum.positionSnapshot.snapshotAda;
|
|
153
|
-
const adaReward =
|
|
154
|
-
((currentSnapshotAda - oldSnapshotAda) * existingIndyAmount) /
|
|
155
|
-
(1000000n * 1000000n);
|
|
156
|
-
|
|
157
|
-
const newLockedAmount = new Map<bigint, [bigint, bigint]>();
|
|
158
|
-
for (const [key, value] of stakingPositionOut.datum.lockedAmount) {
|
|
159
|
-
if (value[1] > now) {
|
|
160
|
-
newLockedAmount.set(key, [value[0], value[1]]);
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
return lucid
|
|
164
|
-
.newTx()
|
|
165
|
-
.validFrom(Date.now())
|
|
166
|
-
.readFrom([stakingScriptRef])
|
|
167
|
-
.collectFrom([stakingPositionOut.utxo], Data.to(new Constr(3, [amount])))
|
|
168
|
-
.collectFrom([stakingManagerOut.utxo], Data.to(new Constr(1, [])))
|
|
169
|
-
.pay.ToContract(
|
|
170
|
-
stakingManagerOut.utxo.address,
|
|
171
|
-
{
|
|
172
|
-
kind: 'inline',
|
|
173
|
-
value: serialiseStakingDatum({
|
|
174
|
-
StakingManager: {
|
|
175
|
-
content: {
|
|
176
|
-
...stakingManagerOut.datum,
|
|
177
|
-
totalStake: stakingManagerOut.datum.totalStake + amount,
|
|
178
|
-
},
|
|
179
|
-
},
|
|
180
|
-
}),
|
|
181
|
-
},
|
|
182
|
-
{
|
|
183
|
-
lovelace: stakingManagerOut.utxo.assets.lovelace - adaReward,
|
|
184
|
-
[stakingManagerToken]: 1n,
|
|
185
|
-
},
|
|
186
|
-
)
|
|
187
|
-
.pay.ToContract(
|
|
188
|
-
stakingPositionOut.utxo.address,
|
|
189
|
-
{
|
|
190
|
-
kind: 'inline',
|
|
191
|
-
value: serialiseStakingDatum({
|
|
192
|
-
StakingPosition: {
|
|
193
|
-
content: {
|
|
194
|
-
...stakingPositionOut.datum,
|
|
195
|
-
lockedAmount: newLockedAmount,
|
|
196
|
-
},
|
|
197
|
-
},
|
|
198
|
-
}),
|
|
199
|
-
},
|
|
200
|
-
{
|
|
201
|
-
[stakingToken]: 1n,
|
|
202
|
-
[indyToken]: stakingPositionOut.utxo.assets[indyToken] + amount,
|
|
203
|
-
},
|
|
204
|
-
)
|
|
205
|
-
.addSignerKey(pkh.hash);
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
static async closePosition(
|
|
209
|
-
stakingPositionRef: OutRef,
|
|
210
|
-
params: SystemParams,
|
|
211
|
-
lucid: LucidEvolution,
|
|
212
|
-
stakingManagerRef?: OutRef,
|
|
213
|
-
): Promise<TxBuilder> {
|
|
214
|
-
const [pkh, _] = await addrDetails(lucid);
|
|
215
|
-
|
|
216
|
-
const stakingPositionOut = await StakingHelpers.findStakingPositionByOutRef(
|
|
217
|
-
stakingPositionRef,
|
|
218
|
-
lucid,
|
|
219
|
-
);
|
|
220
|
-
const stakingManagerOut = stakingManagerRef
|
|
221
|
-
? await StakingHelpers.findStakingManagerByOutRef(
|
|
222
|
-
stakingManagerRef,
|
|
223
|
-
lucid,
|
|
224
|
-
)
|
|
225
|
-
: await StakingHelpers.findStakingManager(params, lucid);
|
|
226
|
-
|
|
227
|
-
const stakingScriptRef = await StakingContract.scriptRef(
|
|
228
|
-
params.scriptReferences,
|
|
229
|
-
lucid,
|
|
230
|
-
);
|
|
231
|
-
const stakingTokenScriptRefUtxo =
|
|
232
|
-
await StakingContract.stakingTokenScriptRef(
|
|
233
|
-
params.scriptReferences,
|
|
234
|
-
lucid,
|
|
235
|
-
);
|
|
236
|
-
|
|
237
|
-
const stakingToken =
|
|
238
|
-
params.stakingParams.stakingToken[0].unCurrencySymbol +
|
|
239
|
-
fromText(params.stakingParams.stakingToken[1].unTokenName);
|
|
240
|
-
const stakingManagerToken =
|
|
241
|
-
params.stakingParams.stakingManagerNFT[0].unCurrencySymbol +
|
|
242
|
-
fromText(params.stakingParams.stakingManagerNFT[1].unTokenName);
|
|
243
|
-
const indyToken =
|
|
244
|
-
params.stakingParams.indyToken[0].unCurrencySymbol +
|
|
245
|
-
fromText(params.stakingParams.indyToken[1].unTokenName);
|
|
246
|
-
|
|
247
|
-
const existingIndyAmount = stakingPositionOut.utxo.assets[indyToken] ?? 0n;
|
|
248
|
-
const currentSnapshotAda =
|
|
249
|
-
stakingManagerOut.datum.managerSnapshot.snapshotAda;
|
|
250
|
-
const oldSnapshotAda =
|
|
251
|
-
stakingPositionOut.datum.positionSnapshot.snapshotAda;
|
|
252
|
-
const adaReward =
|
|
253
|
-
((currentSnapshotAda - oldSnapshotAda) * existingIndyAmount) /
|
|
254
|
-
(1000000n * 1000000n);
|
|
255
|
-
|
|
256
|
-
return lucid
|
|
257
|
-
.newTx()
|
|
258
|
-
.validFrom(Date.now())
|
|
259
|
-
.readFrom([stakingScriptRef])
|
|
260
|
-
.readFrom([stakingTokenScriptRefUtxo])
|
|
261
|
-
.collectFrom([stakingPositionOut.utxo], Data.to(new Constr(4, [])))
|
|
262
|
-
.collectFrom([stakingManagerOut.utxo], Data.to(new Constr(1, [])))
|
|
263
|
-
.pay.ToContract(
|
|
264
|
-
stakingManagerOut.utxo.address,
|
|
265
|
-
{
|
|
266
|
-
kind: 'inline',
|
|
267
|
-
value: serialiseStakingDatum({
|
|
268
|
-
StakingManager: {
|
|
269
|
-
content: {
|
|
270
|
-
...stakingManagerOut.datum,
|
|
271
|
-
totalStake:
|
|
272
|
-
stakingManagerOut.datum.totalStake - existingIndyAmount,
|
|
273
|
-
},
|
|
274
|
-
},
|
|
275
|
-
}),
|
|
276
|
-
},
|
|
277
|
-
{
|
|
278
|
-
lovelace: stakingManagerOut.utxo.assets.lovelace - adaReward,
|
|
279
|
-
[stakingManagerToken]: 1n,
|
|
280
|
-
},
|
|
281
|
-
)
|
|
282
|
-
.mintAssets(
|
|
283
|
-
{
|
|
284
|
-
[stakingToken]: -1n,
|
|
285
|
-
},
|
|
286
|
-
Data.void(),
|
|
287
|
-
)
|
|
288
|
-
.addSignerKey(pkh.hash);
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
// Staking Validator
|
|
292
|
-
static validator(params: StakingParams): SpendingValidator {
|
|
293
|
-
return {
|
|
294
|
-
type: 'PlutusV2',
|
|
295
|
-
script: applyParamsToScript(_stakingValidator.cborHex, [
|
|
296
|
-
new Constr(0, [
|
|
297
|
-
new Constr(0, [
|
|
298
|
-
params.stakingManagerNFT[0].unCurrencySymbol,
|
|
299
|
-
fromText(params.stakingManagerNFT[1].unTokenName),
|
|
300
|
-
]),
|
|
301
|
-
new Constr(0, [
|
|
302
|
-
params.stakingToken[0].unCurrencySymbol,
|
|
303
|
-
fromText(params.stakingToken[1].unTokenName),
|
|
304
|
-
]),
|
|
305
|
-
new Constr(0, [
|
|
306
|
-
params.indyToken[0].unCurrencySymbol,
|
|
307
|
-
fromText(params.indyToken[1].unTokenName),
|
|
308
|
-
]),
|
|
309
|
-
new Constr(0, [
|
|
310
|
-
params.pollToken[0].unCurrencySymbol,
|
|
311
|
-
fromText(params.pollToken[1].unTokenName),
|
|
312
|
-
]),
|
|
313
|
-
new Constr(0, [
|
|
314
|
-
params.versionRecordToken[0].unCurrencySymbol,
|
|
315
|
-
fromText(params.versionRecordToken[1].unTokenName),
|
|
316
|
-
]),
|
|
317
|
-
params.collectorValHash,
|
|
318
|
-
]),
|
|
319
|
-
]),
|
|
320
|
-
};
|
|
321
|
-
}
|
|
322
|
-
|
|
323
|
-
static validatorHash(params: StakingParams): string {
|
|
324
|
-
return validatorToScriptHash(StakingContract.validator(params));
|
|
325
|
-
}
|
|
326
|
-
|
|
327
|
-
static address(params: StakingParams, lucid: LucidEvolution): Address {
|
|
328
|
-
const network = lucid.config().network;
|
|
329
|
-
if (!network) {
|
|
330
|
-
throw new Error('Network configuration is undefined');
|
|
331
|
-
}
|
|
332
|
-
return validatorToAddress(network, StakingContract.validator(params));
|
|
333
|
-
}
|
|
334
|
-
|
|
335
|
-
static async scriptRef(
|
|
336
|
-
params: ScriptReferences,
|
|
337
|
-
lucid: LucidEvolution,
|
|
338
|
-
): Promise<UTxO> {
|
|
339
|
-
return scriptRef(params.stakingValidatorRef, lucid);
|
|
340
|
-
}
|
|
341
|
-
|
|
342
|
-
static async stakingTokenScriptRef(
|
|
343
|
-
params: ScriptReferences,
|
|
344
|
-
lucid: LucidEvolution,
|
|
345
|
-
): Promise<UTxO> {
|
|
346
|
-
return scriptRef(params.authTokenPolicies.stakingTokenRef, lucid);
|
|
347
|
-
}
|
|
348
|
-
}
|
|
@@ -1,112 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
Address,
|
|
3
|
-
applyParamsToScript,
|
|
4
|
-
Constr,
|
|
5
|
-
Data,
|
|
6
|
-
fromText,
|
|
7
|
-
LucidEvolution,
|
|
8
|
-
OutRef,
|
|
9
|
-
SpendingValidator,
|
|
10
|
-
TxBuilder,
|
|
11
|
-
UTxO,
|
|
12
|
-
validatorToAddress,
|
|
13
|
-
validatorToScriptHash,
|
|
14
|
-
} from '@lucid-evolution/lucid';
|
|
15
|
-
import { _treasuryValidator } from '../scripts/treasury-validator';
|
|
16
|
-
import {
|
|
17
|
-
ScriptReferences,
|
|
18
|
-
SystemParams,
|
|
19
|
-
TreasuryParams,
|
|
20
|
-
} from '../types/system-params';
|
|
21
|
-
import { scriptRef } from '../helpers/lucid-utils';
|
|
22
|
-
import { getRandomElement } from '../helpers/helpers';
|
|
23
|
-
|
|
24
|
-
export class TreasuryContract {
|
|
25
|
-
static async feeTx(
|
|
26
|
-
fee: bigint,
|
|
27
|
-
lucid: LucidEvolution,
|
|
28
|
-
params: SystemParams,
|
|
29
|
-
tx: TxBuilder,
|
|
30
|
-
treasuryRef?: OutRef,
|
|
31
|
-
): Promise<void> {
|
|
32
|
-
const treasuryUtxo: UTxO = treasuryRef
|
|
33
|
-
? getRandomElement(await lucid.utxosByOutRef([treasuryRef]))
|
|
34
|
-
: getRandomElement(
|
|
35
|
-
await lucid.utxosAt(
|
|
36
|
-
TreasuryContract.address(params.treasuryParams, lucid),
|
|
37
|
-
),
|
|
38
|
-
);
|
|
39
|
-
|
|
40
|
-
const treasuryScriptRefUtxo = await TreasuryContract.scriptRef(
|
|
41
|
-
params.scriptReferences,
|
|
42
|
-
lucid,
|
|
43
|
-
);
|
|
44
|
-
|
|
45
|
-
tx.collectFrom([treasuryUtxo], Data.to(new Constr(4, [])))
|
|
46
|
-
.pay.ToContract(
|
|
47
|
-
treasuryUtxo.address,
|
|
48
|
-
{ kind: 'inline', value: treasuryUtxo.datum || '' },
|
|
49
|
-
{
|
|
50
|
-
...treasuryUtxo.assets,
|
|
51
|
-
lovelace: treasuryUtxo.assets['lovelace'] + fee,
|
|
52
|
-
},
|
|
53
|
-
)
|
|
54
|
-
.readFrom([treasuryScriptRefUtxo]);
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
// treasury Validator
|
|
58
|
-
static validator(params: TreasuryParams): SpendingValidator {
|
|
59
|
-
return {
|
|
60
|
-
type: 'PlutusV2',
|
|
61
|
-
script: applyParamsToScript(_treasuryValidator.cborHex, [
|
|
62
|
-
new Constr(0, [
|
|
63
|
-
new Constr(0, [
|
|
64
|
-
params.upgradeToken[0].unCurrencySymbol,
|
|
65
|
-
fromText(params.upgradeToken[1].unTokenName),
|
|
66
|
-
]),
|
|
67
|
-
new Constr(0, [
|
|
68
|
-
params.versionRecordToken[0].unCurrencySymbol,
|
|
69
|
-
fromText(params.versionRecordToken[1].unTokenName),
|
|
70
|
-
]),
|
|
71
|
-
params.treasuryUtxosStakeCredential
|
|
72
|
-
? new Constr(0, [
|
|
73
|
-
new Constr(0, [
|
|
74
|
-
new Constr(1, [
|
|
75
|
-
params.treasuryUtxosStakeCredential.contents.contents,
|
|
76
|
-
]),
|
|
77
|
-
]),
|
|
78
|
-
])
|
|
79
|
-
: new Constr(1, []),
|
|
80
|
-
]),
|
|
81
|
-
]),
|
|
82
|
-
};
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
static validatorHash(params: TreasuryParams): string {
|
|
86
|
-
return validatorToScriptHash(TreasuryContract.validator(params));
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
static address(params: TreasuryParams, lucid: LucidEvolution): Address {
|
|
90
|
-
const network = lucid.config().network;
|
|
91
|
-
if (!network) {
|
|
92
|
-
throw new Error('Network configuration is undefined');
|
|
93
|
-
}
|
|
94
|
-
return validatorToAddress(
|
|
95
|
-
network,
|
|
96
|
-
TreasuryContract.validator(params),
|
|
97
|
-
params.treasuryUtxosStakeCredential
|
|
98
|
-
? {
|
|
99
|
-
type: 'Script',
|
|
100
|
-
hash: params.treasuryUtxosStakeCredential.contents.contents,
|
|
101
|
-
}
|
|
102
|
-
: undefined,
|
|
103
|
-
);
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
static async scriptRef(
|
|
107
|
-
params: ScriptReferences,
|
|
108
|
-
lucid: LucidEvolution,
|
|
109
|
-
): Promise<UTxO> {
|
|
110
|
-
return scriptRef(params.treasuryValidatorRef, lucid);
|
|
111
|
-
}
|
|
112
|
-
}
|
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
import { fromText, LucidEvolution, OutRef, UTxO } from '@lucid-evolution/lucid';
|
|
2
|
-
import { CDPContract } from '../contracts/cdp';
|
|
3
|
-
import { SystemParams } from '../types/system-params';
|
|
4
|
-
import { IAssetContent, parseIAssetDatum } from '../types/indigo/cdp';
|
|
5
|
-
|
|
6
|
-
export type IAssetOutput = { utxo: UTxO; datum: IAssetContent };
|
|
7
|
-
|
|
8
|
-
export class IAssetHelpers {
|
|
9
|
-
static async findIAssetByRef(
|
|
10
|
-
outRef: OutRef,
|
|
11
|
-
lucid: LucidEvolution,
|
|
12
|
-
): Promise<IAssetOutput> {
|
|
13
|
-
return lucid
|
|
14
|
-
.utxosByOutRef([outRef])
|
|
15
|
-
.then((utxos) =>
|
|
16
|
-
utxos
|
|
17
|
-
.map((utxo) => {
|
|
18
|
-
if (!utxo.datum) return undefined;
|
|
19
|
-
const datum = parseIAssetDatum(utxo.datum);
|
|
20
|
-
return { utxo, datum };
|
|
21
|
-
})
|
|
22
|
-
.find((utxo) => utxo !== undefined),
|
|
23
|
-
)
|
|
24
|
-
.then((result) => {
|
|
25
|
-
if (!result)
|
|
26
|
-
throw new Error('Unable to locate IAsset by output reference.');
|
|
27
|
-
return result;
|
|
28
|
-
});
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
static async findIAssetByName(
|
|
32
|
-
assetName: string,
|
|
33
|
-
params: SystemParams,
|
|
34
|
-
lucid: LucidEvolution,
|
|
35
|
-
): Promise<IAssetOutput> {
|
|
36
|
-
return lucid
|
|
37
|
-
.utxosAtWithUnit(
|
|
38
|
-
CDPContract.address(params.cdpParams, lucid),
|
|
39
|
-
params.cdpParams.iAssetAuthToken[0].unCurrencySymbol +
|
|
40
|
-
fromText(params.cdpParams.iAssetAuthToken[1].unTokenName),
|
|
41
|
-
)
|
|
42
|
-
.then((utxos) =>
|
|
43
|
-
utxos
|
|
44
|
-
.map((utxo) => {
|
|
45
|
-
if (!utxo.datum) return undefined;
|
|
46
|
-
const datum = parseIAssetDatum(utxo.datum);
|
|
47
|
-
if (datum.assetName !== fromText(assetName)) return undefined;
|
|
48
|
-
return { utxo, datum };
|
|
49
|
-
})
|
|
50
|
-
.find((utxo) => utxo !== undefined),
|
|
51
|
-
)
|
|
52
|
-
.then((result) => {
|
|
53
|
-
if (!result) throw new Error('Unable to locate IAsset by name.');
|
|
54
|
-
return result;
|
|
55
|
-
});
|
|
56
|
-
}
|
|
57
|
-
}
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
LucidEvolution,
|
|
3
|
-
Network,
|
|
4
|
-
OutRef,
|
|
5
|
-
SpendingValidator,
|
|
6
|
-
validatorToAddress,
|
|
7
|
-
} from '@lucid-evolution/lucid';
|
|
8
|
-
import { alwaysFailValidator } from '../scripts/always-fail-validator';
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* Uses an always fail validator for the destination address.
|
|
12
|
-
*/
|
|
13
|
-
export async function runCreateScriptRefTx(
|
|
14
|
-
lucid: LucidEvolution,
|
|
15
|
-
scriptRefValidator: SpendingValidator,
|
|
16
|
-
network: Network,
|
|
17
|
-
): Promise<OutRef> {
|
|
18
|
-
const scriptAddr = validatorToAddress(network, alwaysFailValidator);
|
|
19
|
-
|
|
20
|
-
const txHash = await lucid
|
|
21
|
-
.newTx()
|
|
22
|
-
.pay.ToAddressWithData(scriptAddr, undefined, {}, scriptRefValidator)
|
|
23
|
-
.complete()
|
|
24
|
-
.then((tx) => tx.sign.withWallet().complete())
|
|
25
|
-
.then((tx) => tx.submit());
|
|
26
|
-
|
|
27
|
-
await lucid.awaitTx(txHash);
|
|
28
|
-
|
|
29
|
-
return { txHash: txHash, outputIndex: 0 };
|
|
30
|
-
}
|
package/src/helpers/helpers.ts
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import * as fs from 'fs';
|
|
2
|
-
import { SystemParams } from '../types/system-params';
|
|
3
|
-
import { match, P } from 'ts-pattern';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Accept only a single item in the array and return it.
|
|
7
|
-
* When not exclusively a single item, throw an error.
|
|
8
|
-
*/
|
|
9
|
-
export function matchSingle<T>(xs: T[], mkErr: (xs: T[]) => Error): T {
|
|
10
|
-
return match(xs)
|
|
11
|
-
.with([P.select()], (res) => res as T)
|
|
12
|
-
.otherwise(() => {
|
|
13
|
-
throw mkErr(xs);
|
|
14
|
-
});
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export function calculateFeeFromPercentage(
|
|
18
|
-
percent: bigint,
|
|
19
|
-
amount: bigint,
|
|
20
|
-
): bigint {
|
|
21
|
-
if (amount <= 0n) return 0n;
|
|
22
|
-
const a = (amount * percent) / 1_000_000n;
|
|
23
|
-
const b = BigInt(100_000_000);
|
|
24
|
-
return (a * 1_000_000n) / b;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export function loadSystemParamsFromFile(file: string): SystemParams {
|
|
28
|
-
return JSON.parse(fs.readFileSync(file, 'utf-8')) as SystemParams;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export function loadSystemParamsFromUrl(url: string): Promise<SystemParams> {
|
|
32
|
-
return fetch(url)
|
|
33
|
-
.then((res: Response) => res.json())
|
|
34
|
-
.then((data: unknown) => data as SystemParams);
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
export const getRandomElement = <T>(arr: T[]) =>
|
|
38
|
-
arr.length ? arr[Math.floor(Math.random() * arr.length)] : undefined;
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
OCD_DECIMAL_UNIT,
|
|
3
|
-
ocdDiv,
|
|
4
|
-
ocdMul,
|
|
5
|
-
OnChainDecimal,
|
|
6
|
-
} from '../types/on-chain-decimal';
|
|
7
|
-
|
|
8
|
-
export function calculateFeeFromPercentage(
|
|
9
|
-
feePercentage: OnChainDecimal,
|
|
10
|
-
amount: bigint,
|
|
11
|
-
): bigint {
|
|
12
|
-
if (amount < 0) {
|
|
13
|
-
return 0n;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
return ocdDiv(ocdMul({ getOnChainInt: amount }, feePercentage), {
|
|
17
|
-
getOnChainInt: 100n * OCD_DECIMAL_UNIT,
|
|
18
|
-
}).getOnChainInt;
|
|
19
|
-
}
|
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
import { InterestOracleDatum } from '../types/indigo/interest-oracle';
|
|
2
|
-
import { oneYear } from '../helpers/time-helpers';
|
|
3
|
-
|
|
4
|
-
const unitaryInterestPrecision = 1_000_000_000_000_000_000n;
|
|
5
|
-
const decimalUnit = 1_000_000n;
|
|
6
|
-
|
|
7
|
-
export function calculateUnitaryInterest(
|
|
8
|
-
timePeriod: bigint,
|
|
9
|
-
interestRate: bigint,
|
|
10
|
-
): bigint {
|
|
11
|
-
return (
|
|
12
|
-
(timePeriod * interestRate * unitaryInterestPrecision) /
|
|
13
|
-
oneYear /
|
|
14
|
-
decimalUnit
|
|
15
|
-
);
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export function calculateUnitaryInterestSinceOracleLastUpdated(
|
|
19
|
-
now: bigint,
|
|
20
|
-
oracleDatum: InterestOracleDatum,
|
|
21
|
-
): bigint {
|
|
22
|
-
return calculateUnitaryInterest(
|
|
23
|
-
now - oracleDatum.lastUpdated,
|
|
24
|
-
oracleDatum.interestRate.getOnChainInt,
|
|
25
|
-
);
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export function calculateAccruedInterest(
|
|
29
|
-
now: bigint,
|
|
30
|
-
unitaryInterestSnapshot: bigint,
|
|
31
|
-
mintedAmount: bigint,
|
|
32
|
-
interestLastSettled: bigint,
|
|
33
|
-
interestOracleDatum: InterestOracleDatum,
|
|
34
|
-
): bigint {
|
|
35
|
-
if (interestOracleDatum.unitaryInterest >= unitaryInterestSnapshot) {
|
|
36
|
-
const interestFromPreviousRates =
|
|
37
|
-
((interestOracleDatum.unitaryInterest - unitaryInterestSnapshot) *
|
|
38
|
-
mintedAmount) /
|
|
39
|
-
unitaryInterestPrecision;
|
|
40
|
-
const lastRateInterest =
|
|
41
|
-
((now - interestOracleDatum.lastUpdated) *
|
|
42
|
-
interestOracleDatum.interestRate.getOnChainInt *
|
|
43
|
-
mintedAmount) /
|
|
44
|
-
oneYear /
|
|
45
|
-
decimalUnit;
|
|
46
|
-
|
|
47
|
-
return interestFromPreviousRates + lastRateInterest;
|
|
48
|
-
} else {
|
|
49
|
-
return (
|
|
50
|
-
((now - interestLastSettled) *
|
|
51
|
-
interestOracleDatum.interestRate.getOnChainInt *
|
|
52
|
-
mintedAmount) /
|
|
53
|
-
oneYear /
|
|
54
|
-
decimalUnit
|
|
55
|
-
);
|
|
56
|
-
}
|
|
57
|
-
}
|