@indigo-labs/indigo-sdk 0.5.0 → 0.5.2
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/.prettierignore +1 -0
- package/dist/index.d.mts +33 -14
- package/dist/index.d.ts +33 -14
- package/dist/index.js +80 -35
- package/dist/index.mjs +96 -43
- package/package.json +2 -2
- package/scripts/run-repeat.sh +22 -0
- package/src/contracts/stableswap/helpers.ts +139 -22
- package/src/contracts/stableswap/transactions.ts +15 -36
- package/tests/cdp/cdp.test.ts +1 -2
- package/tests/indigo-test-helpers.ts +2 -2
- package/tests/stableswap/stableswap-queries.ts +26 -17
- package/tests/stableswap/stableswap.test.ts +120 -8
|
@@ -16,7 +16,6 @@ import {
|
|
|
16
16
|
fromSystemParamsScriptRef,
|
|
17
17
|
SystemParams,
|
|
18
18
|
} from '../../types/system-params';
|
|
19
|
-
import { estimateUtxoMinLovelace } from '../../utils/lucid-utils';
|
|
20
19
|
import {
|
|
21
20
|
parseStableswapOrderDatumOrThrow,
|
|
22
21
|
serialiseStableswapOrderDatum,
|
|
@@ -46,8 +45,8 @@ import { isEmpty } from 'fp-ts/lib/Array';
|
|
|
46
45
|
import {
|
|
47
46
|
BASE_MAX_EXECUTION_FEE,
|
|
48
47
|
createDestinationDatum,
|
|
49
|
-
|
|
50
|
-
|
|
48
|
+
estimateMinOrderOutputLovelaces,
|
|
49
|
+
PSMProcessingConfig,
|
|
51
50
|
StableswapInfo,
|
|
52
51
|
StableswapOrderInfo,
|
|
53
52
|
summariseOrder,
|
|
@@ -98,23 +97,17 @@ export async function createStableswapOrder(
|
|
|
98
97
|
amount,
|
|
99
98
|
);
|
|
100
99
|
|
|
101
|
-
|
|
102
|
-
const expectedOutputLovelaces = estimateUtxoMinLovelace(
|
|
103
|
-
lucid.config().protocolParameters!,
|
|
104
|
-
myAddress,
|
|
100
|
+
const orderAssetsPlusFees = addAssets(
|
|
105
101
|
assetsToSwap,
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
value: createDestinationDatum(destinationInlineDatum ?? null, {
|
|
109
|
-
txHash:
|
|
110
|
-
'0000000000000000000000000000000000000000000000000000000000000000',
|
|
111
|
-
outputIndex: 0,
|
|
112
|
-
}),
|
|
113
|
-
},
|
|
102
|
+
mkLovelacesOf(maxExecutionFee),
|
|
103
|
+
mkLovelacesOf(additionalLovelaces),
|
|
114
104
|
);
|
|
115
105
|
|
|
116
|
-
const
|
|
117
|
-
|
|
106
|
+
const expectedMinOutputLovelaces = estimateMinOrderOutputLovelaces(
|
|
107
|
+
lucid,
|
|
108
|
+
datum,
|
|
109
|
+
orderAssetsPlusFees,
|
|
110
|
+
);
|
|
118
111
|
|
|
119
112
|
return lucid.newTx().pay.ToContract(
|
|
120
113
|
credentialToAddress(lucid.config().network!, {
|
|
@@ -125,12 +118,7 @@ export async function createStableswapOrder(
|
|
|
125
118
|
kind: 'inline',
|
|
126
119
|
value: serialiseStableswapOrderDatum(datum),
|
|
127
120
|
},
|
|
128
|
-
addAssets(
|
|
129
|
-
assetsToSwap,
|
|
130
|
-
mkLovelacesOf(
|
|
131
|
-
roundedExpectedOutputLovelaces + maxExecutionFee + additionalLovelaces,
|
|
132
|
-
),
|
|
133
|
-
),
|
|
121
|
+
addAssets(orderAssetsPlusFees, mkLovelacesOf(expectedMinOutputLovelaces)),
|
|
134
122
|
);
|
|
135
123
|
}
|
|
136
124
|
|
|
@@ -173,7 +161,7 @@ export async function batchProcessStableswapOrders(
|
|
|
173
161
|
treasuryOref: OutRef,
|
|
174
162
|
sysParams: SystemParams,
|
|
175
163
|
lucid: LucidEvolution,
|
|
176
|
-
|
|
164
|
+
psmProcessingConfig?: PSMProcessingConfig,
|
|
177
165
|
): Promise<TxBuilder> {
|
|
178
166
|
const stableswapScriptRefUtxo = matchSingle(
|
|
179
167
|
await lucid.utxosByOutRef([
|
|
@@ -245,25 +233,16 @@ export async function batchProcessStableswapOrders(
|
|
|
245
233
|
getInlineDatumOrThrow(orderUtxo),
|
|
246
234
|
);
|
|
247
235
|
|
|
248
|
-
if (
|
|
249
|
-
toHex(orderDatum.iasset) != toHex(mainOrderDatum.iasset) ||
|
|
250
|
-
toHex(orderDatum.collateralAsset.currencySymbol) !=
|
|
251
|
-
toHex(mainOrderDatum.collateralAsset.currencySymbol) ||
|
|
252
|
-
toHex(orderDatum.collateralAsset.tokenName) !=
|
|
253
|
-
toHex(mainOrderDatum.collateralAsset.tokenName)
|
|
254
|
-
) {
|
|
255
|
-
throw new Error('Wrong batch of orders');
|
|
256
|
-
}
|
|
257
|
-
|
|
258
236
|
const res = summariseOrder(
|
|
237
|
+
lucid,
|
|
259
238
|
{ utxo: orderUtxo, datum: orderDatum },
|
|
260
239
|
{ utxo: stableswapPoolUtxo, datum: stableswapPoolDatum },
|
|
261
240
|
sysParams,
|
|
262
|
-
|
|
241
|
+
psmProcessingConfig,
|
|
263
242
|
);
|
|
264
243
|
|
|
265
244
|
if (res._tag === 'ERROR') {
|
|
266
|
-
throw new Error(res.
|
|
245
|
+
throw new Error(res.description);
|
|
267
246
|
}
|
|
268
247
|
|
|
269
248
|
return res.result;
|
package/tests/cdp/cdp.test.ts
CHANGED
|
@@ -148,8 +148,7 @@ describe('CDP', () => {
|
|
|
148
148
|
context.lucid = await Lucid(context.emulator, 'Custom');
|
|
149
149
|
});
|
|
150
150
|
|
|
151
|
-
|
|
152
|
-
test.only<MyContext>('Open CDP; ADA collateral', async (context: MyContext) => {
|
|
151
|
+
test<MyContext>('Open CDP; ADA collateral', async (context: MyContext) => {
|
|
153
152
|
context.lucid.selectWallet.fromSeed(context.users.admin.seedPhrase);
|
|
154
153
|
|
|
155
154
|
const [sysParams, [iusdAssetInfo]] = await init(context.lucid, [
|
|
@@ -17,7 +17,7 @@ import {
|
|
|
17
17
|
} from '@3rd-eye-labs/cardano-offchain-common';
|
|
18
18
|
import { IndigoTestContext } from './test-helpers';
|
|
19
19
|
import { init } from './endpoints/initialize';
|
|
20
|
-
import { iusdInitialAssetCfg } from './mock/assets-mock';
|
|
20
|
+
import { ieurInitialAssetCfg, iusdInitialAssetCfg } from './mock/assets-mock';
|
|
21
21
|
|
|
22
22
|
export const MAINNET_PROTOCOL_PARAMETERS: ProtocolParameters = {
|
|
23
23
|
...PROTOCOL_PARAMETERS_DEFAULT,
|
|
@@ -81,7 +81,7 @@ export async function createIndigoTestContext(
|
|
|
81
81
|
|
|
82
82
|
const [systemParams, assetConfigs] = await init(
|
|
83
83
|
context.lucid,
|
|
84
|
-
[iusdInitialAssetCfg()],
|
|
84
|
+
[iusdInitialAssetCfg(), ieurInitialAssetCfg()],
|
|
85
85
|
() => [],
|
|
86
86
|
{
|
|
87
87
|
foundationMultisig: {
|
|
@@ -52,33 +52,21 @@ export async function findSingleStableswapOrder(
|
|
|
52
52
|
);
|
|
53
53
|
}
|
|
54
54
|
|
|
55
|
-
export async function
|
|
55
|
+
export async function findAllStableswapOrders(
|
|
56
56
|
lucid: LucidEvolution,
|
|
57
57
|
stableswapScriptHash: ScriptHash,
|
|
58
|
-
iassetName: string,
|
|
59
|
-
collateralAsset: AssetClass,
|
|
60
58
|
): Promise<{ utxo: UTxO; datum: StableswapOrderDatum }[]> {
|
|
61
|
-
const
|
|
59
|
+
const psmUtxos = await lucid.utxosAt(
|
|
62
60
|
createScriptAddress(lucid.config().network!, stableswapScriptHash),
|
|
63
61
|
);
|
|
64
62
|
|
|
65
63
|
const orders = F.pipe(
|
|
66
|
-
|
|
64
|
+
psmUtxos.map((utxo) =>
|
|
67
65
|
F.pipe(
|
|
68
66
|
O.fromNullable(utxo.datum),
|
|
69
67
|
O.flatMap(parseStableswapOrderDatum),
|
|
70
|
-
O.
|
|
71
|
-
|
|
72
|
-
toHex(datum.iasset) === iassetName &&
|
|
73
|
-
toHex(datum.collateralAsset.currencySymbol) ===
|
|
74
|
-
toHex(collateralAsset.currencySymbol) &&
|
|
75
|
-
toHex(datum.collateralAsset.tokenName) ===
|
|
76
|
-
toHex(collateralAsset.tokenName)
|
|
77
|
-
) {
|
|
78
|
-
return O.some({ utxo, datum: datum });
|
|
79
|
-
} else {
|
|
80
|
-
return O.none;
|
|
81
|
-
}
|
|
68
|
+
O.map((datum) => {
|
|
69
|
+
return { utxo, datum: datum };
|
|
82
70
|
}),
|
|
83
71
|
),
|
|
84
72
|
),
|
|
@@ -87,3 +75,24 @@ export async function findStableswapOrders(
|
|
|
87
75
|
|
|
88
76
|
return orders;
|
|
89
77
|
}
|
|
78
|
+
|
|
79
|
+
export async function findStableswapOrders(
|
|
80
|
+
lucid: LucidEvolution,
|
|
81
|
+
stableswapScriptHash: ScriptHash,
|
|
82
|
+
iassetName: string,
|
|
83
|
+
collateralAsset: AssetClass,
|
|
84
|
+
): Promise<{ utxo: UTxO; datum: StableswapOrderDatum }[]> {
|
|
85
|
+
const allPsmOrders = await findAllStableswapOrders(
|
|
86
|
+
lucid,
|
|
87
|
+
stableswapScriptHash,
|
|
88
|
+
);
|
|
89
|
+
|
|
90
|
+
return allPsmOrders.filter(
|
|
91
|
+
(order) =>
|
|
92
|
+
toHex(order.datum.iasset) === iassetName &&
|
|
93
|
+
toHex(order.datum.collateralAsset.currencySymbol) ===
|
|
94
|
+
toHex(collateralAsset.currencySymbol) &&
|
|
95
|
+
toHex(order.datum.collateralAsset.tokenName) ===
|
|
96
|
+
toHex(collateralAsset.tokenName),
|
|
97
|
+
);
|
|
98
|
+
}
|
|
@@ -8,6 +8,7 @@ import {
|
|
|
8
8
|
import {
|
|
9
9
|
createIndigoTestContext,
|
|
10
10
|
EXAMPLE_TOKEN_1,
|
|
11
|
+
EXAMPLE_TOKEN_2,
|
|
11
12
|
} from '../indigo-test-helpers';
|
|
12
13
|
import { benchmarkAndAwaitTx } from '../utils/benchmark-utils';
|
|
13
14
|
import {
|
|
@@ -36,6 +37,7 @@ import {
|
|
|
36
37
|
findStableswapPool,
|
|
37
38
|
} from '../cdp/cdp-queries';
|
|
38
39
|
import {
|
|
40
|
+
findAllStableswapOrders,
|
|
39
41
|
findSingleStableswapOrder,
|
|
40
42
|
findStableswapOrders,
|
|
41
43
|
} from './stableswap-queries';
|
|
@@ -422,10 +424,16 @@ test<IndigoTestContext>('Stableswap - pickValidSatisfiableOrders', async (contex
|
|
|
422
424
|
context.assetConfigs[0],
|
|
423
425
|
adaAssetClass,
|
|
424
426
|
);
|
|
427
|
+
await refreshPriceOracle(
|
|
428
|
+
context,
|
|
429
|
+
context.systemParams,
|
|
430
|
+
context.assetConfigs[1],
|
|
431
|
+
adaAssetClass,
|
|
432
|
+
);
|
|
425
433
|
|
|
426
434
|
context.lucid.selectWallet.fromSeed(context.users.user.seedPhrase);
|
|
427
435
|
|
|
428
|
-
// Add some
|
|
436
|
+
// Add some IUSD to wallet
|
|
429
437
|
await runAndAwaitTx(
|
|
430
438
|
context.lucid,
|
|
431
439
|
runOpenCdp(
|
|
@@ -438,6 +446,19 @@ test<IndigoTestContext>('Stableswap - pickValidSatisfiableOrders', async (contex
|
|
|
438
446
|
),
|
|
439
447
|
);
|
|
440
448
|
|
|
449
|
+
// iEUR
|
|
450
|
+
await runAndAwaitTx(
|
|
451
|
+
context.lucid,
|
|
452
|
+
runOpenCdp(
|
|
453
|
+
context,
|
|
454
|
+
context.systemParams,
|
|
455
|
+
context.assetConfigs[1].iassetTokenNameAscii,
|
|
456
|
+
adaAssetClass,
|
|
457
|
+
100_000_000n,
|
|
458
|
+
50_000_000n,
|
|
459
|
+
),
|
|
460
|
+
);
|
|
461
|
+
|
|
441
462
|
// mint order
|
|
442
463
|
await runAndAwaitTx(
|
|
443
464
|
context.lucid,
|
|
@@ -452,6 +473,50 @@ test<IndigoTestContext>('Stableswap - pickValidSatisfiableOrders', async (contex
|
|
|
452
473
|
),
|
|
453
474
|
);
|
|
454
475
|
|
|
476
|
+
// mint order, can't process because not enough lovelaces
|
|
477
|
+
const mintOrderMinLovelacesTx = await runAndAwaitTx(
|
|
478
|
+
context.lucid,
|
|
479
|
+
createStableswapOrder(
|
|
480
|
+
context.assetConfigs[0].iassetTokenNameAscii,
|
|
481
|
+
EXAMPLE_TOKEN_1,
|
|
482
|
+
10_000_000n,
|
|
483
|
+
true,
|
|
484
|
+
stableswapPool.datum,
|
|
485
|
+
context.systemParams,
|
|
486
|
+
context.lucid,
|
|
487
|
+
await context.lucid.wallet().address(),
|
|
488
|
+
undefined,
|
|
489
|
+
BASE_MAX_EXECUTION_FEE,
|
|
490
|
+
-200_000n,
|
|
491
|
+
),
|
|
492
|
+
);
|
|
493
|
+
|
|
494
|
+
const mintOtherCollateralTx = await runAndAwaitTx(
|
|
495
|
+
context.lucid,
|
|
496
|
+
createStableswapOrder(
|
|
497
|
+
context.assetConfigs[0].iassetTokenNameAscii,
|
|
498
|
+
EXAMPLE_TOKEN_2,
|
|
499
|
+
1_000_000n,
|
|
500
|
+
true,
|
|
501
|
+
stableswapPool.datum,
|
|
502
|
+
context.systemParams,
|
|
503
|
+
context.lucid,
|
|
504
|
+
),
|
|
505
|
+
);
|
|
506
|
+
|
|
507
|
+
const mintOtherIassetTx = await runAndAwaitTx(
|
|
508
|
+
context.lucid,
|
|
509
|
+
createStableswapOrder(
|
|
510
|
+
'IBTC',
|
|
511
|
+
EXAMPLE_TOKEN_1,
|
|
512
|
+
1_000_000n,
|
|
513
|
+
true,
|
|
514
|
+
stableswapPool.datum,
|
|
515
|
+
context.systemParams,
|
|
516
|
+
context.lucid,
|
|
517
|
+
),
|
|
518
|
+
);
|
|
519
|
+
|
|
455
520
|
// mint order, can't process because max exec fee is low
|
|
456
521
|
const mintOrderMaxExecFeeTx = await runAndAwaitTx(
|
|
457
522
|
context.lucid,
|
|
@@ -516,6 +581,33 @@ test<IndigoTestContext>('Stableswap - pickValidSatisfiableOrders', async (contex
|
|
|
516
581
|
),
|
|
517
582
|
);
|
|
518
583
|
|
|
584
|
+
// Redemption order
|
|
585
|
+
const redeemOtherCollateralTx = await runAndAwaitTx(
|
|
586
|
+
context.lucid,
|
|
587
|
+
createStableswapOrder(
|
|
588
|
+
context.assetConfigs[0].iassetTokenNameAscii,
|
|
589
|
+
EXAMPLE_TOKEN_2,
|
|
590
|
+
1_000_000n,
|
|
591
|
+
false,
|
|
592
|
+
stableswapPool.datum,
|
|
593
|
+
context.systemParams,
|
|
594
|
+
context.lucid,
|
|
595
|
+
),
|
|
596
|
+
);
|
|
597
|
+
|
|
598
|
+
const redeemOtherIassetTx = await runAndAwaitTx(
|
|
599
|
+
context.lucid,
|
|
600
|
+
createStableswapOrder(
|
|
601
|
+
context.assetConfigs[1].iassetTokenNameAscii,
|
|
602
|
+
EXAMPLE_TOKEN_1,
|
|
603
|
+
1_000_000n,
|
|
604
|
+
false,
|
|
605
|
+
stableswapPool.datum,
|
|
606
|
+
context.systemParams,
|
|
607
|
+
context.lucid,
|
|
608
|
+
),
|
|
609
|
+
);
|
|
610
|
+
|
|
519
611
|
// Redemption order, can't be satisfied because of maxFeeRatio
|
|
520
612
|
const redeemOrderMaxFeeTx = await runAndAwaitTx(
|
|
521
613
|
context.lucid,
|
|
@@ -552,11 +644,27 @@ test<IndigoTestContext>('Stableswap - pickValidSatisfiableOrders', async (contex
|
|
|
552
644
|
),
|
|
553
645
|
);
|
|
554
646
|
|
|
555
|
-
|
|
647
|
+
// redemption order, can't process because not enough lovelaces
|
|
648
|
+
const redemptionOrderMinLovelacesTx = await runAndAwaitTx(
|
|
649
|
+
context.lucid,
|
|
650
|
+
createStableswapOrder(
|
|
651
|
+
context.assetConfigs[0].iassetTokenNameAscii,
|
|
652
|
+
EXAMPLE_TOKEN_1,
|
|
653
|
+
5_000_000n,
|
|
654
|
+
false,
|
|
655
|
+
stableswapPool.datum,
|
|
656
|
+
context.systemParams,
|
|
657
|
+
context.lucid,
|
|
658
|
+
await context.lucid.wallet().address(),
|
|
659
|
+
undefined,
|
|
660
|
+
BASE_MAX_EXECUTION_FEE,
|
|
661
|
+
-500_000n,
|
|
662
|
+
),
|
|
663
|
+
);
|
|
664
|
+
|
|
665
|
+
const stableswapOrders = await findAllStableswapOrders(
|
|
556
666
|
context.lucid,
|
|
557
667
|
context.systemParams.validatorHashes.stableswapHash,
|
|
558
|
-
fromText(context.assetConfigs[0].iassetTokenNameAscii),
|
|
559
|
-
EXAMPLE_TOKEN_1,
|
|
560
668
|
);
|
|
561
669
|
|
|
562
670
|
await createUtxoAtTreasury(
|
|
@@ -571,10 +679,10 @@ test<IndigoTestContext>('Stableswap - pickValidSatisfiableOrders', async (contex
|
|
|
571
679
|
);
|
|
572
680
|
|
|
573
681
|
const satisfiableOrders = pickValidSatisfiableOrders(
|
|
682
|
+
context.lucid,
|
|
574
683
|
stableswapOrders,
|
|
575
684
|
stableswapPool,
|
|
576
685
|
context.systemParams,
|
|
577
|
-
{ ignoreMaxExecutionFeeConstraint: false },
|
|
578
686
|
);
|
|
579
687
|
|
|
580
688
|
expect(satisfiableOrders.length, 'Only 2 orders are satisfiable').toEqual(2);
|
|
@@ -594,11 +702,9 @@ test<IndigoTestContext>('Stableswap - pickValidSatisfiableOrders', async (contex
|
|
|
594
702
|
// Checks after last transaction //
|
|
595
703
|
///////////////////////////////////
|
|
596
704
|
|
|
597
|
-
const remainingOrders = await
|
|
705
|
+
const remainingOrders = await findAllStableswapOrders(
|
|
598
706
|
context.lucid,
|
|
599
707
|
context.systemParams.validatorHashes.stableswapHash,
|
|
600
|
-
fromText(context.assetConfigs[0].iassetTokenNameAscii),
|
|
601
|
-
EXAMPLE_TOKEN_1,
|
|
602
708
|
);
|
|
603
709
|
|
|
604
710
|
expect(
|
|
@@ -606,11 +712,17 @@ test<IndigoTestContext>('Stableswap - pickValidSatisfiableOrders', async (contex
|
|
|
606
712
|
'Expected different orders to not be processed',
|
|
607
713
|
).toEqual(
|
|
608
714
|
expect.arrayContaining([
|
|
715
|
+
mintOtherIassetTx,
|
|
716
|
+
mintOtherCollateralTx,
|
|
717
|
+
redeemOtherIassetTx,
|
|
718
|
+
redeemOtherCollateralTx,
|
|
609
719
|
redeemOrderMaxFeeTx,
|
|
610
720
|
redeemOrderTx0,
|
|
611
721
|
mintOrderMaxFeeTx,
|
|
612
722
|
mintOrderMaxExecFeeTx,
|
|
613
723
|
redemptionOrderMaxExecFeeTx,
|
|
724
|
+
redemptionOrderMinLovelacesTx,
|
|
725
|
+
mintOrderMinLovelacesTx,
|
|
614
726
|
]),
|
|
615
727
|
);
|
|
616
728
|
});
|