@indigo-labs/indigo-sdk 0.2.42 → 0.3.1

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 (202) hide show
  1. package/.github/workflows/ci.yml +4 -2
  2. package/README.md +88 -15
  3. package/dist/index.d.mts +3012 -2194
  4. package/dist/index.d.ts +3012 -2194
  5. package/dist/index.js +9849 -6198
  6. package/dist/index.mjs +8733 -4933
  7. package/package.json +14 -3
  8. package/src/contracts/cdp/helpers.ts +68 -72
  9. package/src/contracts/cdp/scripts.ts +50 -13
  10. package/src/contracts/cdp/transactions.ts +841 -546
  11. package/src/contracts/cdp/types-new.ts +256 -0
  12. package/src/contracts/cdp/types.ts +26 -144
  13. package/src/contracts/cdp-creator/scripts.ts +15 -9
  14. package/src/contracts/cdp-creator/types-new.ts +50 -0
  15. package/src/contracts/cdp-creator/types.ts +5 -31
  16. package/src/contracts/collector/scripts.ts +1 -1
  17. package/src/contracts/collector/transactions.ts +23 -13
  18. package/src/contracts/collector/types-new.ts +17 -0
  19. package/src/contracts/execute/scripts.ts +19 -10
  20. package/src/contracts/execute/types-new.ts +44 -0
  21. package/src/contracts/execute/types.ts +5 -38
  22. package/src/contracts/gov/helpers.ts +187 -51
  23. package/src/contracts/gov/scripts.ts +17 -10
  24. package/src/contracts/gov/transactions.ts +599 -271
  25. package/src/contracts/gov/types-new.ts +253 -100
  26. package/src/contracts/gov/types.ts +4 -71
  27. package/src/contracts/iasset/helpers.ts +172 -0
  28. package/src/contracts/iasset/scripts.ts +38 -0
  29. package/src/contracts/iasset/types.ts +154 -0
  30. package/src/contracts/initialize/actions.ts +768 -0
  31. package/src/contracts/initialize/helpers.ts +611 -36
  32. package/src/contracts/initialize/types.ts +102 -28
  33. package/src/contracts/interest-collection/helpers.ts +19 -0
  34. package/src/contracts/interest-collection/scripts.ts +44 -0
  35. package/src/contracts/interest-collection/transactions.ts +436 -0
  36. package/src/contracts/interest-collection/types-new.ts +50 -0
  37. package/src/contracts/interest-collection/types.ts +26 -0
  38. package/src/contracts/interest-oracle/helpers.ts +2 -30
  39. package/src/contracts/interest-oracle/scripts.ts +1 -1
  40. package/src/contracts/interest-oracle/transactions.ts +21 -16
  41. package/src/contracts/interest-oracle/types-new.ts +32 -0
  42. package/src/contracts/interest-oracle/types.ts +1 -40
  43. package/src/contracts/one-shot/transactions.ts +1 -2
  44. package/src/contracts/poll/helpers.ts +5 -23
  45. package/src/contracts/poll/scripts.ts +12 -13
  46. package/src/contracts/poll/types-poll-manager.ts +1 -19
  47. package/src/contracts/poll/types-poll-new.ts +170 -0
  48. package/src/contracts/poll/types-poll-shard.ts +2 -24
  49. package/src/contracts/price-oracle/helpers.ts +1 -4
  50. package/src/contracts/price-oracle/scripts.ts +3 -8
  51. package/src/contracts/price-oracle/transactions.ts +32 -25
  52. package/src/contracts/price-oracle/types-new.ts +50 -0
  53. package/src/contracts/price-oracle/types.ts +2 -36
  54. package/src/contracts/pyth-feed/helpers.ts +58 -0
  55. package/src/contracts/pyth-feed/scripts.ts +15 -0
  56. package/src/contracts/pyth-feed/types.ts +181 -0
  57. package/src/contracts/rob/helpers.ts +405 -0
  58. package/src/contracts/rob/scripts.ts +35 -0
  59. package/src/contracts/rob/transactions.ts +410 -0
  60. package/src/contracts/rob/types-new.ts +128 -0
  61. package/src/contracts/rob/types.ts +16 -0
  62. package/src/contracts/rob-leverage/helpers.ts +424 -0
  63. package/src/contracts/{leverage → rob-leverage}/transactions.ts +68 -48
  64. package/src/contracts/stability-pool/helpers.ts +714 -230
  65. package/src/contracts/stability-pool/scripts.ts +20 -15
  66. package/src/contracts/stability-pool/transactions.ts +628 -496
  67. package/src/contracts/stability-pool/types-new.ts +247 -100
  68. package/src/contracts/stability-pool/types.ts +5 -22
  69. package/src/contracts/stableswap/helpers.ts +22 -0
  70. package/src/contracts/stableswap/scripts.ts +37 -0
  71. package/src/contracts/stableswap/transactions.ts +647 -0
  72. package/src/contracts/stableswap/types-new.ts +131 -0
  73. package/src/contracts/stableswap/types.ts +17 -0
  74. package/src/contracts/staking/helpers.ts +49 -34
  75. package/src/contracts/staking/scripts.ts +1 -1
  76. package/src/contracts/staking/transactions.ts +85 -130
  77. package/src/contracts/staking/types-new.ts +60 -28
  78. package/src/contracts/staking/types.ts +1 -28
  79. package/src/contracts/treasury/helpers.ts +21 -0
  80. package/src/contracts/treasury/scripts.ts +16 -26
  81. package/src/contracts/treasury/transactions.ts +256 -27
  82. package/src/contracts/treasury/types-new.ts +69 -0
  83. package/src/contracts/treasury/types.ts +2 -43
  84. package/src/contracts/version-registry/scripts.ts +2 -2
  85. package/src/contracts/version-registry/types-new.ts +6 -7
  86. package/src/index.ts +37 -20
  87. package/src/scripts/auth-token-policy.ts +3 -2
  88. package/src/scripts/iasset-policy.ts +3 -2
  89. package/src/types/evolution-schema-options.ts +3 -3
  90. package/src/types/generic.ts +17 -89
  91. package/src/types/multisig.ts +48 -0
  92. package/src/types/on-chain-decimal.ts +14 -7
  93. package/src/types/rational.ts +61 -0
  94. package/src/types/system-params.ts +237 -41
  95. package/src/utils/array-utils.ts +70 -1
  96. package/src/utils/bigint-utils.ts +12 -0
  97. package/src/utils/indigo-helpers.ts +8 -10
  98. package/src/utils/lucid-utils.ts +47 -40
  99. package/src/utils/oracle-helpers.ts +62 -0
  100. package/src/utils/pyth/decode.ts +223 -0
  101. package/src/utils/pyth/encode.ts +262 -0
  102. package/src/utils/pyth/index.ts +14 -0
  103. package/src/utils/pyth/types.ts +87 -0
  104. package/src/validators/always-succeed-validator.ts +6 -0
  105. package/src/validators/cdp-creator-validator.ts +2 -2
  106. package/src/validators/cdp-redeem-validator.ts +7 -0
  107. package/src/validators/cdp-validator.ts +2 -2
  108. package/src/validators/collector-validator.ts +2 -2
  109. package/src/validators/execute-validator.ts +2 -2
  110. package/src/validators/governance-validator.ts +2 -2
  111. package/src/validators/iasset-validator.ts +7 -0
  112. package/src/validators/interest-collection-validator.ts +7 -0
  113. package/src/validators/interest-oracle-validator.ts +2 -2
  114. package/src/validators/poll-manager-validator.ts +2 -2
  115. package/src/validators/poll-shard-validator.ts +2 -2
  116. package/src/validators/price-oracle-validator.ts +7 -0
  117. package/src/validators/pyth-feed-validator.ts +7 -0
  118. package/src/validators/rob-validator.ts +7 -0
  119. package/src/validators/stability-pool-validator.ts +2 -2
  120. package/src/validators/stableswap-validator.ts +7 -0
  121. package/src/validators/staking-validator.ts +2 -2
  122. package/src/validators/treasury-validator.ts +2 -2
  123. package/src/validators/version-record-policy.ts +2 -2
  124. package/src/validators/version-registry-validator.ts +2 -2
  125. package/tests/always-succeed/script.ts +7 -0
  126. package/tests/bigint-utils.test.ts +41 -0
  127. package/tests/cdp/actions.ts +610 -0
  128. package/tests/cdp/cdp-helpers.ts +55 -0
  129. package/tests/cdp/cdp-queries.ts +440 -0
  130. package/tests/cdp/cdp.test.ts +6087 -0
  131. package/tests/cdp/transactions-mutated.ts +1729 -0
  132. package/tests/data/system-params.json +177 -34
  133. package/tests/datums.test.ts +209 -210
  134. package/tests/endpoints/initialize.ts +68 -0
  135. package/tests/endpoints/interest-collector.ts +37 -0
  136. package/tests/endpoints/treasury.ts +70 -0
  137. package/tests/gov/actions.ts +406 -0
  138. package/tests/gov/gov.test.ts +4450 -0
  139. package/tests/{queries → gov}/governance-queries.ts +6 -3
  140. package/tests/hash-checks.test.ts +38 -11
  141. package/tests/indigo-test-helpers.ts +100 -0
  142. package/tests/initialize.test.ts +61 -9
  143. package/tests/interest-collection/interest-collection.test.ts +892 -0
  144. package/tests/interest-collection/interest-collector-queries.ts +49 -0
  145. package/tests/interest-collection/transactions-mutated.ts +260 -0
  146. package/tests/interest-oracle.test.ts +43 -35
  147. package/tests/mock/assets-mock.ts +234 -23
  148. package/tests/mock/protocol-params-mock.ts +21 -0
  149. package/tests/price-oracle/actions.ts +163 -0
  150. package/tests/price-oracle/price-oracle-queries.ts +12 -0
  151. package/tests/price-oracle/price-oracle.test.ts +240 -0
  152. package/tests/price-oracle/transactions-mutated.ts +62 -0
  153. package/tests/pyth/endpoints.ts +96 -0
  154. package/tests/pyth/helpers.ts +37 -0
  155. package/tests/pyth/pyth-encoding.test.ts +376 -0
  156. package/tests/pyth/pyth-indigo.test.ts +509 -0
  157. package/tests/pyth/pyth.test.ts +300 -0
  158. package/tests/queries/execute-queries.ts +6 -5
  159. package/tests/queries/iasset-queries.ts +175 -5
  160. package/tests/queries/interest-oracle-queries.ts +4 -2
  161. package/tests/queries/poll-queries.ts +8 -9
  162. package/tests/queries/stability-pool-queries.ts +95 -48
  163. package/tests/queries/staking-queries.ts +4 -2
  164. package/tests/queries/treasury-queries.ts +80 -5
  165. package/tests/rob/actions.ts +58 -0
  166. package/tests/{lrp-leverage.test.ts → rob/rob-leverage.test.ts} +393 -296
  167. package/tests/rob/rob-queries.ts +95 -0
  168. package/tests/rob/rob.test.ts +3762 -0
  169. package/tests/rob/transactions-mutated.ts +853 -0
  170. package/tests/script-size.test.ts +240 -0
  171. package/tests/setup.ts +135 -0
  172. package/tests/stability-pool/actions.ts +220 -0
  173. package/tests/stability-pool.test.ts +6121 -667
  174. package/tests/stableswap/stableswap-actions.ts +84 -0
  175. package/tests/stableswap/stableswap-queries.ts +89 -0
  176. package/tests/stableswap/stableswap.test.ts +3891 -0
  177. package/tests/stableswap/transactions-mutated.ts +348 -0
  178. package/tests/staking.test.ts +82 -99
  179. package/tests/test-helpers.ts +58 -11
  180. package/tests/treasury.test.ts +242 -0
  181. package/tests/utils/asserts.ts +74 -0
  182. package/tests/utils/benchmark-utils.ts +81 -0
  183. package/tests/utils/index.ts +122 -4
  184. package/tsconfig.json +9 -1
  185. package/vitest.config.ts +3 -1
  186. package/src/contracts/collector/types.ts +0 -16
  187. package/src/contracts/initialize/transactions.ts +0 -891
  188. package/src/contracts/leverage/helpers.ts +0 -424
  189. package/src/contracts/lrp/helpers.ts +0 -294
  190. package/src/contracts/lrp/scripts.ts +0 -27
  191. package/src/contracts/lrp/transactions.ts +0 -250
  192. package/src/contracts/lrp/types.ts +0 -131
  193. package/src/contracts/poll/types-poll.ts +0 -88
  194. package/src/contracts/vesting/helpers.ts +0 -218
  195. package/src/utils/value-helpers.ts +0 -37
  196. package/src/validators/lrp-validator.ts +0 -7
  197. package/tests/cdp.test.ts +0 -1528
  198. package/tests/gov.test.ts +0 -2011
  199. package/tests/lrp.test.ts +0 -673
  200. package/tests/queries/cdp-queries.ts +0 -220
  201. package/tests/queries/lrp-queries.ts +0 -76
  202. 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 { fromText, UTxO, validatorToScriptHash } from '@lucid-evolution/lucid';
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 new Map(
33
- stakingPosLockedAmt
34
- .entries()
35
- .filter(([_, { votingEnd }]) => votingEnd > currentTime),
33
+ return stakingPosLockedAmt.filter(
34
+ ([_, { votingEnd }]) => votingEnd > currentTime,
36
35
  );
37
36
  }
