@indigo-labs/indigo-sdk 0.3.9 → 0.3.10
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 +293 -765
- package/dist/index.d.ts +293 -765
- package/dist/index.js +1033 -476
- package/dist/index.mjs +948 -388
- package/package.json +1 -1
- package/src/contracts/cdp/helpers.ts +12 -0
- package/src/contracts/gov/transactions.ts +4 -14
- package/src/contracts/iasset/helpers.ts +1 -0
- package/src/contracts/price-oracle/types-new.ts +1 -1
- package/src/contracts/rob/helpers.ts +274 -196
- package/src/contracts/rob/transactions.ts +11 -5
- package/src/contracts/rob-leverage/helpers.ts +379 -371
- package/src/contracts/rob-leverage/transactions.ts +176 -104
- package/src/index.ts +0 -2
- package/src/utils/utils.ts +0 -3
- package/tests/cdp/cdp-queries.ts +1 -1
- package/tests/endpoints/initialize.ts +14 -2
- package/tests/interest-collection/interest-collector-queries.ts +2 -1
- package/tests/queries/collector-queries.ts +2 -1
- package/tests/queries/poll-queries.ts +2 -1
- package/tests/queries/treasury-queries.ts +2 -1
- package/tests/rob/rob-leverage.test.ts +1646 -612
- package/tests/rob/rob.test.ts +35 -19
- package/tests/rob/transactions-mutated.ts +6 -4
package/dist/index.mjs
CHANGED
|
@@ -387,7 +387,6 @@ function loadSystemParamsFromFile(file) {
|
|
|
387
387
|
function loadSystemParamsFromUrl(url) {
|
|
388
388
|
return fetch(url).then((res) => res.json()).then((data) => data);
|
|
389
389
|
}
|
|
390
|
-
var getRandomElement = (arr) => arr.length ? arr[Math.floor(Math.random() * arr.length)] : void 0;
|
|
391
390
|
|
|
392
391
|
// src/utils/time-helpers.ts
|
|
393
392
|
var ONE_SECOND = 1e3;
|
|
@@ -467,6 +466,11 @@ function calculateFeeFromRatio(feeRatio, amount) {
|
|
|
467
466
|
|
|
468
467
|
// src/contracts/cdp/helpers.ts
|
|
469
468
|
import { assetClassValueOf } from "@3rd-eye-labs/cardano-offchain-common";
|
|
469
|
+
function iassetValueOfCollateral(collateralAmt, oraclePrice) {
|
|
470
|
+
return rationalFloor(
|
|
471
|
+
rationalDiv(rationalFromInt(collateralAmt), oraclePrice)
|
|
472
|
+
);
|
|
473
|
+
}
|
|
470
474
|
function cdpCollateralRatioPercentage(currentSlot, iassetPrice, cdpUtxo, cdpContent, interestOracleDatum, network) {
|
|
471
475
|
const currentTime = BigInt(slotToUnixTime2(network, currentSlot));
|
|
472
476
|
return match4(cdpContent.cdpFees).with({ ActiveCDPInterestTracking: P4.select() }, (interest) => {
|
|
@@ -3653,6 +3657,7 @@ function attachOracle(iasset, collateralAsset, priceInfo, priceOracleOref, pythS
|
|
|
3653
3657
|
}).with(
|
|
3654
3658
|
{ DeferredValidation: { feedValHash: P11.select() } },
|
|
3655
3659
|
async (feedValHash) => {
|
|
3660
|
+
if (priceOracleOref) throw new Error("Cannot pass price oracle oref");
|
|
3656
3661
|
if (!pythMessage) throw new Error("Missing Pyth message");
|
|
3657
3662
|
if (!pythStateOref) throw new Error("Missing pyth state out ref");
|
|
3658
3663
|
const pythStateUtxo = matchSingle(
|
|
@@ -5120,184 +5125,21 @@ var mkPollShardValidatorFromSP = (params) => {
|
|
|
5120
5125
|
};
|
|
5121
5126
|
};
|
|
5122
5127
|
|
|
5123
|
-
// src/contracts/poll/types-poll-new.ts
|
|
5124
|
-
import { TSchema as TSchema15, Data as Data25 } from "@evolution-sdk/evolution";
|
|
5125
|
-
import {
|
|
5126
|
-
AddressSchema as AddressSchema3,
|
|
5127
|
-
OutputReferenceSchema as OutputReferenceSchema5
|
|
5128
|
-
} from "@3rd-eye-labs/cardano-offchain-common";
|
|
5129
|
-
import { option as O10, function as F12 } from "fp-ts";
|
|
5130
|
-
import { match as match14, P as P14 } from "ts-pattern";
|
|
5131
|
-
var PollStatusSchema = TSchema15.Struct({
|
|
5132
|
-
yesVotes: TSchema15.Integer,
|
|
5133
|
-
noVotes: TSchema15.Integer
|
|
5134
|
-
});
|
|
5135
|
-
var PollManagerContentSchema = TSchema15.Struct({
|
|
5136
|
-
pollId: TSchema15.Integer,
|
|
5137
|
-
pollOwner: TSchema15.ByteArray,
|
|
5138
|
-
content: ProposalContentSchema,
|
|
5139
|
-
treasuryWithdrawal: TSchema15.NullOr(TreasuryWithdrawalSchema),
|
|
5140
|
-
status: PollStatusSchema,
|
|
5141
|
-
votingEndTime: TSchema15.Integer,
|
|
5142
|
-
createdShardsCount: TSchema15.Integer,
|
|
5143
|
-
talliedShardsCount: TSchema15.Integer,
|
|
5144
|
-
totalShardsCount: TSchema15.Integer,
|
|
5145
|
-
proposingEndTime: TSchema15.Integer,
|
|
5146
|
-
expirationTime: TSchema15.Integer,
|
|
5147
|
-
protocolVersion: TSchema15.Integer,
|
|
5148
|
-
minimumQuorum: TSchema15.Integer
|
|
5149
|
-
});
|
|
5150
|
-
var PollShardContentSchema = TSchema15.Struct({
|
|
5151
|
-
pollId: TSchema15.Integer,
|
|
5152
|
-
status: PollStatusSchema,
|
|
5153
|
-
votingEndTime: TSchema15.Integer,
|
|
5154
|
-
managerAddress: AddressSchema3
|
|
5155
|
-
});
|
|
5156
|
-
var PollDatumSchema = TSchema15.Union(
|
|
5157
|
-
TSchema15.Struct(
|
|
5158
|
-
{
|
|
5159
|
-
PollManager: PollManagerContentSchema
|
|
5160
|
-
},
|
|
5161
|
-
{ flatInUnion: true }
|
|
5162
|
-
),
|
|
5163
|
-
TSchema15.Struct({ PollShard: PollShardContentSchema }, { flatInUnion: true })
|
|
5164
|
-
);
|
|
5165
|
-
var VoteOptionSchema = TSchema15.Union(
|
|
5166
|
-
TSchema15.Literal("Yes", { flatInUnion: true }),
|
|
5167
|
-
TSchema15.Literal("No", { flatInUnion: true })
|
|
5168
|
-
);
|
|
5169
|
-
var PollShardRedeemerSchema = TSchema15.Union(
|
|
5170
|
-
TSchema15.Struct({ Vote: VoteOptionSchema }, { flatInUnion: true }),
|
|
5171
|
-
TSchema15.Struct(
|
|
5172
|
-
{
|
|
5173
|
-
MergeShards: TSchema15.Struct(
|
|
5174
|
-
{
|
|
5175
|
-
currentTime: TSchema15.Integer,
|
|
5176
|
-
pollManagerRef: OutputReferenceSchema5
|
|
5177
|
-
},
|
|
5178
|
-
{ flatFields: true }
|
|
5179
|
-
)
|
|
5180
|
-
},
|
|
5181
|
-
{ flatInUnion: true }
|
|
5182
|
-
)
|
|
5183
|
-
);
|
|
5184
|
-
var PollManagerRedeemerSchema = TSchema15.Union(
|
|
5185
|
-
TSchema15.Struct(
|
|
5186
|
-
{
|
|
5187
|
-
EndPoll: TSchema15.Struct(
|
|
5188
|
-
{ currentTime: TSchema15.Integer },
|
|
5189
|
-
{ flatFields: true }
|
|
5190
|
-
)
|
|
5191
|
-
},
|
|
5192
|
-
{ flatInUnion: true }
|
|
5193
|
-
),
|
|
5194
|
-
TSchema15.Struct(
|
|
5195
|
-
{
|
|
5196
|
-
CreateShards: TSchema15.Struct(
|
|
5197
|
-
{ currentTime: TSchema15.Integer },
|
|
5198
|
-
{ flatFields: true }
|
|
5199
|
-
)
|
|
5200
|
-
},
|
|
5201
|
-
{ flatInUnion: true }
|
|
5202
|
-
),
|
|
5203
|
-
TSchema15.Struct(
|
|
5204
|
-
{
|
|
5205
|
-
MergeShardsManager: TSchema15.Struct(
|
|
5206
|
-
{ currentTime: TSchema15.Integer },
|
|
5207
|
-
{ flatFields: true }
|
|
5208
|
-
)
|
|
5209
|
-
},
|
|
5210
|
-
{ flatInUnion: true }
|
|
5211
|
-
)
|
|
5212
|
-
);
|
|
5213
|
-
function serialisePollManagerRedeemer(r) {
|
|
5214
|
-
return Data25.withSchema(PollManagerRedeemerSchema).toCBORHex(r);
|
|
5215
|
-
}
|
|
5216
|
-
function serialisePollShardRedeemer(r) {
|
|
5217
|
-
return Data25.withSchema(PollShardRedeemerSchema).toCBORHex(r);
|
|
5218
|
-
}
|
|
5219
|
-
function parsePollManager(datum) {
|
|
5220
|
-
try {
|
|
5221
|
-
return match14(
|
|
5222
|
-
Data25.withSchema(PollDatumSchema, DEFAULT_SCHEMA_OPTIONS).fromCBORHex(
|
|
5223
|
-
datum
|
|
5224
|
-
)
|
|
5225
|
-
).with({ PollManager: P14.select() }, (res) => O10.some(res)).otherwise(() => O10.none);
|
|
5226
|
-
} catch (_) {
|
|
5227
|
-
return O10.none;
|
|
5228
|
-
}
|
|
5229
|
-
}
|
|
5230
|
-
function parsePollManagerOrThrow(datum) {
|
|
5231
|
-
return F12.pipe(
|
|
5232
|
-
parsePollManager(datum),
|
|
5233
|
-
O10.match(() => {
|
|
5234
|
-
throw new Error("Expected a poll manager datum.");
|
|
5235
|
-
}, F12.identity)
|
|
5236
|
-
);
|
|
5237
|
-
}
|
|
5238
|
-
function parsePollShard(datum) {
|
|
5239
|
-
try {
|
|
5240
|
-
return match14(
|
|
5241
|
-
Data25.withSchema(PollDatumSchema, DEFAULT_SCHEMA_OPTIONS).fromCBORHex(
|
|
5242
|
-
datum
|
|
5243
|
-
)
|
|
5244
|
-
).with({ PollShard: P14.select() }, (res) => O10.some(res)).otherwise(() => O10.none);
|
|
5245
|
-
} catch (_) {
|
|
5246
|
-
return O10.none;
|
|
5247
|
-
}
|
|
5248
|
-
}
|
|
5249
|
-
function parsePollShardOrThrow(datum) {
|
|
5250
|
-
return F12.pipe(
|
|
5251
|
-
parsePollShard(datum),
|
|
5252
|
-
O10.match(() => {
|
|
5253
|
-
throw new Error("Expected a poll shard datum.");
|
|
5254
|
-
}, F12.identity)
|
|
5255
|
-
);
|
|
5256
|
-
}
|
|
5257
|
-
function serialisePollDatum(d) {
|
|
5258
|
-
return Data25.withSchema(PollDatumSchema).toCBORHex(d);
|
|
5259
|
-
}
|
|
5260
|
-
|
|
5261
|
-
// src/contracts/poll/helpers.ts
|
|
5262
|
-
function q(pollStatus, e) {
|
|
5263
|
-
if (pollStatus.yesVotes + pollStatus.noVotes === 0n)
|
|
5264
|
-
return { getOnChainInt: 0n };
|
|
5265
|
-
else {
|
|
5266
|
-
const q2 = ocdDiv(
|
|
5267
|
-
{ getOnChainInt: pollStatus.yesVotes },
|
|
5268
|
-
{ getOnChainInt: BigInt(Math.floor(Math.sqrt(Number(e)))) }
|
|
5269
|
-
).getOnChainInt - ocdDiv(
|
|
5270
|
-
{ getOnChainInt: pollStatus.noVotes },
|
|
5271
|
-
{
|
|
5272
|
-
getOnChainInt: BigInt(
|
|
5273
|
-
Math.floor(
|
|
5274
|
-
Math.sqrt(Number(pollStatus.yesVotes + pollStatus.noVotes))
|
|
5275
|
-
)
|
|
5276
|
-
)
|
|
5277
|
-
}
|
|
5278
|
-
).getOnChainInt;
|
|
5279
|
-
return { getOnChainInt: BigInt(q2) };
|
|
5280
|
-
}
|
|
5281
|
-
}
|
|
5282
|
-
function pollPassQuorum(pollStatus, electorate, minQuorum) {
|
|
5283
|
-
return pollStatus.yesVotes + pollStatus.noVotes >= minQuorum && q(pollStatus, electorate).getOnChainInt > 50000n;
|
|
5284
|
-
}
|
|
5285
|
-
|
|
5286
5128
|
// src/contracts/collector/transactions.ts
|
|
5287
5129
|
import {
|
|
5288
5130
|
addAssets as addAssets7,
|
|
5289
|
-
Data as
|
|
5131
|
+
Data as Data26
|
|
5290
5132
|
} from "@lucid-evolution/lucid";
|
|
5291
5133
|
|
|
5292
5134
|
// src/contracts/collector/types-new.ts
|
|
5293
|
-
import { TSchema as
|
|
5294
|
-
var CollectorRedeemerSchema =
|
|
5295
|
-
|
|
5296
|
-
|
|
5297
|
-
|
|
5135
|
+
import { TSchema as TSchema15, Data as Data25 } from "@evolution-sdk/evolution";
|
|
5136
|
+
var CollectorRedeemerSchema = TSchema15.Union(
|
|
5137
|
+
TSchema15.Literal("Collect", { flatInUnion: true }),
|
|
5138
|
+
TSchema15.Literal("DistributeToStakers", { flatInUnion: true }),
|
|
5139
|
+
TSchema15.Literal("UpgradeVersion", { flatInUnion: true })
|
|
5298
5140
|
);
|
|
5299
5141
|
function serialiseCollectorRedeemer(r) {
|
|
5300
|
-
return
|
|
5142
|
+
return Data25.withSchema(
|
|
5301
5143
|
CollectorRedeemerSchema,
|
|
5302
5144
|
DEFAULT_SCHEMA_OPTIONS
|
|
5303
5145
|
).toCBORHex(r);
|
|
@@ -5318,7 +5160,7 @@ async function collectorFeeTx(fee, lucid, params, tx, collectorOref) {
|
|
|
5318
5160
|
);
|
|
5319
5161
|
tx.collectFrom([collectorUtxo], serialiseCollectorRedeemer("Collect")).pay.ToContract(
|
|
5320
5162
|
collectorUtxo.address,
|
|
5321
|
-
{ kind: "inline", value:
|
|
5163
|
+
{ kind: "inline", value: Data26.void() },
|
|
5322
5164
|
addAssets7(collectorUtxo.assets, mkLovelacesOf2(fee))
|
|
5323
5165
|
).readFrom([collectorRefScriptUtxo]);
|
|
5324
5166
|
return collectorRefScriptUtxo;
|
|
@@ -5348,95 +5190,95 @@ import { array as A8, option as O12, function as F14 } from "fp-ts";
|
|
|
5348
5190
|
import { match as match16, P as P16 } from "ts-pattern";
|
|
5349
5191
|
|
|
5350
5192
|
// src/contracts/staking/types-new.ts
|
|
5351
|
-
import { TSchema as
|
|
5352
|
-
import { option as
|
|
5353
|
-
import { match as
|
|
5354
|
-
var StakingPosLockedAmtSchema =
|
|
5355
|
-
|
|
5356
|
-
|
|
5357
|
-
|
|
5358
|
-
voteAmt:
|
|
5359
|
-
votingEnd:
|
|
5193
|
+
import { TSchema as TSchema16, Data as Data27 } from "@evolution-sdk/evolution";
|
|
5194
|
+
import { option as O10, function as F12 } from "fp-ts";
|
|
5195
|
+
import { match as match14, P as P14 } from "ts-pattern";
|
|
5196
|
+
var StakingPosLockedAmtSchema = TSchema16.Array(
|
|
5197
|
+
TSchema16.Tuple([
|
|
5198
|
+
TSchema16.Integer,
|
|
5199
|
+
TSchema16.Struct({
|
|
5200
|
+
voteAmt: TSchema16.Integer,
|
|
5201
|
+
votingEnd: TSchema16.Integer
|
|
5360
5202
|
})
|
|
5361
5203
|
])
|
|
5362
5204
|
);
|
|
5363
|
-
var RewardSnapshotSchema =
|
|
5364
|
-
snapshotAda:
|
|
5205
|
+
var RewardSnapshotSchema = TSchema16.Struct({
|
|
5206
|
+
snapshotAda: TSchema16.Integer
|
|
5365
5207
|
});
|
|
5366
|
-
var StakingPositionSchema =
|
|
5367
|
-
owner:
|
|
5208
|
+
var StakingPositionSchema = TSchema16.Struct({
|
|
5209
|
+
owner: TSchema16.ByteArray,
|
|
5368
5210
|
lockedAmount: StakingPosLockedAmtSchema,
|
|
5369
5211
|
positionSnapshot: RewardSnapshotSchema
|
|
5370
5212
|
});
|
|
5371
|
-
var StakingManagerSchema =
|
|
5372
|
-
totalStake:
|
|
5213
|
+
var StakingManagerSchema = TSchema16.Struct({
|
|
5214
|
+
totalStake: TSchema16.Integer,
|
|
5373
5215
|
managerSnapshot: RewardSnapshotSchema
|
|
5374
5216
|
});
|
|
5375
|
-
var StakingDatumSchema =
|
|
5217
|
+
var StakingDatumSchema = TSchema16.Union(
|
|
5376
5218
|
StakingManagerSchema,
|
|
5377
5219
|
StakingPositionSchema
|
|
5378
5220
|
);
|
|
5379
|
-
var StakingRedeemerSchema =
|
|
5380
|
-
|
|
5221
|
+
var StakingRedeemerSchema = TSchema16.Union(
|
|
5222
|
+
TSchema16.Struct(
|
|
5381
5223
|
{
|
|
5382
|
-
CreateStakingPosition:
|
|
5383
|
-
{ creatorPkh:
|
|
5224
|
+
CreateStakingPosition: TSchema16.Struct(
|
|
5225
|
+
{ creatorPkh: TSchema16.ByteArray },
|
|
5384
5226
|
{ flatFields: true }
|
|
5385
5227
|
)
|
|
5386
5228
|
},
|
|
5387
5229
|
{ flatInUnion: true }
|
|
5388
5230
|
),
|
|
5389
|
-
|
|
5390
|
-
|
|
5391
|
-
|
|
5231
|
+
TSchema16.Literal("UpdateTotalStake", { flatInUnion: true }),
|
|
5232
|
+
TSchema16.Literal("Distribute", { flatInUnion: true }),
|
|
5233
|
+
TSchema16.Struct(
|
|
5392
5234
|
{
|
|
5393
|
-
AdjustStakedAmount:
|
|
5394
|
-
{ adjustAmount:
|
|
5235
|
+
AdjustStakedAmount: TSchema16.Struct(
|
|
5236
|
+
{ adjustAmount: TSchema16.Integer },
|
|
5395
5237
|
{ flatFields: true }
|
|
5396
5238
|
)
|
|
5397
5239
|
},
|
|
5398
5240
|
{ flatInUnion: true }
|
|
5399
5241
|
),
|
|
5400
|
-
|
|
5401
|
-
|
|
5402
|
-
|
|
5242
|
+
TSchema16.Literal("Unstake", { flatInUnion: true }),
|
|
5243
|
+
TSchema16.Literal("Lock", { flatInUnion: true }),
|
|
5244
|
+
TSchema16.Literal("UpgradeVersion", { flatInUnion: true })
|
|
5403
5245
|
);
|
|
5404
5246
|
function serialiseStakingRedeemer(r) {
|
|
5405
|
-
return
|
|
5247
|
+
return Data27.withSchema(
|
|
5406
5248
|
StakingRedeemerSchema,
|
|
5407
5249
|
DEFAULT_SCHEMA_OPTIONS
|
|
5408
5250
|
).toCBORHex(r);
|
|
5409
5251
|
}
|
|
5410
5252
|
function parseStakingPosition(datum) {
|
|
5411
5253
|
try {
|
|
5412
|
-
return
|
|
5413
|
-
|
|
5254
|
+
return match14(
|
|
5255
|
+
Data27.withSchema(StakingDatumSchema, DEFAULT_SCHEMA_OPTIONS).fromCBORHex(
|
|
5414
5256
|
datum
|
|
5415
5257
|
)
|
|
5416
|
-
).with({ owner:
|
|
5258
|
+
).with({ owner: P14.any }, (res) => O10.some(res)).otherwise(() => O10.none);
|
|
5417
5259
|
} catch (_) {
|
|
5418
|
-
return
|
|
5260
|
+
return O10.none;
|
|
5419
5261
|
}
|
|
5420
5262
|
}
|
|
5421
5263
|
function parseStakingPositionOrThrow(datum) {
|
|
5422
|
-
return
|
|
5264
|
+
return F12.pipe(
|
|
5423
5265
|
parseStakingPosition(datum),
|
|
5424
|
-
|
|
5266
|
+
O10.match(() => {
|
|
5425
5267
|
throw new Error("Expected a StakingPosition datum.");
|
|
5426
|
-
},
|
|
5268
|
+
}, F12.identity)
|
|
5427
5269
|
);
|
|
5428
5270
|
}
|
|
5429
5271
|
function parseStakingManagerDatum(datum) {
|
|
5430
|
-
return
|
|
5431
|
-
|
|
5272
|
+
return match14(
|
|
5273
|
+
Data27.withSchema(StakingDatumSchema, DEFAULT_SCHEMA_OPTIONS).fromCBORHex(
|
|
5432
5274
|
datum
|
|
5433
5275
|
)
|
|
5434
|
-
).with({ totalStake:
|
|
5276
|
+
).with({ totalStake: P14.any }, (res) => res).otherwise(() => {
|
|
5435
5277
|
throw new Error("Expected a StakingPosition datum.");
|
|
5436
5278
|
});
|
|
5437
5279
|
}
|
|
5438
5280
|
function serialiseStakingDatum(d) {
|
|
5439
|
-
return
|
|
5281
|
+
return Data27.withSchema(StakingDatumSchema, DEFAULT_SCHEMA_OPTIONS).toCBORHex(
|
|
5440
5282
|
d
|
|
5441
5283
|
);
|
|
5442
5284
|
}
|
|
@@ -5558,51 +5400,214 @@ function calculateAdaReward(currentSnapshotAda, oldSnapshotAda, existingIndyAmou
|
|
|
5558
5400
|
return (currentSnapshotAda - oldSnapshotAda) * existingIndyAmount / rewardSnapshotPrecision;
|
|
5559
5401
|
}
|
|
5560
5402
|
|
|
5403
|
+
// src/contracts/poll/helpers.ts
|
|
5404
|
+
function q(pollStatus, e) {
|
|
5405
|
+
if (pollStatus.yesVotes + pollStatus.noVotes === 0n)
|
|
5406
|
+
return { getOnChainInt: 0n };
|
|
5407
|
+
else {
|
|
5408
|
+
const q2 = ocdDiv(
|
|
5409
|
+
{ getOnChainInt: pollStatus.yesVotes },
|
|
5410
|
+
{ getOnChainInt: BigInt(Math.floor(Math.sqrt(Number(e)))) }
|
|
5411
|
+
).getOnChainInt - ocdDiv(
|
|
5412
|
+
{ getOnChainInt: pollStatus.noVotes },
|
|
5413
|
+
{
|
|
5414
|
+
getOnChainInt: BigInt(
|
|
5415
|
+
Math.floor(
|
|
5416
|
+
Math.sqrt(Number(pollStatus.yesVotes + pollStatus.noVotes))
|
|
5417
|
+
)
|
|
5418
|
+
)
|
|
5419
|
+
}
|
|
5420
|
+
).getOnChainInt;
|
|
5421
|
+
return { getOnChainInt: BigInt(q2) };
|
|
5422
|
+
}
|
|
5423
|
+
}
|
|
5424
|
+
function pollPassQuorum(pollStatus, electorate, minQuorum) {
|
|
5425
|
+
return pollStatus.yesVotes + pollStatus.noVotes >= minQuorum && q(pollStatus, electorate).getOnChainInt > 50000n;
|
|
5426
|
+
}
|
|
5427
|
+
|
|
5561
5428
|
// src/contracts/version-registry/types-new.ts
|
|
5562
|
-
import { TSchema as
|
|
5563
|
-
var VersionRecordDatumSchema =
|
|
5564
|
-
upgradeId:
|
|
5429
|
+
import { TSchema as TSchema17, Data as Data28 } from "@evolution-sdk/evolution";
|
|
5430
|
+
var VersionRecordDatumSchema = TSchema17.Struct({
|
|
5431
|
+
upgradeId: TSchema17.Integer,
|
|
5565
5432
|
/// Underlying representation of the following mapping: ValidatorHash -> UpgradePath
|
|
5566
|
-
upgradePaths:
|
|
5567
|
-
|
|
5433
|
+
upgradePaths: TSchema17.Array(
|
|
5434
|
+
TSchema17.Tuple([TSchema17.ByteArray, TSchema17.ByteArray])
|
|
5568
5435
|
)
|
|
5569
5436
|
});
|
|
5570
5437
|
function serialiseVersionRecordDatum(d) {
|
|
5571
|
-
return
|
|
5438
|
+
return Data28.withSchema(
|
|
5572
5439
|
VersionRecordDatumSchema,
|
|
5573
5440
|
DEFAULT_SCHEMA_OPTIONS
|
|
5574
5441
|
).toCBORHex(d);
|
|
5575
5442
|
}
|
|
5576
5443
|
|
|
5577
|
-
// src/contracts/
|
|
5444
|
+
// src/contracts/poll/types-poll-new.ts
|
|
5445
|
+
import { TSchema as TSchema18, Data as Data29 } from "@evolution-sdk/evolution";
|
|
5578
5446
|
import {
|
|
5579
|
-
|
|
5580
|
-
|
|
5447
|
+
AddressSchema as AddressSchema3,
|
|
5448
|
+
OutputReferenceSchema as OutputReferenceSchema5
|
|
5581
5449
|
} from "@3rd-eye-labs/cardano-offchain-common";
|
|
5582
|
-
|
|
5583
|
-
|
|
5584
|
-
|
|
5585
|
-
|
|
5586
|
-
|
|
5587
|
-
|
|
5588
|
-
|
|
5589
|
-
|
|
5590
|
-
|
|
5450
|
+
import { option as O11, function as F13 } from "fp-ts";
|
|
5451
|
+
import { match as match15, P as P15 } from "ts-pattern";
|
|
5452
|
+
var PollStatusSchema = TSchema18.Struct({
|
|
5453
|
+
yesVotes: TSchema18.Integer,
|
|
5454
|
+
noVotes: TSchema18.Integer
|
|
5455
|
+
});
|
|
5456
|
+
var PollManagerContentSchema = TSchema18.Struct({
|
|
5457
|
+
pollId: TSchema18.Integer,
|
|
5458
|
+
pollOwner: TSchema18.ByteArray,
|
|
5459
|
+
content: ProposalContentSchema,
|
|
5460
|
+
treasuryWithdrawal: TSchema18.NullOr(TreasuryWithdrawalSchema),
|
|
5461
|
+
status: PollStatusSchema,
|
|
5462
|
+
votingEndTime: TSchema18.Integer,
|
|
5463
|
+
createdShardsCount: TSchema18.Integer,
|
|
5464
|
+
talliedShardsCount: TSchema18.Integer,
|
|
5465
|
+
totalShardsCount: TSchema18.Integer,
|
|
5466
|
+
proposingEndTime: TSchema18.Integer,
|
|
5467
|
+
expirationTime: TSchema18.Integer,
|
|
5468
|
+
protocolVersion: TSchema18.Integer,
|
|
5469
|
+
minimumQuorum: TSchema18.Integer
|
|
5470
|
+
});
|
|
5471
|
+
var PollShardContentSchema = TSchema18.Struct({
|
|
5472
|
+
pollId: TSchema18.Integer,
|
|
5473
|
+
status: PollStatusSchema,
|
|
5474
|
+
votingEndTime: TSchema18.Integer,
|
|
5475
|
+
managerAddress: AddressSchema3
|
|
5476
|
+
});
|
|
5477
|
+
var PollDatumSchema = TSchema18.Union(
|
|
5478
|
+
TSchema18.Struct(
|
|
5479
|
+
{
|
|
5480
|
+
PollManager: PollManagerContentSchema
|
|
5481
|
+
},
|
|
5482
|
+
{ flatInUnion: true }
|
|
5483
|
+
),
|
|
5484
|
+
TSchema18.Struct({ PollShard: PollShardContentSchema }, { flatInUnion: true })
|
|
5485
|
+
);
|
|
5486
|
+
var VoteOptionSchema = TSchema18.Union(
|
|
5487
|
+
TSchema18.Literal("Yes", { flatInUnion: true }),
|
|
5488
|
+
TSchema18.Literal("No", { flatInUnion: true })
|
|
5489
|
+
);
|
|
5490
|
+
var PollShardRedeemerSchema = TSchema18.Union(
|
|
5491
|
+
TSchema18.Struct({ Vote: VoteOptionSchema }, { flatInUnion: true }),
|
|
5492
|
+
TSchema18.Struct(
|
|
5493
|
+
{
|
|
5494
|
+
MergeShards: TSchema18.Struct(
|
|
5495
|
+
{
|
|
5496
|
+
currentTime: TSchema18.Integer,
|
|
5497
|
+
pollManagerRef: OutputReferenceSchema5
|
|
5498
|
+
},
|
|
5499
|
+
{ flatFields: true }
|
|
5591
5500
|
)
|
|
5592
|
-
|
|
5593
|
-
|
|
5594
|
-
)
|
|
5595
|
-
|
|
5596
|
-
|
|
5597
|
-
|
|
5598
|
-
|
|
5501
|
+
},
|
|
5502
|
+
{ flatInUnion: true }
|
|
5503
|
+
)
|
|
5504
|
+
);
|
|
5505
|
+
var PollManagerRedeemerSchema = TSchema18.Union(
|
|
5506
|
+
TSchema18.Struct(
|
|
5507
|
+
{
|
|
5508
|
+
EndPoll: TSchema18.Struct(
|
|
5509
|
+
{ currentTime: TSchema18.Integer },
|
|
5510
|
+
{ flatFields: true }
|
|
5599
5511
|
)
|
|
5600
|
-
|
|
5601
|
-
|
|
5512
|
+
},
|
|
5513
|
+
{ flatInUnion: true }
|
|
5514
|
+
),
|
|
5515
|
+
TSchema18.Struct(
|
|
5516
|
+
{
|
|
5517
|
+
CreateShards: TSchema18.Struct(
|
|
5518
|
+
{ currentTime: TSchema18.Integer },
|
|
5519
|
+
{ flatFields: true }
|
|
5520
|
+
)
|
|
5521
|
+
},
|
|
5522
|
+
{ flatInUnion: true }
|
|
5523
|
+
),
|
|
5524
|
+
TSchema18.Struct(
|
|
5525
|
+
{
|
|
5526
|
+
MergeShardsManager: TSchema18.Struct(
|
|
5527
|
+
{ currentTime: TSchema18.Integer },
|
|
5528
|
+
{ flatFields: true }
|
|
5529
|
+
)
|
|
5530
|
+
},
|
|
5531
|
+
{ flatInUnion: true }
|
|
5532
|
+
)
|
|
5533
|
+
);
|
|
5534
|
+
function serialisePollManagerRedeemer(r) {
|
|
5535
|
+
return Data29.withSchema(PollManagerRedeemerSchema).toCBORHex(r);
|
|
5536
|
+
}
|
|
5537
|
+
function serialisePollShardRedeemer(r) {
|
|
5538
|
+
return Data29.withSchema(PollShardRedeemerSchema).toCBORHex(r);
|
|
5539
|
+
}
|
|
5540
|
+
function parsePollManager(datum) {
|
|
5541
|
+
try {
|
|
5542
|
+
return match15(
|
|
5543
|
+
Data29.withSchema(PollDatumSchema, DEFAULT_SCHEMA_OPTIONS).fromCBORHex(
|
|
5544
|
+
datum
|
|
5545
|
+
)
|
|
5546
|
+
).with({ PollManager: P15.select() }, (res) => O11.some(res)).otherwise(() => O11.none);
|
|
5547
|
+
} catch (_) {
|
|
5548
|
+
return O11.none;
|
|
5549
|
+
}
|
|
5550
|
+
}
|
|
5551
|
+
function parsePollManagerOrThrow(datum) {
|
|
5552
|
+
return F13.pipe(
|
|
5553
|
+
parsePollManager(datum),
|
|
5554
|
+
O11.match(() => {
|
|
5555
|
+
throw new Error("Expected a poll manager datum.");
|
|
5556
|
+
}, F13.identity)
|
|
5602
5557
|
);
|
|
5603
|
-
|
|
5604
|
-
|
|
5605
|
-
|
|
5558
|
+
}
|
|
5559
|
+
function parsePollShard(datum) {
|
|
5560
|
+
try {
|
|
5561
|
+
return match15(
|
|
5562
|
+
Data29.withSchema(PollDatumSchema, DEFAULT_SCHEMA_OPTIONS).fromCBORHex(
|
|
5563
|
+
datum
|
|
5564
|
+
)
|
|
5565
|
+
).with({ PollShard: P15.select() }, (res) => O11.some(res)).otherwise(() => O11.none);
|
|
5566
|
+
} catch (_) {
|
|
5567
|
+
return O11.none;
|
|
5568
|
+
}
|
|
5569
|
+
}
|
|
5570
|
+
function parsePollShardOrThrow(datum) {
|
|
5571
|
+
return F13.pipe(
|
|
5572
|
+
parsePollShard(datum),
|
|
5573
|
+
O11.match(() => {
|
|
5574
|
+
throw new Error("Expected a poll shard datum.");
|
|
5575
|
+
}, F13.identity)
|
|
5576
|
+
);
|
|
5577
|
+
}
|
|
5578
|
+
function serialisePollDatum(d) {
|
|
5579
|
+
return Data29.withSchema(PollDatumSchema).toCBORHex(d);
|
|
5580
|
+
}
|
|
5581
|
+
|
|
5582
|
+
// src/contracts/gov/transactions.ts
|
|
5583
|
+
import {
|
|
5584
|
+
addressFromBech32,
|
|
5585
|
+
addressToBech32
|
|
5586
|
+
} from "@3rd-eye-labs/cardano-offchain-common";
|
|
5587
|
+
async function createProposal(proposalContent, treasuryWithdrawal, sysParams, lucid, currentSlot, govOref, allIAssetOrefs) {
|
|
5588
|
+
const network = lucid.config().network;
|
|
5589
|
+
const currentTime = BigInt(slotToUnixTime6(network, currentSlot));
|
|
5590
|
+
const ownAddr = await lucid.wallet().address();
|
|
5591
|
+
const pkh = paymentCredentialOf2(ownAddr);
|
|
5592
|
+
const govRefScriptUtxo = matchSingle(
|
|
5593
|
+
await lucid.utxosByOutRef([
|
|
5594
|
+
fromSystemParamsScriptRef(
|
|
5595
|
+
sysParams.scriptReferences.governanceValidatorRef
|
|
5596
|
+
)
|
|
5597
|
+
]),
|
|
5598
|
+
(_) => new Error("Expected a single Gov Ref Script UTXO")
|
|
5599
|
+
);
|
|
5600
|
+
const pollAuthTokenPolicyRefScriptUtxo = matchSingle(
|
|
5601
|
+
await lucid.utxosByOutRef([
|
|
5602
|
+
fromSystemParamsScriptRef(
|
|
5603
|
+
sysParams.scriptReferences.authTokenPolicies.pollManagerTokenRef
|
|
5604
|
+
)
|
|
5605
|
+
]),
|
|
5606
|
+
(_) => new Error("Expected a single poll auth token policy ref Script UTXO")
|
|
5607
|
+
);
|
|
5608
|
+
const govUtxo = matchSingle(
|
|
5609
|
+
await lucid.utxosByOutRef([govOref]),
|
|
5610
|
+
(_) => new Error("Expected a single Gov UTXO")
|
|
5606
5611
|
);
|
|
5607
5612
|
const govDatum = parseGovDatumOrThrow(getInlineDatumOrThrow(govUtxo));
|
|
5608
5613
|
const votingEndTime = currentTime + govDatum.protocolParams.votingPeriod;
|
|
@@ -5694,9 +5699,7 @@ async function createProposal(proposalContent, treasuryWithdrawal, sysParams, lu
|
|
|
5694
5699
|
)
|
|
5695
5700
|
)
|
|
5696
5701
|
)
|
|
5697
|
-
).validFrom(Number(currentTime) - ONE_SECOND).validTo(
|
|
5698
|
-
Number(currentTime) + Number(sysParams.govParams.gBiasTime) - ONE_SECOND
|
|
5699
|
-
).addSigner(ownAddr),
|
|
5702
|
+
).validFrom(Number(currentTime) - ONE_SECOND).validTo(Number(currentTime + sysParams.govParams.gBiasTime) - ONE_SECOND).addSigner(ownAddr),
|
|
5700
5703
|
newPollId
|
|
5701
5704
|
];
|
|
5702
5705
|
}
|
|
@@ -5738,7 +5741,7 @@ async function createShardsChunks(chunkSize, pollManagerOref, sysParams, current
|
|
|
5738
5741
|
);
|
|
5739
5742
|
const pollNft = fromSystemParamsAsset(sysParams.govParams.pollToken);
|
|
5740
5743
|
const tx = lucid.newTx().validFrom(Number(currentTime) - ONE_SECOND).validTo(
|
|
5741
|
-
Number(currentTime
|
|
5744
|
+
Number(currentTime + sysParams.pollManagerParams.pBiasTime) - ONE_SECOND
|
|
5742
5745
|
).mintAssets(mkAssetsOf6(pollNft, shardsCount), Data30.void()).readFrom([pollAuthTokenPolicyRefScriptUtxo, pollManagerRefScriptUtxo]).collectFrom(
|
|
5743
5746
|
[pollManagerUtxo],
|
|
5744
5747
|
serialisePollManagerRedeemer({ CreateShards: { currentTime } })
|
|
@@ -5918,7 +5921,7 @@ async function mergeShards(pollManagerOref, shardsOutRefs, sysParams, lucid, cur
|
|
|
5918
5921
|
)(shardUtxos);
|
|
5919
5922
|
const pollNft = fromSystemParamsAsset(sysParams.govParams.pollToken);
|
|
5920
5923
|
return lucid.newTx().validFrom(Number(currentTime) - ONE_SECOND).validTo(
|
|
5921
|
-
Number(currentTime
|
|
5924
|
+
Number(currentTime + sysParams.pollManagerParams.pBiasTime) - ONE_SECOND
|
|
5922
5925
|
).readFrom([
|
|
5923
5926
|
pollShardRefScriptUtxo,
|
|
5924
5927
|
pollManagerRefScriptUtxo,
|
|
@@ -6705,9 +6708,7 @@ async function executeProposal(executeOref, govOref, treasuryWithdrawalOref, all
|
|
|
6705
6708
|
);
|
|
6706
6709
|
}).otherwise(() => {
|
|
6707
6710
|
});
|
|
6708
|
-
tx.readFrom([upgradeTokenPolicyRefScriptUtxo, executeRefScriptUtxo]).validFrom(Number(currentTime) - ONE_SECOND).validTo(
|
|
6709
|
-
Number(currentTime) + Number(sysParams.govParams.gBiasTime) - ONE_SECOND
|
|
6710
|
-
).collectFrom([executeUtxo], Data30.void()).mintAssets(
|
|
6711
|
+
tx.readFrom([upgradeTokenPolicyRefScriptUtxo, executeRefScriptUtxo]).validFrom(Number(currentTime) - ONE_SECOND).validTo(Number(currentTime + sysParams.govParams.gBiasTime) - ONE_SECOND).collectFrom([executeUtxo], Data30.void()).mintAssets(
|
|
6711
6712
|
mkAssetsOf6(fromSystemParamsAsset(sysParams.govParams.upgradeToken), -1n),
|
|
6712
6713
|
Data30.void()
|
|
6713
6714
|
).addSigner(ownAddr).setMinFee(922932n);
|
|
@@ -8291,7 +8292,8 @@ import {
|
|
|
8291
8292
|
readonlyArray as RA3,
|
|
8292
8293
|
array as A9,
|
|
8293
8294
|
function as F16,
|
|
8294
|
-
option as O14
|
|
8295
|
+
option as O14,
|
|
8296
|
+
ord as Ord4
|
|
8295
8297
|
} from "fp-ts";
|
|
8296
8298
|
import { match as match18, P as P18 } from "ts-pattern";
|
|
8297
8299
|
import {
|
|
@@ -8302,7 +8304,7 @@ import {
|
|
|
8302
8304
|
mkAssetsOf as mkAssetsOf9
|
|
8303
8305
|
} from "@3rd-eye-labs/cardano-offchain-common";
|
|
8304
8306
|
var MIN_ROB_COLLATERAL_AMT = 3000000n;
|
|
8305
|
-
function
|
|
8307
|
+
function robCollateralAmtToSpend(utxo, datum) {
|
|
8306
8308
|
return match18(datum.orderType).returnType().with({ BuyIAssetOrder: P18.select() }, (content) => {
|
|
8307
8309
|
if (isSameAssetClass3(adaAssetClass4, content.collateralAsset)) {
|
|
8308
8310
|
return zeroNegatives(
|
|
@@ -8311,24 +8313,61 @@ function robAmountToSpend(utxo, datum, sysParams) {
|
|
|
8311
8313
|
} else {
|
|
8312
8314
|
return assetClassValueOf7(utxo.assets, content.collateralAsset);
|
|
8313
8315
|
}
|
|
8314
|
-
}).
|
|
8316
|
+
}).otherwise(() => {
|
|
8317
|
+
throw new Error("Collateral to spend is relevant only for Buy orders.");
|
|
8318
|
+
});
|
|
8319
|
+
}
|
|
8320
|
+
function robIAssetAmtToSpend(utxo, datum, iassetCurrencySymbol) {
|
|
8321
|
+
return match18(datum.orderType).returnType().with({ SellIAssetOrder: P18.any }, (_) => {
|
|
8315
8322
|
return assetClassValueOf7(utxo.assets, {
|
|
8316
|
-
currencySymbol: fromHex12(
|
|
8317
|
-
sysParams.cdpParams.cdpAssetSymbol.unCurrencySymbol
|
|
8318
|
-
),
|
|
8323
|
+
currencySymbol: fromHex12(iassetCurrencySymbol.unCurrencySymbol),
|
|
8319
8324
|
tokenName: datum.iasset
|
|
8320
8325
|
});
|
|
8321
|
-
}).
|
|
8326
|
+
}).otherwise(() => {
|
|
8327
|
+
throw new Error("IAssets to spend is relevant only for Sell orders.");
|
|
8328
|
+
});
|
|
8322
8329
|
}
|
|
8323
|
-
function
|
|
8324
|
-
return
|
|
8330
|
+
function robAmtToSpend(utxo, datum, iassetCurrencySymbol) {
|
|
8331
|
+
return match18(datum.orderType).with({ BuyIAssetOrder: P18.any }, () => robCollateralAmtToSpend(utxo, datum)).with(
|
|
8332
|
+
{ SellIAssetOrder: P18.any },
|
|
8333
|
+
() => robIAssetAmtToSpend(utxo, datum, iassetCurrencySymbol)
|
|
8334
|
+
).exhaustive();
|
|
8335
|
+
}
|
|
8336
|
+
function robBuyOrderSummary(utxo, datum, oraclePrice) {
|
|
8337
|
+
const redeemable = robCollateralAmtToSpend(utxo, datum);
|
|
8338
|
+
const payoutAmt = iassetValueOfCollateral(redeemable, oraclePrice);
|
|
8339
|
+
return {
|
|
8340
|
+
redeemableCollateral: redeemable,
|
|
8341
|
+
payoutIAsset: payoutAmt
|
|
8342
|
+
};
|
|
8343
|
+
}
|
|
8344
|
+
function isBuyOrderFullyRedeemed(utxo, datum, oraclePrice) {
|
|
8345
|
+
const summary = robBuyOrderSummary(utxo, datum, oraclePrice);
|
|
8346
|
+
return summary.redeemableCollateral <= 0n || summary.payoutIAsset <= 0n;
|
|
8347
|
+
}
|
|
8348
|
+
function isFullyRedeemed(utxo, datum, iassetCurrencySymbol) {
|
|
8349
|
+
return match18(datum.orderType).returnType().with(
|
|
8350
|
+
{ BuyIAssetOrder: P18.select() },
|
|
8351
|
+
(content) => isBuyOrderFullyRedeemed(utxo, datum, content.maxPrice)
|
|
8352
|
+
).with({ SellIAssetOrder: P18.select() }, (content) => {
|
|
8353
|
+
const iassetToSpend = robIAssetAmtToSpend(
|
|
8354
|
+
utxo,
|
|
8355
|
+
datum,
|
|
8356
|
+
iassetCurrencySymbol
|
|
8357
|
+
);
|
|
8358
|
+
const payoutAmts = content.allowedCollateralAssets.map(
|
|
8359
|
+
(c) => rationalFloor(rationalMul(rationalFromInt(iassetToSpend), c[1]))
|
|
8360
|
+
);
|
|
8361
|
+
return iassetToSpend <= 0n || // When for every allowed collateral asset the payout would be 0
|
|
8362
|
+
payoutAmts.every((amt) => amt <= 0n);
|
|
8363
|
+
}).exhaustive();
|
|
8325
8364
|
}
|
|
8326
8365
|
function buildRedemptionsTx(redemptions, iasset, collateralAsset, price, redemptionReimbursementRatio, sysParams, tx, txOutputsBeforeCount, collateralAssetRefInputIdx, iassetRefInputIdx, oracleIdx) {
|
|
8327
8366
|
return F16.pipe(
|
|
8328
8367
|
redemptions,
|
|
8329
8368
|
A9.reduceWithIndex(
|
|
8330
8369
|
tx,
|
|
8331
|
-
(idx, acc, [robUtxo,
|
|
8370
|
+
(idx, acc, [robUtxo, payoutAmt]) => {
|
|
8332
8371
|
const robDatum = parseRobDatumOrThrow(getInlineDatumOrThrow(robUtxo));
|
|
8333
8372
|
if (toHex12(robDatum.iasset) !== toHex12(iasset)) {
|
|
8334
8373
|
throw new Error("Only same iAsset");
|
|
@@ -8339,7 +8378,7 @@ function buildRedemptionsTx(redemptions, iasset, collateralAsset, price, redempt
|
|
|
8339
8378
|
if (!isSameAssetClass3(content.collateralAsset, collateralAsset)) {
|
|
8340
8379
|
throw new Error("Only same collateral asset");
|
|
8341
8380
|
}
|
|
8342
|
-
const payoutIAssetAmt =
|
|
8381
|
+
const payoutIAssetAmt = payoutAmt;
|
|
8343
8382
|
const reimburstmentIAsset = calculateFeeFromRatio(
|
|
8344
8383
|
redemptionReimbursementRatio,
|
|
8345
8384
|
payoutIAssetAmt
|
|
@@ -8374,16 +8413,14 @@ function buildRedemptionsTx(redemptions, iasset, collateralAsset, price, redempt
|
|
|
8374
8413
|
throw new Error("Doesn't allow required collateral asset.");
|
|
8375
8414
|
})
|
|
8376
8415
|
);
|
|
8377
|
-
const payoutCollateralAmt =
|
|
8416
|
+
const payoutCollateralAmt = payoutAmt;
|
|
8378
8417
|
const reimbursementCollateral = calculateFeeFromRatio(
|
|
8379
8418
|
redemptionReimbursementRatio,
|
|
8380
8419
|
payoutCollateralAmt
|
|
8381
8420
|
);
|
|
8382
|
-
const redeemedIAssetAmt =
|
|
8383
|
-
|
|
8384
|
-
|
|
8385
|
-
price
|
|
8386
|
-
)
|
|
8421
|
+
const redeemedIAssetAmt = iassetValueOfCollateral(
|
|
8422
|
+
payoutCollateralAmt - reimbursementCollateral,
|
|
8423
|
+
price
|
|
8387
8424
|
);
|
|
8388
8425
|
const resultVal = addAssets12(
|
|
8389
8426
|
robUtxo.assets,
|
|
@@ -8401,7 +8438,9 @@ function buildRedemptionsTx(redemptions, iasset, collateralAsset, price, redempt
|
|
|
8401
8438
|
return [resultVal, BigInt(allowedAssetIdx)];
|
|
8402
8439
|
}).exhaustive();
|
|
8403
8440
|
if (lovelacesAmt3(robOutputVal) < MIN_ROB_COLLATERAL_AMT) {
|
|
8404
|
-
throw new Error(
|
|
8441
|
+
throw new Error(
|
|
8442
|
+
"Redeeming more than available or selected ROB was incorrectly initialised."
|
|
8443
|
+
);
|
|
8405
8444
|
}
|
|
8406
8445
|
return acc.collectFrom([robUtxo], {
|
|
8407
8446
|
kind: "self",
|
|
@@ -8412,7 +8451,7 @@ function buildRedemptionsTx(redemptions, iasset, collateralAsset, price, redempt
|
|
|
8412
8451
|
iassetRefInputIdx,
|
|
8413
8452
|
continuingOutputIdx: txOutputsBeforeCount + BigInt(idx),
|
|
8414
8453
|
sellOrderAllowedAssetsIdx,
|
|
8415
|
-
priceOracleIdx: oracleIdx
|
|
8454
|
+
priceOracleIdx: oracleIdx
|
|
8416
8455
|
}
|
|
8417
8456
|
})
|
|
8418
8457
|
}).pay.ToContract(
|
|
@@ -8433,6 +8472,85 @@ function buildRedemptionsTx(redemptions, iasset, collateralAsset, price, redempt
|
|
|
8433
8472
|
)
|
|
8434
8473
|
);
|
|
8435
8474
|
}
|
|
8475
|
+
function calculateTotalCollateralForRedemption(iasset, collateralAsset, iassetPrice, allRobs, maxRobsInTx) {
|
|
8476
|
+
return F16.pipe(
|
|
8477
|
+
allRobs,
|
|
8478
|
+
A9.filterMap(([utxo, datum]) => {
|
|
8479
|
+
const isCorrectOrder = match18(datum.orderType).returnType().with(
|
|
8480
|
+
{ BuyIAssetOrder: P18.select() },
|
|
8481
|
+
(content) => isSameAssetClass3(content.collateralAsset, collateralAsset) && rationalToFloat(content.maxPrice) >= rationalToFloat(iassetPrice) && !isBuyOrderFullyRedeemed(utxo, datum, iassetPrice)
|
|
8482
|
+
).otherwise(() => false);
|
|
8483
|
+
if (toHex12(datum.iasset) !== toHex12(iasset) || !isCorrectOrder) {
|
|
8484
|
+
return O14.none;
|
|
8485
|
+
}
|
|
8486
|
+
const collateralToSpend = robCollateralAmtToSpend(utxo, datum);
|
|
8487
|
+
return O14.some(collateralToSpend);
|
|
8488
|
+
}),
|
|
8489
|
+
// From largest to smallest
|
|
8490
|
+
A9.sort(Ord4.reverse(BigIntOrd)),
|
|
8491
|
+
// We can fit only this number of redemptions with CDP open into a single Tx.
|
|
8492
|
+
A9.takeLeft(maxRobsInTx),
|
|
8493
|
+
sum
|
|
8494
|
+
);
|
|
8495
|
+
}
|
|
8496
|
+
function randomRobsSubsetSatisfyingTargetCollateral(iasset, collateralAsset, targetCollateralToSpend, iassetPrice, allLrps, maxLrpsInTx, randomiseFn = shuffle) {
|
|
8497
|
+
if (targetCollateralToSpend <= 0n || iassetValueOfCollateral(targetCollateralToSpend, iassetPrice) <= 0n) {
|
|
8498
|
+
throw new Error("Must redeem and payout more than 0.");
|
|
8499
|
+
}
|
|
8500
|
+
const shuffled = randomiseFn(
|
|
8501
|
+
F16.pipe(
|
|
8502
|
+
allLrps,
|
|
8503
|
+
A9.filter(
|
|
8504
|
+
([utxo, datum]) => toHex12(datum.iasset) === toHex12(iasset) && match18(datum.orderType).with(
|
|
8505
|
+
{ BuyIAssetOrder: P18.select() },
|
|
8506
|
+
(content) => isSameAssetClass3(collateralAsset, content.collateralAsset) && rationalToFloat(content.maxPrice) >= rationalToFloat(iassetPrice)
|
|
8507
|
+
).otherwise(() => false) && !isBuyOrderFullyRedeemed(utxo, datum, iassetPrice)
|
|
8508
|
+
)
|
|
8509
|
+
)
|
|
8510
|
+
);
|
|
8511
|
+
let result = [];
|
|
8512
|
+
let runningSum = 0n;
|
|
8513
|
+
for (let i = 0; i < shuffled.length; i++) {
|
|
8514
|
+
const element = shuffled[i];
|
|
8515
|
+
const lovelacesToSpend = robCollateralAmtToSpend(element[0], element[1]);
|
|
8516
|
+
const remainingToRedeem = targetCollateralToSpend - runningSum;
|
|
8517
|
+
const remainingToPayout = iassetValueOfCollateral(
|
|
8518
|
+
remainingToRedeem,
|
|
8519
|
+
iassetPrice
|
|
8520
|
+
);
|
|
8521
|
+
if (result.length > 0 && remainingToPayout <= 0n) {
|
|
8522
|
+
const last = result[result.length - 1];
|
|
8523
|
+
const lastSummary = robBuyOrderSummary(last[0], last[1], iassetPrice);
|
|
8524
|
+
if (lastSummary.redeemableCollateral < lovelacesToSpend) {
|
|
8525
|
+
result.pop();
|
|
8526
|
+
runningSum -= lastSummary.redeemableCollateral;
|
|
8527
|
+
} else {
|
|
8528
|
+
continue;
|
|
8529
|
+
}
|
|
8530
|
+
}
|
|
8531
|
+
result = insertSorted(
|
|
8532
|
+
result,
|
|
8533
|
+
element,
|
|
8534
|
+
Ord4.contramap(
|
|
8535
|
+
([utxo, dat]) => robCollateralAmtToSpend(utxo, dat)
|
|
8536
|
+
// From highest to lowest
|
|
8537
|
+
)(Ord4.reverse(BigIntOrd))
|
|
8538
|
+
);
|
|
8539
|
+
runningSum += lovelacesToSpend;
|
|
8540
|
+
if (result.length > maxLrpsInTx) {
|
|
8541
|
+
const popped = result.pop();
|
|
8542
|
+
runningSum -= robCollateralAmtToSpend(popped[0], popped[1]);
|
|
8543
|
+
}
|
|
8544
|
+
if (runningSum >= targetCollateralToSpend) {
|
|
8545
|
+
return result;
|
|
8546
|
+
}
|
|
8547
|
+
}
|
|
8548
|
+
const remainingToSpend = targetCollateralToSpend - runningSum;
|
|
8549
|
+
if (remainingToSpend > 0n && iassetValueOfCollateral(remainingToSpend, iassetPrice) > 0n) {
|
|
8550
|
+
throw new Error("Couldn't achieve target lovelaces");
|
|
8551
|
+
}
|
|
8552
|
+
return result;
|
|
8553
|
+
}
|
|
8436
8554
|
|
|
8437
8555
|
// src/contracts/rob/types.ts
|
|
8438
8556
|
import { Data as Data44 } from "@lucid-evolution/lucid";
|
|
@@ -8590,7 +8708,7 @@ async function redeemRob(redemptionRobsData, priceOracleOutRef, iassetOutRef, co
|
|
|
8590
8708
|
0n,
|
|
8591
8709
|
refInputIdxs[2],
|
|
8592
8710
|
refInputIdxs[1],
|
|
8593
|
-
priceOracleOutRef !== void 0 ? refInputIdxs[3] :
|
|
8711
|
+
priceOracleOutRef !== void 0 ? { OracleRefInputIdx: refInputIdxs[3] } : "OracleVoid"
|
|
8594
8712
|
);
|
|
8595
8713
|
tx.readFrom(allRefInputs);
|
|
8596
8714
|
return tx;
|
|
@@ -8607,12 +8725,12 @@ async function adjustRob(lucid, robOutRef, adjustmentAmt, newLimitPrice, sysPara
|
|
|
8607
8725
|
(_) => new Error("Expected a single ROB UTXO.")
|
|
8608
8726
|
);
|
|
8609
8727
|
const robDatum = parseRobDatumOrThrow(getInlineDatumOrThrow(robUtxo));
|
|
8610
|
-
if (adjustmentAmt === 0n && isFullyRedeemed(robUtxo, robDatum, sysParams)) {
|
|
8728
|
+
if (adjustmentAmt === 0n && isFullyRedeemed(robUtxo, robDatum, sysParams.cdpParams.cdpAssetSymbol)) {
|
|
8611
8729
|
throw new Error(
|
|
8612
8730
|
"When there's no more lovelaces to spend, use close instead of claim."
|
|
8613
8731
|
);
|
|
8614
8732
|
}
|
|
8615
|
-
if (adjustmentAmt < 0 &&
|
|
8733
|
+
if (adjustmentAmt < 0 && robAmtToSpend(robUtxo, robDatum, sysParams.cdpParams.cdpAssetSymbol) <= -adjustmentAmt) {
|
|
8616
8734
|
throw new Error(
|
|
8617
8735
|
"Can't adjust negatively by more than available. Also, for adjusting by exactly the amount deposited, a close action should be used instead."
|
|
8618
8736
|
);
|
|
@@ -8733,13 +8851,451 @@ var mkRobValidatorFromSP = (params) => {
|
|
|
8733
8851
|
};
|
|
8734
8852
|
};
|
|
8735
8853
|
|
|
8736
|
-
// src/contracts/
|
|
8854
|
+
// src/contracts/rob-leverage/transactions.ts
|
|
8737
8855
|
import {
|
|
8738
8856
|
addAssets as addAssets14,
|
|
8857
|
+
slotToUnixTime as slotToUnixTime10,
|
|
8739
8858
|
Data as Data45,
|
|
8740
8859
|
fromHex as fromHex14,
|
|
8860
|
+
getInputIndices as getInputIndices5
|
|
8861
|
+
} from "@lucid-evolution/lucid";
|
|
8862
|
+
|
|
8863
|
+
// src/contracts/rob-leverage/helpers.ts
|
|
8864
|
+
import { array as A11, function as F18 } from "fp-ts";
|
|
8865
|
+
import { Decimal } from "decimal.js";
|
|
8866
|
+
var MAX_REDEMPTIONS_WITH_CDP_OPEN = 4;
|
|
8867
|
+
function approximateLeverageRedemptions(baseCollateral, targetLeverage, redemptionReimbursementRatio, debtMintingFeeRatio) {
|
|
8868
|
+
const debtMintingFeeRatioDecimal = Decimal(debtMintingFeeRatio.numerator).div(
|
|
8869
|
+
debtMintingFeeRatio.denominator
|
|
8870
|
+
);
|
|
8871
|
+
const redemptionReimbursementRatioDecimal = Decimal(
|
|
8872
|
+
redemptionReimbursementRatio.numerator
|
|
8873
|
+
).div(redemptionReimbursementRatio.denominator);
|
|
8874
|
+
const totalFeeRatio = debtMintingFeeRatioDecimal.add(
|
|
8875
|
+
redemptionReimbursementRatioDecimal
|
|
8876
|
+
);
|
|
8877
|
+
const bExFees = Decimal(baseCollateral).mul(targetLeverage).minus(baseCollateral).floor();
|
|
8878
|
+
const b = bExFees.div(Decimal(1).minus(totalFeeRatio)).floor();
|
|
8879
|
+
const collateralRatio = {
|
|
8880
|
+
numerator: fromDecimal(Decimal(baseCollateral).add(bExFees)),
|
|
8881
|
+
denominator: fromDecimal(b)
|
|
8882
|
+
};
|
|
8883
|
+
return {
|
|
8884
|
+
leverage: targetLeverage,
|
|
8885
|
+
collateralRatio,
|
|
8886
|
+
redeemedCollateral: fromDecimal(bExFees)
|
|
8887
|
+
};
|
|
8888
|
+
}
|
|
8889
|
+
function summarizeActualLeverageRedemptions(lovelacesForRedemptionWithReimbursement, redemptionReimbursementRatio, iassetPrice, redemptionLrps) {
|
|
8890
|
+
const priceDecimal = Decimal(iassetPrice.numerator).div(
|
|
8891
|
+
iassetPrice.denominator
|
|
8892
|
+
);
|
|
8893
|
+
const reimbRatio = Decimal(redemptionReimbursementRatio.numerator).div(
|
|
8894
|
+
redemptionReimbursementRatio.denominator
|
|
8895
|
+
);
|
|
8896
|
+
const redemptionDetails = F18.pipe(
|
|
8897
|
+
redemptionLrps,
|
|
8898
|
+
A11.reduce(
|
|
8899
|
+
{
|
|
8900
|
+
remainingCollateralToSpend: lovelacesForRedemptionWithReimbursement,
|
|
8901
|
+
redemptions: []
|
|
8902
|
+
},
|
|
8903
|
+
(acc, lrp) => {
|
|
8904
|
+
if (acc.remainingCollateralToSpend <= 0n || iassetValueOfCollateral(
|
|
8905
|
+
acc.remainingCollateralToSpend,
|
|
8906
|
+
iassetPrice
|
|
8907
|
+
) <= 0n) {
|
|
8908
|
+
return acc;
|
|
8909
|
+
}
|
|
8910
|
+
const collateralToSpend = robCollateralAmtToSpend(lrp[0], lrp[1]);
|
|
8911
|
+
if (collateralToSpend === 0n) {
|
|
8912
|
+
return acc;
|
|
8913
|
+
}
|
|
8914
|
+
const newRemainingCollateral = bigintMax(
|
|
8915
|
+
acc.remainingCollateralToSpend - collateralToSpend,
|
|
8916
|
+
0n
|
|
8917
|
+
);
|
|
8918
|
+
const collateralToSpendInitial = acc.remainingCollateralToSpend - newRemainingCollateral;
|
|
8919
|
+
const finalPayoutIAssets = fromDecimal(
|
|
8920
|
+
Decimal(collateralToSpendInitial).div(priceDecimal).floor().div(Decimal(1).minus(reimbRatio)).floor()
|
|
8921
|
+
);
|
|
8922
|
+
const feeIAssetAmt = calculateFeeFromRatio(
|
|
8923
|
+
redemptionReimbursementRatio,
|
|
8924
|
+
finalPayoutIAssets
|
|
8925
|
+
);
|
|
8926
|
+
const finalCollateralToSpend = rationalFloor(
|
|
8927
|
+
rationalMul(
|
|
8928
|
+
rationalFromInt(finalPayoutIAssets - feeIAssetAmt),
|
|
8929
|
+
iassetPrice
|
|
8930
|
+
)
|
|
8931
|
+
);
|
|
8932
|
+
return {
|
|
8933
|
+
remainingCollateralToSpend: acc.remainingCollateralToSpend - finalCollateralToSpend,
|
|
8934
|
+
redemptions: [
|
|
8935
|
+
...acc.redemptions,
|
|
8936
|
+
{
|
|
8937
|
+
utxo: lrp[0],
|
|
8938
|
+
iassetsPayoutAmt: finalPayoutIAssets,
|
|
8939
|
+
redeemedCollateral: finalCollateralToSpend,
|
|
8940
|
+
reimbursementIAssetAmt: feeIAssetAmt
|
|
8941
|
+
}
|
|
8942
|
+
]
|
|
8943
|
+
};
|
|
8944
|
+
}
|
|
8945
|
+
)
|
|
8946
|
+
);
|
|
8947
|
+
const res = F18.pipe(
|
|
8948
|
+
redemptionDetails.redemptions,
|
|
8949
|
+
A11.reduce(
|
|
8950
|
+
{
|
|
8951
|
+
redeemedCollateral: 0n,
|
|
8952
|
+
payoutIAssets: 0n,
|
|
8953
|
+
reimbursementIAssets: 0n
|
|
8954
|
+
},
|
|
8955
|
+
(acc, details) => {
|
|
8956
|
+
return {
|
|
8957
|
+
redeemedCollateral: acc.redeemedCollateral + details.redeemedCollateral,
|
|
8958
|
+
reimbursementIAssets: acc.reimbursementIAssets + details.reimbursementIAssetAmt,
|
|
8959
|
+
payoutIAssets: acc.payoutIAssets + details.iassetsPayoutAmt
|
|
8960
|
+
};
|
|
8961
|
+
}
|
|
8962
|
+
)
|
|
8963
|
+
);
|
|
8964
|
+
return {
|
|
8965
|
+
redemptions: redemptionDetails.redemptions,
|
|
8966
|
+
totalRedeemedCollateral: res.redeemedCollateral,
|
|
8967
|
+
totalReimbursedIAsset: res.reimbursementIAssets,
|
|
8968
|
+
totalIAssetPayout: res.payoutIAssets
|
|
8969
|
+
};
|
|
8970
|
+
}
|
|
8971
|
+
function calculateCollateralRatioFromLeverage(iasset, collateralAsset, leverage, baseCollateral, iassetPrice, debtMintingFeePercentage, redemptionReimbursementPercentage, allLrps) {
|
|
8972
|
+
const debtMintingFeeRatioDecimal = Decimal(
|
|
8973
|
+
debtMintingFeePercentage.getOnChainInt
|
|
8974
|
+
).div(OCD_DECIMAL_UNIT).div(100);
|
|
8975
|
+
const redemptionReimbursementRatioDecimal = Decimal(
|
|
8976
|
+
redemptionReimbursementPercentage.getOnChainInt
|
|
8977
|
+
).div(OCD_DECIMAL_UNIT).div(100);
|
|
8978
|
+
const totalFeeRatio = debtMintingFeeRatioDecimal.add(
|
|
8979
|
+
redemptionReimbursementRatioDecimal
|
|
8980
|
+
);
|
|
8981
|
+
const maxAvailableCollateralForRedemption = calculateTotalCollateralForRedemption(
|
|
8982
|
+
iasset,
|
|
8983
|
+
collateralAsset,
|
|
8984
|
+
iassetPrice,
|
|
8985
|
+
allLrps,
|
|
8986
|
+
MAX_REDEMPTIONS_WITH_CDP_OPEN
|
|
8987
|
+
);
|
|
8988
|
+
if (leverage <= 1 || baseCollateral <= 0n || maxAvailableCollateralForRedemption <= 0n) {
|
|
8989
|
+
return void 0;
|
|
8990
|
+
}
|
|
8991
|
+
const bExFees = Decimal(baseCollateral).mul(leverage).minus(baseCollateral).floor();
|
|
8992
|
+
const b = bExFees.div(Decimal(1).minus(totalFeeRatio)).floor();
|
|
8993
|
+
const cappedB = bigintMin(
|
|
8994
|
+
maxAvailableCollateralForRedemption,
|
|
8995
|
+
fromDecimal(b)
|
|
8996
|
+
);
|
|
8997
|
+
const cappedBExFees = Decimal(cappedB).mul(Decimal(1).minus(totalFeeRatio)).floor();
|
|
8998
|
+
const collateralRatio = Decimal(
|
|
8999
|
+
Decimal(baseCollateral).add(cappedBExFees)
|
|
9000
|
+
).div(cappedB);
|
|
9001
|
+
return {
|
|
9002
|
+
getOnChainInt: fromDecimal(
|
|
9003
|
+
collateralRatio.mul(100n * OCD_DECIMAL_UNIT).floor()
|
|
9004
|
+
)
|
|
9005
|
+
};
|
|
9006
|
+
}
|
|
9007
|
+
function calculateLeverageFromCollateralRatio(iasset, collateralAsset, collateralRatio, baseCollateral, iassetPrice, debtMintingFeeRatio, redemptionReimbursementRatio, allLrps) {
|
|
9008
|
+
const debtMintingFeeRatioDecimal = Decimal(debtMintingFeeRatio.numerator).div(
|
|
9009
|
+
debtMintingFeeRatio.denominator
|
|
9010
|
+
);
|
|
9011
|
+
const redemptionReimbursementRatioDecimal = Decimal(
|
|
9012
|
+
redemptionReimbursementRatio.numerator
|
|
9013
|
+
).div(redemptionReimbursementRatio.denominator);
|
|
9014
|
+
const totalFeeRatio = debtMintingFeeRatioDecimal.add(
|
|
9015
|
+
redemptionReimbursementRatioDecimal
|
|
9016
|
+
);
|
|
9017
|
+
const collateralRatioDecimal = Decimal(collateralRatio.numerator).div(
|
|
9018
|
+
collateralRatio.denominator
|
|
9019
|
+
);
|
|
9020
|
+
const maxAvailableCollateralForRedemption = calculateTotalCollateralForRedemption(
|
|
9021
|
+
iasset,
|
|
9022
|
+
collateralAsset,
|
|
9023
|
+
iassetPrice,
|
|
9024
|
+
allLrps,
|
|
9025
|
+
MAX_REDEMPTIONS_WITH_CDP_OPEN
|
|
9026
|
+
);
|
|
9027
|
+
if (collateralRatioDecimal.toNumber() <= 1 || baseCollateral <= 0n || maxAvailableCollateralForRedemption <= 0n) {
|
|
9028
|
+
return void 0;
|
|
9029
|
+
}
|
|
9030
|
+
const theoreticalMaxLeverage = Decimal(Decimal(1).minus(totalFeeRatio)).div(collateralRatioDecimal.minus(1).add(totalFeeRatio)).add(1);
|
|
9031
|
+
const bExFees = theoreticalMaxLeverage.mul(baseCollateral).minus(baseCollateral).floor();
|
|
9032
|
+
const b = bExFees.div(Decimal(1).minus(totalFeeRatio)).floor();
|
|
9033
|
+
const cappedB = bigintMin(
|
|
9034
|
+
maxAvailableCollateralForRedemption,
|
|
9035
|
+
fromDecimal(b)
|
|
9036
|
+
);
|
|
9037
|
+
const cappedBExFees = Decimal(cappedB).mul(Decimal(1).minus(totalFeeRatio)).floor();
|
|
9038
|
+
return Decimal(baseCollateral).add(cappedBExFees).div(baseCollateral).toNumber();
|
|
9039
|
+
}
|
|
9040
|
+
|
|
9041
|
+
// src/contracts/rob-leverage/transactions.ts
|
|
9042
|
+
import { mkAssetsOf as mkAssetsOf11 } from "@3rd-eye-labs/cardano-offchain-common";
|
|
9043
|
+
import Decimal2 from "decimal.js";
|
|
9044
|
+
async function leverageCdpWithRob(leverage, baseCollateral, priceOracleOutRef, iassetOutRef, collateralAssetOutRef, cdpCreatorOref, interestOracleOref, treasuryOref, sysParams, lucid, allRobs, currentSlot, pythMessage = void 0, pythStateOref = void 0) {
|
|
9045
|
+
const network = lucid.config().network;
|
|
9046
|
+
const currentTime = BigInt(slotToUnixTime10(network, currentSlot));
|
|
9047
|
+
const [pkh, skh] = await addrDetails(lucid);
|
|
9048
|
+
const robScriptRefUtxo = matchSingle(
|
|
9049
|
+
await lucid.utxosByOutRef([
|
|
9050
|
+
fromSystemParamsScriptRef(sysParams.scriptReferences.robValidatorRef)
|
|
9051
|
+
]),
|
|
9052
|
+
(_2) => new Error("Expected a single ROB Ref Script UTXO")
|
|
9053
|
+
);
|
|
9054
|
+
const cdpCreatorRefScriptUtxo = matchSingle(
|
|
9055
|
+
await lucid.utxosByOutRef([
|
|
9056
|
+
fromSystemParamsScriptRef(
|
|
9057
|
+
sysParams.scriptReferences.cdpCreatorValidatorRef
|
|
9058
|
+
)
|
|
9059
|
+
]),
|
|
9060
|
+
(_2) => new Error("Expected a single cdp creator Ref Script UTXO")
|
|
9061
|
+
);
|
|
9062
|
+
const cdpAuthTokenPolicyRefScriptUtxo = matchSingle(
|
|
9063
|
+
await lucid.utxosByOutRef([
|
|
9064
|
+
fromSystemParamsScriptRef(
|
|
9065
|
+
sysParams.scriptReferences.authTokenPolicies.cdpAuthTokenRef
|
|
9066
|
+
)
|
|
9067
|
+
]),
|
|
9068
|
+
(_2) => new Error("Expected a single cdp auth token policy Ref Script UTXO")
|
|
9069
|
+
);
|
|
9070
|
+
const iAssetTokenPolicyRefScriptUtxo = matchSingle(
|
|
9071
|
+
await lucid.utxosByOutRef([
|
|
9072
|
+
fromSystemParamsScriptRef(
|
|
9073
|
+
sysParams.scriptReferences.iAssetTokenPolicyRef
|
|
9074
|
+
)
|
|
9075
|
+
]),
|
|
9076
|
+
(_2) => new Error("Expected a single iasset token policy Ref Script UTXO")
|
|
9077
|
+
);
|
|
9078
|
+
const cdpCreatorUtxo = matchSingle(
|
|
9079
|
+
await lucid.utxosByOutRef([cdpCreatorOref]),
|
|
9080
|
+
(_2) => new Error("Expected a single CDP creator UTXO")
|
|
9081
|
+
);
|
|
9082
|
+
const interestOracleUtxo = matchSingle(
|
|
9083
|
+
await lucid.utxosByOutRef([interestOracleOref]),
|
|
9084
|
+
(_2) => new Error("Expected a single interest oracle UTXO")
|
|
9085
|
+
);
|
|
9086
|
+
const interestOracleDatum = parseInterestOracleDatum(
|
|
9087
|
+
getInlineDatumOrThrow(interestOracleUtxo)
|
|
9088
|
+
);
|
|
9089
|
+
const iassetUtxo = matchSingle(
|
|
9090
|
+
await lucid.utxosByOutRef([iassetOutRef]),
|
|
9091
|
+
(_2) => new Error("Expected a single IAsset UTXO")
|
|
9092
|
+
);
|
|
9093
|
+
const iassetDatum = parseIAssetDatumOrThrow(
|
|
9094
|
+
getInlineDatumOrThrow(iassetUtxo)
|
|
9095
|
+
);
|
|
9096
|
+
const collateralAssetUtxo = matchSingle(
|
|
9097
|
+
await lucid.utxosByOutRef([collateralAssetOutRef]),
|
|
9098
|
+
(_2) => new Error("Expected a single collateral asset UTXO")
|
|
9099
|
+
);
|
|
9100
|
+
const collateralAssetDatum = parseCollateralAssetDatumOrThrow(
|
|
9101
|
+
getInlineDatumOrThrow(collateralAssetUtxo)
|
|
9102
|
+
);
|
|
9103
|
+
const [price, _] = await retrieveAdjustedPrice(
|
|
9104
|
+
iassetDatum.assetName,
|
|
9105
|
+
collateralAssetDatum.collateralAsset,
|
|
9106
|
+
collateralAssetDatum.priceInfo,
|
|
9107
|
+
collateralAssetDatum.extraDecimals,
|
|
9108
|
+
priceOracleOutRef,
|
|
9109
|
+
pythMessage,
|
|
9110
|
+
sysParams.pythConfig,
|
|
9111
|
+
lucid
|
|
9112
|
+
);
|
|
9113
|
+
const maxLeverage = calculateLeverageFromCollateralRatio(
|
|
9114
|
+
collateralAssetDatum.iasset,
|
|
9115
|
+
collateralAssetDatum.collateralAsset,
|
|
9116
|
+
collateralAssetDatum.maintenanceRatio,
|
|
9117
|
+
baseCollateral,
|
|
9118
|
+
price,
|
|
9119
|
+
iassetDatum.debtMintingFeeRatio,
|
|
9120
|
+
iassetDatum.redemptionReimbursementRatio,
|
|
9121
|
+
allRobs
|
|
9122
|
+
);
|
|
9123
|
+
if (!maxLeverage) {
|
|
9124
|
+
throw new Error("Can't calculate max leverage with those parameters.");
|
|
9125
|
+
}
|
|
9126
|
+
const leverageSummary = approximateLeverageRedemptions(
|
|
9127
|
+
baseCollateral,
|
|
9128
|
+
leverage,
|
|
9129
|
+
iassetDatum.redemptionProcessingFeeRatio,
|
|
9130
|
+
iassetDatum.debtMintingFeeRatio
|
|
9131
|
+
);
|
|
9132
|
+
if (maxLeverage < leverageSummary.leverage) {
|
|
9133
|
+
throw new Error("Can't use more leverage than max.");
|
|
9134
|
+
}
|
|
9135
|
+
if (rationalToFloat(leverageSummary.collateralRatio) < rationalToFloat(collateralAssetDatum.maintenanceRatio)) {
|
|
9136
|
+
throw new Error(
|
|
9137
|
+
"Can't have collateral ratio smaller than maintenance ratio"
|
|
9138
|
+
);
|
|
9139
|
+
}
|
|
9140
|
+
const redemptionDetails = summarizeActualLeverageRedemptions(
|
|
9141
|
+
leverageSummary.redeemedCollateral,
|
|
9142
|
+
iassetDatum.redemptionReimbursementRatio,
|
|
9143
|
+
price,
|
|
9144
|
+
randomRobsSubsetSatisfyingTargetCollateral(
|
|
9145
|
+
iassetDatum.assetName,
|
|
9146
|
+
collateralAssetDatum.collateralAsset,
|
|
9147
|
+
leverageSummary.redeemedCollateral,
|
|
9148
|
+
price,
|
|
9149
|
+
allRobs,
|
|
9150
|
+
MAX_REDEMPTIONS_WITH_CDP_OPEN
|
|
9151
|
+
)
|
|
9152
|
+
);
|
|
9153
|
+
const mintedAmt = fromDecimal(
|
|
9154
|
+
Decimal2(redemptionDetails.totalIAssetPayout).div(
|
|
9155
|
+
Decimal2(1).minus(
|
|
9156
|
+
Decimal2(iassetDatum.debtMintingFeeRatio.numerator).div(
|
|
9157
|
+
iassetDatum.debtMintingFeeRatio.denominator
|
|
9158
|
+
)
|
|
9159
|
+
)
|
|
9160
|
+
).floor()
|
|
9161
|
+
);
|
|
9162
|
+
const debtMintingFee = calculateFeeFromRatio(
|
|
9163
|
+
iassetDatum.debtMintingFeeRatio,
|
|
9164
|
+
mintedAmt
|
|
9165
|
+
);
|
|
9166
|
+
const collateralAmt = redemptionDetails.totalRedeemedCollateral + baseCollateral;
|
|
9167
|
+
const cdpNftVal = mkAssetsOf11(
|
|
9168
|
+
fromSystemParamsAsset(sysParams.cdpParams.cdpAuthToken),
|
|
9169
|
+
1n
|
|
9170
|
+
);
|
|
9171
|
+
const iassetClass = {
|
|
9172
|
+
currencySymbol: fromHex14(
|
|
9173
|
+
sysParams.cdpParams.cdpAssetSymbol.unCurrencySymbol
|
|
9174
|
+
),
|
|
9175
|
+
tokenName: iassetDatum.assetName
|
|
9176
|
+
};
|
|
9177
|
+
const iassetTokensVal = mkAssetsOf11(iassetClass, mintedAmt);
|
|
9178
|
+
const refScripts = [
|
|
9179
|
+
cdpCreatorRefScriptUtxo,
|
|
9180
|
+
cdpAuthTokenPolicyRefScriptUtxo,
|
|
9181
|
+
iAssetTokenPolicyRefScriptUtxo,
|
|
9182
|
+
robScriptRefUtxo
|
|
9183
|
+
];
|
|
9184
|
+
const tx = lucid.newTx().readFrom(refScripts).mintAssets(cdpNftVal, Data45.void()).mintAssets(iassetTokensVal, Data45.void()).pay.ToContract(
|
|
9185
|
+
createScriptAddress(network, sysParams.validatorHashes.cdpHash, skh),
|
|
9186
|
+
{
|
|
9187
|
+
kind: "inline",
|
|
9188
|
+
value: serialiseCdpDatum({
|
|
9189
|
+
cdpOwner: fromHex14(pkh.hash),
|
|
9190
|
+
iasset: iassetDatum.assetName,
|
|
9191
|
+
collateralAsset: collateralAssetDatum.collateralAsset,
|
|
9192
|
+
mintedAmt,
|
|
9193
|
+
cdpFees: {
|
|
9194
|
+
ActiveCDPInterestTracking: {
|
|
9195
|
+
lastSettled: currentTime,
|
|
9196
|
+
unitaryInterestSnapshot: calculateUnitaryInterestSinceOracleLastUpdated(
|
|
9197
|
+
currentTime,
|
|
9198
|
+
interestOracleDatum
|
|
9199
|
+
) + interestOracleDatum.unitaryInterest
|
|
9200
|
+
}
|
|
9201
|
+
}
|
|
9202
|
+
})
|
|
9203
|
+
},
|
|
9204
|
+
addAssets14(
|
|
9205
|
+
cdpNftVal,
|
|
9206
|
+
mkAssetsOf11(collateralAssetDatum.collateralAsset, collateralAmt)
|
|
9207
|
+
)
|
|
9208
|
+
).pay.ToContract(
|
|
9209
|
+
cdpCreatorUtxo.address,
|
|
9210
|
+
{
|
|
9211
|
+
kind: "inline",
|
|
9212
|
+
value: serialiseCDPCreatorDatum({
|
|
9213
|
+
creatorInputOref: {
|
|
9214
|
+
outputIndex: BigInt(cdpCreatorUtxo.outputIndex),
|
|
9215
|
+
txHash: fromHex14(cdpCreatorUtxo.txHash)
|
|
9216
|
+
}
|
|
9217
|
+
})
|
|
9218
|
+
},
|
|
9219
|
+
cdpCreatorUtxo.assets
|
|
9220
|
+
);
|
|
9221
|
+
const treasuryRefScriptUtxo = debtMintingFee > 0n ? await treasuryFeeTx(
|
|
9222
|
+
iassetClass,
|
|
9223
|
+
debtMintingFee,
|
|
9224
|
+
0n,
|
|
9225
|
+
lucid,
|
|
9226
|
+
sysParams,
|
|
9227
|
+
tx,
|
|
9228
|
+
cdpCreatorUtxo,
|
|
9229
|
+
treasuryOref
|
|
9230
|
+
) : void 0;
|
|
9231
|
+
const { interval, referenceInputs: refInputs } = await attachOracle(
|
|
9232
|
+
iassetDatum.assetName,
|
|
9233
|
+
collateralAssetDatum.collateralAsset,
|
|
9234
|
+
collateralAssetDatum.priceInfo,
|
|
9235
|
+
priceOracleOutRef,
|
|
9236
|
+
pythStateOref,
|
|
9237
|
+
pythMessage,
|
|
9238
|
+
sysParams.pythConfig,
|
|
9239
|
+
sysParams.cdpCreatorParams.biasTime,
|
|
9240
|
+
currentSlot,
|
|
9241
|
+
lucid,
|
|
9242
|
+
tx
|
|
9243
|
+
);
|
|
9244
|
+
const referenceInputs = [
|
|
9245
|
+
interestOracleUtxo,
|
|
9246
|
+
iassetUtxo,
|
|
9247
|
+
collateralAssetUtxo,
|
|
9248
|
+
...refInputs
|
|
9249
|
+
];
|
|
9250
|
+
const refInputsIndices = getInputIndices5(referenceInputs, [
|
|
9251
|
+
...referenceInputs,
|
|
9252
|
+
...refScripts,
|
|
9253
|
+
...treasuryRefScriptUtxo != null ? [treasuryRefScriptUtxo] : []
|
|
9254
|
+
]);
|
|
9255
|
+
const oracleIdx = priceOracleOutRef !== void 0 ? { OracleRefInputIdx: refInputsIndices[3] } : "OracleVoid";
|
|
9256
|
+
tx.validFrom(interval.validFrom).validTo(interval.validTo).readFrom(referenceInputs).collectFrom([cdpCreatorUtxo], {
|
|
9257
|
+
kind: "self",
|
|
9258
|
+
makeRedeemer: (inputIdx) => {
|
|
9259
|
+
return serialiseCDPCreatorRedeemer({
|
|
9260
|
+
CreateCDP: {
|
|
9261
|
+
cdpOwner: fromHex14(pkh.hash),
|
|
9262
|
+
minted: mintedAmt,
|
|
9263
|
+
collateralAmt,
|
|
9264
|
+
currentTime,
|
|
9265
|
+
creatorInputIdx: inputIdx,
|
|
9266
|
+
creatorOutputIdx: 1n,
|
|
9267
|
+
cdpOutputIdx: 0n,
|
|
9268
|
+
iassetRefInputIdx: refInputsIndices[1],
|
|
9269
|
+
collateralAssetRefInputIdx: refInputsIndices[2],
|
|
9270
|
+
interestOracleRefInputIdx: refInputsIndices[0],
|
|
9271
|
+
priceOracleIdx: oracleIdx
|
|
9272
|
+
}
|
|
9273
|
+
});
|
|
9274
|
+
}
|
|
9275
|
+
});
|
|
9276
|
+
buildRedemptionsTx(
|
|
9277
|
+
redemptionDetails.redemptions.map((r) => [r.utxo, r.iassetsPayoutAmt]),
|
|
9278
|
+
iassetDatum.assetName,
|
|
9279
|
+
collateralAssetDatum.collateralAsset,
|
|
9280
|
+
price,
|
|
9281
|
+
iassetDatum.redemptionReimbursementRatio,
|
|
9282
|
+
sysParams,
|
|
9283
|
+
tx,
|
|
9284
|
+
2n + (debtMintingFee > 0n ? 1n : 0n),
|
|
9285
|
+
refInputsIndices[2],
|
|
9286
|
+
refInputsIndices[1],
|
|
9287
|
+
oracleIdx
|
|
9288
|
+
);
|
|
9289
|
+
return tx;
|
|
9290
|
+
}
|
|
9291
|
+
|
|
9292
|
+
// src/contracts/price-oracle/transactions.ts
|
|
9293
|
+
import {
|
|
9294
|
+
addAssets as addAssets15,
|
|
9295
|
+
Data as Data46,
|
|
9296
|
+
fromHex as fromHex15,
|
|
8741
9297
|
fromText as fromText10,
|
|
8742
|
-
slotToUnixTime as
|
|
9298
|
+
slotToUnixTime as slotToUnixTime11,
|
|
8743
9299
|
validatorToAddress as validatorToAddress2
|
|
8744
9300
|
} from "@lucid-evolution/lucid";
|
|
8745
9301
|
|
|
@@ -8766,11 +9322,11 @@ function mkPriceOracleValidator(params) {
|
|
|
8766
9322
|
}
|
|
8767
9323
|
|
|
8768
9324
|
// src/contracts/price-oracle/transactions.ts
|
|
8769
|
-
import { mkAssetsOf as
|
|
9325
|
+
import { mkAssetsOf as mkAssetsOf12 } from "@3rd-eye-labs/cardano-offchain-common";
|
|
8770
9326
|
import * as Core2 from "@evolution-sdk/evolution";
|
|
8771
9327
|
async function startPriceOracleTx(lucid, assetName, startPrice, oracleParams, currentSlot, refOutRef, auxiliaryData) {
|
|
8772
9328
|
const network = lucid.config().network;
|
|
8773
|
-
const now = BigInt(
|
|
9329
|
+
const now = BigInt(slotToUnixTime11(network, currentSlot));
|
|
8774
9330
|
if (!refOutRef) {
|
|
8775
9331
|
refOutRef = (await lucid.wallet().getUtxos())[0];
|
|
8776
9332
|
}
|
|
@@ -8787,25 +9343,25 @@ async function startPriceOracleTx(lucid, assetName, startPrice, oracleParams, cu
|
|
|
8787
9343
|
]
|
|
8788
9344
|
});
|
|
8789
9345
|
const priceOracleNft = {
|
|
8790
|
-
currencySymbol:
|
|
8791
|
-
tokenName:
|
|
9346
|
+
currencySymbol: fromHex15(oracleNftPolicyId),
|
|
9347
|
+
tokenName: fromHex15(fromText10(assetName))
|
|
8792
9348
|
};
|
|
8793
9349
|
const oracleValidator = mkPriceOracleValidator(oracleParams);
|
|
8794
9350
|
const oracleDatum = {
|
|
8795
9351
|
price: startPrice,
|
|
8796
9352
|
expirationTime: BigInt(now) + oracleParams.expirationPeriod,
|
|
8797
|
-
auxiliaryData: auxiliaryData ?? Core2.Data.fromCBORHex(
|
|
9353
|
+
auxiliaryData: auxiliaryData ?? Core2.Data.fromCBORHex(Data46.void())
|
|
8798
9354
|
};
|
|
8799
9355
|
tx.pay.ToContract(
|
|
8800
9356
|
validatorToAddress2(lucid.config().network, oracleValidator),
|
|
8801
9357
|
{ kind: "inline", value: serialisePriceOracleDatum(oracleDatum) },
|
|
8802
|
-
|
|
9358
|
+
addAssets15(mkAssetsOf12(priceOracleNft, 1n))
|
|
8803
9359
|
);
|
|
8804
9360
|
return [tx, priceOracleNft];
|
|
8805
9361
|
}
|
|
8806
9362
|
async function feedPriceOracleTx(lucid, oracleOref, newPrice, oracleParams, currentSlot, auxiliaryData) {
|
|
8807
9363
|
const network = lucid.config().network;
|
|
8808
|
-
const currentTime = BigInt(
|
|
9364
|
+
const currentTime = BigInt(slotToUnixTime11(network, currentSlot));
|
|
8809
9365
|
const priceOracleUtxo = matchSingle(
|
|
8810
9366
|
await lucid.utxosByOutRef([oracleOref]),
|
|
8811
9367
|
(_) => new Error("Expected a single price oracle UTXO")
|
|
@@ -8824,7 +9380,7 @@ async function feedPriceOracleTx(lucid, oracleOref, newPrice, oracleParams, curr
|
|
|
8824
9380
|
value: serialisePriceOracleDatum({
|
|
8825
9381
|
price: newPrice,
|
|
8826
9382
|
expirationTime: currentTime + oracleParams.expirationPeriod,
|
|
8827
|
-
auxiliaryData: auxiliaryData ?? Core2.Data.fromCBORHex(
|
|
9383
|
+
auxiliaryData: auxiliaryData ?? Core2.Data.fromCBORHex(Data46.void())
|
|
8828
9384
|
})
|
|
8829
9385
|
},
|
|
8830
9386
|
priceOracleUtxo.assets
|
|
@@ -8852,10 +9408,10 @@ async function runCreateScriptRefTx(lucid, scriptRefValidator, network) {
|
|
|
8852
9408
|
|
|
8853
9409
|
// src/contracts/initialize/helpers.ts
|
|
8854
9410
|
import {
|
|
8855
|
-
addAssets as
|
|
9411
|
+
addAssets as addAssets16,
|
|
8856
9412
|
credentialToAddress as credentialToAddress4,
|
|
8857
|
-
Data as
|
|
8858
|
-
fromHex as
|
|
9413
|
+
Data as Data49,
|
|
9414
|
+
fromHex as fromHex16,
|
|
8859
9415
|
fromText as fromText11,
|
|
8860
9416
|
mintingPolicyToId as mintingPolicyToId2,
|
|
8861
9417
|
validatorToScriptHash as validatorToScriptHash4
|
|
@@ -8864,7 +9420,7 @@ import {
|
|
|
8864
9420
|
assetClassToUnit as assetClassToUnit2,
|
|
8865
9421
|
assetClassValueOf as assetClassValueOf9,
|
|
8866
9422
|
lovelacesAmt as lovelacesAmt4,
|
|
8867
|
-
mkAssetsOf as
|
|
9423
|
+
mkAssetsOf as mkAssetsOf13,
|
|
8868
9424
|
mkLovelacesOf as mkLovelacesOf7
|
|
8869
9425
|
} from "@3rd-eye-labs/cardano-offchain-common";
|
|
8870
9426
|
|
|
@@ -8899,8 +9455,8 @@ var _iassetValidator = {
|
|
|
8899
9455
|
};
|
|
8900
9456
|
|
|
8901
9457
|
// src/contracts/iasset/scripts.ts
|
|
8902
|
-
import { Data as
|
|
8903
|
-
var IAssetScriptParamsSchema =
|
|
9458
|
+
import { Data as Data48 } from "@lucid-evolution/lucid";
|
|
9459
|
+
var IAssetScriptParamsSchema = Data48.Object({
|
|
8904
9460
|
upgradeToken: AssetClassSchema2,
|
|
8905
9461
|
versionRecordToken: AssetClassSchema2
|
|
8906
9462
|
});
|
|
@@ -8909,7 +9465,7 @@ var mkIAssetValidatorFromSP = (params) => {
|
|
|
8909
9465
|
return {
|
|
8910
9466
|
type: "PlutusV3",
|
|
8911
9467
|
script: applyParamsToScript17(_iassetValidator.cborHex, [
|
|
8912
|
-
|
|
9468
|
+
Data48.castTo(
|
|
8913
9469
|
{
|
|
8914
9470
|
upgradeToken: fromSystemParamsAssetLucid(params.upgradeToken),
|
|
8915
9471
|
versionRecordToken: fromSystemParamsAssetLucid(
|
|
@@ -8926,7 +9482,7 @@ var mkIAssetValidatorFromSP = (params) => {
|
|
|
8926
9482
|
import { match as match20 } from "ts-pattern";
|
|
8927
9483
|
|
|
8928
9484
|
// tests/test-helpers.ts
|
|
8929
|
-
import { array as
|
|
9485
|
+
import { array as A12, function as F19, option as O16 } from "fp-ts";
|
|
8930
9486
|
async function runAndAwaitTxBuilder(lucid, transaction, extraSigners = []) {
|
|
8931
9487
|
const bTx = await transaction.complete();
|
|
8932
9488
|
const signatures = [await bTx.partialSign.withWallet()];
|
|
@@ -8989,15 +9545,15 @@ async function mintOneTimeToken(lucid, tokenName, amount) {
|
|
|
8989
9545
|
async function mintOneTimeAsset(lucid, tokenName, amount) {
|
|
8990
9546
|
const policyId = await mintOneTimeToken(lucid, fromText11(tokenName), amount);
|
|
8991
9547
|
return {
|
|
8992
|
-
currencySymbol:
|
|
8993
|
-
tokenName:
|
|
9548
|
+
currencySymbol: fromHex16(policyId),
|
|
9549
|
+
tokenName: fromHex16(fromText11(tokenName))
|
|
8994
9550
|
};
|
|
8995
9551
|
}
|
|
8996
9552
|
function deriveAuthToken(parent, tokenName) {
|
|
8997
9553
|
const policy = mkAuthTokenPolicy(parent, fromText11(tokenName));
|
|
8998
9554
|
return {
|
|
8999
|
-
currencySymbol:
|
|
9000
|
-
tokenName:
|
|
9555
|
+
currencySymbol: fromHex16(mintingPolicyToId2(policy)),
|
|
9556
|
+
tokenName: fromHex16(fromText11(tokenName))
|
|
9001
9557
|
};
|
|
9002
9558
|
}
|
|
9003
9559
|
async function initScriptRef(lucid, validator) {
|
|
@@ -9024,7 +9580,7 @@ async function initCollector(lucid, collectorParams, numCollectors) {
|
|
|
9024
9580
|
),
|
|
9025
9581
|
{
|
|
9026
9582
|
kind: "inline",
|
|
9027
|
-
value:
|
|
9583
|
+
value: Data49.void()
|
|
9028
9584
|
}
|
|
9029
9585
|
);
|
|
9030
9586
|
}
|
|
@@ -9035,7 +9591,7 @@ async function initInterestCollector(lucid, interestCollectionValHash, multisigU
|
|
|
9035
9591
|
const interestCollectionAdminDatum = {
|
|
9036
9592
|
admin_permissions: {
|
|
9037
9593
|
Signature: {
|
|
9038
|
-
keyHash:
|
|
9594
|
+
keyHash: fromHex16(pkh.hash)
|
|
9039
9595
|
}
|
|
9040
9596
|
}
|
|
9041
9597
|
};
|
|
@@ -9047,7 +9603,7 @@ async function initInterestCollector(lucid, interestCollectionValHash, multisigU
|
|
|
9047
9603
|
kind: "inline",
|
|
9048
9604
|
value: serialiseInterestCollectionDatum(interestCollectionAdminDatum)
|
|
9049
9605
|
},
|
|
9050
|
-
|
|
9606
|
+
mkAssetsOf13(multisigUtxoNft, 1n)
|
|
9051
9607
|
)
|
|
9052
9608
|
);
|
|
9053
9609
|
const interestCollectionUtxoDeploymentTx = lucid.newTx();
|
|
@@ -9070,7 +9626,7 @@ async function initCDPCreator(lucid, cdpCreatorParams, numCdpCreators) {
|
|
|
9070
9626
|
),
|
|
9071
9627
|
type: "Script"
|
|
9072
9628
|
}),
|
|
9073
|
-
{ kind: "inline", value:
|
|
9629
|
+
{ kind: "inline", value: Data49.void() },
|
|
9074
9630
|
{
|
|
9075
9631
|
[cdpCreatorParams.cdpCreatorNft[0].unCurrencySymbol + fromText11(cdpCreatorParams.cdpCreatorNft[1].unTokenName)]: 1n
|
|
9076
9632
|
}
|
|
@@ -9086,16 +9642,16 @@ async function initTreasury(lucid, treasuryParams, daoAsset, indyAsset, treasury
|
|
|
9086
9642
|
);
|
|
9087
9643
|
const tx = lucid.newTx().pay.ToContract(
|
|
9088
9644
|
treasuryAddr,
|
|
9089
|
-
{ kind: "inline", value:
|
|
9090
|
-
|
|
9091
|
-
|
|
9092
|
-
|
|
9645
|
+
{ kind: "inline", value: Data49.void() },
|
|
9646
|
+
addAssets16(
|
|
9647
|
+
mkAssetsOf13(daoAsset, 1n),
|
|
9648
|
+
mkAssetsOf13(indyAsset, treasuryIndyAmount)
|
|
9093
9649
|
)
|
|
9094
9650
|
);
|
|
9095
9651
|
for (let i = 0; i < Number(numTreasuryCollectors); i++) {
|
|
9096
9652
|
tx.pay.ToContract(treasuryAddr, {
|
|
9097
9653
|
kind: "inline",
|
|
9098
|
-
value:
|
|
9654
|
+
value: Data49.void()
|
|
9099
9655
|
});
|
|
9100
9656
|
}
|
|
9101
9657
|
await submitTx(lucid, tx);
|
|
@@ -9140,10 +9696,10 @@ async function handleOracleForCollateralAsset(lucid, iasset, collateralAsset, py
|
|
|
9140
9696
|
return Promise.resolve({
|
|
9141
9697
|
info: {
|
|
9142
9698
|
DeferredValidation: {
|
|
9143
|
-
feedValHash:
|
|
9699
|
+
feedValHash: fromHex16(
|
|
9144
9700
|
getPythFeedConfig(
|
|
9145
9701
|
pythConfig,
|
|
9146
|
-
|
|
9702
|
+
fromHex16(fromText11(iasset.name)),
|
|
9147
9703
|
collateralAsset.collateralAsset
|
|
9148
9704
|
).pythFeedValHash
|
|
9149
9705
|
)
|
|
@@ -9154,7 +9710,7 @@ async function handleOracleForCollateralAsset(lucid, iasset, collateralAsset, py
|
|
|
9154
9710
|
}).exhaustive();
|
|
9155
9711
|
}
|
|
9156
9712
|
async function initializeAsset(lucid, iassetParams, iassetToken, collateralAssetAuthToken, cdpAuthToken, cdpParams, stableswapValidatorHash, stabilityPoolParams, stabilityPoolToken, asset, pythConfig, currentSlot) {
|
|
9157
|
-
const iassetName =
|
|
9713
|
+
const iassetName = fromHex16(fromText11(asset.name));
|
|
9158
9714
|
const collateralAssetInfos = [];
|
|
9159
9715
|
for (const collateralAsset of asset.collateralAssets) {
|
|
9160
9716
|
const [pkh, _] = await addrDetails(lucid);
|
|
@@ -9202,7 +9758,7 @@ async function initializeAsset(lucid, iassetParams, iassetToken, collateralAsset
|
|
|
9202
9758
|
CollateralAssetContent: collateralAssetContent
|
|
9203
9759
|
})
|
|
9204
9760
|
},
|
|
9205
|
-
|
|
9761
|
+
mkAssetsOf13(collateralAssetAuthToken, 1n)
|
|
9206
9762
|
);
|
|
9207
9763
|
await submitTx(lucid, collateralAssetTx);
|
|
9208
9764
|
collateralAssetInfos.push({
|
|
@@ -9220,11 +9776,11 @@ async function initializeAsset(lucid, iassetParams, iassetToken, collateralAsset
|
|
|
9220
9776
|
redemptionFeeRatio: stableswapPool.redemptionFeeRatio,
|
|
9221
9777
|
mintingEnabled: stableswapPool.mintingEnabled,
|
|
9222
9778
|
redemptionEnabled: stableswapPool.redemptionEnabled,
|
|
9223
|
-
feeManager: stableswapPool.feeManager ?
|
|
9779
|
+
feeManager: stableswapPool.feeManager ? fromHex16(stableswapPool.feeManager) : null,
|
|
9224
9780
|
minMintOrderAmount: stableswapPool.minMintOrderAmount,
|
|
9225
9781
|
minRedemptionOrderAmount: stableswapPool.minRedemptionOrderAmount,
|
|
9226
9782
|
iasset: iassetName,
|
|
9227
|
-
stableswapValHash:
|
|
9783
|
+
stableswapValHash: fromHex16(stableswapValidatorHash)
|
|
9228
9784
|
};
|
|
9229
9785
|
await submitTx(
|
|
9230
9786
|
lucid,
|
|
@@ -9237,7 +9793,7 @@ async function initializeAsset(lucid, iassetParams, iassetToken, collateralAsset
|
|
|
9237
9793
|
kind: "inline",
|
|
9238
9794
|
value: serialiseStableswapPoolDatum(stableswapPoolDatum)
|
|
9239
9795
|
},
|
|
9240
|
-
|
|
9796
|
+
mkAssetsOf13(cdpAuthToken, 1n)
|
|
9241
9797
|
)
|
|
9242
9798
|
);
|
|
9243
9799
|
}
|
|
@@ -9250,7 +9806,7 @@ async function initializeAsset(lucid, iassetParams, iassetToken, collateralAsset
|
|
|
9250
9806
|
redemptionReimbursementRatio: asset.redemptionReimbursementRatio,
|
|
9251
9807
|
redemptionProcessingFeeRatio: asset.redemptionProcessingFeeRatio,
|
|
9252
9808
|
firstIAsset: asset.firstAsset,
|
|
9253
|
-
nextIAsset: asset.nextAsset ?
|
|
9809
|
+
nextIAsset: asset.nextAsset ? fromHex16(fromText11(asset.nextAsset)) : null
|
|
9254
9810
|
};
|
|
9255
9811
|
const assetTx = lucid.newTx().pay.ToContract(
|
|
9256
9812
|
createScriptAddress(
|
|
@@ -9261,11 +9817,11 @@ async function initializeAsset(lucid, iassetParams, iassetToken, collateralAsset
|
|
|
9261
9817
|
kind: "inline",
|
|
9262
9818
|
value: serialiseIAssetDatum({ IAssetContent: iassetDatum })
|
|
9263
9819
|
},
|
|
9264
|
-
|
|
9820
|
+
mkAssetsOf13(iassetToken, 1n)
|
|
9265
9821
|
);
|
|
9266
9822
|
await submitTx(lucid, assetTx);
|
|
9267
9823
|
const stabilityPoolDatum = {
|
|
9268
|
-
iasset:
|
|
9824
|
+
iasset: fromHex16(fromText11(asset.name)),
|
|
9269
9825
|
state: initSpState,
|
|
9270
9826
|
assetStates: []
|
|
9271
9827
|
};
|
|
@@ -9280,7 +9836,7 @@ async function initializeAsset(lucid, iassetParams, iassetToken, collateralAsset
|
|
|
9280
9836
|
kind: "inline",
|
|
9281
9837
|
value: serialiseStabilityPoolDatum({ StabilityPool: stabilityPoolDatum })
|
|
9282
9838
|
},
|
|
9283
|
-
|
|
9839
|
+
mkAssetsOf13(stabilityPoolToken, 1n)
|
|
9284
9840
|
);
|
|
9285
9841
|
await submitTx(lucid, spTx);
|
|
9286
9842
|
return {
|
|
@@ -9302,7 +9858,7 @@ async function initGovernance(lucid, governanceParams, govToken, initialAssets,
|
|
|
9302
9858
|
type: "Script"
|
|
9303
9859
|
}),
|
|
9304
9860
|
{ kind: "inline", value: serialiseGovDatum(datum) },
|
|
9305
|
-
|
|
9861
|
+
mkAssetsOf13(govToken, 1n)
|
|
9306
9862
|
);
|
|
9307
9863
|
await submitTx(lucid, tx);
|
|
9308
9864
|
}
|
|
@@ -9319,16 +9875,16 @@ async function mintAuthTokenDirect(lucid, asset, tokenName, amount) {
|
|
|
9319
9875
|
{
|
|
9320
9876
|
[policyId + fromText11(tokenName)]: amount
|
|
9321
9877
|
},
|
|
9322
|
-
|
|
9878
|
+
Data49.void()
|
|
9323
9879
|
);
|
|
9324
9880
|
await submitTx(lucid, tx);
|
|
9325
9881
|
}
|
|
9326
9882
|
|
|
9327
9883
|
// src/contracts/initialize/actions.ts
|
|
9328
9884
|
import {
|
|
9329
|
-
addAssets as
|
|
9885
|
+
addAssets as addAssets17,
|
|
9330
9886
|
credentialToRewardAddress as credentialToRewardAddress4,
|
|
9331
|
-
fromHex as
|
|
9887
|
+
fromHex as fromHex18,
|
|
9332
9888
|
fromText as fromText13,
|
|
9333
9889
|
mintingPolicyToId as mintingPolicyToId3,
|
|
9334
9890
|
scriptHashToCredential as scriptHashToCredential4,
|
|
@@ -9336,7 +9892,7 @@ import {
|
|
|
9336
9892
|
validatorToScriptHash as validatorToScriptHash6
|
|
9337
9893
|
} from "@lucid-evolution/lucid";
|
|
9338
9894
|
import {
|
|
9339
|
-
mkAssetsOf as
|
|
9895
|
+
mkAssetsOf as mkAssetsOf15,
|
|
9340
9896
|
mkLovelacesOf as mkLovelacesOf8
|
|
9341
9897
|
} from "@3rd-eye-labs/cardano-offchain-common";
|
|
9342
9898
|
|
|
@@ -9363,17 +9919,17 @@ function mkIAssetTokenPolicy(ac) {
|
|
|
9363
9919
|
import { applyParamsToScript as applyParamsToScript19 } from "@lucid-evolution/lucid";
|
|
9364
9920
|
|
|
9365
9921
|
// src/contracts/stableswap/types.ts
|
|
9366
|
-
import { Data as
|
|
9367
|
-
var StableswapParamsSchema =
|
|
9368
|
-
iassetSymbol:
|
|
9922
|
+
import { Data as Data50 } from "@lucid-evolution/lucid";
|
|
9923
|
+
var StableswapParamsSchema = Data50.Object({
|
|
9924
|
+
iassetSymbol: Data50.Bytes(),
|
|
9369
9925
|
cdpToken: AssetClassSchema2,
|
|
9370
9926
|
versionRecordToken: AssetClassSchema2,
|
|
9371
|
-
cdpValHash:
|
|
9372
|
-
treasuryValHash:
|
|
9927
|
+
cdpValHash: Data50.Bytes(),
|
|
9928
|
+
treasuryValHash: Data50.Bytes()
|
|
9373
9929
|
});
|
|
9374
9930
|
var StableswapParams = StableswapParamsSchema;
|
|
9375
9931
|
function castStableswapParams(params) {
|
|
9376
|
-
return
|
|
9932
|
+
return Data50.castTo(params, StableswapParams);
|
|
9377
9933
|
}
|
|
9378
9934
|
|
|
9379
9935
|
// src/validators/stableswap-validator.ts
|
|
@@ -9434,7 +9990,7 @@ import { match as match21 } from "ts-pattern";
|
|
|
9434
9990
|
import {
|
|
9435
9991
|
credentialToAddress as credentialToAddress5,
|
|
9436
9992
|
credentialToRewardAddress as credentialToRewardAddress3,
|
|
9437
|
-
fromHex as
|
|
9993
|
+
fromHex as fromHex17,
|
|
9438
9994
|
fromText as fromText12,
|
|
9439
9995
|
validatorToScriptHash as validatorToScriptHash5
|
|
9440
9996
|
} from "@lucid-evolution/lucid";
|
|
@@ -9453,14 +10009,14 @@ var alwaysSucceedValidator = {
|
|
|
9453
10009
|
};
|
|
9454
10010
|
|
|
9455
10011
|
// tests/pyth/endpoints.ts
|
|
9456
|
-
import { mkAssetsOf as
|
|
10012
|
+
import { mkAssetsOf as mkAssetsOf14 } from "@3rd-eye-labs/cardano-offchain-common";
|
|
9457
10013
|
var TEST_TRUSTED_SIGNER_PUB_KEY = "4a50d7c3d16b2be5c16ba996109a455a34cce08a81b3e15b86ef407e2f72e71f";
|
|
9458
10014
|
async function initPyth(lucid) {
|
|
9459
10015
|
const pythStateName = fromText12("Pyth State");
|
|
9460
10016
|
const pythStatePolicyId = await mintOneTimeToken(lucid, pythStateName, 1n);
|
|
9461
10017
|
const pythStateAsset = {
|
|
9462
|
-
currencySymbol:
|
|
9463
|
-
tokenName:
|
|
10018
|
+
currencySymbol: fromHex17(pythStatePolicyId),
|
|
10019
|
+
tokenName: fromHex17(pythStateName)
|
|
9464
10020
|
};
|
|
9465
10021
|
const alwaysSucceedsValidatorHash = validatorToScriptHash5(
|
|
9466
10022
|
alwaysSucceedValidator
|
|
@@ -9468,14 +10024,14 @@ async function initPyth(lucid) {
|
|
|
9468
10024
|
const pythStateDatum = {
|
|
9469
10025
|
// The governance doesn't matter for the purpose of testing, so we can use any values
|
|
9470
10026
|
governance: {
|
|
9471
|
-
wormhole:
|
|
10027
|
+
wormhole: fromHex17(""),
|
|
9472
10028
|
emitterChain: 0n,
|
|
9473
|
-
emitterAddress:
|
|
10029
|
+
emitterAddress: fromHex17(""),
|
|
9474
10030
|
seenSequence: 0n
|
|
9475
10031
|
},
|
|
9476
10032
|
trustedSigners: /* @__PURE__ */ new Map([
|
|
9477
10033
|
[
|
|
9478
|
-
|
|
10034
|
+
fromHex17(TEST_TRUSTED_SIGNER_PUB_KEY),
|
|
9479
10035
|
{
|
|
9480
10036
|
lower_bound: {
|
|
9481
10037
|
bound_type: {
|
|
@@ -9495,7 +10051,7 @@ async function initPyth(lucid) {
|
|
|
9495
10051
|
deprecatedWithdrawScripts: /* @__PURE__ */ new Map(),
|
|
9496
10052
|
// For the purpose of testing, we use the always succeeds validator to validate the Pyth Messages.
|
|
9497
10053
|
// This means the message signature checking will always succeed for the purpose of emulation testing.
|
|
9498
|
-
withdraw_script:
|
|
10054
|
+
withdraw_script: fromHex17(alwaysSucceedsValidatorHash)
|
|
9499
10055
|
};
|
|
9500
10056
|
await runAndAwaitTxBuilder(
|
|
9501
10057
|
lucid,
|
|
@@ -9505,7 +10061,7 @@ async function initPyth(lucid) {
|
|
|
9505
10061
|
type: "Script"
|
|
9506
10062
|
}),
|
|
9507
10063
|
{ kind: "inline", value: serialisePythStateDatum(pythStateDatum) },
|
|
9508
|
-
|
|
10064
|
+
mkAssetsOf14(pythStateAsset, 1n),
|
|
9509
10065
|
alwaysSucceedValidator
|
|
9510
10066
|
)
|
|
9511
10067
|
);
|
|
@@ -9608,8 +10164,8 @@ async function init(lucid, defaultInitialAssets, currentSlot, mkPythBasedAssets
|
|
|
9608
10164
|
}
|
|
9609
10165
|
});
|
|
9610
10166
|
const versionRecordToken = {
|
|
9611
|
-
currencySymbol:
|
|
9612
|
-
tokenName:
|
|
10167
|
+
currencySymbol: fromHex18(mintingPolicyToId3(versionRecordTokenPolicy)),
|
|
10168
|
+
tokenName: fromHex18(fromText13(tn.versionRecord))
|
|
9613
10169
|
};
|
|
9614
10170
|
const versionRegistryValidator = mkVersionRegistryValidator();
|
|
9615
10171
|
const versionRegistryValHash = validatorToScriptHash6(
|
|
@@ -9808,8 +10364,8 @@ async function init(lucid, defaultInitialAssets, currentSlot, mkPythBasedAssets
|
|
|
9808
10364
|
lucid,
|
|
9809
10365
|
lucid.newTx().pay.ToAddress(
|
|
9810
10366
|
await lucid.wallet().address(),
|
|
9811
|
-
|
|
9812
|
-
|
|
10367
|
+
addAssets17(
|
|
10368
|
+
mkAssetsOf15(iassetToken, 1n),
|
|
9813
10369
|
mkLovelacesOf8(BigInt(totalCollateralAssetsCount) * 5000000n)
|
|
9814
10370
|
)
|
|
9815
10371
|
)
|
|
@@ -9826,8 +10382,8 @@ async function init(lucid, defaultInitialAssets, currentSlot, mkPythBasedAssets
|
|
|
9826
10382
|
lucid,
|
|
9827
10383
|
lucid.newTx().pay.ToAddress(
|
|
9828
10384
|
await lucid.wallet().address(),
|
|
9829
|
-
|
|
9830
|
-
|
|
10385
|
+
addAssets17(
|
|
10386
|
+
mkAssetsOf15(cdpCreatorAsset, 1n),
|
|
9831
10387
|
mkLovelacesOf8(BigInt(totalStablePoolsCount) * 5000000n)
|
|
9832
10388
|
)
|
|
9833
10389
|
)
|
|
@@ -10108,7 +10664,7 @@ async function init(lucid, defaultInitialAssets, currentSlot, mkPythBasedAssets
|
|
|
10108
10664
|
}
|
|
10109
10665
|
|
|
10110
10666
|
// src/contracts/stableswap/helpers.ts
|
|
10111
|
-
import { fromHex as
|
|
10667
|
+
import { fromHex as fromHex19, fromText as fromText14 } from "@lucid-evolution/lucid";
|
|
10112
10668
|
|
|
10113
10669
|
// src/contracts/stableswap/types-new.ts
|
|
10114
10670
|
import {
|
|
@@ -10116,10 +10672,10 @@ import {
|
|
|
10116
10672
|
AssetClassSchema as AssetClassSchema8,
|
|
10117
10673
|
OutputReferenceSchema as OutputReferenceSchema7
|
|
10118
10674
|
} from "@3rd-eye-labs/cardano-offchain-common";
|
|
10119
|
-
import { TSchema as TSchema20, Data as
|
|
10120
|
-
import { option as O17, function as
|
|
10675
|
+
import { TSchema as TSchema20, Data as Data51 } from "@evolution-sdk/evolution";
|
|
10676
|
+
import { option as O17, function as F20 } from "fp-ts";
|
|
10121
10677
|
import { Schema as Schema4 } from "effect";
|
|
10122
|
-
var OpaqueData4 = Schema4.typeSchema(
|
|
10678
|
+
var OpaqueData4 = Schema4.typeSchema(Data51.DataSchema);
|
|
10123
10679
|
var StableswapOrderDatumSchema = TSchema20.Struct({
|
|
10124
10680
|
iasset: TSchema20.ByteArray,
|
|
10125
10681
|
collateralAsset: AssetClassSchema8,
|
|
@@ -10152,7 +10708,7 @@ var StableswapOutputDatumSchema = TSchema20.Tuple([
|
|
|
10152
10708
|
OutputReferenceSchema7
|
|
10153
10709
|
]);
|
|
10154
10710
|
function serialiseStableswapOrderDatum(d) {
|
|
10155
|
-
return
|
|
10711
|
+
return Data51.withSchema(
|
|
10156
10712
|
StableswapOrderDatumSchema,
|
|
10157
10713
|
DEFAULT_SCHEMA_OPTIONS
|
|
10158
10714
|
).toCBORHex(d);
|
|
@@ -10160,7 +10716,7 @@ function serialiseStableswapOrderDatum(d) {
|
|
|
10160
10716
|
function parseStableswapOrderDatum(datum) {
|
|
10161
10717
|
try {
|
|
10162
10718
|
return O17.some(
|
|
10163
|
-
|
|
10719
|
+
Data51.withSchema(
|
|
10164
10720
|
StableswapOrderDatumSchema,
|
|
10165
10721
|
DEFAULT_SCHEMA_OPTIONS
|
|
10166
10722
|
).fromCBORHex(datum)
|
|
@@ -10170,21 +10726,21 @@ function parseStableswapOrderDatum(datum) {
|
|
|
10170
10726
|
}
|
|
10171
10727
|
}
|
|
10172
10728
|
function parseStableswapOrderDatumOrThrow(datum) {
|
|
10173
|
-
return
|
|
10729
|
+
return F20.pipe(
|
|
10174
10730
|
parseStableswapOrderDatum(datum),
|
|
10175
10731
|
O17.match(() => {
|
|
10176
10732
|
throw new Error("Expected a Stableswap Order datum.");
|
|
10177
|
-
},
|
|
10733
|
+
}, F20.identity)
|
|
10178
10734
|
);
|
|
10179
10735
|
}
|
|
10180
10736
|
function serialiseStableswapOutputDatum(d) {
|
|
10181
|
-
return
|
|
10737
|
+
return Data51.withSchema(
|
|
10182
10738
|
StableswapOutputDatumSchema,
|
|
10183
10739
|
DEFAULT_SCHEMA_OPTIONS
|
|
10184
10740
|
).toCBORHex(d);
|
|
10185
10741
|
}
|
|
10186
10742
|
function serialiseStableswapOrderRedeemer(r) {
|
|
10187
|
-
return
|
|
10743
|
+
return Data51.withSchema(
|
|
10188
10744
|
StableswapOrderRedeemerSchema,
|
|
10189
10745
|
DEFAULT_SCHEMA_OPTIONS
|
|
10190
10746
|
).toCBORHex(r);
|
|
@@ -10192,7 +10748,7 @@ function serialiseStableswapOrderRedeemer(r) {
|
|
|
10192
10748
|
function parseStableswapOrderRedeemer(redeemerCborHex) {
|
|
10193
10749
|
try {
|
|
10194
10750
|
return O17.some(
|
|
10195
|
-
|
|
10751
|
+
Data51.withSchema(
|
|
10196
10752
|
StableswapOrderRedeemerSchema,
|
|
10197
10753
|
DEFAULT_SCHEMA_OPTIONS
|
|
10198
10754
|
).fromCBORHex(redeemerCborHex)
|
|
@@ -10202,36 +10758,36 @@ function parseStableswapOrderRedeemer(redeemerCborHex) {
|
|
|
10202
10758
|
}
|
|
10203
10759
|
}
|
|
10204
10760
|
function parseStableswapOrderRedeemerOrThrow(redeemerCborHex) {
|
|
10205
|
-
return
|
|
10761
|
+
return F20.pipe(
|
|
10206
10762
|
parseStableswapOrderRedeemer(redeemerCborHex),
|
|
10207
10763
|
O17.match(() => {
|
|
10208
10764
|
throw new Error("Expected a Stableswap Order redeemer.");
|
|
10209
|
-
},
|
|
10765
|
+
}, F20.identity)
|
|
10210
10766
|
);
|
|
10211
10767
|
}
|
|
10212
10768
|
|
|
10213
10769
|
// src/contracts/stableswap/helpers.ts
|
|
10214
|
-
import { Data as
|
|
10770
|
+
import { Data as Data52 } from "@evolution-sdk/evolution";
|
|
10215
10771
|
var BASE_MAX_EXECUTION_FEE = 1000000n;
|
|
10216
10772
|
function createDestinationDatum(datum, outRef) {
|
|
10217
10773
|
if (!datum) {
|
|
10218
10774
|
return serialiseStableswapOutputDatum([
|
|
10219
|
-
|
|
10775
|
+
fromHex19(fromText14("IndigoStableswap")),
|
|
10220
10776
|
{
|
|
10221
|
-
txHash:
|
|
10777
|
+
txHash: fromHex19(outRef.txHash),
|
|
10222
10778
|
outputIndex: BigInt(outRef.outputIndex)
|
|
10223
10779
|
}
|
|
10224
10780
|
]);
|
|
10225
10781
|
}
|
|
10226
|
-
return
|
|
10782
|
+
return Data52.toCBORHex(datum);
|
|
10227
10783
|
}
|
|
10228
10784
|
|
|
10229
10785
|
// src/contracts/stableswap/transactions.ts
|
|
10230
10786
|
import {
|
|
10231
|
-
addAssets as
|
|
10787
|
+
addAssets as addAssets18,
|
|
10232
10788
|
credentialToAddress as credentialToAddress6,
|
|
10233
|
-
Data as
|
|
10234
|
-
fromHex as
|
|
10789
|
+
Data as Data53,
|
|
10790
|
+
fromHex as fromHex20,
|
|
10235
10791
|
fromText as fromText15,
|
|
10236
10792
|
paymentCredentialOf as paymentCredentialOf3,
|
|
10237
10793
|
sortUTxOs as sortUTxOs2,
|
|
@@ -10243,32 +10799,32 @@ import {
|
|
|
10243
10799
|
addressToBech32 as addressToBech323,
|
|
10244
10800
|
getInlineDatumOrThrow as getInlineDatumOrThrow6,
|
|
10245
10801
|
matchSingle as matchSingle4,
|
|
10246
|
-
mkAssetsOf as
|
|
10802
|
+
mkAssetsOf as mkAssetsOf16,
|
|
10247
10803
|
mkLovelacesOf as mkLovelacesOf9,
|
|
10248
10804
|
lovelacesAmt as lovelacesAmt5,
|
|
10249
10805
|
isSameOutRef as isSameOutRef2,
|
|
10250
10806
|
assetClassValueOf as assetClassValueOf10
|
|
10251
10807
|
} from "@3rd-eye-labs/cardano-offchain-common";
|
|
10252
|
-
import { array as
|
|
10808
|
+
import { array as A13, function as F21 } from "fp-ts";
|
|
10253
10809
|
import { isEmpty } from "fp-ts/lib/Array";
|
|
10254
10810
|
async function createStableswapOrder(iasset, collateralAsset, amount, minting, poolDatum, params, lucid, destinationAddress, destinationInlineDatum, maxExecutionFee = BASE_MAX_EXECUTION_FEE, additionalLovelaces = 0n, maxFeeRatio) {
|
|
10255
10811
|
const myAddress = await lucid.wallet().address();
|
|
10256
10812
|
const pkh = paymentCredentialOf3(myAddress);
|
|
10257
10813
|
const datum = {
|
|
10258
|
-
iasset:
|
|
10814
|
+
iasset: fromHex20(fromText15(iasset)),
|
|
10259
10815
|
collateralAsset,
|
|
10260
|
-
owner:
|
|
10816
|
+
owner: fromHex20(pkh.hash),
|
|
10261
10817
|
destination: addressFromBech323(destinationAddress ?? myAddress),
|
|
10262
10818
|
destinationInlineDatum: destinationInlineDatum ?? null,
|
|
10263
10819
|
maxExecutionFee,
|
|
10264
10820
|
maxFeeRatio: maxFeeRatio ?? (minting ? poolDatum.mintingFeeRatio : poolDatum.redemptionFeeRatio)
|
|
10265
10821
|
};
|
|
10266
|
-
const assetsToSwap = minting ?
|
|
10822
|
+
const assetsToSwap = minting ? mkAssetsOf16(collateralAsset, amount) : mkAssetsOf16(
|
|
10267
10823
|
{
|
|
10268
|
-
currencySymbol:
|
|
10824
|
+
currencySymbol: fromHex20(
|
|
10269
10825
|
params.stableswapParams.iassetSymbol.unCurrencySymbol
|
|
10270
10826
|
),
|
|
10271
|
-
tokenName:
|
|
10827
|
+
tokenName: fromHex20(fromText15(iasset))
|
|
10272
10828
|
},
|
|
10273
10829
|
amount
|
|
10274
10830
|
);
|
|
@@ -10296,7 +10852,7 @@ async function createStableswapOrder(iasset, collateralAsset, amount, minting, p
|
|
|
10296
10852
|
kind: "inline",
|
|
10297
10853
|
value: serialiseStableswapOrderDatum(datum)
|
|
10298
10854
|
},
|
|
10299
|
-
|
|
10855
|
+
addAssets18(
|
|
10300
10856
|
assetsToSwap,
|
|
10301
10857
|
mkLovelacesOf9(
|
|
10302
10858
|
roundedExpectedOutputLovelaces + maxExecutionFee + additionalLovelaces
|
|
@@ -10364,7 +10920,7 @@ async function batchProcessStableswapOrders(stableswapOrderOrefs, stableswapPool
|
|
|
10364
10920
|
getInlineDatumOrThrow6(mainOrderUtxo)
|
|
10365
10921
|
);
|
|
10366
10922
|
const iassetAc = {
|
|
10367
|
-
currencySymbol:
|
|
10923
|
+
currencySymbol: fromHex20(
|
|
10368
10924
|
sysParams.stableswapParams.iassetSymbol.unCurrencySymbol
|
|
10369
10925
|
),
|
|
10370
10926
|
tokenName: mainOrderDatum.iasset
|
|
@@ -10510,9 +11066,9 @@ async function batchProcessStableswapOrders(stableswapOrderOrefs, stableswapPool
|
|
|
10510
11066
|
}
|
|
10511
11067
|
}
|
|
10512
11068
|
);
|
|
10513
|
-
const totalSwapInfo =
|
|
11069
|
+
const totalSwapInfo = F21.pipe(
|
|
10514
11070
|
ordersInfo,
|
|
10515
|
-
|
|
11071
|
+
A13.reduce(
|
|
10516
11072
|
{
|
|
10517
11073
|
suppliedCollateralAsset: 0n,
|
|
10518
11074
|
suppliedIasset: 0n,
|
|
@@ -10554,17 +11110,17 @@ async function batchProcessStableswapOrders(stableswapOrderOrefs, stableswapPool
|
|
|
10554
11110
|
kind: "inline",
|
|
10555
11111
|
value: serialiseStableswapPoolDatum(stableswapPoolDatum)
|
|
10556
11112
|
},
|
|
10557
|
-
collateralAmtChangePool != 0n ?
|
|
11113
|
+
collateralAmtChangePool != 0n ? addAssets18(
|
|
10558
11114
|
stableswapPoolUtxo.assets,
|
|
10559
|
-
|
|
11115
|
+
mkAssetsOf16(collateralAc, collateralAmtChangePool)
|
|
10560
11116
|
) : stableswapPoolUtxo.assets
|
|
10561
11117
|
).setMinFee(2055720n);
|
|
10562
11118
|
if (amountToMint !== 0n) {
|
|
10563
|
-
tx.mintAssets(
|
|
11119
|
+
tx.mintAssets(mkAssetsOf16(iassetAc, amountToMint), Data53.void());
|
|
10564
11120
|
}
|
|
10565
|
-
|
|
11121
|
+
F21.pipe(
|
|
10566
11122
|
ordersInfo,
|
|
10567
|
-
|
|
11123
|
+
A13.reduce(tx, (acc, orderInfo) => {
|
|
10568
11124
|
return acc.collectFrom(
|
|
10569
11125
|
[orderInfo.utxo],
|
|
10570
11126
|
isSameOutRef2(orderInfo.utxo, mainOrderUtxo) ? serialiseStableswapOrderRedeemer("BatchProcessStableswapOrders") : {
|
|
@@ -10588,13 +11144,13 @@ async function batchProcessStableswapOrders(stableswapOrderOrefs, stableswapPool
|
|
|
10588
11144
|
orderInfo.utxo
|
|
10589
11145
|
)
|
|
10590
11146
|
},
|
|
10591
|
-
|
|
11147
|
+
addAssets18(
|
|
10592
11148
|
// Currently, we always take the max execution fee from the order utxo.
|
|
10593
11149
|
// This can be improved so that we take the actual execution fee.
|
|
10594
11150
|
mkLovelacesOf9(
|
|
10595
11151
|
lovelacesAmt5(orderInfo.utxo.assets) - orderInfo.datum.maxExecutionFee
|
|
10596
11152
|
),
|
|
10597
|
-
orderInfo.swapInfo.owedIasset > 0 ?
|
|
11153
|
+
orderInfo.swapInfo.owedIasset > 0 ? mkAssetsOf16(iassetAc, orderInfo.swapInfo.owedIasset) : mkAssetsOf16(
|
|
10598
11154
|
collateralAc,
|
|
10599
11155
|
orderInfo.swapInfo.owedCollateralAsset
|
|
10600
11156
|
)
|
|
@@ -10694,6 +11250,7 @@ export {
|
|
|
10694
11250
|
InterestOracleDatumSchema,
|
|
10695
11251
|
InterestOracleParamsSchema,
|
|
10696
11252
|
MAX_E2S2S_ENTRIES_COUNT,
|
|
11253
|
+
MAX_REDEMPTIONS_WITH_CDP_OPEN,
|
|
10697
11254
|
MIN_ROB_COLLATERAL_AMT,
|
|
10698
11255
|
ONE_DAY,
|
|
10699
11256
|
ONE_HOUR,
|
|
@@ -10728,6 +11285,7 @@ export {
|
|
|
10728
11285
|
adjustStakingPosition,
|
|
10729
11286
|
alwaysFailValidator,
|
|
10730
11287
|
annulRequest,
|
|
11288
|
+
approximateLeverageRedemptions,
|
|
10731
11289
|
attachOracle,
|
|
10732
11290
|
balance,
|
|
10733
11291
|
batchCollectInterest,
|
|
@@ -10738,8 +11296,11 @@ export {
|
|
|
10738
11296
|
burnCdp,
|
|
10739
11297
|
calculateAccruedInterest,
|
|
10740
11298
|
calculateAdaReward,
|
|
11299
|
+
calculateCollateralRatioFromLeverage,
|
|
10741
11300
|
calculateIAssetRedemptionAmt,
|
|
11301
|
+
calculateLeverageFromCollateralRatio,
|
|
10742
11302
|
calculateMinCollateralCappedIAssetRedemptionAmt,
|
|
11303
|
+
calculateTotalCollateralForRedemption,
|
|
10743
11304
|
calculateUnitaryInterest,
|
|
10744
11305
|
calculateUnitaryInterestSinceOracleLastUpdated,
|
|
10745
11306
|
cancelRob,
|
|
@@ -10805,9 +11366,9 @@ export {
|
|
|
10805
11366
|
getAssetClassComparisonStr,
|
|
10806
11367
|
getInlineDatumOrThrow,
|
|
10807
11368
|
getPythFeedConfig,
|
|
10808
|
-
getRandomElement,
|
|
10809
11369
|
getUpdatedAccountDeposit,
|
|
10810
11370
|
handleOracleForCollateralAsset,
|
|
11371
|
+
iassetValueOfCollateral,
|
|
10811
11372
|
init,
|
|
10812
11373
|
initCDPCreator,
|
|
10813
11374
|
initCollector,
|
|
@@ -10821,8 +11382,10 @@ export {
|
|
|
10821
11382
|
initTreasury,
|
|
10822
11383
|
initializeAsset,
|
|
10823
11384
|
insertSorted,
|
|
11385
|
+
isBuyOrderFullyRedeemed,
|
|
10824
11386
|
isFullyRedeemed,
|
|
10825
11387
|
isSameEpochToScaleKey,
|
|
11388
|
+
leverageCdpWithRob,
|
|
10826
11389
|
liquidateCdp,
|
|
10827
11390
|
liquidationHelper,
|
|
10828
11391
|
loadSystemParamsFromFile,
|
|
@@ -10886,10 +11449,6 @@ export {
|
|
|
10886
11449
|
parseIAssetDatumOrThrow,
|
|
10887
11450
|
parseInterestCollectionDatum,
|
|
10888
11451
|
parseInterestOracleDatum,
|
|
10889
|
-
parsePollManager,
|
|
10890
|
-
parsePollManagerOrThrow,
|
|
10891
|
-
parsePollShard,
|
|
10892
|
-
parsePollShardOrThrow,
|
|
10893
11452
|
parsePriceOracleDatum,
|
|
10894
11453
|
parsePythStateDatum,
|
|
10895
11454
|
parseRobDatum,
|
|
@@ -10910,8 +11469,8 @@ export {
|
|
|
10910
11469
|
parseStakingPosition,
|
|
10911
11470
|
parseStakingPositionOrThrow,
|
|
10912
11471
|
partitionEpochToScaleToSums,
|
|
10913
|
-
pollPassQuorum,
|
|
10914
11472
|
processSpRequest,
|
|
11473
|
+
randomRobsSubsetSatisfyingTargetCollateral,
|
|
10915
11474
|
rationalAdd,
|
|
10916
11475
|
rationalCeil,
|
|
10917
11476
|
rationalDiv,
|
|
@@ -10930,7 +11489,10 @@ export {
|
|
|
10930
11489
|
requestSpAccountClosure,
|
|
10931
11490
|
requestSpAccountCreation,
|
|
10932
11491
|
rewardSnapshotPrecision,
|
|
10933
|
-
|
|
11492
|
+
robAmtToSpend,
|
|
11493
|
+
robBuyOrderSummary,
|
|
11494
|
+
robCollateralAmtToSpend,
|
|
11495
|
+
robIAssetAmtToSpend,
|
|
10934
11496
|
runCreateScriptRefTx,
|
|
10935
11497
|
runOneShotMintTx,
|
|
10936
11498
|
scriptRef,
|
|
@@ -10947,9 +11509,6 @@ export {
|
|
|
10947
11509
|
serialiseInterestCollectionDatum,
|
|
10948
11510
|
serialiseInterestCollectionRedeemer,
|
|
10949
11511
|
serialiseInterestOracleDatum,
|
|
10950
|
-
serialisePollDatum,
|
|
10951
|
-
serialisePollManagerRedeemer,
|
|
10952
|
-
serialisePollShardRedeemer,
|
|
10953
11512
|
serialisePriceOracleDatum,
|
|
10954
11513
|
serialisePriceOracleRedeemer,
|
|
10955
11514
|
serialisePythFeedParams,
|
|
@@ -10978,6 +11537,7 @@ export {
|
|
|
10978
11537
|
startPriceOracleTx,
|
|
10979
11538
|
submitTx,
|
|
10980
11539
|
sum,
|
|
11540
|
+
summarizeActualLeverageRedemptions,
|
|
10981
11541
|
toAssetClassFromLucid,
|
|
10982
11542
|
toDataDerivedPythPrice,
|
|
10983
11543
|
toSystemParamsAsset,
|