@indigo-labs/indigo-sdk 0.5.5 → 0.5.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (204) hide show
  1. package/package.json +11 -1
  2. package/.claude/settings.local.json +0 -7
  3. package/.github/workflows/ci.yml +0 -66
  4. package/.husky/pre-commit +0 -1
  5. package/.nvmrc +0 -1
  6. package/.prettierignore +0 -1
  7. package/.prettierrc +0 -6
  8. package/eslint.config.mjs +0 -42
  9. package/scripts/bench.sh +0 -62
  10. package/scripts/run-repeat.sh +0 -22
  11. package/src/contracts/cdp/helpers.ts +0 -173
  12. package/src/contracts/cdp/scripts.ts +0 -70
  13. package/src/contracts/cdp/transactions.ts +0 -1584
  14. package/src/contracts/cdp/types-new.ts +0 -277
  15. package/src/contracts/cdp/types.ts +0 -43
  16. package/src/contracts/cdp-creator/queries.ts +0 -30
  17. package/src/contracts/cdp-creator/scripts.ts +0 -45
  18. package/src/contracts/cdp-creator/types-new.ts +0 -50
  19. package/src/contracts/cdp-creator/types.ts +0 -24
  20. package/src/contracts/collector/scripts.ts +0 -32
  21. package/src/contracts/collector/transactions.ts +0 -50
  22. package/src/contracts/collector/types-new.ts +0 -17
  23. package/src/contracts/execute/scripts.ts +0 -57
  24. package/src/contracts/execute/types-new.ts +0 -44
  25. package/src/contracts/execute/types.ts +0 -24
  26. package/src/contracts/gov/helpers.ts +0 -351
  27. package/src/contracts/gov/scripts.ts +0 -41
  28. package/src/contracts/gov/transactions.ts +0 -1607
  29. package/src/contracts/gov/types-new.ts +0 -268
  30. package/src/contracts/gov/types.ts +0 -22
  31. package/src/contracts/iasset/helpers.ts +0 -170
  32. package/src/contracts/iasset/queries.ts +0 -102
  33. package/src/contracts/iasset/scripts.ts +0 -38
  34. package/src/contracts/iasset/types.ts +0 -154
  35. package/src/contracts/initialize/actions.ts +0 -789
  36. package/src/contracts/initialize/helpers.ts +0 -616
  37. package/src/contracts/initialize/types.ts +0 -116
  38. package/src/contracts/interest-collection/helpers.ts +0 -19
  39. package/src/contracts/interest-collection/queries.ts +0 -66
  40. package/src/contracts/interest-collection/scripts.ts +0 -44
  41. package/src/contracts/interest-collection/transactions.ts +0 -433
  42. package/src/contracts/interest-collection/types-new.ts +0 -50
  43. package/src/contracts/interest-collection/types.ts +0 -26
  44. package/src/contracts/interest-oracle/helpers.ts +0 -61
  45. package/src/contracts/interest-oracle/scripts.ts +0 -18
  46. package/src/contracts/interest-oracle/transactions.ts +0 -151
  47. package/src/contracts/interest-oracle/types-new.ts +0 -32
  48. package/src/contracts/interest-oracle/types.ts +0 -16
  49. package/src/contracts/one-shot/scripts.ts +0 -12
  50. package/src/contracts/one-shot/transactions.ts +0 -66
  51. package/src/contracts/one-shot/types.ts +0 -22
  52. package/src/contracts/poll/helpers.ts +0 -37
  53. package/src/contracts/poll/scripts.ts +0 -71
  54. package/src/contracts/poll/types-poll-manager.ts +0 -20
  55. package/src/contracts/poll/types-poll-new.ts +0 -196
  56. package/src/contracts/poll/types-poll-shard.ts +0 -16
  57. package/src/contracts/price-oracle/helpers.ts +0 -30
  58. package/src/contracts/price-oracle/scripts.ts +0 -18
  59. package/src/contracts/price-oracle/transactions.ts +0 -115
  60. package/src/contracts/price-oracle/types-new.ts +0 -50
  61. package/src/contracts/price-oracle/types.ts +0 -16
  62. package/src/contracts/pyth-feed/helpers.ts +0 -82
  63. package/src/contracts/pyth-feed/scripts.ts +0 -15
  64. package/src/contracts/pyth-feed/types.ts +0 -181
  65. package/src/contracts/rob/helpers.ts +0 -666
  66. package/src/contracts/rob/scripts.ts +0 -35
  67. package/src/contracts/rob/transactions.ts +0 -423
  68. package/src/contracts/rob/types-new.ts +0 -128
  69. package/src/contracts/rob/types.ts +0 -16
  70. package/src/contracts/rob-leverage/helpers.ts +0 -422
  71. package/src/contracts/rob-leverage/transactions.ts +0 -368
  72. package/src/contracts/stability-pool/helpers.ts +0 -798
  73. package/src/contracts/stability-pool/queries.ts +0 -132
  74. package/src/contracts/stability-pool/scripts.ts +0 -51
  75. package/src/contracts/stability-pool/transactions.ts +0 -921
  76. package/src/contracts/stability-pool/types-new.ts +0 -385
  77. package/src/contracts/stability-pool/types.ts +0 -25
  78. package/src/contracts/stableswap/helpers.ts +0 -507
  79. package/src/contracts/stableswap/scripts.ts +0 -37
  80. package/src/contracts/stableswap/transactions.ts +0 -475
  81. package/src/contracts/stableswap/types-new.ts +0 -131
  82. package/src/contracts/stableswap/types.ts +0 -17
  83. package/src/contracts/staking/helpers.ts +0 -136
  84. package/src/contracts/staking/scripts.ts +0 -41
  85. package/src/contracts/staking/transactions.ts +0 -349
  86. package/src/contracts/staking/types-new.ts +0 -137
  87. package/src/contracts/staking/types.ts +0 -17
  88. package/src/contracts/treasury/helpers.ts +0 -21
  89. package/src/contracts/treasury/queries.ts +0 -100
  90. package/src/contracts/treasury/scripts.ts +0 -27
  91. package/src/contracts/treasury/transactions.ts +0 -334
  92. package/src/contracts/treasury/types-new.ts +0 -69
  93. package/src/contracts/treasury/types.ts +0 -14
  94. package/src/contracts/version-registry/scripts.ts +0 -29
  95. package/src/contracts/version-registry/types-new.ts +0 -18
  96. package/src/contracts/version-registry/types.ts +0 -17
  97. package/src/index.ts +0 -99
  98. package/src/scripts/always-fail-validator.ts +0 -7
  99. package/src/scripts/auth-token-policy.ts +0 -24
  100. package/src/scripts/iasset-policy.ts +0 -23
  101. package/src/types/evolution-schema-options.ts +0 -16
  102. package/src/types/generic.ts +0 -52
  103. package/src/types/multisig.ts +0 -48
  104. package/src/types/on-chain-decimal.ts +0 -57
  105. package/src/types/rational.ts +0 -76
  106. package/src/types/system-params.ts +0 -463
  107. package/src/utils/array-utils.ts +0 -108
  108. package/src/utils/bigint-utils.ts +0 -35
  109. package/src/utils/helper-txs.ts +0 -31
  110. package/src/utils/indigo-helpers.ts +0 -17
  111. package/src/utils/lucid-utils.ts +0 -125
  112. package/src/utils/oracle-helpers.ts +0 -62
  113. package/src/utils/pyth/decode.ts +0 -223
  114. package/src/utils/pyth/encode.ts +0 -262
  115. package/src/utils/pyth/index.ts +0 -14
  116. package/src/utils/pyth/types.ts +0 -87
  117. package/src/utils/time-helpers.ts +0 -4
  118. package/src/utils/utils.ts +0 -25
  119. package/src/validators/always-succeed-validator.ts +0 -6
  120. package/src/validators/cdp-creator-validator.ts +0 -7
  121. package/src/validators/cdp-redeem-validator.ts +0 -7
  122. package/src/validators/cdp-validator.ts +0 -7
  123. package/src/validators/collector-validator.ts +0 -7
  124. package/src/validators/execute-validator.ts +0 -7
  125. package/src/validators/governance-validator.ts +0 -7
  126. package/src/validators/iasset-validator.ts +0 -7
  127. package/src/validators/interest-collection-validator.ts +0 -7
  128. package/src/validators/interest-oracle-validator.ts +0 -7
  129. package/src/validators/one-shot-policy.ts +0 -7
  130. package/src/validators/poll-manager-validator.ts +0 -7
  131. package/src/validators/poll-shard-validator.ts +0 -7
  132. package/src/validators/price-oracle-validator.ts +0 -7
  133. package/src/validators/pyth-feed-validator.ts +0 -7
  134. package/src/validators/rob-validator.ts +0 -7
  135. package/src/validators/stability-pool-validator.ts +0 -7
  136. package/src/validators/stableswap-validator.ts +0 -7
  137. package/src/validators/staking-validator.ts +0 -7
  138. package/src/validators/treasury-validator.ts +0 -7
  139. package/src/validators/version-record-policy.ts +0 -7
  140. package/src/validators/version-registry-validator.ts +0 -7
  141. package/tests/always-succeed/script.ts +0 -7
  142. package/tests/array-utils.test.ts +0 -92
  143. package/tests/bigint-utils.test.ts +0 -41
  144. package/tests/cdp/actions.ts +0 -680
  145. package/tests/cdp/cdp-helpers.ts +0 -55
  146. package/tests/cdp/cdp-queries.ts +0 -423
  147. package/tests/cdp/cdp.test.ts +0 -6730
  148. package/tests/cdp/transactions-mutated.ts +0 -1733
  149. package/tests/data/system-params.json +0 -1155
  150. package/tests/datums.test.ts +0 -302
  151. package/tests/endpoints/initialize.ts +0 -78
  152. package/tests/endpoints/interest-collector.ts +0 -37
  153. package/tests/endpoints/treasury.ts +0 -70
  154. package/tests/gov/actions.ts +0 -381
  155. package/tests/gov/gov.test.ts +0 -5147
  156. package/tests/gov/governance-queries.ts +0 -37
  157. package/tests/hash-checks.test.ts +0 -112
  158. package/tests/indigo-test-helpers.ts +0 -99
  159. package/tests/initialize.test.ts +0 -79
  160. package/tests/interest-calculations.test.ts +0 -120
  161. package/tests/interest-collection/interest-collection.test.ts +0 -843
  162. package/tests/interest-collection/transactions-mutated.ts +0 -260
  163. package/tests/interest-oracle.test.ts +0 -93
  164. package/tests/mock/assets-mock.ts +0 -243
  165. package/tests/mock/protocol-params-mock.ts +0 -21
  166. package/tests/price-oracle/actions.ts +0 -161
  167. package/tests/price-oracle/price-oracle-queries.ts +0 -12
  168. package/tests/price-oracle/price-oracle.test.ts +0 -228
  169. package/tests/price-oracle/transactions-mutated.ts +0 -59
  170. package/tests/pyth/endpoints.ts +0 -96
  171. package/tests/pyth/helpers.ts +0 -37
  172. package/tests/pyth/pyth-encoding.test.ts +0 -376
  173. package/tests/pyth/pyth-feeds.test.ts +0 -943
  174. package/tests/pyth/pyth-indigo.test.ts +0 -505
  175. package/tests/pyth/pyth.test.ts +0 -299
  176. package/tests/queries/collector-queries.ts +0 -27
  177. package/tests/queries/execute-queries.ts +0 -47
  178. package/tests/queries/iasset-queries.ts +0 -149
  179. package/tests/queries/interest-oracle-queries.ts +0 -12
  180. package/tests/queries/poll-queries.ts +0 -97
  181. package/tests/queries/staking-queries.ts +0 -54
  182. package/tests/rob/actions.ts +0 -56
  183. package/tests/rob/rob-leverage.test.ts +0 -2555
  184. package/tests/rob/rob-queries.ts +0 -95
  185. package/tests/rob/rob.test.ts +0 -3677
  186. package/tests/rob/transactions-mutated.ts +0 -844
  187. package/tests/script-size.test.ts +0 -240
  188. package/tests/setup.ts +0 -135
  189. package/tests/stability-pool/actions.ts +0 -229
  190. package/tests/stability-pool.test.ts +0 -6491
  191. package/tests/stableswap/stableswap-actions.ts +0 -83
  192. package/tests/stableswap/stableswap-queries.ts +0 -98
  193. package/tests/stableswap/stableswap.test.ts +0 -4471
  194. package/tests/stableswap/transactions-mutated.ts +0 -348
  195. package/tests/staking.test.ts +0 -172
  196. package/tests/test-helpers.ts +0 -94
  197. package/tests/treasury/actions.ts +0 -67
  198. package/tests/treasury/treasury.test.ts +0 -320
  199. package/tests/utils/asserts.ts +0 -87
  200. package/tests/utils/benchmark-utils.ts +0 -130
  201. package/tests/utils/index.ts +0 -168
  202. package/tsconfig.build.json +0 -4
  203. package/tsconfig.json +0 -27
  204. package/vitest.config.ts +0 -11