38
37
 
39
- export async function findStakingManagerByOutRef(
40
- stakingManager: UtxoOrOutRef,
38
+ export function findStakingManagerByOutRef(
39
+ stakingManagerRef: OutRef,
41
40
  lucid: LucidEvolution,
42
41
  ): Promise<StakingManagerOutput> {
43
- const utxo = await resolveUtxo(
44
- stakingManager,
45
- lucid,
46
- 'Unable to locate Staking Manager by output reference.',
47
- );
48
- if (!utxo.datum) {
49
- throw new Error('Staking Manager UTxO has no datum.');
50
- }
51
- const datum = parseStakingManagerDatum(utxo.datum);
52
- return { utxo, datum };
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 async function findStakingPositionByOutRef(
87
- stakingPosition: UtxoOrOutRef,
93
+ export function findStakingPositionByOutRef(
94
+ stakingPositionRef: OutRef,
88
95
  lucid: LucidEvolution,
89
96
  ): Promise<StakingPositionOutput> {
90
- const utxo = await resolveUtxo(
91
- stakingPosition,
92
- lucid,
93
- 'Unable to locate Staking Position by output reference.',
94
- );
95
- if (!utxo.datum) {
96
- throw new Error('Staking Position UTxO has no datum.');
97
- }
98
- const datum = parseStakingPositionOrThrow(utxo.datum);
99
- return { utxo, datum };
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;
@@ -11,7 +11,7 @@ export const mkStakingValidatorFromSP = (
11
11
  params: StakingParamsSP,
12
12
  ): SpendingValidator => {
13
13
  return {
14
- type: 'PlutusV2',
14
+ type: 'PlutusV3',
15
15
  script: applyParamsToScript(_stakingValidator.cborHex, [
16
16
  new Constr(0, [
17
17
  new Constr(0, [
@@ -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
- addrDetails,
18
- getInlineDatumOrThrow,
19
- resolveUtxo,
20
- UtxoOrOutRef,
21
- } from '../../utils/lucid-utils';
22
- import {
19
+ calculateAdaReward,
23
20
  distributeReward,
24
- rewardSnapshotPrecision,
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 { serialiseStakingRedeemer } from './types';
36
- import { serialiseCollectorRedeemer } from '../collector/types';
37
- import { mkAssetsOf, mkLovelacesOf } from '../../utils/value-helpers';
38
- import { ONE_SECOND } from '../../utils/time-helpers';
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
- stakingManager: UtxoOrOutRef,
46
+ stakingManagerRef?: OutRef,
45
47
  ): Promise<TxBuilder> {
46
48
  const [pkh, _] = await addrDetails(lucid);
47
49
 
48
- const stakingManagerUtxo = await resolveUtxo(
49
- stakingManager,
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: stakingManagerDatum.totalStake + amount,
70
+ totalStake: stakingManagerOut.datum.totalStake + amount,
74
71
  managerSnapshot: {
75
- snapshotAda: stakingManagerDatum.managerSnapshot.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: new Map([]),
78
+ lockedAmount: [],
82
79
  positionSnapshot: {
83
- snapshotAda: stakingManagerDatum.managerSnapshot.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
- [stakingManagerUtxo],
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
- stakingManagerUtxo.address,
100
+ stakingManagerOut.utxo.address,
104
101
  {
105
102
  kind: 'inline',
106
103
  value: serialiseStakingDatum(newStakingManagerDatum),
107
104
  },
108
- stakingManagerUtxo.assets,
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
- stakingManagerUtxo.address,
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
- stakingPosition: UtxoOrOutRef,
129
+ stakingPositionRef: OutRef,
133
130
  amount: bigint,
134
131
  params: SystemParams,
135
132
  lucid: LucidEvolution,
136
133
  currentSlot: number,
137
- stakingManager: UtxoOrOutRef,
134
+ stakingManagerRef?: OutRef,
138
135
  ): Promise<TxBuilder> {
139
136
  const network = lucid.config().network!;
140
- const currentTime = slotToUnixTime(network, currentSlot) - 120 * ONE_SECOND;
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 stakingManagerUtxo = await resolveUtxo(
152
- stakingManager,
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 existingIndyAmount = stakingPositionUtxo.assets[indyToken] ?? 0n;
172
- const currentSnapshotAda = stakingManagerDatum.managerSnapshot.snapshotAda;
173
- const oldSnapshotAda = stakingPositionDatum.positionSnapshot.snapshotAda;
174
- const adaReward =
175
- ((currentSnapshotAda - oldSnapshotAda) * existingIndyAmount) /
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
- stakingPositionDatum.lockedAmount,
180
- BigInt(currentTime),
163
+ stakingPositionOut.datum.lockedAmount,
164
+ BigInt(now),
181
165
  );
182
166
 
183
167
  return lucid
184
168
  .newTx()
185
- .validFrom(currentTime)
169
+ .validFrom(Number(now))
186
170
  .readFrom([stakingRefScriptUtxo])
187
171
  .collectFrom(
188
- [stakingPositionUtxo],
172
+ [stakingPositionOut.utxo],
189
173
  serialiseStakingRedeemer({
190
174
  AdjustStakedAmount: { adjustAmount: amount },
191
175
  }),
192
176
  )
193
177
  .collectFrom(
194
- [stakingManagerUtxo],
178
+ [stakingManagerOut.utxo],
195
179
  serialiseStakingRedeemer('UpdateTotalStake'),
196
180
  )
197
181
  .pay.ToContract(
198
- stakingManagerUtxo.address,
182
+ stakingManagerOut.utxo.address,
199
183
  {
200
184
  kind: 'inline',
201
185
  value: serialiseStakingDatum({
202
- ...stakingManagerDatum,
203
- totalStake: stakingManagerDatum.totalStake + amount,
186
+ ...stakingManagerOut.datum,
187
+ totalStake: stakingManagerOut.datum.totalStake + amount,
204
188
  }),
205
189
  },
206
- addAssets(stakingManagerUtxo.assets, mkLovelacesOf(-adaReward)),
190
+ addAssets(stakingManagerOut.utxo.assets, mkLovelacesOf(-adaReward)),
207
191
  )
208
192
  .pay.ToContract(
209
- stakingPositionUtxo.address,
193
+ stakingPositionOut.utxo.address,
210
194
  {
211
195
  kind: 'inline',
212
196
  value: serialiseStakingDatum({
213
- ...stakingPositionDatum,
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(stakingPositionDatum.owner));
203
+ .addSignerKey(toHex(stakingPositionOut.datum.owner));
230
204
  }
231
205
 
232
206
  export async function closeStakingPosition(
233
- stakingPosition: UtxoOrOutRef,
207
+ stakingPositionRef: OutRef,
234
208
  params: SystemParams,
235
209
  lucid: LucidEvolution,
236
210
  currentSlot: number,
237
- stakingManager: UtxoOrOutRef,
211
+ stakingManagerRef?: OutRef,
238
212
  ): Promise<TxBuilder> {
239
213
  const network = lucid.config().network!;
240
- const currentTime = slotToUnixTime(network, currentSlot) - ONE_SECOND;
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 stakingManagerUtxo = await resolveUtxo(
252
- stakingManager,
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 stakingToken =
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 = stakingPositionUtxo.assets[indyToken] ?? 0n;
283
- const currentSnapshotAda = stakingManagerDatum.managerSnapshot.snapshotAda;
284
- const oldSnapshotAda = stakingPositionDatum.positionSnapshot.snapshotAda;
285
- const adaReward =
286
- ((currentSnapshotAda - oldSnapshotAda) * existingIndyAmount) /
287
- (1000000n * 1000000n);
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(currentTime)
253
+ .validFrom(Number(now))
292
254
  .readFrom([stakingRefScriptUtxo, stakingTokenPolicyRefScriptUtxo])
293
- .collectFrom([stakingPositionUtxo], serialiseStakingRedeemer('Unstake'))
255
+ .collectFrom([stakingPositionOut.utxo], serialiseStakingRedeemer('Unstake'))
294
256
  .collectFrom(
295
- [stakingManagerUtxo],
257
+ [stakingManagerOut.utxo],
296
258
  serialiseStakingRedeemer('UpdateTotalStake'),
297
259
  )
298
260
  .pay.ToContract(
299
- stakingManagerUtxo.address,
261
+ stakingManagerOut.utxo.address,
300
262
  {
301
263
  kind: 'inline',
302
264
  value: serialiseStakingDatum({
303
- ...stakingManagerDatum,
304
- totalStake: stakingManagerDatum.totalStake - existingIndyAmount,
265
+ ...stakingManagerOut.datum,
266
+ totalStake: stakingManagerOut.datum.totalStake - existingIndyAmount,
305
267
  }),
306
268
  },
307
- addAssets(stakingManagerUtxo.assets, mkLovelacesOf(-adaReward)),
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(stakingPositionDatum.owner));
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
- stakingManager: UtxoOrOutRef,
281
+ stakingManagerRef: OutRef,
322
282
  collectorRefs: OutRef[],
323
283
  params: SystemParams,
324
284
  lucid: LucidEvolution,
325
285
  ): Promise<TxBuilder> {
326
- const stakingManagerUtxo = await resolveUtxo(
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.lovelace > MIN_UTXO_AMOUNT);
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.lovelace - MIN_UTXO_AMOUNT,
299
+ (acc, utxo) => acc + utxo.assets['lovelace'] - MIN_UTXO_AMOUNT,
345
300
  0n,
346
301
  );
347
302
  const newSnapshot = distributeReward(