@indigo-labs/indigo-sdk 0.1.21 → 0.1.22
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 +3 -8
- package/.github/workflows/test.yml +44 -0
- package/dist/index.d.mts +670 -1291
- package/dist/index.d.ts +670 -1291
- package/dist/index.js +2235 -4669
- package/dist/index.mjs +2216 -4618
- package/eslint.config.mjs +1 -7
- package/package.json +4 -9
- package/src/contracts/cdp.ts +746 -0
- package/src/contracts/collector.ts +98 -0
- package/src/contracts/gov.ts +1 -0
- package/src/contracts/interest-oracle.ts +149 -0
- package/src/contracts/{lrp/transactions.ts → lrp.ts} +14 -14
- package/src/contracts/{one-shot/transactions.ts → one-shot.ts} +3 -3
- package/src/contracts/stability-pool.ts +690 -0
- package/src/contracts/staking.ts +348 -0
- package/src/contracts/treasury.ts +112 -0
- package/src/helpers/asset-helpers.ts +57 -0
- package/src/{utils → helpers}/helper-txs.ts +0 -1
- package/src/{utils/utils.ts → helpers/helpers.ts} +10 -0
- package/src/{contracts/interest-oracle/helpers.ts → helpers/interest-oracle.ts} +9 -37
- package/src/{contracts/stability-pool/helpers.ts → helpers/stability-pool-helpers.ts} +6 -110
- package/src/helpers/staking-helpers.ts +94 -0
- package/src/helpers/time-helpers.ts +4 -0
- package/src/{utils → helpers}/value-helpers.ts +0 -10
- package/src/index.ts +33 -38
- package/src/{validators → scripts}/cdp-creator-validator.ts +50 -4
- package/src/{validators → scripts}/cdp-validator.ts +5 -3
- package/src/{validators → scripts}/collector-validator.ts +3 -2
- package/src/scripts/execute-validator.ts +52 -0
- package/src/{validators/governance-validator.ts → scripts/gov-validator.ts} +40 -3
- package/src/{validators → scripts}/interest-oracle-validator.ts +20 -4
- package/src/scripts/lrp-validator.ts +40 -0
- package/src/{contracts/one-shot/scripts.ts → scripts/one-shot-policy.ts} +1 -1
- package/src/scripts/poll-manager-validator.ts +52 -0
- package/src/{validators → scripts}/poll-shard-validator.ts +43 -3
- package/src/{contracts/price-oracle/scripts.ts → scripts/price-oracle-validator.ts} +4 -1
- package/src/{validators → scripts}/stability-pool-validator.ts +57 -4
- package/src/{validators → scripts}/staking-validator.ts +3 -2
- package/src/{validators → scripts}/treasury-validator.ts +3 -2
- package/src/{validators → scripts}/version-record-policy.ts +23 -4
- package/src/{validators/execute-validator.ts → scripts/version-registry.ts} +11 -3
- package/src/types/generic.ts +60 -78
- package/src/{contracts/cdp-creator/types.ts → types/indigo/cdp-creator.ts} +4 -6
- package/src/types/indigo/cdp.ts +88 -0
- package/src/types/indigo/execute.ts +21 -0
- package/src/types/indigo/gov.ts +51 -0
- package/src/{contracts/interest-oracle/types.ts → types/indigo/interest-oracle.ts} +1 -1
- package/src/{contracts/lrp/types.ts → types/indigo/lrp.ts} +2 -2
- package/src/types/indigo/poll-manager.ts +21 -0
- package/src/types/indigo/poll-shard.ts +16 -0
- package/src/{contracts/price-oracle/types.ts → types/indigo/price-oracle.ts} +4 -16
- package/src/types/indigo/stability-pool.ts +233 -0
- package/src/types/indigo/staking.ts +99 -0
- package/src/{contracts/version-registry/types.ts → types/indigo/version-record.ts} +1 -1
- package/src/types/on-chain-decimal.ts +0 -22
- package/src/types/system-params.ts +11 -22
- package/tests/datums.test.ts +108 -125
- package/tests/endpoints/initialize.ts +338 -240
- package/tests/hash-checks.test.ts +21 -26
- package/tests/indigo-test-helpers.ts +55 -1
- package/tests/initialize.test.ts +5 -10
- package/tests/interest-calculations.test.ts +18 -18
- package/tests/interest-oracle.test.ts +18 -20
- package/tests/lrp.test.ts +65 -191
- package/tests/queries/governance-queries.ts +16 -19
- package/tests/queries/iasset-queries.ts +23 -46
- package/tests/queries/interest-oracle-queries.ts +6 -3
- package/tests/queries/lrp-queries.ts +2 -2
- package/tests/queries/price-oracle-queries.ts +22 -5
- package/tests/queries/stability-pool-queries.ts +8 -10
- package/tests/queries/staking-queries.ts +19 -28
- package/tests/stability-pool.test.ts +71 -186
- package/tests/staking.test.ts +23 -30
- package/tests/test-helpers.ts +2 -11
- package/tsconfig.json +1 -3
- package/vitest.config.ts +1 -1
- package/src/contracts/cdp/helpers.ts +0 -167
- package/src/contracts/cdp/scripts.ts +0 -33
- package/src/contracts/cdp/transactions.ts +0 -1310
- package/src/contracts/cdp/types.ts +0 -161
- package/src/contracts/cdp-creator/scripts.ts +0 -39
- package/src/contracts/collector/scripts.ts +0 -32
- package/src/contracts/collector/transactions.ts +0 -44
- package/src/contracts/execute/scripts.ts +0 -48
- package/src/contracts/execute/types.ts +0 -57
- package/src/contracts/gov/helpers.ts +0 -157
- package/src/contracts/gov/scripts.ts +0 -34
- package/src/contracts/gov/transactions.ts +0 -1224
- package/src/contracts/gov/types-new.ts +0 -115
- package/src/contracts/gov/types.ts +0 -89
- package/src/contracts/interest-oracle/scripts.ts +0 -18
- package/src/contracts/interest-oracle/transactions.ts +0 -149
- package/src/contracts/lrp/scripts.ts +0 -27
- package/src/contracts/poll/helpers.ts +0 -55
- package/src/contracts/poll/scripts.ts +0 -72
- package/src/contracts/poll/types-poll-manager.ts +0 -38
- package/src/contracts/poll/types-poll-shard.ts +0 -38
- package/src/contracts/poll/types-poll.ts +0 -88
- package/src/contracts/price-oracle/transactions.ts +0 -112
- package/src/contracts/stability-pool/scripts.ts +0 -46
- package/src/contracts/stability-pool/transactions.ts +0 -660
- package/src/contracts/stability-pool/types-new.ts +0 -208
- package/src/contracts/stability-pool/types.ts +0 -42
- package/src/contracts/staking/helpers.ts +0 -116
- package/src/contracts/staking/scripts.ts +0 -41
- package/src/contracts/staking/transactions.ts +0 -268
- package/src/contracts/staking/types-new.ts +0 -81
- package/src/contracts/staking/types.ts +0 -41
- package/src/contracts/treasury/scripts.ts +0 -37
- package/src/contracts/treasury/transactions.ts +0 -44
- package/src/contracts/treasury/types.ts +0 -55
- package/src/contracts/version-registry/scripts.ts +0 -29
- package/src/contracts/version-registry/types-new.ts +0 -19
- package/src/contracts/vesting/helpers.ts +0 -267
- package/src/types/evolution-schema-options.ts +0 -16
- package/src/utils/bigint-utils.ts +0 -7
- package/src/utils/time-helpers.ts +0 -4
- package/src/validators/lrp-validator.ts +0 -7
- package/src/validators/poll-manager-validator.ts +0 -7
- package/src/validators/version-registry-validator.ts +0 -7
- package/tests/cdp.test.ts +0 -1565
- package/tests/gov.test.ts +0 -1874
- package/tests/mock/assets-mock.ts +0 -59
- package/tests/queries/cdp-queries.ts +0 -144
- package/tests/queries/collector-queries.ts +0 -26
- package/tests/queries/execute-queries.ts +0 -46
- package/tests/queries/poll-queries.ts +0 -97
- package/tests/queries/treasury-queries.ts +0 -19
- package/tests/utils/asserts.ts +0 -13
- package/tests/utils/index.ts +0 -50
- /package/src/{utils → helpers}/indigo-helpers.ts +0 -0
- /package/src/{utils → helpers}/lucid-utils.ts +0 -0
- /package/src/{contracts/price-oracle/helpers.ts → helpers/price-oracle-helpers.ts} +0 -0
- /package/src/{contracts/one-shot/types.ts → types/one-shot.ts} +0 -0
|
@@ -1,208 +0,0 @@
|
|
|
1
|
-
import { Core as EvoCore } from '@evolution-sdk/evolution';
|
|
2
|
-
import { match, P } from 'ts-pattern';
|
|
3
|
-
import { EvoCommon } from '@3rd-eye-labs/cardano-offchain-common';
|
|
4
|
-
import { DEFAULT_SCHEMA_OPTIONS } from '../../types/evolution-schema-options';
|
|
5
|
-
|
|
6
|
-
export const SPIntegerSchema = EvoCore.TSchema.Struct({
|
|
7
|
-
value: EvoCore.TSchema.Integer,
|
|
8
|
-
});
|
|
9
|
-
|
|
10
|
-
export type SPInteger = typeof SPIntegerSchema.Type;
|
|
11
|
-
|
|
12
|
-
const AccountActionSchema = EvoCore.TSchema.Union(
|
|
13
|
-
EvoCore.TSchema.Literal('Create', { flatInUnion: true }),
|
|
14
|
-
EvoCore.TSchema.Struct(
|
|
15
|
-
{
|
|
16
|
-
Adjust: EvoCore.TSchema.Struct(
|
|
17
|
-
{
|
|
18
|
-
amount: EvoCore.TSchema.Integer,
|
|
19
|
-
outputAddress: EvoCommon.AddressSchema,
|
|
20
|
-
},
|
|
21
|
-
{ flatFields: true },
|
|
22
|
-
),
|
|
23
|
-
},
|
|
24
|
-
{ flatInUnion: true },
|
|
25
|
-
),
|
|
26
|
-
EvoCore.TSchema.Struct(
|
|
27
|
-
{
|
|
28
|
-
Close: EvoCore.TSchema.Struct(
|
|
29
|
-
{ outputAddress: EvoCommon.AddressSchema },
|
|
30
|
-
{ flatFields: true },
|
|
31
|
-
),
|
|
32
|
-
},
|
|
33
|
-
{ flatInUnion: true },
|
|
34
|
-
),
|
|
35
|
-
);
|
|
36
|
-
|
|
37
|
-
export type AccountAction = typeof AccountActionSchema.Type;
|
|
38
|
-
|
|
39
|
-
export const EpochToScaleToSumSchema = EvoCore.TSchema.Map(
|
|
40
|
-
EvoCore.TSchema.Struct({
|
|
41
|
-
epoch: EvoCore.TSchema.Integer,
|
|
42
|
-
scale: EvoCore.TSchema.Integer,
|
|
43
|
-
}),
|
|
44
|
-
SPIntegerSchema,
|
|
45
|
-
);
|
|
46
|
-
|
|
47
|
-
export type EpochToScaleToSum = typeof EpochToScaleToSumSchema.Type;
|
|
48
|
-
|
|
49
|
-
const StabilityPoolSnapshotSchema = EvoCore.TSchema.Struct({
|
|
50
|
-
productVal: SPIntegerSchema,
|
|
51
|
-
depositVal: SPIntegerSchema,
|
|
52
|
-
sumVal: SPIntegerSchema,
|
|
53
|
-
epoch: EvoCore.TSchema.Integer,
|
|
54
|
-
scale: EvoCore.TSchema.Integer,
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
export type StabilityPoolSnapshot = typeof StabilityPoolSnapshotSchema.Type;
|
|
58
|
-
|
|
59
|
-
export const StabilityPoolContentSchema = EvoCore.TSchema.Struct({
|
|
60
|
-
asset: EvoCore.TSchema.ByteArray,
|
|
61
|
-
poolSnapshot: StabilityPoolSnapshotSchema,
|
|
62
|
-
epochToScaleToSum: EpochToScaleToSumSchema,
|
|
63
|
-
});
|
|
64
|
-
|
|
65
|
-
export type StabilityPoolContent = typeof StabilityPoolContentSchema.Type;
|
|
66
|
-
|
|
67
|
-
export const AccountContentSchema = EvoCore.TSchema.Struct({
|
|
68
|
-
owner: EvoCore.TSchema.ByteArray,
|
|
69
|
-
asset: EvoCore.TSchema.ByteArray,
|
|
70
|
-
accountSnapshot: StabilityPoolSnapshotSchema,
|
|
71
|
-
request: EvoCore.TSchema.NullOr(AccountActionSchema),
|
|
72
|
-
});
|
|
73
|
-
|
|
74
|
-
export type AccountContent = typeof AccountContentSchema.Type;
|
|
75
|
-
|
|
76
|
-
export const SnapshotEpochToScaleToSumContentSchema = EvoCore.TSchema.Struct({
|
|
77
|
-
snapshot: EpochToScaleToSumSchema,
|
|
78
|
-
asset: EvoCore.TSchema.ByteArray,
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
export type SnapshotEpochToScaleToSumContent =
|
|
82
|
-
typeof SnapshotEpochToScaleToSumContentSchema.Type;
|
|
83
|
-
|
|
84
|
-
export const StabilityPoolDatumSchema = EvoCore.TSchema.Union(
|
|
85
|
-
EvoCore.TSchema.Struct(
|
|
86
|
-
{ StabilityPool: StabilityPoolContentSchema },
|
|
87
|
-
{ flatInUnion: true },
|
|
88
|
-
),
|
|
89
|
-
EvoCore.TSchema.Struct(
|
|
90
|
-
{ Account: AccountContentSchema },
|
|
91
|
-
{ flatInUnion: true },
|
|
92
|
-
),
|
|
93
|
-
EvoCore.TSchema.Struct(
|
|
94
|
-
{ SnapshotEpochToScaleToSum: SnapshotEpochToScaleToSumContentSchema },
|
|
95
|
-
{ flatInUnion: true },
|
|
96
|
-
),
|
|
97
|
-
);
|
|
98
|
-
|
|
99
|
-
export const StabilityPoolRedeemerSchema = EvoCore.TSchema.Union(
|
|
100
|
-
EvoCore.TSchema.Struct(
|
|
101
|
-
{ RequestAction: AccountActionSchema },
|
|
102
|
-
{ flatInUnion: true },
|
|
103
|
-
),
|
|
104
|
-
EvoCore.TSchema.Struct(
|
|
105
|
-
{
|
|
106
|
-
ProcessRequest: EvoCore.TSchema.Struct({
|
|
107
|
-
txHash: EvoCore.TSchema.Struct({ hash: EvoCore.TSchema.ByteArray }),
|
|
108
|
-
outputIndex: EvoCore.TSchema.Integer,
|
|
109
|
-
}),
|
|
110
|
-
},
|
|
111
|
-
{ flatInUnion: true },
|
|
112
|
-
),
|
|
113
|
-
EvoCore.TSchema.Literal('AnnulRequest', { flatInUnion: true }),
|
|
114
|
-
EvoCore.TSchema.Literal('LiquidateCDP', { flatInUnion: true }),
|
|
115
|
-
EvoCore.TSchema.Literal('RecordEpochToScaleToSum', { flatInUnion: true }),
|
|
116
|
-
EvoCore.TSchema.Literal('UpgradeVersion', { flatInUnion: true }),
|
|
117
|
-
);
|
|
118
|
-
|
|
119
|
-
export type StabilityPoolRedeemer = typeof StabilityPoolRedeemerSchema.Type;
|
|
120
|
-
|
|
121
|
-
export function serialiseStabilityPoolRedeemer(
|
|
122
|
-
r: StabilityPoolRedeemer,
|
|
123
|
-
): string {
|
|
124
|
-
return EvoCore.Data.withSchema(
|
|
125
|
-
StabilityPoolRedeemerSchema,
|
|
126
|
-
DEFAULT_SCHEMA_OPTIONS,
|
|
127
|
-
).toCBORHex(r);
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
export function serialiseStabilityPoolDatum(
|
|
131
|
-
d: typeof StabilityPoolDatumSchema.Type,
|
|
132
|
-
/**
|
|
133
|
-
* This is necessary to change only in case of execute propose asset.
|
|
134
|
-
*/
|
|
135
|
-
useIndefiniteMaps: boolean = false,
|
|
136
|
-
): string {
|
|
137
|
-
return EvoCore.Data.withSchema(StabilityPoolDatumSchema, {
|
|
138
|
-
...DEFAULT_SCHEMA_OPTIONS,
|
|
139
|
-
useIndefiniteMaps: useIndefiniteMaps,
|
|
140
|
-
}).toCBORHex(d);
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
export function parseStabilityPoolDatum(datum: string): StabilityPoolContent {
|
|
144
|
-
return match(
|
|
145
|
-
EvoCore.Data.withSchema(
|
|
146
|
-
StabilityPoolDatumSchema,
|
|
147
|
-
DEFAULT_SCHEMA_OPTIONS,
|
|
148
|
-
).fromCBORHex(datum),
|
|
149
|
-
)
|
|
150
|
-
.with({ StabilityPool: P.select() }, (res) => res)
|
|
151
|
-
.otherwise(() => {
|
|
152
|
-
throw new Error('Expected a Stability Pool datum.');
|
|
153
|
-
});
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
export function parseAccountDatum(datum: string): AccountContent {
|
|
157
|
-
return match(
|
|
158
|
-
EvoCore.Data.withSchema(
|
|
159
|
-
StabilityPoolDatumSchema,
|
|
160
|
-
DEFAULT_SCHEMA_OPTIONS,
|
|
161
|
-
).fromCBORHex(datum),
|
|
162
|
-
)
|
|
163
|
-
.with({ Account: P.select() }, (res) => res)
|
|
164
|
-
.otherwise(() => {
|
|
165
|
-
throw new Error('Expected a Stability Pool datum.');
|
|
166
|
-
});
|
|
167
|
-
}
|
|
168
|
-
export function parseSnapshotEpochToScaleToSumDatum(
|
|
169
|
-
datum: string,
|
|
170
|
-
): SnapshotEpochToScaleToSumContent {
|
|
171
|
-
return match(
|
|
172
|
-
EvoCore.Data.withSchema(
|
|
173
|
-
StabilityPoolDatumSchema,
|
|
174
|
-
DEFAULT_SCHEMA_OPTIONS,
|
|
175
|
-
).fromCBORHex(datum),
|
|
176
|
-
)
|
|
177
|
-
.with({ SnapshotEpochToScaleToSum: P.select() }, (res) => res)
|
|
178
|
-
.otherwise(() => {
|
|
179
|
-
throw new Error('Expected a Stability Pool datum.');
|
|
180
|
-
});
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
/** SP Integer */
|
|
184
|
-
const spPrecision: bigint = 1000000000000000000n;
|
|
185
|
-
|
|
186
|
-
export function mkSPInteger(value: bigint): SPInteger {
|
|
187
|
-
return { value: value * spPrecision };
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
export function fromSPInteger(value: SPInteger): bigint {
|
|
191
|
-
return value.value / spPrecision;
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
export function spAdd(a: SPInteger, b: SPInteger): SPInteger {
|
|
195
|
-
return { value: a.value + b.value };
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
export function spSub(a: SPInteger, b: SPInteger): SPInteger {
|
|
199
|
-
return { value: a.value - b.value };
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
export function spMul(a: SPInteger, b: SPInteger): SPInteger {
|
|
203
|
-
return { value: (a.value * b.value) / spPrecision };
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
export function spDiv(a: SPInteger, b: SPInteger): SPInteger {
|
|
207
|
-
return { value: (a.value * spPrecision) / b.value };
|
|
208
|
-
}
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
import { Data } from '@lucid-evolution/lucid';
|
|
2
|
-
import { AssetClassSchema, OutputReferenceSchema } from '../../types/generic';
|
|
3
|
-
|
|
4
|
-
export const ActionReturnDatumSchema = Data.Enum([
|
|
5
|
-
Data.Object({
|
|
6
|
-
IndigoStabilityPoolAccountAdjustment: Data.Object({
|
|
7
|
-
spent_account: OutputReferenceSchema,
|
|
8
|
-
}),
|
|
9
|
-
}),
|
|
10
|
-
Data.Object({
|
|
11
|
-
IndigoStabilityPoolAccountClosure: Data.Object({
|
|
12
|
-
closed_account: OutputReferenceSchema,
|
|
13
|
-
}),
|
|
14
|
-
}),
|
|
15
|
-
]);
|
|
16
|
-
|
|
17
|
-
export type ActionReturnDatum = Data.Static<typeof ActionReturnDatumSchema>;
|
|
18
|
-
export const ActionReturnDatum =
|
|
19
|
-
ActionReturnDatumSchema as unknown as ActionReturnDatum;
|
|
20
|
-
|
|
21
|
-
/** SP Parameters */
|
|
22
|
-
const StabilityPoolParamsSchema = Data.Object({
|
|
23
|
-
assetSymbol: Data.Bytes(),
|
|
24
|
-
stabilityPoolToken: AssetClassSchema,
|
|
25
|
-
snapshotEpochToScaleToSumToken: AssetClassSchema,
|
|
26
|
-
accountToken: AssetClassSchema,
|
|
27
|
-
cdpToken: AssetClassSchema,
|
|
28
|
-
iAssetAuthToken: AssetClassSchema,
|
|
29
|
-
versionRecordToken: AssetClassSchema,
|
|
30
|
-
collectorValHash: Data.Bytes(),
|
|
31
|
-
govNFT: AssetClassSchema,
|
|
32
|
-
accountCreateFeeLovelaces: Data.Integer(),
|
|
33
|
-
accountAdjustmentFeeLovelaces: Data.Integer(),
|
|
34
|
-
requestCollateralLovelaces: Data.Integer(),
|
|
35
|
-
});
|
|
36
|
-
export type StabilityPoolParams = Data.Static<typeof StabilityPoolParamsSchema>;
|
|
37
|
-
export const StabilityPoolParams =
|
|
38
|
-
StabilityPoolParamsSchema as unknown as StabilityPoolParams;
|
|
39
|
-
|
|
40
|
-
export function castStabilityPoolParams(params: StabilityPoolParams): Data {
|
|
41
|
-
return Data.castTo(params, StabilityPoolParams);
|
|
42
|
-
}
|
|
@@ -1,116 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
fromText,
|
|
3
|
-
OutRef,
|
|
4
|
-
UTxO,
|
|
5
|
-
validatorToScriptHash,
|
|
6
|
-
} from '@lucid-evolution/lucid';
|
|
7
|
-
import { LucidEvolution } from '@lucid-evolution/lucid';
|
|
8
|
-
import { SystemParams } from '../../types/system-params';
|
|
9
|
-
import {
|
|
10
|
-
parseStakingManagerDatum,
|
|
11
|
-
parseStakingPositionOrThrow,
|
|
12
|
-
StakingManager,
|
|
13
|
-
StakingPosition,
|
|
14
|
-
StakingPosLockedAmt,
|
|
15
|
-
} from './types-new';
|
|
16
|
-
import { createScriptAddress } from '../../utils/lucid-utils';
|
|
17
|
-
import { mkStakingValidatorFromSP } from './scripts';
|
|
18
|
-
|
|
19
|
-
export type StakingPositionOutput = {
|
|
20
|
-
utxo: UTxO;
|
|
21
|
-
datum: StakingPosition;
|
|
22
|
-
};
|
|
23
|
-
export type StakingManagerOutput = { utxo: UTxO; datum: StakingManager };
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Update the staking position locked amount. In case proposal's voting finished, unlock the amount.
|
|
27
|
-
*/
|
|
28
|
-
export function updateStakingLockedAmount(
|
|
29
|
-
stakingPosLockedAmt: StakingPosLockedAmt,
|
|
30
|
-
currentTime: bigint,
|
|
31
|
-
): StakingPosLockedAmt {
|
|
32
|
-
return new Map(
|
|
33
|
-
stakingPosLockedAmt
|
|
34
|
-
.entries()
|
|
35
|
-
.filter(([_, { votingEnd }]) => votingEnd > currentTime),
|
|
36
|
-
);
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
export function findStakingManagerByOutRef(
|
|
40
|
-
stakingManagerRef: OutRef,
|
|
41
|
-
lucid: LucidEvolution,
|
|
42
|
-
): Promise<StakingManagerOutput> {
|
|
43
|
-
return lucid
|
|
44
|
-
.utxosByOutRef([stakingManagerRef])
|
|
45
|
-
.then((utxos) =>
|
|
46
|
-
utxos
|
|
47
|
-
.map((utxo) => {
|
|
48
|
-
if (!utxo.datum) return undefined;
|
|
49
|
-
const datum = parseStakingManagerDatum(utxo.datum);
|
|
50
|
-
return { utxo, datum };
|
|
51
|
-
})
|
|
52
|
-
.find((utxo) => utxo !== undefined),
|
|
53
|
-
)
|
|
54
|
-
.then((result) => {
|
|
55
|
-
if (!result)
|
|
56
|
-
throw new Error(
|
|
57
|
-
'Unable to locate Staking Manager by output reference.',
|
|
58
|
-
);
|
|
59
|
-
return result;
|
|
60
|
-
});
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
export function findStakingManager(
|
|
64
|
-
params: SystemParams,
|
|
65
|
-
lucid: LucidEvolution,
|
|
66
|
-
): Promise<StakingManagerOutput> {
|
|
67
|
-
return lucid
|
|
68
|
-
.utxosAtWithUnit(
|
|
69
|
-
createScriptAddress(
|
|
70
|
-
lucid.config().network!,
|
|
71
|
-
validatorToScriptHash(mkStakingValidatorFromSP(params.stakingParams)),
|
|
72
|
-
),
|
|
73
|
-
params.stakingParams.stakingManagerNFT[0].unCurrencySymbol +
|
|
74
|
-
fromText(params.stakingParams.stakingManagerNFT[1].unTokenName),
|
|
75
|
-
)
|
|
76
|
-
.then((utxos) =>
|
|
77
|
-
utxos
|
|
78
|
-
.map((utxo) => {
|
|
79
|
-
if (!utxo.datum) return undefined;
|
|
80
|
-
const datum = parseStakingManagerDatum(utxo.datum);
|
|
81
|
-
return { utxo, datum };
|
|
82
|
-
})
|
|
83
|
-
.find((utxo) => utxo !== undefined),
|
|
84
|
-
)
|
|
85
|
-
.then((result) => {
|
|
86
|
-
if (!result)
|
|
87
|
-
throw new Error(
|
|
88
|
-
'Unable to locate Staking Manager by output reference.',
|
|
89
|
-
);
|
|
90
|
-
return result;
|
|
91
|
-
});
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
export function findStakingPositionByOutRef(
|
|
95
|
-
stakingPositionRef: OutRef,
|
|
96
|
-
lucid: LucidEvolution,
|
|
97
|
-
): Promise<StakingPositionOutput> {
|
|
98
|
-
return lucid
|
|
99
|
-
.utxosByOutRef([stakingPositionRef])
|
|
100
|
-
.then((utxos) =>
|
|
101
|
-
utxos
|
|
102
|
-
.map((utxo) => {
|
|
103
|
-
if (!utxo.datum) return undefined;
|
|
104
|
-
const datum = parseStakingPositionOrThrow(utxo.datum);
|
|
105
|
-
return { utxo, datum };
|
|
106
|
-
})
|
|
107
|
-
.find((utxo) => utxo !== undefined),
|
|
108
|
-
)
|
|
109
|
-
.then((result) => {
|
|
110
|
-
if (!result)
|
|
111
|
-
throw new Error(
|
|
112
|
-
'Unable to locate Staking Position by output reference.',
|
|
113
|
-
);
|
|
114
|
-
return result;
|
|
115
|
-
});
|
|
116
|
-
}
|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
applyParamsToScript,
|
|
3
|
-
Constr,
|
|
4
|
-
fromText,
|
|
5
|
-
SpendingValidator,
|
|
6
|
-
} from '@lucid-evolution/lucid';
|
|
7
|
-
import { StakingParamsSP } from '../../types/system-params';
|
|
8
|
-
import { _stakingValidator } from '../../validators/staking-validator';
|
|
9
|
-
|
|
10
|
-
export const mkStakingValidatorFromSP = (
|
|
11
|
-
params: StakingParamsSP,
|
|
12
|
-
): SpendingValidator => {
|
|
13
|
-
return {
|
|
14
|
-
type: 'PlutusV2',
|
|
15
|
-
script: applyParamsToScript(_stakingValidator.cborHex, [
|
|
16
|
-
new Constr(0, [
|
|
17
|
-
new Constr(0, [
|
|
18
|
-
params.stakingManagerNFT[0].unCurrencySymbol,
|
|
19
|
-
fromText(params.stakingManagerNFT[1].unTokenName),
|
|
20
|
-
]),
|
|
21
|
-
new Constr(0, [
|
|
22
|
-
params.stakingToken[0].unCurrencySymbol,
|
|
23
|
-
fromText(params.stakingToken[1].unTokenName),
|
|
24
|
-
]),
|
|
25
|
-
new Constr(0, [
|
|
26
|
-
params.indyToken[0].unCurrencySymbol,
|
|
27
|
-
fromText(params.indyToken[1].unTokenName),
|
|
28
|
-
]),
|
|
29
|
-
new Constr(0, [
|
|
30
|
-
params.pollToken[0].unCurrencySymbol,
|
|
31
|
-
fromText(params.pollToken[1].unTokenName),
|
|
32
|
-
]),
|
|
33
|
-
new Constr(0, [
|
|
34
|
-
params.versionRecordToken[0].unCurrencySymbol,
|
|
35
|
-
fromText(params.versionRecordToken[1].unTokenName),
|
|
36
|
-
]),
|
|
37
|
-
params.collectorValHash,
|
|
38
|
-
]),
|
|
39
|
-
]),
|
|
40
|
-
};
|
|
41
|
-
};
|
|
@@ -1,268 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
Constr,
|
|
3
|
-
Data,
|
|
4
|
-
fromHex,
|
|
5
|
-
fromText,
|
|
6
|
-
LucidEvolution,
|
|
7
|
-
OutRef,
|
|
8
|
-
toHex,
|
|
9
|
-
TxBuilder,
|
|
10
|
-
} from '@lucid-evolution/lucid';
|
|
11
|
-
import {
|
|
12
|
-
fromSystemParamsScriptRef,
|
|
13
|
-
SystemParams,
|
|
14
|
-
} from '../../types/system-params';
|
|
15
|
-
import { addrDetails } from '../../utils/lucid-utils';
|
|
16
|
-
import {
|
|
17
|
-
findStakingManager,
|
|
18
|
-
findStakingManagerByOutRef,
|
|
19
|
-
findStakingPositionByOutRef,
|
|
20
|
-
updateStakingLockedAmount,
|
|
21
|
-
} from './helpers';
|
|
22
|
-
import {
|
|
23
|
-
serialiseStakingDatum,
|
|
24
|
-
StakingManager,
|
|
25
|
-
StakingPosition,
|
|
26
|
-
} from './types-new';
|
|
27
|
-
import { matchSingle } from '../../utils/utils';
|
|
28
|
-
|
|
29
|
-
export async function openStakingPosition(
|
|
30
|
-
amount: bigint,
|
|
31
|
-
params: SystemParams,
|
|
32
|
-
lucid: LucidEvolution,
|
|
33
|
-
stakingManagerRef?: OutRef,
|
|
34
|
-
): Promise<TxBuilder> {
|
|
35
|
-
const [pkh, _] = await addrDetails(lucid);
|
|
36
|
-
|
|
37
|
-
const stakingManagerOut = stakingManagerRef
|
|
38
|
-
? await findStakingManagerByOutRef(stakingManagerRef, lucid)
|
|
39
|
-
: await findStakingManager(params, lucid);
|
|
40
|
-
|
|
41
|
-
const stakingRefScriptUtxo = matchSingle(
|
|
42
|
-
await lucid.utxosByOutRef([
|
|
43
|
-
fromSystemParamsScriptRef(params.scriptReferences.stakingValidatorRef),
|
|
44
|
-
]),
|
|
45
|
-
(_) => new Error('Expected a single staking Ref Script UTXO'),
|
|
46
|
-
);
|
|
47
|
-
const stakingTokenPolicyRefScriptUtxo = matchSingle(
|
|
48
|
-
await lucid.utxosByOutRef([
|
|
49
|
-
fromSystemParamsScriptRef(
|
|
50
|
-
params.scriptReferences.authTokenPolicies.stakingTokenRef,
|
|
51
|
-
),
|
|
52
|
-
]),
|
|
53
|
-
(_) => new Error('Expected a single staking token policy Ref Script UTXO'),
|
|
54
|
-
);
|
|
55
|
-
|
|
56
|
-
const newStakingManagerDatum: StakingManager = {
|
|
57
|
-
totalStake: stakingManagerOut.datum.totalStake + amount,
|
|
58
|
-
managerSnapshot: {
|
|
59
|
-
snapshotAda: stakingManagerOut.datum.managerSnapshot.snapshotAda,
|
|
60
|
-
},
|
|
61
|
-
};
|
|
62
|
-
|
|
63
|
-
const stakingPositionDatum: StakingPosition = {
|
|
64
|
-
owner: fromHex(pkh.hash),
|
|
65
|
-
lockedAmount: new Map([]),
|
|
66
|
-
positionSnapshot: {
|
|
67
|
-
snapshotAda: stakingManagerOut.datum.managerSnapshot.snapshotAda,
|
|
68
|
-
},
|
|
69
|
-
};
|
|
70
|
-
|
|
71
|
-
const stakingToken =
|
|
72
|
-
params.stakingParams.stakingToken[0].unCurrencySymbol +
|
|
73
|
-
fromText(params.stakingParams.stakingToken[1].unTokenName);
|
|
74
|
-
const indyToken =
|
|
75
|
-
params.stakingParams.indyToken[0].unCurrencySymbol +
|
|
76
|
-
fromText(params.stakingParams.indyToken[1].unTokenName);
|
|
77
|
-
return lucid
|
|
78
|
-
.newTx()
|
|
79
|
-
.collectFrom([stakingManagerOut.utxo], Data.to(new Constr(0, [pkh.hash])))
|
|
80
|
-
.readFrom([stakingRefScriptUtxo])
|
|
81
|
-
.pay.ToContract(
|
|
82
|
-
stakingManagerOut.utxo.address,
|
|
83
|
-
{
|
|
84
|
-
kind: 'inline',
|
|
85
|
-
value: serialiseStakingDatum(newStakingManagerDatum),
|
|
86
|
-
},
|
|
87
|
-
stakingManagerOut.utxo.assets,
|
|
88
|
-
)
|
|
89
|
-
.readFrom([stakingTokenPolicyRefScriptUtxo])
|
|
90
|
-
.mintAssets(
|
|
91
|
-
{
|
|
92
|
-
[stakingToken]: 1n,
|
|
93
|
-
},
|
|
94
|
-
Data.void(),
|
|
95
|
-
)
|
|
96
|
-
.pay.ToContract(
|
|
97
|
-
stakingManagerOut.utxo.address,
|
|
98
|
-
{
|
|
99
|
-
kind: 'inline',
|
|
100
|
-
value: serialiseStakingDatum(stakingPositionDatum),
|
|
101
|
-
},
|
|
102
|
-
{
|
|
103
|
-
[stakingToken]: 1n,
|
|
104
|
-
[indyToken]: amount,
|
|
105
|
-
},
|
|
106
|
-
)
|
|
107
|
-
.addSignerKey(pkh.hash);
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
export async function adjustStakingPosition(
|
|
111
|
-
stakingPositionRef: OutRef,
|
|
112
|
-
amount: bigint,
|
|
113
|
-
params: SystemParams,
|
|
114
|
-
lucid: LucidEvolution,
|
|
115
|
-
stakingManagerRef?: OutRef,
|
|
116
|
-
): Promise<TxBuilder> {
|
|
117
|
-
const now = Date.now();
|
|
118
|
-
|
|
119
|
-
const stakingPositionOut = await findStakingPositionByOutRef(
|
|
120
|
-
stakingPositionRef,
|
|
121
|
-
lucid,
|
|
122
|
-
);
|
|
123
|
-
const stakingManagerOut = stakingManagerRef
|
|
124
|
-
? await findStakingManagerByOutRef(stakingManagerRef, lucid)
|
|
125
|
-
: await findStakingManager(params, lucid);
|
|
126
|
-
|
|
127
|
-
const stakingRefScriptUtxo = matchSingle(
|
|
128
|
-
await lucid.utxosByOutRef([
|
|
129
|
-
fromSystemParamsScriptRef(params.scriptReferences.stakingValidatorRef),
|
|
130
|
-
]),
|
|
131
|
-
(_) => new Error('Expected a single staking Ref Script UTXO'),
|
|
132
|
-
);
|
|
133
|
-
|
|
134
|
-
const stakingToken =
|
|
135
|
-
params.stakingParams.stakingToken[0].unCurrencySymbol +
|
|
136
|
-
fromText(params.stakingParams.stakingToken[1].unTokenName);
|
|
137
|
-
const stakingManagerToken =
|
|
138
|
-
params.stakingParams.stakingManagerNFT[0].unCurrencySymbol +
|
|
139
|
-
fromText(params.stakingParams.stakingManagerNFT[1].unTokenName);
|
|
140
|
-
const indyToken =
|
|
141
|
-
params.stakingParams.indyToken[0].unCurrencySymbol +
|
|
142
|
-
fromText(params.stakingParams.indyToken[1].unTokenName);
|
|
143
|
-
|
|
144
|
-
const existingIndyAmount = stakingPositionOut.utxo.assets[indyToken] ?? 0n;
|
|
145
|
-
const currentSnapshotAda =
|
|
146
|
-
stakingManagerOut.datum.managerSnapshot.snapshotAda;
|
|
147
|
-
const oldSnapshotAda = stakingPositionOut.datum.positionSnapshot.snapshotAda;
|
|
148
|
-
const adaReward =
|
|
149
|
-
((currentSnapshotAda - oldSnapshotAda) * existingIndyAmount) /
|
|
150
|
-
(1000000n * 1000000n);
|
|
151
|
-
|
|
152
|
-
const newLockedAmount = updateStakingLockedAmount(
|
|
153
|
-
stakingPositionOut.datum.lockedAmount,
|
|
154
|
-
BigInt(now),
|
|
155
|
-
);
|
|
156
|
-
|
|
157
|
-
return lucid
|
|
158
|
-
.newTx()
|
|
159
|
-
.validFrom(Date.now())
|
|
160
|
-
.readFrom([stakingRefScriptUtxo])
|
|
161
|
-
.collectFrom([stakingPositionOut.utxo], Data.to(new Constr(3, [amount])))
|
|
162
|
-
.collectFrom([stakingManagerOut.utxo], Data.to(new Constr(1, [])))
|
|
163
|
-
.pay.ToContract(
|
|
164
|
-
stakingManagerOut.utxo.address,
|
|
165
|
-
{
|
|
166
|
-
kind: 'inline',
|
|
167
|
-
value: serialiseStakingDatum({
|
|
168
|
-
...stakingManagerOut.datum,
|
|
169
|
-
totalStake: stakingManagerOut.datum.totalStake + amount,
|
|
170
|
-
}),
|
|
171
|
-
},
|
|
172
|
-
{
|
|
173
|
-
lovelace: stakingManagerOut.utxo.assets.lovelace - adaReward,
|
|
174
|
-
[stakingManagerToken]: 1n,
|
|
175
|
-
},
|
|
176
|
-
)
|
|
177
|
-
.pay.ToContract(
|
|
178
|
-
stakingPositionOut.utxo.address,
|
|
179
|
-
{
|
|
180
|
-
kind: 'inline',
|
|
181
|
-
value: serialiseStakingDatum({
|
|
182
|
-
...stakingPositionOut.datum,
|
|
183
|
-
lockedAmount: newLockedAmount,
|
|
184
|
-
}),
|
|
185
|
-
},
|
|
186
|
-
{
|
|
187
|
-
[stakingToken]: 1n,
|
|
188
|
-
[indyToken]: stakingPositionOut.utxo.assets[indyToken] + amount,
|
|
189
|
-
},
|
|
190
|
-
)
|
|
191
|
-
.addSignerKey(toHex(stakingPositionOut.datum.owner));
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
export async function closeStakingPosition(
|
|
195
|
-
stakingPositionRef: OutRef,
|
|
196
|
-
params: SystemParams,
|
|
197
|
-
lucid: LucidEvolution,
|
|
198
|
-
stakingManagerRef?: OutRef,
|
|
199
|
-
): Promise<TxBuilder> {
|
|
200
|
-
const stakingPositionOut = await findStakingPositionByOutRef(
|
|
201
|
-
stakingPositionRef,
|
|
202
|
-
lucid,
|
|
203
|
-
);
|
|
204
|
-
const stakingManagerOut = stakingManagerRef
|
|
205
|
-
? await findStakingManagerByOutRef(stakingManagerRef, lucid)
|
|
206
|
-
: await findStakingManager(params, lucid);
|
|
207
|
-
|
|
208
|
-
const stakingRefScriptUtxo = matchSingle(
|
|
209
|
-
await lucid.utxosByOutRef([
|
|
210
|
-
fromSystemParamsScriptRef(params.scriptReferences.stakingValidatorRef),
|
|
211
|
-
]),
|
|
212
|
-
(_) => new Error('Expected a single staking Ref Script UTXO'),
|
|
213
|
-
);
|
|
214
|
-
const stakingTokenPolicyRefScriptUtxo = matchSingle(
|
|
215
|
-
await lucid.utxosByOutRef([
|
|
216
|
-
fromSystemParamsScriptRef(
|
|
217
|
-
params.scriptReferences.authTokenPolicies.stakingTokenRef,
|
|
218
|
-
),
|
|
219
|
-
]),
|
|
220
|
-
(_) => new Error('Expected a single staking token policy Ref Script UTXO'),
|
|
221
|
-
);
|
|
222
|
-
|
|
223
|
-
const stakingToken =
|
|
224
|
-
params.stakingParams.stakingToken[0].unCurrencySymbol +
|
|
225
|
-
fromText(params.stakingParams.stakingToken[1].unTokenName);
|
|
226
|
-
const stakingManagerToken =
|
|
227
|
-
params.stakingParams.stakingManagerNFT[0].unCurrencySymbol +
|
|
228
|
-
fromText(params.stakingParams.stakingManagerNFT[1].unTokenName);
|
|
229
|
-
const indyToken =
|
|
230
|
-
params.stakingParams.indyToken[0].unCurrencySymbol +
|
|
231
|
-
fromText(params.stakingParams.indyToken[1].unTokenName);
|
|
232
|
-
|
|
233
|
-
const existingIndyAmount = stakingPositionOut.utxo.assets[indyToken] ?? 0n;
|
|
234
|
-
const currentSnapshotAda =
|
|
235
|
-
stakingManagerOut.datum.managerSnapshot.snapshotAda;
|
|
236
|
-
const oldSnapshotAda = stakingPositionOut.datum.positionSnapshot.snapshotAda;
|
|
237
|
-
const adaReward =
|
|
238
|
-
((currentSnapshotAda - oldSnapshotAda) * existingIndyAmount) /
|
|
239
|
-
(1000000n * 1000000n);
|
|
240
|
-
|
|
241
|
-
return lucid
|
|
242
|
-
.newTx()
|
|
243
|
-
.validFrom(Date.now())
|
|
244
|
-
.readFrom([stakingRefScriptUtxo, stakingTokenPolicyRefScriptUtxo])
|
|
245
|
-
.collectFrom([stakingPositionOut.utxo], Data.to(new Constr(4, [])))
|
|
246
|
-
.collectFrom([stakingManagerOut.utxo], Data.to(new Constr(1, [])))
|
|
247
|
-
.pay.ToContract(
|
|
248
|
-
stakingManagerOut.utxo.address,
|
|
249
|
-
{
|
|
250
|
-
kind: 'inline',
|
|
251
|
-
value: serialiseStakingDatum({
|
|
252
|
-
...stakingManagerOut.datum,
|
|
253
|
-
totalStake: stakingManagerOut.datum.totalStake - existingIndyAmount,
|
|
254
|
-
}),
|
|
255
|
-
},
|
|
256
|
-
{
|
|
257
|
-
lovelace: stakingManagerOut.utxo.assets.lovelace - adaReward,
|
|
258
|
-
[stakingManagerToken]: 1n,
|
|
259
|
-
},
|
|
260
|
-
)
|
|
261
|
-
.mintAssets(
|
|
262
|
-
{
|
|
263
|
-
[stakingToken]: -1n,
|
|
264
|
-
},
|
|
265
|
-
Data.void(),
|
|
266
|
-
)
|
|
267
|
-
.addSignerKey(toHex(stakingPositionOut.datum.owner));
|
|
268
|
-
}
|