@@ -1,136 +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
- import { OCD_DECIMAL_UNIT } from '../../types/on-chain-decimal';
19
-
20
- export type StakingPositionOutput = {
21
- utxo: UTxO;
22
- datum: StakingPosition;
23
- };
24
- export type StakingManagerOutput = { utxo: UTxO; datum: StakingManager };
25
-
26
- /**
27
- * Update the staking position locked amount. In case proposal's voting finished, unlock the amount.
28
- */
29
- export function updateStakingLockedAmount(
30
- stakingPosLockedAmt: StakingPosLockedAmt,
31
- currentTime: bigint,
32
- ): StakingPosLockedAmt {
33
- return stakingPosLockedAmt.filter(
34
- ([_, { votingEnd }]) => votingEnd > currentTime,
35
- );
36
- }
37
-
38
- export function findStakingManagerByOutRef(
39
- stakingManagerRef: OutRef,
40
- lucid: LucidEvolution,
41
- ): Promise<StakingManagerOutput> {
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
- });
60
- }
61
-
62
- export function findStakingManager(
63
- params: SystemParams,
64
- lucid: LucidEvolution,
65
- ): Promise<StakingManagerOutput> {
66
- return lucid
67
- .utxosAtWithUnit(
68
- createScriptAddress(
69
- lucid.config().network!,
70
- validatorToScriptHash(mkStakingValidatorFromSP(params.stakingParams)),
71
- ),
72
- params.stakingParams.stakingManagerNFT[0].unCurrencySymbol +
73
- fromText(params.stakingParams.stakingManagerNFT[1].unTokenName),
74
- )
75
- .then((utxos) =>
76
- utxos
77
- .map((utxo) => {
78
- if (!utxo.datum) return undefined;
79
- const datum = parseStakingManagerDatum(utxo.datum);
80
- return { utxo, datum };
81
- })
82
- .find((utxo) => utxo !== undefined),
83
- )
84
- .then((result) => {
85
- if (!result)
86
- throw new Error(
87
- 'Unable to locate Staking Manager by output reference.',
88
- );
89
- return result;
90
- });
91
- }
92
-
93
- export function findStakingPositionByOutRef(
94
- stakingPositionRef: OutRef,
95
- lucid: LucidEvolution,
96
- ): Promise<StakingPositionOutput> {
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
- });
115
- }
116
-
117
- export const rewardSnapshotPrecision = OCD_DECIMAL_UNIT * OCD_DECIMAL_UNIT;
118
-
119
- export function distributeReward(
120
- snapshotAda: bigint,
121
- adaReward: bigint,
122
- totalStake: bigint,
123
- ): bigint {
124
- return snapshotAda + (adaReward * rewardSnapshotPrecision) / totalStake;
125
- }
126
-
127
- export function calculateAdaReward(
128
- currentSnapshotAda: bigint,
129
- oldSnapshotAda: bigint,
130
- existingIndyAmount: bigint,
131
- ): bigint {
132
- return (
133
- ((currentSnapshotAda - oldSnapshotAda) * existingIndyAmount) /
134
- rewardSnapshotPrecision
135
- );
136
- }
@@ -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: 'PlutusV3',
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,349 +0,0 @@
1
- import {
2
- addAssets,
3
- Data,
4
- fromHex,
5
- fromText,
6
- LucidEvolution,
7
- OutRef,
8
- toHex,
9
- TxBuilder,
10
- } from '@lucid-evolution/lucid';
11
- import {
12
- fromSystemParamsAsset,
13
- fromSystemParamsScriptRef,
14
- SystemParams,
15
- } from '../../types/system-params';
16
- import { addrDetails } from '../../utils/lucid-utils';
17
- import {
18
- calculateAdaReward,
19
- distributeReward,
20
- findStakingManager,
21
- findStakingManagerByOutRef,
22
- findStakingPositionByOutRef,
23
- updateStakingLockedAmount,
24
- } from './helpers';
25
- import {
26
- parseStakingManagerDatum,
27
- serialiseStakingDatum,
28
- serialiseStakingRedeemer,
29
- StakingManager,
30
- StakingPosition,
31
- } from './types-new';
32
- import { matchSingle } from '../../utils/utils';
33
- import { serialiseCollectorRedeemer } from '../collector/types-new';
34
- import {
35
- assetClassValueOf,
36
- getInlineDatumOrThrow,
37
- mkAssetsOf,
38
- mkLovelacesOf,
39
- } from '@3rd-eye-labs/cardano-offchain-common';
40
-
41
- export async function openStakingPosition(
42
- amount: bigint,
43
- params: SystemParams,
44
- lucid: LucidEvolution,
45
- stakingManagerRef?: OutRef,
46
- ): Promise<TxBuilder> {
47
- const [pkh, _] = await addrDetails(lucid);
48
-
49
- const stakingManagerOut = stakingManagerRef
50
- ? await findStakingManagerByOutRef(stakingManagerRef, lucid)
51
- : await findStakingManager(params, lucid);
52
-
53
- const stakingRefScriptUtxo = matchSingle(
54
- await lucid.utxosByOutRef([
55
- fromSystemParamsScriptRef(params.scriptReferences.stakingValidatorRef),
56
- ]),
57
- (_) => new Error('Expected a single staking Ref Script UTXO'),
58
- );
59
- const stakingTokenPolicyRefScriptUtxo = matchSingle(
60
- await lucid.utxosByOutRef([
61
- fromSystemParamsScriptRef(
62
- params.scriptReferences.authTokenPolicies.stakingTokenRef,
63
- ),
64
- ]),
65
- (_) => new Error('Expected a single staking token policy Ref Script UTXO'),
66
- );
67
-
68
- const newStakingManagerDatum: StakingManager = {
69
- totalStake: stakingManagerOut.datum.totalStake + amount,
70
- managerSnapshot: {
71
- snapshotAda: stakingManagerOut.datum.managerSnapshot.snapshotAda,
72
- },
73
- };
74
-
75
- const stakingPositionDatum: StakingPosition = {
76
- owner: fromHex(pkh.hash),
77
- lockedAmount: [],
78
- positionSnapshot: {
79
- snapshotAda: stakingManagerOut.datum.managerSnapshot.snapshotAda,
80
- },
81
- };
82
-
83
- const stakingToken =
84
- params.stakingParams.stakingToken[0].unCurrencySymbol +
85
- fromText(params.stakingParams.stakingToken[1].unTokenName);
86
- const indyToken =
87
- params.stakingParams.indyToken[0].unCurrencySymbol +
88
- fromText(params.stakingParams.indyToken[1].unTokenName);
89
- return lucid
90
- .newTx()
91
- .collectFrom(
92
- [stakingManagerOut.utxo],
93
- serialiseStakingRedeemer({
94
- CreateStakingPosition: { creatorPkh: fromHex(pkh.hash) },
95
- }),
96
- )
97
- .readFrom([stakingRefScriptUtxo])
98
- .pay.ToContract(
99
- stakingManagerOut.utxo.address,
100
- {
101
- kind: 'inline',
102
- value: serialiseStakingDatum(newStakingManagerDatum),
103
- },
104
- stakingManagerOut.utxo.assets,
105
- )
106
- .readFrom([stakingTokenPolicyRefScriptUtxo])
107
- .mintAssets(
108
- {
109
- [stakingToken]: 1n,
110
- },
111
- Data.void(),
112
- )
113
- .pay.ToContract(
114
- stakingManagerOut.utxo.address,
115
- {
116
- kind: 'inline',
117
- value: serialiseStakingDatum(stakingPositionDatum),
118
- },
119
- {
120
- [stakingToken]: 1n,
121
- [indyToken]: amount,
122
- },
123
- )
124
- .addSignerKey(pkh.hash);
125
- }
126
-
127
- export async function adjustStakingPosition(
128
- stakingPositionRef: OutRef,
129
- amount: bigint,
130
- params: SystemParams,
131
- lucid: LucidEvolution,
132
- stakingManagerRef?: OutRef,
133
- ): Promise<TxBuilder> {
134
- const now = BigInt(lucid.slotToUnixTime(lucid.currentSlot()));
135
-
136
- const stakingPositionOut = await findStakingPositionByOutRef(
137
- stakingPositionRef,
138
- lucid,
139
- );
140
- const stakingManagerOut = stakingManagerRef
141
- ? await findStakingManagerByOutRef(stakingManagerRef, lucid)
142
- : await findStakingManager(params, lucid);
143
-
144
- const stakingRefScriptUtxo = matchSingle(
145
- await lucid.utxosByOutRef([
146
- fromSystemParamsScriptRef(params.scriptReferences.stakingValidatorRef),
147
- ]),
148
- (_) => new Error('Expected a single staking Ref Script UTXO'),
149
- );
150
-
151
- const indyToken = fromSystemParamsAsset(params.stakingParams.indyToken);
152
-
153
- const adaReward = calculateAdaReward(
154
- stakingManagerOut.datum.managerSnapshot.snapshotAda,
155
- stakingPositionOut.datum.positionSnapshot.snapshotAda,
156
- assetClassValueOf(stakingPositionOut.utxo.assets, indyToken),
157
- );
158
-
159
- const newLockedAmount = updateStakingLockedAmount(
160
- stakingPositionOut.datum.lockedAmount,
161
- BigInt(now),
162
- );
163
-
164
- return lucid
165
- .newTx()
166
- .validFrom(Number(now))
167
- .readFrom([stakingRefScriptUtxo])
168
- .collectFrom(
169
- [stakingPositionOut.utxo],
170
- serialiseStakingRedeemer({
171
- AdjustStakedAmount: { adjustAmount: amount },
172
- }),
173
- )
174
- .collectFrom(
175
- [stakingManagerOut.utxo],
176
- serialiseStakingRedeemer('UpdateTotalStake'),
177
- )
178
- .pay.ToContract(
179
- stakingManagerOut.utxo.address,
180
- {
181
- kind: 'inline',
182
- value: serialiseStakingDatum({
183
- ...stakingManagerOut.datum,
184
- totalStake: stakingManagerOut.datum.totalStake + amount,
185
- }),
186
- },
187
- addAssets(stakingManagerOut.utxo.assets, mkLovelacesOf(-adaReward)),
188
- )
189
- .pay.ToContract(
190
- stakingPositionOut.utxo.address,
191
- {
192
- kind: 'inline',
193
- value: serialiseStakingDatum({
194
- ...stakingPositionOut.datum,
195
- positionSnapshot: {
196
- snapshotAda: stakingManagerOut.datum.managerSnapshot.snapshotAda,
197
- },
198
- lockedAmount: newLockedAmount,
199
- }),
200
- },
201
- addAssets(stakingPositionOut.utxo.assets, mkAssetsOf(indyToken, amount)),
202
- )
203
- .addSignerKey(toHex(stakingPositionOut.datum.owner));
204
- }
205
-
206
- export async function closeStakingPosition(
207
- stakingPositionRef: OutRef,
208
- params: SystemParams,
209
- lucid: LucidEvolution,
210
- stakingManagerRef?: OutRef,
211
- ): Promise<TxBuilder> {
212
- const now = BigInt(lucid.slotToUnixTime(lucid.currentSlot()));
213
-
214
- const stakingPositionOut = await findStakingPositionByOutRef(
215
- stakingPositionRef,
216
- lucid,
217
- );
218
- const stakingManagerOut = stakingManagerRef
219
- ? await findStakingManagerByOutRef(stakingManagerRef, lucid)
220
- : await findStakingManager(params, lucid);
221
-
222
- const stakingRefScriptUtxo = matchSingle(
223
- await lucid.utxosByOutRef([
224
- fromSystemParamsScriptRef(params.scriptReferences.stakingValidatorRef),
225
- ]),
226
- (_) => new Error('Expected a single staking Ref Script UTXO'),
227
- );
228
- const stakingTokenPolicyRefScriptUtxo = matchSingle(
229
- await lucid.utxosByOutRef([
230
- fromSystemParamsScriptRef(
231
- params.scriptReferences.authTokenPolicies.stakingTokenRef,
232
- ),
233
- ]),
234
- (_) => new Error('Expected a single staking token policy Ref Script UTXO'),
235
- );
236
-
237
- const indyToken = fromSystemParamsAsset(params.stakingParams.indyToken);
238
-
239
- const existingIndyAmount = assetClassValueOf(
240
- stakingPositionOut.utxo.assets,
241
- indyToken,
242
- );
243
- const adaReward = calculateAdaReward(
244
- stakingManagerOut.datum.managerSnapshot.snapshotAda,
245
- stakingPositionOut.datum.positionSnapshot.snapshotAda,
246
- assetClassValueOf(stakingPositionOut.utxo.assets, indyToken),
247
- );
248
-
249
- return lucid
250
- .newTx()
251
- .validFrom(Number(now))
252
- .readFrom([stakingRefScriptUtxo, stakingTokenPolicyRefScriptUtxo])
253
- .collectFrom([stakingPositionOut.utxo], serialiseStakingRedeemer('Unstake'))
254
- .collectFrom(
255
- [stakingManagerOut.utxo],
256
- serialiseStakingRedeemer('UpdateTotalStake'),
257
- )
258
- .pay.ToContract(
259
- stakingManagerOut.utxo.address,
260
- {
261
- kind: 'inline',
262
- value: serialiseStakingDatum({
263
- ...stakingManagerOut.datum,
264
- totalStake: stakingManagerOut.datum.totalStake - existingIndyAmount,
265
- }),
266
- },
267
- addAssets(stakingManagerOut.utxo.assets, mkLovelacesOf(-adaReward)),
268
- )
269
- .mintAssets(
270
- mkAssetsOf(fromSystemParamsAsset(params.stakingParams.stakingToken), -1n),
271
- Data.void(),
272
- )
273
- .addSignerKey(toHex(stakingPositionOut.datum.owner));
274
- }
275
-
276
- const MIN_UTXO_AMOUNT = 2_000_000n;
277
-
278
- export async function distributeAda(
279
- stakingManagerRef: OutRef,
280
- collectorRefs: OutRef[],
281
- params: SystemParams,
282
- lucid: LucidEvolution,
283
- ): Promise<TxBuilder> {
284
- const [stakingManagerUtxo] = await lucid.utxosByOutRef([stakingManagerRef]);
285
- const stakingManagerDatum = parseStakingManagerDatum(
286
- getInlineDatumOrThrow(stakingManagerUtxo),
287
- );
288
- const collectorUtxos = (await lucid.utxosByOutRef(collectorRefs))
289
- .filter((utxo) => utxo.datum && utxo.datum === Data.void())
290
- .filter((utxo) => utxo.assets['lovelace'] > MIN_UTXO_AMOUNT);
291
-
292
- if (collectorUtxos.length === 0) {
293
- throw new Error('No available collectors found');
294
- }
295
-
296
- const adaRewardCollected = collectorUtxos.reduce(
297
- (acc, utxo) => acc + utxo.assets['lovelace'] - MIN_UTXO_AMOUNT,
298
- 0n,
299
- );
300
- const newSnapshot = distributeReward(
301
- stakingManagerDatum.managerSnapshot.snapshotAda,
302
- adaRewardCollected,
303
- stakingManagerDatum.totalStake,
304
- );
305
-
306
- const stakingRefScriptUtxo = matchSingle(
307
- await lucid.utxosByOutRef([
308
- fromSystemParamsScriptRef(params.scriptReferences.stakingValidatorRef),
309
- ]),
310
- (_) => new Error('Expected a single staking Ref Script UTXO'),
311
- );
312
-
313
- const collectorRefScriptUtxo = matchSingle(
314
- await lucid.utxosByOutRef([
315
- fromSystemParamsScriptRef(params.scriptReferences.collectorValidatorRef),
316
- ]),
317
- (_) => new Error('Expected a single staking Ref Script UTXO'),
318
- );
319
-
320
- const tx = lucid
321
- .newTx()
322
- .readFrom([stakingRefScriptUtxo, collectorRefScriptUtxo])
323
- .collectFrom([stakingManagerUtxo], serialiseStakingRedeemer('Distribute'))
324
- .collectFrom(
325
- collectorUtxos,
326
- serialiseCollectorRedeemer('DistributeToStakers'),
327
- )
328
- .pay.ToContract(
329
- stakingManagerUtxo.address,
330
- {
331
- kind: 'inline',
332
- value: serialiseStakingDatum({
333
- ...stakingManagerDatum,
334
- managerSnapshot: { snapshotAda: newSnapshot },
335
- }),
336
- },
337
- addAssets(stakingManagerUtxo.assets, mkLovelacesOf(adaRewardCollected)),
338
- );
339
-
340
- for (const collectorUtxo of collectorUtxos) {
341
- tx.pay.ToContract(
342
- collectorUtxo.address,
343
- { kind: 'inline', value: Data.void() },
344
- mkLovelacesOf(MIN_UTXO_AMOUNT),
345
- );
346
- }
347
-
348
- return tx;
349
- }
@@ -1,137 +0,0 @@
1
- import { TSchema, Data } from '@evolution-sdk/evolution';
2
- import { option as O, function as F } from 'fp-ts';
3
- import { match, P } from 'ts-pattern';
4
- import { DEFAULT_SCHEMA_OPTIONS } from '../../types/evolution-schema-options';
5
-
6
- const StakingPosLockedAmtSchema = TSchema.Array(
7
- TSchema.Tuple([
8
- TSchema.Integer,
9
- TSchema.Struct({
10
- voteAmt: TSchema.Integer,
11
- votingEnd: TSchema.Integer,
12
- }),
13
- ]),
14
- );
15
-
16
- export type StakingPosLockedAmt = typeof StakingPosLockedAmtSchema.Type;
17
-
18
- const RewardSnapshotSchema = TSchema.Struct({
19
- snapshotAda: TSchema.Integer,
20
- });
21
-
22
- const StakingPositionSchema = TSchema.Struct({
23
- owner: TSchema.ByteArray,
24
- lockedAmount: StakingPosLockedAmtSchema,
25
- positionSnapshot: RewardSnapshotSchema,
26
- });
27
- export type StakingPosition = typeof StakingPositionSchema.Type;
28
-
29
- const StakingManagerSchema = TSchema.Struct({
30
- totalStake: TSchema.Integer,
31
- managerSnapshot: RewardSnapshotSchema,
32
- });
33
- export type StakingManager = typeof StakingManagerSchema.Type;
34
-
35
- const StakingDatumSchema = TSchema.Union(
36
- StakingManagerSchema,
37
- StakingPositionSchema,
38
- );
39
- type StakingDatum = typeof StakingDatumSchema.Type;
40
-
41
- const StakingRedeemerSchema = TSchema.Union(
42
- TSchema.Struct(
43
- {
44
- CreateStakingPosition: TSchema.Struct(
45
- { creatorPkh: TSchema.ByteArray },
46
- { flatFields: true },
47
- ),
48
- },
49
- { flatInUnion: true },
50
- ),
51
- TSchema.Literal('UpdateTotalStake', { flatInUnion: true }),
52
- TSchema.Literal('Distribute', { flatInUnion: true }),
53
- TSchema.Struct(
54
- {
55
- AdjustStakedAmount: TSchema.Struct(
56
- { adjustAmount: TSchema.Integer },
57
- { flatFields: true },
58
- ),
59
- },
60
- { flatInUnion: true },
61
- ),
62
- TSchema.Literal('Unstake', { flatInUnion: true }),
63
- TSchema.Literal('Lock', { flatInUnion: true }),
64
- TSchema.Literal('UpgradeVersion', { flatInUnion: true }),
65
- );
66
-
67
- export type StakingRedeemer = typeof StakingRedeemerSchema.Type;
68
-
69
- export function serialiseStakingRedeemer(r: StakingRedeemer): string {
70
- return Data.withSchema(
71
- StakingRedeemerSchema,
72
- DEFAULT_SCHEMA_OPTIONS,
73
- ).toCBORHex(r);
74
- }
75
-
76
- export function parseStakingRedeemer(datum: string): O.Option<StakingRedeemer> {
77
- try {
78
- return O.some(
79
- Data.withSchema(
80
- StakingRedeemerSchema,
81
- DEFAULT_SCHEMA_OPTIONS,
82
- ).fromCBORHex(datum),
83
- );
84
- } catch (_) {
85
- return O.none;
86
- }
87
- }
88
-
89
- export function parseStakingRedeemerOrThrow(datum: string): StakingRedeemer {
90
- return F.pipe(
91
- parseStakingRedeemer(datum),
92
- O.match(() => {
93
- throw new Error('Expected a StakingRedeemer datum.');
94
- }, F.identity),
95
- );
96
- }
97
-
98
- export function parseStakingPosition(datum: string): O.Option<StakingPosition> {
99
- try {
100
- return match(
101
- Data.withSchema(StakingDatumSchema, DEFAULT_SCHEMA_OPTIONS).fromCBORHex(
102
- datum,
103
- ),
104
- )
105
- .with({ owner: P.any }, (res) => O.some(res))
106
- .otherwise(() => O.none);
107
- } catch (_) {
108
- return O.none;
109
- }
110
- }
111
-
112
- export function parseStakingPositionOrThrow(datum: string): StakingPosition {
113
- return F.pipe(
114
- parseStakingPosition(datum),
115
- O.match(() => {
116
- throw new Error('Expected a StakingPosition datum.');
117
- }, F.identity),
118
- );
119
- }
120
-
121
- export function parseStakingManagerDatum(datum: string): StakingManager {
122
- return match(
123
- Data.withSchema(StakingDatumSchema, DEFAULT_SCHEMA_OPTIONS).fromCBORHex(
124
- datum,
125
- ),
126
- )
127
- .with({ totalStake: P.any }, (res) => res)
128
- .otherwise(() => {
129
- throw new Error('Expected a StakingPosition datum.');
130
- });
131
- }
132
-
133
- export function serialiseStakingDatum(d: StakingDatum): string {
134
- return Data.withSchema(StakingDatumSchema, DEFAULT_SCHEMA_OPTIONS).toCBORHex(
135
- d,
136
- );
137
- }
@@ -1,17 +0,0 @@
1
- import { Data } from '@lucid-evolution/lucid';
2
- import { AssetClassSchema } from '../../types/generic';
3
-
4
- const StakingParamsSchema = Data.Object({
5
- stakingManagerNft: AssetClassSchema,
6
- stakingToken: AssetClassSchema,
7
- indyToken: AssetClassSchema,
8
- pollToken: AssetClassSchema,
9
- versionRecordToken: AssetClassSchema,
10
- collectorValHash: Data.Bytes(),
11
- });
12
- type StakingParams = Data.Static<typeof StakingParamsSchema>;
13
- const StakingParams = StakingParamsSchema as unknown as StakingParams;
14
-
15
- export function castStakingParams(params: StakingParams): Data {
16
- return Data.castTo(params, StakingParams);
17
- }
@@ -1,21 +0,0 @@
1
- import { Address, LucidEvolution } from '@lucid-evolution/lucid';
2
- import {
3
- fromSysParamsCredential,
4
- SystemParams,
5
- } from '../../types/system-params';
6
- import { createScriptAddress } from '../../utils/lucid-utils';
7
-
8
- export function mkTreasuryAddr(
9
- lucid: LucidEvolution,
10
- sysParams: SystemParams,
11
- ): Address {
12
- return createScriptAddress(
13
- lucid.config().network!,
14
- sysParams.validatorHashes.treasuryHash,
15
- sysParams.treasuryParams.treasuryUtxosStakeCredential != null
16
- ? fromSysParamsCredential(
17
- sysParams.treasuryParams.treasuryUtxosStakeCredential,
18
- )
19
- : undefined,
20
- );
21
- }