@indigo-labs/indigo-sdk 0.5.4 → 0.5.6
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/dist/index.d.mts +11 -1
- package/dist/index.d.ts +11 -1
- package/dist/index.js +17 -0
- package/dist/index.mjs +15 -0
- package/package.json +11 -1
- package/.claude/settings.local.json +0 -7
- package/.github/workflows/ci.yml +0 -66
- package/.husky/pre-commit +0 -1
- package/.nvmrc +0 -1
- package/.prettierignore +0 -1
- package/.prettierrc +0 -6
- package/eslint.config.mjs +0 -42
- package/scripts/bench.sh +0 -62
- package/scripts/run-repeat.sh +0 -22
- package/src/contracts/cdp/helpers.ts +0 -173
- package/src/contracts/cdp/scripts.ts +0 -70
- package/src/contracts/cdp/transactions.ts +0 -1584
- package/src/contracts/cdp/types-new.ts +0 -277
- package/src/contracts/cdp/types.ts +0 -43
- package/src/contracts/cdp-creator/queries.ts +0 -30
- package/src/contracts/cdp-creator/scripts.ts +0 -45
- package/src/contracts/cdp-creator/types-new.ts +0 -50
- package/src/contracts/cdp-creator/types.ts +0 -24
- package/src/contracts/collector/scripts.ts +0 -32
- package/src/contracts/collector/transactions.ts +0 -50
- package/src/contracts/collector/types-new.ts +0 -17
- package/src/contracts/execute/scripts.ts +0 -57
- package/src/contracts/execute/types-new.ts +0 -44
- package/src/contracts/execute/types.ts +0 -24
- package/src/contracts/gov/helpers.ts +0 -351
- package/src/contracts/gov/scripts.ts +0 -41
- package/src/contracts/gov/transactions.ts +0 -1607
- package/src/contracts/gov/types-new.ts +0 -268
- package/src/contracts/gov/types.ts +0 -22
- package/src/contracts/iasset/helpers.ts +0 -170
- package/src/contracts/iasset/queries.ts +0 -102
- package/src/contracts/iasset/scripts.ts +0 -38
- package/src/contracts/iasset/types.ts +0 -154
- package/src/contracts/initialize/actions.ts +0 -789
- package/src/contracts/initialize/helpers.ts +0 -616
- package/src/contracts/initialize/types.ts +0 -116
- package/src/contracts/interest-collection/helpers.ts +0 -19
- package/src/contracts/interest-collection/queries.ts +0 -66
- package/src/contracts/interest-collection/scripts.ts +0 -44
- package/src/contracts/interest-collection/transactions.ts +0 -433
- package/src/contracts/interest-collection/types-new.ts +0 -50
- package/src/contracts/interest-collection/types.ts +0 -26
- package/src/contracts/interest-oracle/helpers.ts +0 -61
- package/src/contracts/interest-oracle/scripts.ts +0 -18
- package/src/contracts/interest-oracle/transactions.ts +0 -151
- package/src/contracts/interest-oracle/types-new.ts +0 -32
- package/src/contracts/interest-oracle/types.ts +0 -16
- package/src/contracts/one-shot/scripts.ts +0 -12
- package/src/contracts/one-shot/transactions.ts +0 -66
- package/src/contracts/one-shot/types.ts +0 -22
- package/src/contracts/poll/helpers.ts +0 -37
- package/src/contracts/poll/scripts.ts +0 -71
- package/src/contracts/poll/types-poll-manager.ts +0 -20
- package/src/contracts/poll/types-poll-new.ts +0 -196
- package/src/contracts/poll/types-poll-shard.ts +0 -16
- package/src/contracts/price-oracle/helpers.ts +0 -30
- package/src/contracts/price-oracle/scripts.ts +0 -18
- package/src/contracts/price-oracle/transactions.ts +0 -115
- package/src/contracts/price-oracle/types-new.ts +0 -50
- package/src/contracts/price-oracle/types.ts +0 -16
- package/src/contracts/pyth-feed/helpers.ts +0 -82
- package/src/contracts/pyth-feed/scripts.ts +0 -15
- package/src/contracts/pyth-feed/types.ts +0 -181
- package/src/contracts/rob/helpers.ts +0 -666
- package/src/contracts/rob/scripts.ts +0 -35
- package/src/contracts/rob/transactions.ts +0 -423
- package/src/contracts/rob/types-new.ts +0 -128
- package/src/contracts/rob/types.ts +0 -16
- package/src/contracts/rob-leverage/helpers.ts +0 -422
- package/src/contracts/rob-leverage/transactions.ts +0 -368
- package/src/contracts/stability-pool/helpers.ts +0 -798
- package/src/contracts/stability-pool/queries.ts +0 -132
- package/src/contracts/stability-pool/scripts.ts +0 -51
- package/src/contracts/stability-pool/transactions.ts +0 -921
- package/src/contracts/stability-pool/types-new.ts +0 -385
- package/src/contracts/stability-pool/types.ts +0 -25
- package/src/contracts/stableswap/helpers.ts +0 -507
- package/src/contracts/stableswap/scripts.ts +0 -37
- package/src/contracts/stableswap/transactions.ts +0 -475
- package/src/contracts/stableswap/types-new.ts +0 -131
- package/src/contracts/stableswap/types.ts +0 -17
- package/src/contracts/staking/helpers.ts +0 -136
- package/src/contracts/staking/scripts.ts +0 -41
- package/src/contracts/staking/transactions.ts +0 -349
- package/src/contracts/staking/types-new.ts +0 -137
- package/src/contracts/staking/types.ts +0 -17
- package/src/contracts/treasury/helpers.ts +0 -21
- package/src/contracts/treasury/queries.ts +0 -100
- package/src/contracts/treasury/scripts.ts +0 -27
- package/src/contracts/treasury/transactions.ts +0 -334
- package/src/contracts/treasury/types-new.ts +0 -69
- package/src/contracts/treasury/types.ts +0 -14
- package/src/contracts/version-registry/scripts.ts +0 -29
- package/src/contracts/version-registry/types-new.ts +0 -18
- package/src/contracts/version-registry/types.ts +0 -17
- package/src/index.ts +0 -93
- package/src/scripts/always-fail-validator.ts +0 -7
- package/src/scripts/auth-token-policy.ts +0 -24
- package/src/scripts/iasset-policy.ts +0 -23
- package/src/types/evolution-schema-options.ts +0 -16
- package/src/types/generic.ts +0 -52
- package/src/types/multisig.ts +0 -48
- package/src/types/on-chain-decimal.ts +0 -57
- package/src/types/rational.ts +0 -61
- package/src/types/system-params.ts +0 -463
- package/src/utils/array-utils.ts +0 -108
- package/src/utils/bigint-utils.ts +0 -35
- package/src/utils/helper-txs.ts +0 -31
- package/src/utils/indigo-helpers.ts +0 -17
- package/src/utils/lucid-utils.ts +0 -125
- package/src/utils/oracle-helpers.ts +0 -62
- package/src/utils/pyth/decode.ts +0 -223
- package/src/utils/pyth/encode.ts +0 -262
- package/src/utils/pyth/index.ts +0 -14
- package/src/utils/pyth/types.ts +0 -87
- package/src/utils/time-helpers.ts +0 -4
- package/src/utils/utils.ts +0 -25
- package/src/validators/always-succeed-validator.ts +0 -6
- package/src/validators/cdp-creator-validator.ts +0 -7
- package/src/validators/cdp-redeem-validator.ts +0 -7
- package/src/validators/cdp-validator.ts +0 -7
- package/src/validators/collector-validator.ts +0 -7
- package/src/validators/execute-validator.ts +0 -7
- package/src/validators/governance-validator.ts +0 -7
- package/src/validators/iasset-validator.ts +0 -7
- package/src/validators/interest-collection-validator.ts +0 -7
- package/src/validators/interest-oracle-validator.ts +0 -7
- package/src/validators/one-shot-policy.ts +0 -7
- package/src/validators/poll-manager-validator.ts +0 -7
- package/src/validators/poll-shard-validator.ts +0 -7
- package/src/validators/price-oracle-validator.ts +0 -7
- package/src/validators/pyth-feed-validator.ts +0 -7
- package/src/validators/rob-validator.ts +0 -7
- package/src/validators/stability-pool-validator.ts +0 -7
- package/src/validators/stableswap-validator.ts +0 -7
- package/src/validators/staking-validator.ts +0 -7
- package/src/validators/treasury-validator.ts +0 -7
- package/src/validators/version-record-policy.ts +0 -7
- package/src/validators/version-registry-validator.ts +0 -7
- package/tests/always-succeed/script.ts +0 -7
- package/tests/array-utils.test.ts +0 -92
- package/tests/bigint-utils.test.ts +0 -41
- package/tests/cdp/actions.ts +0 -680
- package/tests/cdp/cdp-helpers.ts +0 -55
- package/tests/cdp/cdp-queries.ts +0 -423
- package/tests/cdp/cdp.test.ts +0 -6730
- package/tests/cdp/transactions-mutated.ts +0 -1733
- package/tests/data/system-params.json +0 -1155
- package/tests/datums.test.ts +0 -302
- package/tests/endpoints/initialize.ts +0 -78
- package/tests/endpoints/interest-collector.ts +0 -37
- package/tests/endpoints/treasury.ts +0 -70
- package/tests/gov/actions.ts +0 -381
- package/tests/gov/gov.test.ts +0 -5147
- package/tests/gov/governance-queries.ts +0 -37
- package/tests/hash-checks.test.ts +0 -112
- package/tests/indigo-test-helpers.ts +0 -99
- package/tests/initialize.test.ts +0 -79
- package/tests/interest-calculations.test.ts +0 -120
- package/tests/interest-collection/interest-collection.test.ts +0 -843
- package/tests/interest-collection/transactions-mutated.ts +0 -260
- package/tests/interest-oracle.test.ts +0 -93
- package/tests/mock/assets-mock.ts +0 -243
- package/tests/mock/protocol-params-mock.ts +0 -21
- package/tests/price-oracle/actions.ts +0 -161
- package/tests/price-oracle/price-oracle-queries.ts +0 -12
- package/tests/price-oracle/price-oracle.test.ts +0 -228
- package/tests/price-oracle/transactions-mutated.ts +0 -59
- package/tests/pyth/endpoints.ts +0 -96
- package/tests/pyth/helpers.ts +0 -37
- package/tests/pyth/pyth-encoding.test.ts +0 -376
- package/tests/pyth/pyth-feeds.test.ts +0 -943
- package/tests/pyth/pyth-indigo.test.ts +0 -505
- package/tests/pyth/pyth.test.ts +0 -299
- package/tests/queries/collector-queries.ts +0 -27
- package/tests/queries/execute-queries.ts +0 -47
- package/tests/queries/iasset-queries.ts +0 -149
- package/tests/queries/interest-oracle-queries.ts +0 -12
- package/tests/queries/poll-queries.ts +0 -97
- package/tests/queries/staking-queries.ts +0 -54
- package/tests/rob/actions.ts +0 -56
- package/tests/rob/rob-leverage.test.ts +0 -2555
- package/tests/rob/rob-queries.ts +0 -95
- package/tests/rob/rob.test.ts +0 -3677
- package/tests/rob/transactions-mutated.ts +0 -844
- package/tests/script-size.test.ts +0 -240
- package/tests/setup.ts +0 -135
- package/tests/stability-pool/actions.ts +0 -229
- package/tests/stability-pool.test.ts +0 -6491
- package/tests/stableswap/stableswap-actions.ts +0 -83
- package/tests/stableswap/stableswap-queries.ts +0 -98
- package/tests/stableswap/stableswap.test.ts +0 -4471
- package/tests/stableswap/transactions-mutated.ts +0 -348
- package/tests/staking.test.ts +0 -172
- package/tests/test-helpers.ts +0 -94
- package/tests/treasury/actions.ts +0 -67
- package/tests/treasury/treasury.test.ts +0 -320
- package/tests/utils/asserts.ts +0 -87
- package/tests/utils/benchmark-utils.ts +0 -130
- package/tests/utils/index.ts +0 -168
- package/tsconfig.build.json +0 -4
- package/tsconfig.json +0 -27
- package/vitest.config.ts +0 -11
|
@@ -1,423 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
LucidEvolution,
|
|
3
|
-
TxBuilder,
|
|
4
|
-
Credential,
|
|
5
|
-
OutRef,
|
|
6
|
-
addAssets,
|
|
7
|
-
fromHex,
|
|
8
|
-
toHex,
|
|
9
|
-
fromText,
|
|
10
|
-
Assets,
|
|
11
|
-
getInputIndices,
|
|
12
|
-
} from '@lucid-evolution/lucid';
|
|
13
|
-
import {
|
|
14
|
-
addrDetails,
|
|
15
|
-
createScriptAddress,
|
|
16
|
-
getInlineDatumOrThrow,
|
|
17
|
-
} from '../../utils/lucid-utils';
|
|
18
|
-
import {
|
|
19
|
-
readonlyArray as RA,
|
|
20
|
-
array as A,
|
|
21
|
-
function as F,
|
|
22
|
-
option as O,
|
|
23
|
-
} from 'fp-ts';
|
|
24
|
-
import { unzip, zip } from 'fp-ts/lib/Array';
|
|
25
|
-
import {
|
|
26
|
-
adaAssetClass,
|
|
27
|
-
AssetClass,
|
|
28
|
-
assetClassValueOf,
|
|
29
|
-
isSameAssetClass,
|
|
30
|
-
mkAssetsOf,
|
|
31
|
-
mkLovelacesOf,
|
|
32
|
-
negateAssets,
|
|
33
|
-
} from '@3rd-eye-labs/cardano-offchain-common';
|
|
34
|
-
import { matchSingle } from '../../utils/utils';
|
|
35
|
-
import {
|
|
36
|
-
RobDatum,
|
|
37
|
-
RobOrderType,
|
|
38
|
-
parseRobDatumOrThrow,
|
|
39
|
-
serialiseRobDatum,
|
|
40
|
-
serialiseRobRedeemer,
|
|
41
|
-
} from './types-new';
|
|
42
|
-
import {
|
|
43
|
-
parseCollateralAssetDatumOrThrow,
|
|
44
|
-
parseIAssetDatumOrThrow,
|
|
45
|
-
} from '../iasset/types';
|
|
46
|
-
import {
|
|
47
|
-
fromSystemParamsScriptRef,
|
|
48
|
-
SystemParams,
|
|
49
|
-
} from '../../types/system-params';
|
|
50
|
-
import {
|
|
51
|
-
buildRedemptionsTx,
|
|
52
|
-
isFullyRedeemed,
|
|
53
|
-
MIN_ROB_COLLATERAL_AMT,
|
|
54
|
-
robAmtToSpend,
|
|
55
|
-
} from './helpers';
|
|
56
|
-
import { match, P } from 'ts-pattern';
|
|
57
|
-
import { Rational } from '../../types/rational';
|
|
58
|
-
import { attachOracle } from '../iasset/helpers';
|
|
59
|
-
import { retrieveAdjustedPrice } from '../../utils/oracle-helpers';
|
|
60
|
-
|
|
61
|
-
export async function openRob(
|
|
62
|
-
assetTokenNameAscii: string,
|
|
63
|
-
depositAmt: bigint,
|
|
64
|
-
orderType: RobOrderType,
|
|
65
|
-
lucid: LucidEvolution,
|
|
66
|
-
sysParams: SystemParams,
|
|
67
|
-
robStakeCredential?: Credential,
|
|
68
|
-
): Promise<TxBuilder> {
|
|
69
|
-
const network = lucid.config().network!;
|
|
70
|
-
const [ownPkh, _] = await addrDetails(lucid);
|
|
71
|
-
|
|
72
|
-
const newDatum: RobDatum = {
|
|
73
|
-
owner: fromHex(ownPkh.hash),
|
|
74
|
-
iasset: fromHex(fromText(assetTokenNameAscii)),
|
|
75
|
-
orderType: orderType,
|
|
76
|
-
robRefInput: {
|
|
77
|
-
txHash: fromHex(
|
|
78
|
-
'0000000000000000000000000000000000000000000000000000000000000000',
|
|
79
|
-
),
|
|
80
|
-
outputIndex: 0n,
|
|
81
|
-
},
|
|
82
|
-
};
|
|
83
|
-
|
|
84
|
-
const depositVal = match(orderType)
|
|
85
|
-
.with({ BuyIAssetOrder: P.select() }, (buyContent) =>
|
|
86
|
-
mkAssetsOf(buyContent.collateralAsset, depositAmt),
|
|
87
|
-
)
|
|
88
|
-
.with({ SellIAssetOrder: P.any }, (_) =>
|
|
89
|
-
mkAssetsOf(
|
|
90
|
-
{
|
|
91
|
-
currencySymbol: fromHex(
|
|
92
|
-
sysParams.cdpParams.cdpAssetSymbol.unCurrencySymbol,
|
|
93
|
-
),
|
|
94
|
-
tokenName: newDatum.iasset,
|
|
95
|
-
},
|
|
96
|
-
depositAmt,
|
|
97
|
-
),
|
|
98
|
-
)
|
|
99
|
-
.exhaustive();
|
|
100
|
-
|
|
101
|
-
return lucid.newTx().pay.ToContract(
|
|
102
|
-
createScriptAddress(
|
|
103
|
-
network,
|
|
104
|
-
sysParams.validatorHashes.robHash,
|
|
105
|
-
robStakeCredential,
|
|
106
|
-
),
|
|
107
|
-
{
|
|
108
|
-
kind: 'inline',
|
|
109
|
-
value: serialiseRobDatum(newDatum),
|
|
110
|
-
},
|
|
111
|
-
addAssets(depositVal, mkLovelacesOf(MIN_ROB_COLLATERAL_AMT)),
|
|
112
|
-
);
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
export async function cancelRob(
|
|
116
|
-
robOutRef: OutRef,
|
|
117
|
-
sysParams: SystemParams,
|
|
118
|
-
lucid: LucidEvolution,
|
|
119
|
-
): Promise<TxBuilder> {
|
|
120
|
-
const robScriptRefUtxo = matchSingle(
|
|
121
|
-
await lucid.utxosByOutRef([
|
|
122
|
-
fromSystemParamsScriptRef(sysParams.scriptReferences.robValidatorRef),
|
|
123
|
-
]),
|
|
124
|
-
(_) => new Error('Expected a single ROB Ref Script UTXO'),
|
|
125
|
-
);
|
|
126
|
-
|
|
127
|
-
const robUtxo = matchSingle(
|
|
128
|
-
await lucid.utxosByOutRef([robOutRef]),
|
|
129
|
-
(_) => new Error('Expected a single ROB UTXO.'),
|
|
130
|
-
);
|
|
131
|
-
|
|
132
|
-
const robDatum = parseRobDatumOrThrow(getInlineDatumOrThrow(robUtxo));
|
|
133
|
-
|
|
134
|
-
return lucid
|
|
135
|
-
.newTx()
|
|
136
|
-
.readFrom([robScriptRefUtxo])
|
|
137
|
-
.collectFrom([robUtxo], serialiseRobRedeemer('Cancel'))
|
|
138
|
-
.addSignerKey(toHex(robDatum.owner));
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
export async function redeemRob(
|
|
142
|
-
/**
|
|
143
|
-
* The tuple represents the ROB UTXO and the amount to payout for a redemption. In case of buy order,
|
|
144
|
-
* it's denominated in iAssets, in case of sell order, it's denominated in collateral asset.
|
|
145
|
-
*/
|
|
146
|
-
redemptionRobsData: [OutRef, bigint][],
|
|
147
|
-
priceOracleOutRef: OutRef | undefined,
|
|
148
|
-
iassetOutRef: OutRef,
|
|
149
|
-
collateralAssetOutRef: OutRef,
|
|
150
|
-
lucid: LucidEvolution,
|
|
151
|
-
sysParams: SystemParams,
|
|
152
|
-
pythMessage: string | undefined = undefined,
|
|
153
|
-
pythStateOutRef: OutRef | undefined = undefined,
|
|
154
|
-
): Promise<TxBuilder> {
|
|
155
|
-
const robScriptRefUtxo = matchSingle(
|
|
156
|
-
await lucid.utxosByOutRef([
|
|
157
|
-
fromSystemParamsScriptRef(sysParams.scriptReferences.robValidatorRef),
|
|
158
|
-
]),
|
|
159
|
-
(_) => new Error('Expected a single ROB Ref Script UTXO'),
|
|
160
|
-
);
|
|
161
|
-
|
|
162
|
-
const iassetUtxo = matchSingle(
|
|
163
|
-
await lucid.utxosByOutRef([iassetOutRef]),
|
|
164
|
-
(_) => new Error('Expected a single IAsset UTXO'),
|
|
165
|
-
);
|
|
166
|
-
|
|
167
|
-
const iassetDatum = parseIAssetDatumOrThrow(
|
|
168
|
-
getInlineDatumOrThrow(iassetUtxo),
|
|
169
|
-
);
|
|
170
|
-
|
|
171
|
-
const collateralAssetUtxo = matchSingle(
|
|
172
|
-
await lucid.utxosByOutRef([collateralAssetOutRef]),
|
|
173
|
-
(_) => new Error('Expected a single collateral asset UTXO'),
|
|
174
|
-
);
|
|
175
|
-
|
|
176
|
-
const collateralAssetDatum = parseCollateralAssetDatumOrThrow(
|
|
177
|
-
getInlineDatumOrThrow(collateralAssetUtxo),
|
|
178
|
-
);
|
|
179
|
-
|
|
180
|
-
const [robsToRedeemOutRefs, robRedemptionIAssetAmt] =
|
|
181
|
-
unzip(redemptionRobsData);
|
|
182
|
-
|
|
183
|
-
const [adjustedPrice, _] = await retrieveAdjustedPrice(
|
|
184
|
-
iassetDatum.assetName,
|
|
185
|
-
collateralAssetDatum.collateralAsset,
|
|
186
|
-
collateralAssetDatum.priceInfo,
|
|
187
|
-
collateralAssetDatum.extraDecimals,
|
|
188
|
-
priceOracleOutRef,
|
|
189
|
-
pythMessage,
|
|
190
|
-
sysParams.pythConfig,
|
|
191
|
-
lucid,
|
|
192
|
-
);
|
|
193
|
-
|
|
194
|
-
const redemptionRobs = await lucid
|
|
195
|
-
.utxosByOutRef(robsToRedeemOutRefs)
|
|
196
|
-
.then((val) => zip(val, robRedemptionIAssetAmt));
|
|
197
|
-
|
|
198
|
-
const allRefInputs = [robScriptRefUtxo, iassetUtxo, collateralAssetUtxo];
|
|
199
|
-
|
|
200
|
-
const tx = lucid.newTx();
|
|
201
|
-
const { interval, referenceInputs } = await attachOracle(
|
|
202
|
-
iassetDatum.assetName,
|
|
203
|
-
collateralAssetDatum.collateralAsset,
|
|
204
|
-
collateralAssetDatum.priceInfo,
|
|
205
|
-
priceOracleOutRef,
|
|
206
|
-
pythStateOutRef,
|
|
207
|
-
pythMessage,
|
|
208
|
-
sysParams.pythConfig,
|
|
209
|
-
sysParams.cdpParams.biasTime,
|
|
210
|
-
lucid,
|
|
211
|
-
tx,
|
|
212
|
-
);
|
|
213
|
-
|
|
214
|
-
// Set the validity interval for the transaction
|
|
215
|
-
tx.validFrom(interval.validFrom).validTo(interval.validTo);
|
|
216
|
-
|
|
217
|
-
// Read from the reference inputs
|
|
218
|
-
allRefInputs.push(...referenceInputs);
|
|
219
|
-
|
|
220
|
-
const refInputIdxs = getInputIndices(allRefInputs, allRefInputs);
|
|
221
|
-
|
|
222
|
-
buildRedemptionsTx(
|
|
223
|
-
redemptionRobs,
|
|
224
|
-
iassetDatum.assetName,
|
|
225
|
-
collateralAssetDatum.collateralAsset,
|
|
226
|
-
adjustedPrice,
|
|
227
|
-
iassetDatum.redemptionReimbursementRatio,
|
|
228
|
-
sysParams,
|
|
229
|
-
tx,
|
|
230
|
-
0n,
|
|
231
|
-
refInputIdxs[2],
|
|
232
|
-
refInputIdxs[1],
|
|
233
|
-
priceOracleOutRef !== undefined
|
|
234
|
-
? { OracleRefInputIdx: refInputIdxs[3] }
|
|
235
|
-
: 'OracleVoid',
|
|
236
|
-
);
|
|
237
|
-
|
|
238
|
-
tx.readFrom(allRefInputs);
|
|
239
|
-
|
|
240
|
-
return tx;
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
/**
|
|
244
|
-
* Create Tx adjusting the ROB and claiming the received iAssets
|
|
245
|
-
*/
|
|
246
|
-
export async function adjustRob(
|
|
247
|
-
lucid: LucidEvolution,
|
|
248
|
-
robOutRef: OutRef,
|
|
249
|
-
/**
|
|
250
|
-
* A positive amount increases the deposit in the ROB,
|
|
251
|
-
* and a negative amount takes deposit from the ROB.
|
|
252
|
-
*/
|
|
253
|
-
adjustmentAmt: bigint,
|
|
254
|
-
newLimitPrice:
|
|
255
|
-
| { BuyOrder: Rational }
|
|
256
|
-
| { SellOrder: { newLimitPrices: [AssetClass, Rational][] } }
|
|
257
|
-
| undefined,
|
|
258
|
-
sysParams: SystemParams,
|
|
259
|
-
): Promise<TxBuilder> {
|
|
260
|
-
const robScriptRefUtxo = matchSingle(
|
|
261
|
-
await lucid.utxosByOutRef([
|
|
262
|
-
fromSystemParamsScriptRef(sysParams.scriptReferences.robValidatorRef),
|
|
263
|
-
]),
|
|
264
|
-
(_) => new Error('Expected a single ROB Ref Script UTXO'),
|
|
265
|
-
);
|
|
266
|
-
|
|
267
|
-
const robUtxo = matchSingle(
|
|
268
|
-
await lucid.utxosByOutRef([robOutRef]),
|
|
269
|
-
(_) => new Error('Expected a single ROB UTXO.'),
|
|
270
|
-
);
|
|
271
|
-
|
|
272
|
-
const robDatum = parseRobDatumOrThrow(getInlineDatumOrThrow(robUtxo));
|
|
273
|
-
|
|
274
|
-
// The claim case
|
|
275
|
-
if (
|
|
276
|
-
adjustmentAmt === 0n &&
|
|
277
|
-
isFullyRedeemed(robUtxo.assets, robDatum.orderType, {
|
|
278
|
-
currencySymbol: fromHex(
|
|
279
|
-
sysParams.cdpParams.cdpAssetSymbol.unCurrencySymbol,
|
|
280
|
-
),
|
|
281
|
-
tokenName: robDatum.iasset,
|
|
282
|
-
})
|
|
283
|
-
) {
|
|
284
|
-
throw new Error(
|
|
285
|
-
"When there's no more lovelaces to spend, use close instead of claim.",
|
|
286
|
-
);
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
// Negative adjust case
|
|
290
|
-
if (
|
|
291
|
-
adjustmentAmt < 0 &&
|
|
292
|
-
robAmtToSpend(robUtxo.assets, robDatum.orderType, {
|
|
293
|
-
currencySymbol: fromHex(
|
|
294
|
-
sysParams.cdpParams.cdpAssetSymbol.unCurrencySymbol,
|
|
295
|
-
),
|
|
296
|
-
tokenName: robDatum.iasset,
|
|
297
|
-
}) <= -adjustmentAmt
|
|
298
|
-
) {
|
|
299
|
-
throw new Error(
|
|
300
|
-
"Can't adjust negatively by more than available. Also, for adjusting by exactly the amount deposited, a close action should be used instead.",
|
|
301
|
-
);
|
|
302
|
-
}
|
|
303
|
-
|
|
304
|
-
const iassetAc: AssetClass = {
|
|
305
|
-
currencySymbol: fromHex(
|
|
306
|
-
sysParams.robParams.iassetPolicyId.unCurrencySymbol,
|
|
307
|
-
),
|
|
308
|
-
tokenName: robDatum.iasset,
|
|
309
|
-
};
|
|
310
|
-
|
|
311
|
-
const [depositAsset, rewardVal] = match(robDatum.orderType)
|
|
312
|
-
.returnType<[AssetClass, Assets]>()
|
|
313
|
-
.with({ BuyIAssetOrder: P.select() }, (buyContent) => {
|
|
314
|
-
const reward = assetClassValueOf(robUtxo.assets, iassetAc);
|
|
315
|
-
|
|
316
|
-
return [buyContent.collateralAsset, mkAssetsOf(iassetAc, reward)];
|
|
317
|
-
})
|
|
318
|
-
.with({ SellIAssetOrder: P.select() }, (content) => {
|
|
319
|
-
const reward = F.pipe(
|
|
320
|
-
content.allowedCollateralAssets,
|
|
321
|
-
RA.reduce<readonly [AssetClass, Rational], Assets>(
|
|
322
|
-
{},
|
|
323
|
-
(acc, [asset, _]) => {
|
|
324
|
-
const amt =
|
|
325
|
-
assetClassValueOf(robUtxo.assets, asset) -
|
|
326
|
-
// in case of ADA, the min has to stay.
|
|
327
|
-
(isSameAssetClass(asset, adaAssetClass)
|
|
328
|
-
? MIN_ROB_COLLATERAL_AMT
|
|
329
|
-
: 0n);
|
|
330
|
-
|
|
331
|
-
return addAssets(acc, mkAssetsOf(asset, amt));
|
|
332
|
-
},
|
|
333
|
-
),
|
|
334
|
-
);
|
|
335
|
-
|
|
336
|
-
return [iassetAc, reward];
|
|
337
|
-
})
|
|
338
|
-
.exhaustive();
|
|
339
|
-
|
|
340
|
-
return lucid
|
|
341
|
-
.newTx()
|
|
342
|
-
.readFrom([robScriptRefUtxo])
|
|
343
|
-
.collectFrom([robUtxo], serialiseRobRedeemer('Cancel'))
|
|
344
|
-
.pay.ToContract(
|
|
345
|
-
robUtxo.address,
|
|
346
|
-
{
|
|
347
|
-
kind: 'inline',
|
|
348
|
-
value: serialiseRobDatum({
|
|
349
|
-
...robDatum,
|
|
350
|
-
orderType:
|
|
351
|
-
newLimitPrice == null
|
|
352
|
-
? robDatum.orderType
|
|
353
|
-
: match(robDatum.orderType)
|
|
354
|
-
.returnType<RobOrderType>()
|
|
355
|
-
.with({ BuyIAssetOrder: P.select() }, (content) => {
|
|
356
|
-
const newPrice = match(newLimitPrice)
|
|
357
|
-
.with({ BuyOrder: P.select() }, (price) => price)
|
|
358
|
-
.otherwise(() => {
|
|
359
|
-
throw new Error(
|
|
360
|
-
'Must use buy order price change on buy order.',
|
|
361
|
-
);
|
|
362
|
-
});
|
|
363
|
-
|
|
364
|
-
return {
|
|
365
|
-
BuyIAssetOrder: { ...content, maxPrice: newPrice },
|
|
366
|
-
};
|
|
367
|
-
})
|
|
368
|
-
.with({ SellIAssetOrder: P.select() }, (content) => {
|
|
369
|
-
const newPrices = match(newLimitPrice)
|
|
370
|
-
.with(
|
|
371
|
-
{ SellOrder: { newLimitPrices: P.select() } },
|
|
372
|
-
(prices) => prices,
|
|
373
|
-
)
|
|
374
|
-
.otherwise(() => {
|
|
375
|
-
throw new Error(
|
|
376
|
-
'Must use sell order price change on sell order.',
|
|
377
|
-
);
|
|
378
|
-
});
|
|
379
|
-
|
|
380
|
-
return {
|
|
381
|
-
SellIAssetOrder: {
|
|
382
|
-
allowedCollateralAssets:
|
|
383
|
-
content.allowedCollateralAssets.map((entry) =>
|
|
384
|
-
F.pipe(
|
|
385
|
-
newPrices,
|
|
386
|
-
A.findFirst((newPrice) =>
|
|
387
|
-
isSameAssetClass(newPrice[0], entry[0]),
|
|
388
|
-
),
|
|
389
|
-
O.match(
|
|
390
|
-
() => entry,
|
|
391
|
-
(newPrice) =>
|
|
392
|
-
[
|
|
393
|
-
entry[0],
|
|
394
|
-
newPrice[1],
|
|
395
|
-
] satisfies typeof entry,
|
|
396
|
-
),
|
|
397
|
-
),
|
|
398
|
-
),
|
|
399
|
-
},
|
|
400
|
-
};
|
|
401
|
-
})
|
|
402
|
-
.exhaustive(),
|
|
403
|
-
}),
|
|
404
|
-
},
|
|
405
|
-
addAssets(
|
|
406
|
-
robUtxo.assets,
|
|
407
|
-
mkAssetsOf(depositAsset, adjustmentAmt),
|
|
408
|
-
negateAssets(rewardVal),
|
|
409
|
-
),
|
|
410
|
-
)
|
|
411
|
-
.addSignerKey(toHex(robDatum.owner));
|
|
412
|
-
}
|
|
413
|
-
|
|
414
|
-
/**
|
|
415
|
-
* Create Tx claiming the received iAssets.
|
|
416
|
-
*/
|
|
417
|
-
export async function claimRob(
|
|
418
|
-
lucid: LucidEvolution,
|
|
419
|
-
robOutRef: OutRef,
|
|
420
|
-
sysParams: SystemParams,
|
|
421
|
-
): Promise<TxBuilder> {
|
|
422
|
-
return adjustRob(lucid, robOutRef, 0n, undefined, sysParams);
|
|
423
|
-
}
|
|
@@ -1,128 +0,0 @@
|
|
|
1
|
-
import { TSchema, Data } from '@evolution-sdk/evolution';
|
|
2
|
-
import { DEFAULT_SCHEMA_OPTIONS } from '../../types/evolution-schema-options';
|
|
3
|
-
import {
|
|
4
|
-
AssetClassSchema,
|
|
5
|
-
OutputReferenceSchema,
|
|
6
|
-
} from '@3rd-eye-labs/cardano-offchain-common';
|
|
7
|
-
import { option as O, function as F } from 'fp-ts';
|
|
8
|
-
import { UTxO } from '@lucid-evolution/lucid';
|
|
9
|
-
import { RationalSchema } from '../../types/rational';
|
|
10
|
-
import { OracleIdxSchema } from '../price-oracle/types-new';
|
|
11
|
-
|
|
12
|
-
const RobOrderTypeSchema = TSchema.Union(
|
|
13
|
-
TSchema.Struct(
|
|
14
|
-
{
|
|
15
|
-
BuyIAssetOrder: TSchema.Struct(
|
|
16
|
-
{
|
|
17
|
-
collateralAsset: AssetClassSchema,
|
|
18
|
-
maxPrice: RationalSchema,
|
|
19
|
-
},
|
|
20
|
-
{ flatFields: true },
|
|
21
|
-
),
|
|
22
|
-
},
|
|
23
|
-
{ flatInUnion: true },
|
|
24
|
-
),
|
|
25
|
-
TSchema.Struct(
|
|
26
|
-
{
|
|
27
|
-
SellIAssetOrder: TSchema.Struct(
|
|
28
|
-
{
|
|
29
|
-
allowedCollateralAssets: TSchema.Array(
|
|
30
|
-
TSchema.Tuple([AssetClassSchema, RationalSchema]),
|
|
31
|
-
),
|
|
32
|
-
},
|
|
33
|
-
{ flatFields: true },
|
|
34
|
-
),
|
|
35
|
-
},
|
|
36
|
-
{ flatInUnion: true },
|
|
37
|
-
),
|
|
38
|
-
);
|
|
39
|
-
|
|
40
|
-
export type RobOrderType = typeof RobOrderTypeSchema.Type;
|
|
41
|
-
|
|
42
|
-
const RobDatumSchema = TSchema.Struct({
|
|
43
|
-
owner: TSchema.ByteArray,
|
|
44
|
-
iasset: TSchema.ByteArray,
|
|
45
|
-
orderType: RobOrderTypeSchema,
|
|
46
|
-
robRefInput: OutputReferenceSchema,
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
export type RobDatum = typeof RobDatumSchema.Type;
|
|
50
|
-
|
|
51
|
-
const RobRedeemerSchema = TSchema.Union(
|
|
52
|
-
TSchema.Struct(
|
|
53
|
-
{
|
|
54
|
-
Redeem: TSchema.Struct(
|
|
55
|
-
{
|
|
56
|
-
ownInputIdx: TSchema.Integer,
|
|
57
|
-
collateralAssetRefInputIdx: TSchema.Integer,
|
|
58
|
-
iassetRefInputIdx: TSchema.Integer,
|
|
59
|
-
continuingOutputIdx: TSchema.Integer,
|
|
60
|
-
sellOrderAllowedAssetsIdx: TSchema.Integer,
|
|
61
|
-
priceOracleIdx: OracleIdxSchema,
|
|
62
|
-
},
|
|
63
|
-
{ flatFields: true },
|
|
64
|
-
),
|
|
65
|
-
},
|
|
66
|
-
{ flatInUnion: true },
|
|
67
|
-
),
|
|
68
|
-
TSchema.Literal('Cancel', { flatInUnion: true }),
|
|
69
|
-
TSchema.Literal('UpgradeVersion', { flatInUnion: true }),
|
|
70
|
-
);
|
|
71
|
-
|
|
72
|
-
export type RobRedeemer = typeof RobRedeemerSchema.Type;
|
|
73
|
-
|
|
74
|
-
export function parseRobDatum(datum: string): O.Option<RobDatum> {
|
|
75
|
-
try {
|
|
76
|
-
return O.some(
|
|
77
|
-
Data.withSchema(RobDatumSchema, DEFAULT_SCHEMA_OPTIONS).fromCBORHex(
|
|
78
|
-
datum,
|
|
79
|
-
),
|
|
80
|
-
);
|
|
81
|
-
} catch (_) {
|
|
82
|
-
return O.none;
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
export function parseRobDatumOrThrow(datum: string): RobDatum {
|
|
87
|
-
return F.pipe(
|
|
88
|
-
parseRobDatum(datum),
|
|
89
|
-
O.match(() => {
|
|
90
|
-
throw new Error('Expected an ROB datum.');
|
|
91
|
-
}, F.identity),
|
|
92
|
-
);
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
export function serialiseRobDatum(d: RobDatum): string {
|
|
96
|
-
return Data.withSchema(RobDatumSchema, DEFAULT_SCHEMA_OPTIONS).toCBORHex(d);
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
export function serialiseRobRedeemer(r: RobRedeemer): string {
|
|
100
|
-
return Data.withSchema(RobRedeemerSchema, DEFAULT_SCHEMA_OPTIONS).toCBORHex(
|
|
101
|
-
r,
|
|
102
|
-
);
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
export function parseRobRedeemer(
|
|
106
|
-
redeemerCborHex: string,
|
|
107
|
-
): O.Option<RobRedeemer> {
|
|
108
|
-
try {
|
|
109
|
-
return O.some(
|
|
110
|
-
Data.withSchema(RobRedeemerSchema, DEFAULT_SCHEMA_OPTIONS).fromCBORHex(
|
|
111
|
-
redeemerCborHex,
|
|
112
|
-
),
|
|
113
|
-
);
|
|
114
|
-
} catch (_) {
|
|
115
|
-
return O.none;
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
export function parseRobRedeemerOrThrow(redeemerCborHex: string): RobRedeemer {
|
|
120
|
-
return F.pipe(
|
|
121
|
-
parseRobRedeemer(redeemerCborHex),
|
|
122
|
-
O.match(() => {
|
|
123
|
-
throw new Error('Expected an ROB redeemer.');
|
|
124
|
-
}, F.identity),
|
|
125
|
-
);
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
export type RobOutput = { datum: RobDatum; utxo: UTxO };
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { Data } from '@lucid-evolution/lucid';
|
|
2
|
-
import { AssetClassSchema } from '../../types/generic';
|
|
3
|
-
|
|
4
|
-
export const RobParamsSchema = Data.Object({
|
|
5
|
-
versionRecordToken: AssetClassSchema,
|
|
6
|
-
iassetAuthToken: AssetClassSchema,
|
|
7
|
-
collateralAssetAuthToken: AssetClassSchema,
|
|
8
|
-
iassetPolicyId: Data.Bytes(),
|
|
9
|
-
iassetValHash: Data.Bytes(),
|
|
10
|
-
});
|
|
11
|
-
export type RobParams = Data.Static<typeof RobParamsSchema>;
|
|
12
|
-
const RobParams = RobParamsSchema as unknown as RobParams;
|
|
13
|
-
|
|
14
|
-
export function castRobParams(params: RobParams): Data {
|
|
15
|
-
return Data.castTo(params, RobParams);
|
|
16
|
-
}
|