@indigo-labs/indigo-sdk 0.2.41 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/ci.yml +4 -2
- package/dist/index.d.mts +3008 -2194
- package/dist/index.d.ts +3008 -2194
- package/dist/index.js +9827 -6194
- package/dist/index.mjs +8591 -4809
- package/package.json +14 -3
- package/src/contracts/cdp/helpers.ts +68 -72
- package/src/contracts/cdp/scripts.ts +50 -13
- package/src/contracts/cdp/transactions.ts +831 -545
- package/src/contracts/cdp/types-new.ts +256 -0
- package/src/contracts/cdp/types.ts +26 -144
- package/src/contracts/cdp-creator/scripts.ts +15 -9
- package/src/contracts/cdp-creator/types-new.ts +50 -0
- package/src/contracts/cdp-creator/types.ts +5 -31
- package/src/contracts/collector/scripts.ts +1 -1
- package/src/contracts/collector/transactions.ts +23 -13
- package/src/contracts/collector/types-new.ts +17 -0
- package/src/contracts/execute/scripts.ts +19 -10
- package/src/contracts/execute/types-new.ts +44 -0
- package/src/contracts/execute/types.ts +5 -38
- package/src/contracts/gov/helpers.ts +187 -51
- package/src/contracts/gov/scripts.ts +17 -10
- package/src/contracts/gov/transactions.ts +599 -271
- package/src/contracts/gov/types-new.ts +253 -100
- package/src/contracts/gov/types.ts +4 -71
- package/src/contracts/iasset/helpers.ts +172 -0
- package/src/contracts/iasset/scripts.ts +38 -0
- package/src/contracts/iasset/types.ts +154 -0
- package/src/contracts/initialize/actions.ts +768 -0
- package/src/contracts/initialize/helpers.ts +611 -36
- package/src/contracts/initialize/types.ts +102 -28
- package/src/contracts/interest-collection/helpers.ts +19 -0
- package/src/contracts/interest-collection/scripts.ts +44 -0
- package/src/contracts/interest-collection/transactions.ts +436 -0
- package/src/contracts/interest-collection/types-new.ts +50 -0
- package/src/contracts/interest-collection/types.ts +26 -0
- package/src/contracts/interest-oracle/helpers.ts +2 -30
- package/src/contracts/interest-oracle/scripts.ts +1 -1
- package/src/contracts/interest-oracle/transactions.ts +21 -16
- package/src/contracts/interest-oracle/types-new.ts +32 -0
- package/src/contracts/interest-oracle/types.ts +1 -40
- package/src/contracts/one-shot/transactions.ts +1 -2
- package/src/contracts/poll/helpers.ts +5 -23
- package/src/contracts/poll/scripts.ts +12 -13
- package/src/contracts/poll/types-poll-manager.ts +1 -19
- package/src/contracts/poll/types-poll-new.ts +170 -0
- package/src/contracts/poll/types-poll-shard.ts +2 -24
- package/src/contracts/price-oracle/helpers.ts +1 -4
- package/src/contracts/price-oracle/scripts.ts +3 -8
- package/src/contracts/price-oracle/transactions.ts +32 -25
- package/src/contracts/price-oracle/types-new.ts +50 -0
- package/src/contracts/price-oracle/types.ts +2 -36
- package/src/contracts/pyth-feed/helpers.ts +58 -0
- package/src/contracts/pyth-feed/scripts.ts +15 -0
- package/src/contracts/pyth-feed/types.ts +181 -0
- package/src/contracts/rob/helpers.ts +405 -0
- package/src/contracts/rob/scripts.ts +35 -0
- package/src/contracts/rob/transactions.ts +410 -0
- package/src/contracts/rob/types-new.ts +128 -0
- package/src/contracts/rob/types.ts +16 -0
- package/src/contracts/rob-leverage/helpers.ts +424 -0
- package/src/contracts/{leverage → rob-leverage}/transactions.ts +68 -48
- package/src/contracts/stability-pool/helpers.ts +714 -230
- package/src/contracts/stability-pool/scripts.ts +20 -15
- package/src/contracts/stability-pool/transactions.ts +625 -495
- package/src/contracts/stability-pool/types-new.ts +237 -100
- package/src/contracts/stability-pool/types.ts +5 -22
- package/src/contracts/stableswap/helpers.ts +22 -0
- package/src/contracts/stableswap/scripts.ts +37 -0
- package/src/contracts/stableswap/transactions.ts +647 -0
- package/src/contracts/stableswap/types-new.ts +131 -0
- package/src/contracts/stableswap/types.ts +17 -0
- package/src/contracts/staking/helpers.ts +49 -34
- package/src/contracts/staking/scripts.ts +1 -1
- package/src/contracts/staking/transactions.ts +85 -130
- package/src/contracts/staking/types-new.ts +60 -28
- package/src/contracts/staking/types.ts +1 -28
- package/src/contracts/treasury/helpers.ts +21 -0
- package/src/contracts/treasury/scripts.ts +16 -26
- package/src/contracts/treasury/transactions.ts +256 -27
- package/src/contracts/treasury/types-new.ts +69 -0
- package/src/contracts/treasury/types.ts +2 -43
- package/src/contracts/version-registry/scripts.ts +2 -2
- package/src/contracts/version-registry/types-new.ts +6 -7
- package/src/index.ts +37 -20
- package/src/scripts/auth-token-policy.ts +3 -2
- package/src/scripts/iasset-policy.ts +3 -2
- package/src/types/evolution-schema-options.ts +3 -3
- package/src/types/generic.ts +17 -89
- package/src/types/multisig.ts +48 -0
- package/src/types/on-chain-decimal.ts +14 -7
- package/src/types/rational.ts +61 -0
- package/src/types/system-params.ts +237 -41
- package/src/utils/array-utils.ts +70 -1
- package/src/utils/bigint-utils.ts +12 -0
- package/src/utils/indigo-helpers.ts +8 -10
- package/src/utils/lucid-utils.ts +47 -40
- package/src/utils/oracle-helpers.ts +62 -0
- package/src/utils/pyth/decode.ts +223 -0
- package/src/utils/pyth/encode.ts +262 -0
- package/src/utils/pyth/index.ts +14 -0
- package/src/utils/pyth/types.ts +87 -0
- package/src/validators/always-succeed-validator.ts +6 -0
- package/src/validators/cdp-creator-validator.ts +2 -2
- package/src/validators/cdp-redeem-validator.ts +7 -0
- package/src/validators/cdp-validator.ts +2 -2
- package/src/validators/collector-validator.ts +2 -2
- package/src/validators/execute-validator.ts +2 -2
- package/src/validators/governance-validator.ts +2 -2
- package/src/validators/iasset-validator.ts +7 -0
- package/src/validators/interest-collection-validator.ts +7 -0
- package/src/validators/interest-oracle-validator.ts +2 -2
- package/src/validators/poll-manager-validator.ts +2 -2
- package/src/validators/poll-shard-validator.ts +2 -2
- package/src/validators/price-oracle-validator.ts +7 -0
- package/src/validators/pyth-feed-validator.ts +7 -0
- package/src/validators/rob-validator.ts +7 -0
- package/src/validators/stability-pool-validator.ts +2 -2
- package/src/validators/stableswap-validator.ts +7 -0
- package/src/validators/staking-validator.ts +2 -2
- package/src/validators/treasury-validator.ts +2 -2
- package/src/validators/version-record-policy.ts +2 -2
- package/src/validators/version-registry-validator.ts +2 -2
- package/tests/always-succeed/script.ts +7 -0
- package/tests/bigint-utils.test.ts +41 -0
- package/tests/cdp/actions.ts +611 -0
- package/tests/cdp/cdp-helpers.ts +55 -0
- package/tests/cdp/cdp-queries.ts +440 -0
- package/tests/cdp/cdp.test.ts +6087 -0
- package/tests/cdp/transactions-mutated.ts +1729 -0
- package/tests/data/system-params.json +177 -34
- package/tests/datums.test.ts +209 -210
- package/tests/endpoints/initialize.ts +68 -0
- package/tests/endpoints/interest-collector.ts +37 -0
- package/tests/endpoints/treasury.ts +70 -0
- package/tests/gov/actions.ts +406 -0
- package/tests/gov/gov.test.ts +4450 -0
- package/tests/{queries → gov}/governance-queries.ts +6 -3
- package/tests/hash-checks.test.ts +38 -11
- package/tests/indigo-test-helpers.ts +100 -0
- package/tests/initialize.test.ts +61 -9
- package/tests/interest-collection/interest-collection.test.ts +892 -0
- package/tests/interest-collection/interest-collector-queries.ts +49 -0
- package/tests/interest-collection/transactions-mutated.ts +260 -0
- package/tests/interest-oracle.test.ts +43 -35
- package/tests/mock/assets-mock.ts +234 -23
- package/tests/mock/protocol-params-mock.ts +21 -0
- package/tests/price-oracle/actions.ts +163 -0
- package/tests/price-oracle/price-oracle-queries.ts +12 -0
- package/tests/price-oracle/price-oracle.test.ts +240 -0
- package/tests/price-oracle/transactions-mutated.ts +62 -0
- package/tests/pyth/endpoints.ts +96 -0
- package/tests/pyth/helpers.ts +37 -0
- package/tests/pyth/pyth-encoding.test.ts +376 -0
- package/tests/pyth/pyth-indigo.test.ts +509 -0
- package/tests/pyth/pyth.test.ts +300 -0
- package/tests/queries/execute-queries.ts +6 -5
- package/tests/queries/iasset-queries.ts +175 -5
- package/tests/queries/interest-oracle-queries.ts +4 -2
- package/tests/queries/poll-queries.ts +8 -9
- package/tests/queries/stability-pool-queries.ts +95 -48
- package/tests/queries/staking-queries.ts +4 -2
- package/tests/queries/treasury-queries.ts +80 -5
- package/tests/rob/actions.ts +58 -0
- package/tests/{lrp-leverage.test.ts → rob/rob-leverage.test.ts} +393 -296
- package/tests/rob/rob-queries.ts +95 -0
- package/tests/rob/rob.test.ts +3762 -0
- package/tests/rob/transactions-mutated.ts +853 -0
- package/tests/script-size.test.ts +240 -0
- package/tests/setup.ts +135 -0
- package/tests/stability-pool/actions.ts +210 -0
- package/tests/stability-pool.test.ts +5469 -666
- package/tests/stableswap/stableswap-actions.ts +84 -0
- package/tests/stableswap/stableswap-queries.ts +89 -0
- package/tests/stableswap/stableswap.test.ts +3891 -0
- package/tests/stableswap/transactions-mutated.ts +348 -0
- package/tests/staking.test.ts +82 -99
- package/tests/test-helpers.ts +58 -11
- package/tests/treasury.test.ts +242 -0
- package/tests/utils/asserts.ts +74 -0
- package/tests/utils/benchmark-utils.ts +81 -0
- package/tests/utils/index.ts +122 -4
- package/tsconfig.json +9 -1
- package/vitest.config.ts +3 -1
- package/src/contracts/collector/types.ts +0 -16
- package/src/contracts/initialize/transactions.ts +0 -891
- package/src/contracts/leverage/helpers.ts +0 -424
- package/src/contracts/lrp/helpers.ts +0 -294
- package/src/contracts/lrp/scripts.ts +0 -27
- package/src/contracts/lrp/transactions.ts +0 -250
- package/src/contracts/lrp/types.ts +0 -131
- package/src/contracts/poll/types-poll.ts +0 -88
- package/src/contracts/vesting/helpers.ts +0 -218
- package/src/utils/value-helpers.ts +0 -37
- package/src/validators/lrp-validator.ts +0 -7
- package/tests/cdp.test.ts +0 -1528
- package/tests/gov.test.ts +0 -2011
- package/tests/lrp.test.ts +0 -673
- package/tests/queries/cdp-queries.ts +0 -220
- package/tests/queries/lrp-queries.ts +0 -76
- package/tests/queries/price-oracle-queries.ts +0 -10
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AddressSchema,
|
|
3
|
+
AssetClassSchema,
|
|
4
|
+
OutputReferenceSchema,
|
|
5
|
+
} from '@3rd-eye-labs/cardano-offchain-common';
|
|
6
|
+
import { TSchema, Data } from '@evolution-sdk/evolution';
|
|
7
|
+
import { DEFAULT_SCHEMA_OPTIONS } from '../../types/evolution-schema-options';
|
|
8
|
+
import { option as O, function as F } from 'fp-ts';
|
|
9
|
+
import { Schema } from 'effect';
|
|
10
|
+
import { RationalSchema } from '../../types/rational';
|
|
11
|
+
|
|
12
|
+
const OpaqueData = Schema.typeSchema(Data.DataSchema);
|
|
13
|
+
|
|
14
|
+
export const StableswapOrderDatumSchema = TSchema.Struct({
|
|
15
|
+
iasset: TSchema.ByteArray,
|
|
16
|
+
collateralAsset: AssetClassSchema,
|
|
17
|
+
owner: TSchema.ByteArray,
|
|
18
|
+
destination: AddressSchema,
|
|
19
|
+
destinationInlineDatum: TSchema.NullOr(OpaqueData),
|
|
20
|
+
maxExecutionFee: TSchema.Integer,
|
|
21
|
+
maxFeeRatio: RationalSchema,
|
|
22
|
+
});
|
|
23
|
+
export type StableswapOrderDatum = typeof StableswapOrderDatumSchema.Type;
|
|
24
|
+
|
|
25
|
+
const StableswapOrderRedeemerSchema = TSchema.Union(
|
|
26
|
+
TSchema.Literal('BatchProcessStableswapOrders', { flatInUnion: true }),
|
|
27
|
+
TSchema.Struct(
|
|
28
|
+
{
|
|
29
|
+
BatchAuxiliary: TSchema.Struct(
|
|
30
|
+
{
|
|
31
|
+
ownInputIndex: TSchema.Integer,
|
|
32
|
+
mainOrderInputIndex: TSchema.Integer,
|
|
33
|
+
},
|
|
34
|
+
{ flatFields: true },
|
|
35
|
+
),
|
|
36
|
+
},
|
|
37
|
+
{ flatInUnion: true },
|
|
38
|
+
),
|
|
39
|
+
TSchema.Literal('CancelStableswapOrder', { flatInUnion: true }),
|
|
40
|
+
TSchema.Literal('UpdateFees', { flatInUnion: true }),
|
|
41
|
+
TSchema.Literal('UpgradeVersion', { flatInUnion: true }),
|
|
42
|
+
);
|
|
43
|
+
|
|
44
|
+
export type StableswapOrderRedeemer = typeof StableswapOrderRedeemerSchema.Type;
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* This is used when order does not require any specific output datum.
|
|
48
|
+
*/
|
|
49
|
+
const StableswapOutputDatumSchema = TSchema.Tuple([
|
|
50
|
+
TSchema.ByteArray,
|
|
51
|
+
OutputReferenceSchema,
|
|
52
|
+
]);
|
|
53
|
+
|
|
54
|
+
export type StableswapOutputDatum = typeof StableswapOutputDatumSchema.Type;
|
|
55
|
+
|
|
56
|
+
export function serialiseStableswapOrderDatum(d: StableswapOrderDatum): string {
|
|
57
|
+
return Data.withSchema(
|
|
58
|
+
StableswapOrderDatumSchema,
|
|
59
|
+
DEFAULT_SCHEMA_OPTIONS,
|
|
60
|
+
).toCBORHex(d);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export function parseStableswapOrderDatum(
|
|
64
|
+
datum: string,
|
|
65
|
+
): O.Option<StableswapOrderDatum> {
|
|
66
|
+
try {
|
|
67
|
+
return O.some(
|
|
68
|
+
Data.withSchema(
|
|
69
|
+
StableswapOrderDatumSchema,
|
|
70
|
+
DEFAULT_SCHEMA_OPTIONS,
|
|
71
|
+
).fromCBORHex(datum),
|
|
72
|
+
);
|
|
73
|
+
} catch (_) {
|
|
74
|
+
return O.none;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export function parseStableswapOrderDatumOrThrow(
|
|
79
|
+
datum: string,
|
|
80
|
+
): StableswapOrderDatum {
|
|
81
|
+
return F.pipe(
|
|
82
|
+
parseStableswapOrderDatum(datum),
|
|
83
|
+
O.match(() => {
|
|
84
|
+
throw new Error('Expected a Stableswap Order datum.');
|
|
85
|
+
}, F.identity),
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export function serialiseStableswapOutputDatum(
|
|
90
|
+
d: StableswapOutputDatum,
|
|
91
|
+
): string {
|
|
92
|
+
return Data.withSchema(
|
|
93
|
+
StableswapOutputDatumSchema,
|
|
94
|
+
DEFAULT_SCHEMA_OPTIONS,
|
|
95
|
+
).toCBORHex(d);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export function serialiseStableswapOrderRedeemer(
|
|
99
|
+
r: StableswapOrderRedeemer,
|
|
100
|
+
): string {
|
|
101
|
+
return Data.withSchema(
|
|
102
|
+
StableswapOrderRedeemerSchema,
|
|
103
|
+
DEFAULT_SCHEMA_OPTIONS,
|
|
104
|
+
).toCBORHex(r);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export function parseStableswapOrderRedeemer(
|
|
108
|
+
redeemerCborHex: string,
|
|
109
|
+
): O.Option<StableswapOrderRedeemer> {
|
|
110
|
+
try {
|
|
111
|
+
return O.some(
|
|
112
|
+
Data.withSchema(
|
|
113
|
+
StableswapOrderRedeemerSchema,
|
|
114
|
+
DEFAULT_SCHEMA_OPTIONS,
|
|
115
|
+
).fromCBORHex(redeemerCborHex),
|
|
116
|
+
);
|
|
117
|
+
} catch (_) {
|
|
118
|
+
return O.none;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export function parseStableswapOrderRedeemerOrThrow(
|
|
123
|
+
redeemerCborHex: string,
|
|
124
|
+
): StableswapOrderRedeemer {
|
|
125
|
+
return F.pipe(
|
|
126
|
+
parseStableswapOrderRedeemer(redeemerCborHex),
|
|
127
|
+
O.match(() => {
|
|
128
|
+
throw new Error('Expected a Stableswap Order redeemer.');
|
|
129
|
+
}, F.identity),
|
|
130
|
+
);
|
|
131
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Data } from '@lucid-evolution/lucid';
|
|
2
|
+
import { AssetClassSchema } from '../../types/generic';
|
|
3
|
+
|
|
4
|
+
const StableswapParamsSchema = Data.Object({
|
|
5
|
+
iassetSymbol: Data.Bytes(),
|
|
6
|
+
cdpToken: AssetClassSchema,
|
|
7
|
+
versionRecordToken: AssetClassSchema,
|
|
8
|
+
cdpValHash: Data.Bytes(),
|
|
9
|
+
treasuryValHash: Data.Bytes(),
|
|
10
|
+
});
|
|
11
|
+
export type StableswapParams = Data.Static<typeof StableswapParamsSchema>;
|
|
12
|
+
export const StableswapParams =
|
|
13
|
+
StableswapParamsSchema as unknown as StableswapParams;
|
|
14
|
+
|
|
15
|
+
export function castStableswapParams(params: StableswapParams): Data {
|
|
16
|
+
return Data.castTo(params, StableswapParams);
|
|
17
|
+
}
|
|
@@ -1,4 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
fromText,
|
|
3
|
+
OutRef,
|
|
4
|
+
UTxO,
|
|
5
|
+
validatorToScriptHash,
|
|
6
|
+
} from '@lucid-evolution/lucid';
|
|
2
7
|
import { LucidEvolution } from '@lucid-evolution/lucid';
|
|
3
8
|
import { SystemParams } from '../../types/system-params';
|
|
4
9
|
import {
|
|
@@ -8,11 +13,7 @@ import {
|
|
|
8
13
|
StakingPosition,
|
|
9
14
|
StakingPosLockedAmt,
|
|
10
15
|
} from './types-new';
|
|
11
|
-
import {
|
|
12
|
-
createScriptAddress,
|
|
13
|
-
resolveUtxo,
|
|
14
|
-
UtxoOrOutRef,
|
|
15
|
-
} from '../../utils/lucid-utils';
|
|
16
|
+
import { createScriptAddress } from '../../utils/lucid-utils';
|
|
16
17
|
import { mkStakingValidatorFromSP } from './scripts';
|
|
17
18
|
import { OCD_DECIMAL_UNIT } from '../../types/on-chain-decimal';
|
|
18
19
|
|
|
@@ -29,27 +30,33 @@ export function updateStakingLockedAmount(
|
|
|
29
30
|
stakingPosLockedAmt: StakingPosLockedAmt,
|
|
30
31
|
currentTime: bigint,
|
|
31
32
|
): StakingPosLockedAmt {
|
|
32
|
-
return
|
|
33
|
-
|
|
34
|
-
.entries()
|
|
35
|
-
.filter(([_, { votingEnd }]) => votingEnd > currentTime),
|
|
33
|
+
return stakingPosLockedAmt.filter(
|
|
34
|
+
([_, { votingEnd }]) => votingEnd > currentTime,
|
|
36
35
|
);
|
|
37
36
|
}
|
|
38
37
|
|
|
39
|
-
export
|
|
40
|
-
|
|
38
|
+
export function findStakingManagerByOutRef(
|
|
39
|
+
stakingManagerRef: OutRef,
|
|
41
40
|
lucid: LucidEvolution,
|
|
42
41
|
): Promise<StakingManagerOutput> {
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
42
|
+
return lucid
|
|
43
|
+
.utxosByOutRef([stakingManagerRef])
|
|
44
|
+
.then((utxos) =>
|
|
45
|
+
utxos
|
|
46
|
+
.map((utxo) => {
|
|
47
|
+
if (!utxo.datum) return undefined;
|
|
48
|
+
const datum = parseStakingManagerDatum(utxo.datum);
|
|
49
|
+
return { utxo, datum };
|
|
50
|
+
})
|
|
51
|
+
.find((utxo) => utxo !== undefined),
|
|
52
|
+
)
|
|
53
|
+
.then((result) => {
|
|
54
|
+
if (!result)
|
|
55
|
+
throw new Error(
|
|
56
|
+
'Unable to locate Staking Manager by output reference.',
|
|
57
|
+
);
|
|
58
|
+
return result;
|
|
59
|
+
});
|
|
53
60
|
}
|
|
54
61
|
|
|
55
62
|
export function findStakingManager(
|
|
@@ -83,20 +90,28 @@ export function findStakingManager(
|
|
|
83
90
|
});
|
|
84
91
|
}
|
|
85
92
|
|
|
86
|
-
export
|
|
87
|
-
|
|
93
|
+
export function findStakingPositionByOutRef(
|
|
94
|
+
stakingPositionRef: OutRef,
|
|
88
95
|
lucid: LucidEvolution,
|
|
89
96
|
): Promise<StakingPositionOutput> {
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
97
|
+
return lucid
|
|
98
|
+
.utxosByOutRef([stakingPositionRef])
|
|
99
|
+
.then((utxos) =>
|
|
100
|
+
utxos
|
|
101
|
+
.map((utxo) => {
|
|
102
|
+
if (!utxo.datum) return undefined;
|
|
103
|
+
const datum = parseStakingPositionOrThrow(utxo.datum);
|
|
104
|
+
return { utxo, datum };
|
|
105
|
+
})
|
|
106
|
+
.find((utxo) => utxo !== undefined),
|
|
107
|
+
)
|
|
108
|
+
.then((result) => {
|
|
109
|
+
if (!result)
|
|
110
|
+
throw new Error(
|
|
111
|
+
'Unable to locate Staking Position by output reference.',
|
|
112
|
+
);
|
|
113
|
+
return result;
|
|
114
|
+
});
|
|
100
115
|
}
|
|
101
116
|
|
|
102
117
|
export const rewardSnapshotPrecision = OCD_DECIMAL_UNIT * OCD_DECIMAL_UNIT;
|
|
@@ -10,49 +10,46 @@ import {
|
|
|
10
10
|
TxBuilder,
|
|
11
11
|
} from '@lucid-evolution/lucid';
|
|
12
12
|
import {
|
|
13
|
+
fromSystemParamsAsset,
|
|
13
14
|
fromSystemParamsScriptRef,
|
|
14
15
|
SystemParams,
|
|
15
16
|
} from '../../types/system-params';
|
|
17
|
+
import { addrDetails } from '../../utils/lucid-utils';
|
|
16
18
|
import {
|
|
17
|
-
|
|
18
|
-
getInlineDatumOrThrow,
|
|
19
|
-
resolveUtxo,
|
|
20
|
-
UtxoOrOutRef,
|
|
21
|
-
} from '../../utils/lucid-utils';
|
|
22
|
-
import {
|
|
19
|
+
calculateAdaReward,
|
|
23
20
|
distributeReward,
|
|
24
|
-
|
|
21
|
+
findStakingManager,
|
|
22
|
+
findStakingManagerByOutRef,
|
|
23
|
+
findStakingPositionByOutRef,
|
|
25
24
|
updateStakingLockedAmount,
|
|
26
25
|
} from './helpers';
|
|
27
26
|
import {
|
|
28
27
|
parseStakingManagerDatum,
|
|
29
|
-
parseStakingPositionOrThrow,
|
|
30
28
|
serialiseStakingDatum,
|
|
29
|
+
serialiseStakingRedeemer,
|
|
31
30
|
StakingManager,
|
|
32
31
|
StakingPosition,
|
|
33
32
|
} from './types-new';
|
|
34
33
|
import { matchSingle } from '../../utils/utils';
|
|
35
|
-
import {
|
|
36
|
-
import {
|
|
37
|
-
|
|
38
|
-
|
|
34
|
+
import { serialiseCollectorRedeemer } from '../collector/types-new';
|
|
35
|
+
import {
|
|
36
|
+
assetClassValueOf,
|
|
37
|
+
getInlineDatumOrThrow,
|
|
38
|
+
mkAssetsOf,
|
|
39
|
+
mkLovelacesOf,
|
|
40
|
+
} from '@3rd-eye-labs/cardano-offchain-common';
|
|
39
41
|
|
|
40
42
|
export async function openStakingPosition(
|
|
41
43
|
amount: bigint,
|
|
42
44
|
params: SystemParams,
|
|
43
45
|
lucid: LucidEvolution,
|
|
44
|
-
|
|
46
|
+
stakingManagerRef?: OutRef,
|
|
45
47
|
): Promise<TxBuilder> {
|
|
46
48
|
const [pkh, _] = await addrDetails(lucid);
|
|
47
49
|
|
|
48
|
-
const
|
|
49
|
-
|
|
50
|
-
lucid
|
|
51
|
-
'Expected a single staking manager UTXO',
|
|
52
|
-
);
|
|
53
|
-
const stakingManagerDatum = parseStakingManagerDatum(
|
|
54
|
-
getInlineDatumOrThrow(stakingManagerUtxo),
|
|
55
|
-
);
|
|
50
|
+
const stakingManagerOut = stakingManagerRef
|
|
51
|
+
? await findStakingManagerByOutRef(stakingManagerRef, lucid)
|
|
52
|
+
: await findStakingManager(params, lucid);
|
|
56
53
|
|
|
57
54
|
const stakingRefScriptUtxo = matchSingle(
|
|
58
55
|
await lucid.utxosByOutRef([
|
|
@@ -70,17 +67,17 @@ export async function openStakingPosition(
|
|
|
70
67
|
);
|
|
71
68
|
|
|
72
69
|
const newStakingManagerDatum: StakingManager = {
|
|
73
|
-
totalStake:
|
|
70
|
+
totalStake: stakingManagerOut.datum.totalStake + amount,
|
|
74
71
|
managerSnapshot: {
|
|
75
|
-
snapshotAda:
|
|
72
|
+
snapshotAda: stakingManagerOut.datum.managerSnapshot.snapshotAda,
|
|
76
73
|
},
|
|
77
74
|
};
|
|
78
75
|
|
|
79
76
|
const stakingPositionDatum: StakingPosition = {
|
|
80
77
|
owner: fromHex(pkh.hash),
|
|
81
|
-
lockedAmount:
|
|
78
|
+
lockedAmount: [],
|
|
82
79
|
positionSnapshot: {
|
|
83
|
-
snapshotAda:
|
|
80
|
+
snapshotAda: stakingManagerOut.datum.managerSnapshot.snapshotAda,
|
|
84
81
|
},
|
|
85
82
|
};
|
|
86
83
|
|
|
@@ -93,19 +90,19 @@ export async function openStakingPosition(
|
|
|
93
90
|
return lucid
|
|
94
91
|
.newTx()
|
|
95
92
|
.collectFrom(
|
|
96
|
-
[
|
|
93
|
+
[stakingManagerOut.utxo],
|
|
97
94
|
serialiseStakingRedeemer({
|
|
98
|
-
CreateStakingPosition: { creatorPkh: pkh.hash },
|
|
95
|
+
CreateStakingPosition: { creatorPkh: fromHex(pkh.hash) },
|
|
99
96
|
}),
|
|
100
97
|
)
|
|
101
98
|
.readFrom([stakingRefScriptUtxo])
|
|
102
99
|
.pay.ToContract(
|
|
103
|
-
|
|
100
|
+
stakingManagerOut.utxo.address,
|
|
104
101
|
{
|
|
105
102
|
kind: 'inline',
|
|
106
103
|
value: serialiseStakingDatum(newStakingManagerDatum),
|
|
107
104
|
},
|
|
108
|
-
|
|
105
|
+
stakingManagerOut.utxo.assets,
|
|
109
106
|
)
|
|
110
107
|
.readFrom([stakingTokenPolicyRefScriptUtxo])
|
|
111
108
|
.mintAssets(
|
|
@@ -115,7 +112,7 @@ export async function openStakingPosition(
|
|
|
115
112
|
Data.void(),
|
|
116
113
|
)
|
|
117
114
|
.pay.ToContract(
|
|
118
|
-
|
|
115
|
+
stakingManagerOut.utxo.address,
|
|
119
116
|
{
|
|
120
117
|
kind: 'inline',
|
|
121
118
|
value: serialiseStakingDatum(stakingPositionDatum),
|
|
@@ -129,33 +126,23 @@ export async function openStakingPosition(
|
|
|
129
126
|
}
|
|
130
127
|
|
|
131
128
|
export async function adjustStakingPosition(
|
|
132
|
-
|
|
129
|
+
stakingPositionRef: OutRef,
|
|
133
130
|
amount: bigint,
|
|
134
131
|
params: SystemParams,
|
|
135
132
|
lucid: LucidEvolution,
|
|
136
133
|
currentSlot: number,
|
|
137
|
-
|
|
134
|
+
stakingManagerRef?: OutRef,
|
|
138
135
|
): Promise<TxBuilder> {
|
|
139
136
|
const network = lucid.config().network!;
|
|
140
|
-
const
|
|
141
|
-
|
|
142
|
-
const stakingPositionUtxo = await resolveUtxo(
|
|
143
|
-
stakingPosition,
|
|
144
|
-
lucid,
|
|
145
|
-
'Expected a single staking position UTXO',
|
|
146
|
-
);
|
|
147
|
-
const stakingPositionDatum = parseStakingPositionOrThrow(
|
|
148
|
-
getInlineDatumOrThrow(stakingPositionUtxo),
|
|
149
|
-
);
|
|
137
|
+
const now = BigInt(slotToUnixTime(network, currentSlot));
|
|
150
138
|
|
|
151
|
-
const
|
|
152
|
-
|
|
139
|
+
const stakingPositionOut = await findStakingPositionByOutRef(
|
|
140
|
+
stakingPositionRef,
|
|
153
141
|
lucid,
|
|
154
|
-
'Expected a single staking manager UTXO',
|
|
155
|
-
);
|
|
156
|
-
const stakingManagerDatum = parseStakingManagerDatum(
|
|
157
|
-
getInlineDatumOrThrow(stakingManagerUtxo),
|
|
158
142
|
);
|
|
143
|
+
const stakingManagerOut = stakingManagerRef
|
|
144
|
+
? await findStakingManagerByOutRef(stakingManagerRef, lucid)
|
|
145
|
+
: await findStakingManager(params, lucid);
|
|
159
146
|
|
|
160
147
|
const stakingRefScriptUtxo = matchSingle(
|
|
161
148
|
await lucid.utxosByOutRef([
|
|
@@ -164,98 +151,75 @@ export async function adjustStakingPosition(
|
|
|
164
151
|
(_) => new Error('Expected a single staking Ref Script UTXO'),
|
|
165
152
|
);
|
|
166
153
|
|
|
167
|
-
const indyToken =
|
|
168
|
-
params.stakingParams.indyToken[0].unCurrencySymbol +
|
|
169
|
-
fromText(params.stakingParams.indyToken[1].unTokenName);
|
|
154
|
+
const indyToken = fromSystemParamsAsset(params.stakingParams.indyToken);
|
|
170
155
|
|
|
171
|
-
const
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
rewardSnapshotPrecision;
|
|
156
|
+
const adaReward = calculateAdaReward(
|
|
157
|
+
stakingManagerOut.datum.managerSnapshot.snapshotAda,
|
|
158
|
+
stakingPositionOut.datum.positionSnapshot.snapshotAda,
|
|
159
|
+
assetClassValueOf(stakingPositionOut.utxo.assets, indyToken),
|
|
160
|
+
);
|
|
177
161
|
|
|
178
162
|
const newLockedAmount = updateStakingLockedAmount(
|
|
179
|
-
|
|
180
|
-
BigInt(
|
|
163
|
+
stakingPositionOut.datum.lockedAmount,
|
|
164
|
+
BigInt(now),
|
|
181
165
|
);
|
|
182
166
|
|
|
183
167
|
return lucid
|
|
184
168
|
.newTx()
|
|
185
|
-
.validFrom(
|
|
169
|
+
.validFrom(Number(now))
|
|
186
170
|
.readFrom([stakingRefScriptUtxo])
|
|
187
171
|
.collectFrom(
|
|
188
|
-
[
|
|
172
|
+
[stakingPositionOut.utxo],
|
|
189
173
|
serialiseStakingRedeemer({
|
|
190
174
|
AdjustStakedAmount: { adjustAmount: amount },
|
|
191
175
|
}),
|
|
192
176
|
)
|
|
193
177
|
.collectFrom(
|
|
194
|
-
[
|
|
178
|
+
[stakingManagerOut.utxo],
|
|
195
179
|
serialiseStakingRedeemer('UpdateTotalStake'),
|
|
196
180
|
)
|
|
197
181
|
.pay.ToContract(
|
|
198
|
-
|
|
182
|
+
stakingManagerOut.utxo.address,
|
|
199
183
|
{
|
|
200
184
|
kind: 'inline',
|
|
201
185
|
value: serialiseStakingDatum({
|
|
202
|
-
...
|
|
203
|
-
totalStake:
|
|
186
|
+
...stakingManagerOut.datum,
|
|
187
|
+
totalStake: stakingManagerOut.datum.totalStake + amount,
|
|
204
188
|
}),
|
|
205
189
|
},
|
|
206
|
-
addAssets(
|
|
190
|
+
addAssets(stakingManagerOut.utxo.assets, mkLovelacesOf(-adaReward)),
|
|
207
191
|
)
|
|
208
192
|
.pay.ToContract(
|
|
209
|
-
|
|
193
|
+
stakingPositionOut.utxo.address,
|
|
210
194
|
{
|
|
211
195
|
kind: 'inline',
|
|
212
196
|
value: serialiseStakingDatum({
|
|
213
|
-
...
|
|
197
|
+
...stakingPositionOut.datum,
|
|
214
198
|
lockedAmount: newLockedAmount,
|
|
215
|
-
positionSnapshot: stakingManagerDatum.managerSnapshot,
|
|
216
199
|
}),
|
|
217
200
|
},
|
|
218
|
-
addAssets(
|
|
219
|
-
stakingPositionUtxo.assets,
|
|
220
|
-
mkAssetsOf(
|
|
221
|
-
{
|
|
222
|
-
currencySymbol: params.stakingParams.indyToken[0].unCurrencySymbol,
|
|
223
|
-
tokenName: fromText(params.stakingParams.indyToken[1].unTokenName),
|
|
224
|
-
},
|
|
225
|
-
amount,
|
|
226
|
-
),
|
|
227
|
-
),
|
|
201
|
+
addAssets(stakingPositionOut.utxo.assets, mkAssetsOf(indyToken, amount)),
|
|
228
202
|
)
|
|
229
|
-
.addSignerKey(toHex(
|
|
203
|
+
.addSignerKey(toHex(stakingPositionOut.datum.owner));
|
|
230
204
|
}
|
|
231
205
|
|
|
232
206
|
export async function closeStakingPosition(
|
|
233
|
-
|
|
207
|
+
stakingPositionRef: OutRef,
|
|
234
208
|
params: SystemParams,
|
|
235
209
|
lucid: LucidEvolution,
|
|
236
210
|
currentSlot: number,
|
|
237
|
-
|
|
211
|
+
stakingManagerRef?: OutRef,
|
|
238
212
|
): Promise<TxBuilder> {
|
|
239
213
|
const network = lucid.config().network!;
|
|
240
|
-
const
|
|
241
|
-
|
|
242
|
-
const stakingPositionUtxo = await resolveUtxo(
|
|
243
|
-
stakingPosition,
|
|
244
|
-
lucid,
|
|
245
|
-
'Expected a single staking position UTXO',
|
|
246
|
-
);
|
|
247
|
-
const stakingPositionDatum = parseStakingPositionOrThrow(
|
|
248
|
-
getInlineDatumOrThrow(stakingPositionUtxo),
|
|
249
|
-
);
|
|
214
|
+
const now = BigInt(slotToUnixTime(network, currentSlot));
|
|
250
215
|
|
|
251
|
-
const
|
|
252
|
-
|
|
216
|
+
const stakingPositionOut = await findStakingPositionByOutRef(
|
|
217
|
+
stakingPositionRef,
|
|
253
218
|
lucid,
|
|
254
|
-
'Expected a single staking manager UTXO',
|
|
255
|
-
);
|
|
256
|
-
const stakingManagerDatum = parseStakingManagerDatum(
|
|
257
|
-
getInlineDatumOrThrow(stakingManagerUtxo),
|
|
258
219
|
);
|
|
220
|
+
const stakingManagerOut = stakingManagerRef
|
|
221
|
+
? await findStakingManagerByOutRef(stakingManagerRef, lucid)
|
|
222
|
+
: await findStakingManager(params, lucid);
|
|
259
223
|
|
|
260
224
|
const stakingRefScriptUtxo = matchSingle(
|
|
261
225
|
await lucid.utxosByOutRef([
|
|
@@ -272,76 +236,67 @@ export async function closeStakingPosition(
|
|
|
272
236
|
(_) => new Error('Expected a single staking token policy Ref Script UTXO'),
|
|
273
237
|
);
|
|
274
238
|
|
|
275
|
-
const
|
|
276
|
-
params.stakingParams.stakingToken[0].unCurrencySymbol +
|
|
277
|
-
fromText(params.stakingParams.stakingToken[1].unTokenName);
|
|
278
|
-
const indyToken =
|
|
279
|
-
params.stakingParams.indyToken[0].unCurrencySymbol +
|
|
280
|
-
fromText(params.stakingParams.indyToken[1].unTokenName);
|
|
239
|
+
const indyToken = fromSystemParamsAsset(params.stakingParams.indyToken);
|
|
281
240
|
|
|
282
|
-
const existingIndyAmount =
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
241
|
+
const existingIndyAmount = assetClassValueOf(
|
|
242
|
+
stakingPositionOut.utxo.assets,
|
|
243
|
+
indyToken,
|
|
244
|
+
);
|
|
245
|
+
const adaReward = calculateAdaReward(
|
|
246
|
+
stakingManagerOut.datum.managerSnapshot.snapshotAda,
|
|
247
|
+
stakingPositionOut.datum.positionSnapshot.snapshotAda,
|
|
248
|
+
assetClassValueOf(stakingPositionOut.utxo.assets, indyToken),
|
|
249
|
+
);
|
|
288
250
|
|
|
289
251
|
return lucid
|
|
290
252
|
.newTx()
|
|
291
|
-
.validFrom(
|
|
253
|
+
.validFrom(Number(now))
|
|
292
254
|
.readFrom([stakingRefScriptUtxo, stakingTokenPolicyRefScriptUtxo])
|
|
293
|
-
.collectFrom([
|
|
255
|
+
.collectFrom([stakingPositionOut.utxo], serialiseStakingRedeemer('Unstake'))
|
|
294
256
|
.collectFrom(
|
|
295
|
-
[
|
|
257
|
+
[stakingManagerOut.utxo],
|
|
296
258
|
serialiseStakingRedeemer('UpdateTotalStake'),
|
|
297
259
|
)
|
|
298
260
|
.pay.ToContract(
|
|
299
|
-
|
|
261
|
+
stakingManagerOut.utxo.address,
|
|
300
262
|
{
|
|
301
263
|
kind: 'inline',
|
|
302
264
|
value: serialiseStakingDatum({
|
|
303
|
-
...
|
|
304
|
-
totalStake:
|
|
265
|
+
...stakingManagerOut.datum,
|
|
266
|
+
totalStake: stakingManagerOut.datum.totalStake - existingIndyAmount,
|
|
305
267
|
}),
|
|
306
268
|
},
|
|
307
|
-
addAssets(
|
|
269
|
+
addAssets(stakingManagerOut.utxo.assets, mkLovelacesOf(-adaReward)),
|
|
308
270
|
)
|
|
309
271
|
.mintAssets(
|
|
310
|
-
|
|
311
|
-
[stakingToken]: -1n,
|
|
312
|
-
},
|
|
272
|
+
mkAssetsOf(fromSystemParamsAsset(params.stakingParams.stakingToken), -1n),
|
|
313
273
|
Data.void(),
|
|
314
274
|
)
|
|
315
|
-
.addSignerKey(toHex(
|
|
275
|
+
.addSignerKey(toHex(stakingPositionOut.datum.owner));
|
|
316
276
|
}
|
|
317
277
|
|
|
318
278
|
const MIN_UTXO_AMOUNT = 2_000_000n;
|
|
319
279
|
|
|
320
280
|
export async function distributeAda(
|
|
321
|
-
|
|
281
|
+
stakingManagerRef: OutRef,
|
|
322
282
|
collectorRefs: OutRef[],
|
|
323
283
|
params: SystemParams,
|
|
324
284
|
lucid: LucidEvolution,
|
|
325
285
|
): Promise<TxBuilder> {
|
|
326
|
-
const stakingManagerUtxo = await
|
|
327
|
-
stakingManager,
|
|
328
|
-
lucid,
|
|
329
|
-
'Expected a single staking manager UTXO',
|
|
330
|
-
);
|
|
286
|
+
const [stakingManagerUtxo] = await lucid.utxosByOutRef([stakingManagerRef]);
|
|
331
287
|
const stakingManagerDatum = parseStakingManagerDatum(
|
|
332
288
|
getInlineDatumOrThrow(stakingManagerUtxo),
|
|
333
289
|
);
|
|
334
|
-
|
|
335
290
|
const collectorUtxos = (await lucid.utxosByOutRef(collectorRefs))
|
|
336
291
|
.filter((utxo) => utxo.datum && utxo.datum === Data.void())
|
|
337
|
-
.filter((utxo) => utxo.assets
|
|
292
|
+
.filter((utxo) => utxo.assets['lovelace'] > MIN_UTXO_AMOUNT);
|
|
338
293
|
|
|
339
294
|
if (collectorUtxos.length === 0) {
|
|
340
295
|
throw new Error('No available collectors found');
|
|
341
296
|
}
|
|
342
297
|
|
|
343
298
|
const adaRewardCollected = collectorUtxos.reduce(
|
|
344
|
-
(acc, utxo) => acc + utxo.assets
|
|
299
|
+
(acc, utxo) => acc + utxo.assets['lovelace'] - MIN_UTXO_AMOUNT,
|
|
345
300
|
0n,
|
|
346
301
|
);
|
|
347
302
|
const newSnapshot = distributeReward(
|