@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
@@ -1,48 +1,84 @@
1
- import { Core as EvoCore } from '@evolution-sdk/evolution';
1
+ import { TSchema, Data } from '@evolution-sdk/evolution';
2
2
  import { option as O, function as F } from 'fp-ts';
3
3
  import { match, P } from 'ts-pattern';
4
4
  import { DEFAULT_SCHEMA_OPTIONS } from '../../types/evolution-schema-options';
5
5
 
6
- const StakingPosLockedAmtSchema = EvoCore.TSchema.Map(
7
- EvoCore.TSchema.Integer,
8
- EvoCore.TSchema.Struct({
9
- voteAmt: EvoCore.TSchema.Integer,
10
- votingEnd: EvoCore.TSchema.Integer,
11
- }),
6
+ const StakingPosLockedAmtSchema = TSchema.Array(
7
+ TSchema.Tuple([
8
+ TSchema.Integer,
9
+ TSchema.Struct({
10
+ voteAmt: TSchema.Integer,
11
+ votingEnd: TSchema.Integer,
12
+ }),
13
+ ]),
12
14
  );
13
15
 
14
16
  export type StakingPosLockedAmt = typeof StakingPosLockedAmtSchema.Type;
15
17
 
16
- const RewardSnapshotSchema = EvoCore.TSchema.Struct({
17
- snapshotAda: EvoCore.TSchema.Integer,
18
+ const RewardSnapshotSchema = TSchema.Struct({
19
+ snapshotAda: TSchema.Integer,
18
20
  });
19
21
 
20
- const StakingPositionSchema = EvoCore.TSchema.Struct({
21
- owner: EvoCore.TSchema.ByteArray,
22
+ const StakingPositionSchema = TSchema.Struct({
23
+ owner: TSchema.ByteArray,
22
24
  lockedAmount: StakingPosLockedAmtSchema,
23
25
  positionSnapshot: RewardSnapshotSchema,
24
26
  });
25
27
  export type StakingPosition = typeof StakingPositionSchema.Type;
26
28
 
27
- const StakingManagerSchema = EvoCore.TSchema.Struct({
28
- totalStake: EvoCore.TSchema.Integer,
29
+ const StakingManagerSchema = TSchema.Struct({
30
+ totalStake: TSchema.Integer,
29
31
  managerSnapshot: RewardSnapshotSchema,
30
32
  });
31
33
  export type StakingManager = typeof StakingManagerSchema.Type;
32
34
 
33
- const StakingDatumSchema = EvoCore.TSchema.Union(
35
+ const StakingDatumSchema = TSchema.Union(
34
36
  StakingManagerSchema,
35
37
  StakingPositionSchema,
36
38
  );
37
39
  type StakingDatum = typeof StakingDatumSchema.Type;
38
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
+
39
76
  export function parseStakingPosition(datum: string): O.Option<StakingPosition> {
40
77
  try {
41
78
  return match(
42
- EvoCore.Data.withSchema(
43
- StakingDatumSchema,
44
- DEFAULT_SCHEMA_OPTIONS,
45
- ).fromCBORHex(datum),
79
+ Data.withSchema(StakingDatumSchema, DEFAULT_SCHEMA_OPTIONS).fromCBORHex(
80
+ datum,
81
+ ),
46
82
  )
47
83
  .with({ owner: P.any }, (res) => O.some(res))
48
84
  .otherwise(() => O.none);
@@ -62,10 +98,9 @@ export function parseStakingPositionOrThrow(datum: string): StakingPosition {
62
98
 
63
99
  export function parseStakingManagerDatum(datum: string): StakingManager {
64
100
  return match(
65
- EvoCore.Data.withSchema(
66
- StakingDatumSchema,
67
- DEFAULT_SCHEMA_OPTIONS,
68
- ).fromCBORHex(datum),
101
+ Data.withSchema(StakingDatumSchema, DEFAULT_SCHEMA_OPTIONS).fromCBORHex(
102
+ datum,
103
+ ),
69
104
  )
70
105
  .with({ totalStake: P.any }, (res) => res)
71
106
  .otherwise(() => {
@@ -74,10 +109,7 @@ export function parseStakingManagerDatum(datum: string): StakingManager {
74
109
  }
75
110
 
76
111
  export function serialiseStakingDatum(d: StakingDatum): string {
77
- const datum = EvoCore.Data.withSchema(
78
- StakingDatumSchema,
79
- DEFAULT_SCHEMA_OPTIONS,
80
- ).toCBORHex(d);
81
-
82
- return datum;
112
+ return Data.withSchema(StakingDatumSchema, DEFAULT_SCHEMA_OPTIONS).toCBORHex(
113
+ d,
114
+ );
83
115
  }
@@ -1,4 +1,4 @@
1
- import { Data, Redeemer } from '@lucid-evolution/lucid';
1
+ import { Data } from '@lucid-evolution/lucid';
2
2
  import { AssetClassSchema } from '../../types/generic';
3
3
 
4
4
  const StakingParamsSchema = Data.Object({
@@ -12,33 +12,6 @@ const StakingParamsSchema = Data.Object({
12
12
  type StakingParams = Data.Static<typeof StakingParamsSchema>;
13
13
  const StakingParams = StakingParamsSchema as unknown as StakingParams;
14
14
 
15
- const StakingRedeemerSchema = Data.Enum([
16
- Data.Object({
17
- CreateStakingPosition: Data.Object({
18
- creatorPkh: Data.Bytes(),
19
- }),
20
- }),
21
- Data.Literal('UpdateTotalStake'),
22
- Data.Literal('Distribute'),
23
- Data.Object({
24
- AdjustStakedAmount: Data.Object({
25
- adjustAmount: Data.Integer(),
26
- }),
27
- }),
28
- Data.Literal('Unstake'),
29
- Data.Literal('Lock'),
30
- Data.Literal('UpgradeVersion'),
31
- ]);
32
- export type StakingRedeemer = Data.Static<typeof StakingRedeemerSchema>;
33
- const StakingRedeemer = StakingRedeemerSchema as unknown as StakingRedeemer;
34
-
35
- export function serialiseStakingRedeemer(redeemer: StakingRedeemer): Redeemer {
36
- const rdmr = Data.to<StakingRedeemer>(redeemer, StakingRedeemer, {
37
- canonical: false,
38
- });
39
- return rdmr;
40
- }
41
-
42
15
  export function castStakingParams(params: StakingParams): Data {
43
16
  return Data.castTo(params, StakingParams);
44
17
  }
@@ -0,0 +1,21 @@
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
+ }
@@ -1,37 +1,27 @@
1
+ import { applyParamsToScript, SpendingValidator } from '@lucid-evolution/lucid';
1
2
  import {
2
- applyParamsToScript,
3
- Constr,
4
- fromText,
5
- SpendingValidator,
6
- } from '@lucid-evolution/lucid';
7
- import { TreasuryParamsSP } from '../../types/system-params';
3
+ fromSysParamsStakeCredential,
4
+ fromSystemParamsAssetLucid,
5
+ TreasuryParamsSP,
6
+ } from '../../types/system-params';
8
7
  import { _treasuryValidator } from '../../validators/treasury-validator';
8
+ import { castTreasuryParams } from './types';
9
9
 
10
10
  export const mkTreasuryValidatorFromSP = (
11
11
  params: TreasuryParamsSP,
12
12
  ): SpendingValidator => {
13
13
  return {
14
- type: 'PlutusV2',
14
+ type: 'PlutusV3',
15
15
  script: applyParamsToScript(_treasuryValidator.cborHex, [
16
- new Constr(0, [
17
- new Constr(0, [
18
- params.upgradeToken[0].unCurrencySymbol,
19
- fromText(params.upgradeToken[1].unTokenName),
20
- ]),
21
- new Constr(0, [
22
- params.versionRecordToken[0].unCurrencySymbol,
23
- fromText(params.versionRecordToken[1].unTokenName),
24
- ]),
25
- params.treasuryUtxosStakeCredential
26
- ? new Constr(0, [
27
- new Constr(0, [
28
- new Constr(1, [
29
- params.treasuryUtxosStakeCredential.contents.contents,
30
- ]),
31
- ]),
32
- ])
33
- : new Constr(1, []),
34
- ]),
16
+ castTreasuryParams({
17
+ upgradeToken: fromSystemParamsAssetLucid(params.upgradeToken),
18
+ versionRecordToken: fromSystemParamsAssetLucid(
19
+ params.versionRecordToken,
20
+ ),
21
+ treasuryUtxosStakeCredential: params.treasuryUtxosStakeCredential
22
+ ? fromSysParamsStakeCredential(params.treasuryUtxosStakeCredential)
23
+ : null,
24
+ }),
35
25
  ]),
36
26
  };
37
27
  };
@@ -1,36 +1,56 @@
1
1
  import {
2
2
  addAssets,
3
+ Assets,
3
4
  Data,
5
+ fromHex,
4
6
  LucidEvolution,
7
+ OutRef,
5
8
  TxBuilder,
9
+ UTxO,
6
10
  } from '@lucid-evolution/lucid';
7
11
  import {
8
- fromSysParamsScriptCredential,
9
12
  fromSystemParamsScriptRef,
10
13
  SystemParams,
11
14
  } from '../../types/system-params';
12
15
  import { matchSingle } from '../../utils/utils';
13
- import { mkLovelacesOf } from '../../utils/value-helpers';
14
- import { serialiseTreasuryRedeemer } from './types';
15
16
  import {
16
- createScriptAddress,
17
- resolveUtxo,
18
- UtxoOrOutRef,
19
- } from '../../utils/lucid-utils';
17
+ adaAssetClass,
18
+ AssetClass,
19
+ mkAssetsOf,
20
+ mkLovelacesOf,
21
+ negateAssets,
22
+ } from '@3rd-eye-labs/cardano-offchain-common';
23
+ import { serialiseTreasuryDatum, serialiseTreasuryRedeemer } from './types-new';
24
+ import { array as A, function as F } from 'fp-ts';
25
+ import { parseExecuteDatumOrThrow } from '../execute/types-new';
26
+ import { getInlineDatumOrThrow } from '../../utils/lucid-utils';
27
+ import { createValueFromWithdrawal } from '../gov/helpers';
28
+ import { mkTreasuryAddr } from './helpers';
20
29
 
21
30
  export async function treasuryFeeTx(
22
- fee: bigint,
31
+ assetToPay: AssetClass,
32
+ amountToPay: bigint,
33
+ extraLovelaces: bigint,
23
34
  lucid: LucidEvolution,
24
35
  sysParams: SystemParams,
25
36
  tx: TxBuilder,
26
- treasury: UtxoOrOutRef,
27
- ): Promise<void> {
28
- if (fee <= 0n) return;
37
+ actionOref: OutRef,
38
+ treasuryOref: OutRef,
39
+ ): Promise<UTxO | null> {
40
+ const [asset, amt, extraLov] = ((): [AssetClass, bigint, bigint] => {
41
+ if (amountToPay <= 0 && extraLovelaces > 0) {
42
+ return [adaAssetClass, extraLovelaces, 0n];
43
+ }
29
44
 
30
- const treasuryUtxo = await resolveUtxo(
31
- treasury,
32
- lucid,
33
- 'Expected a single treasury UTXO',
45
+ return [assetToPay, amountToPay, extraLovelaces];
46
+ })();
47
+
48
+ // If no fee is to be collected, do not include treasury collection.
49
+ if (amt <= 0n) return null;
50
+
51
+ const treasuryUtxo = matchSingle(
52
+ await lucid.utxosByOutRef([treasuryOref]),
53
+ (_) => new Error('Expected a single treasury UTXO'),
34
54
  );
35
55
 
36
56
  const treasuryRefScriptUtxo = matchSingle(
@@ -42,21 +62,230 @@ export async function treasuryFeeTx(
42
62
  (_) => new Error('Expected a single treasury Ref Script UTXO'),
43
63
  );
44
64
 
45
- const stakeCredential = sysParams.treasuryParams.treasuryUtxosStakeCredential
46
- ? fromSysParamsScriptCredential(
47
- sysParams.treasuryParams.treasuryUtxosStakeCredential,
48
- )
49
- : undefined;
50
-
51
65
  tx.readFrom([treasuryRefScriptUtxo])
52
- .collectFrom([treasuryUtxo], serialiseTreasuryRedeemer('CollectAda'))
66
+ .collectFrom(
67
+ [treasuryUtxo],
68
+ serialiseTreasuryRedeemer({
69
+ Collect: {
70
+ assetToCollect: asset,
71
+ amountToCollect: amt,
72
+ extraLovelaces: extraLov,
73
+ actionInputOref: {
74
+ txHash: fromHex(actionOref.txHash),
75
+ outputIndex: BigInt(actionOref.outputIndex),
76
+ },
77
+ },
78
+ }),
79
+ )
80
+ .pay.ToContract(
81
+ mkTreasuryAddr(lucid, sysParams),
82
+ {
83
+ kind: 'inline',
84
+ value: serialiseTreasuryDatum({
85
+ treasuryInputOref: {
86
+ txHash: fromHex(treasuryOref.txHash),
87
+ outputIndex: BigInt(treasuryOref.outputIndex),
88
+ },
89
+ actionInputOref: {
90
+ txHash: fromHex(actionOref.txHash),
91
+ outputIndex: BigInt(actionOref.outputIndex),
92
+ },
93
+ }),
94
+ },
95
+ addAssets(
96
+ treasuryUtxo.assets,
97
+ mkAssetsOf(asset, amt),
98
+ mkLovelacesOf(extraLov),
99
+ ),
100
+ );
101
+
102
+ return treasuryRefScriptUtxo;
103
+ }
104
+
105
+ export async function treasuryCollect(
106
+ assetToPay: AssetClass,
107
+ amountToPay: bigint,
108
+ extraLovelaces: bigint,
109
+ lucid: LucidEvolution,
110
+ sysParams: SystemParams,
111
+ actionOref: OutRef,
112
+ treasuryOref: OutRef,
113
+ ): Promise<TxBuilder> {
114
+ const treasuryUtxo = matchSingle(
115
+ await lucid.utxosByOutRef([treasuryOref]),
116
+ (_) => new Error('Expected a single treasury UTXO'),
117
+ );
118
+
119
+ const treasuryRefScriptUtxo = matchSingle(
120
+ await lucid.utxosByOutRef([
121
+ fromSystemParamsScriptRef(
122
+ sysParams.scriptReferences.treasuryValidatorRef,
123
+ ),
124
+ ]),
125
+ (_) => new Error('Expected a single treasury Ref Script UTXO'),
126
+ );
127
+
128
+ return lucid
129
+ .newTx()
130
+ .readFrom([treasuryRefScriptUtxo])
131
+ .collectFrom(
132
+ [treasuryUtxo],
133
+ serialiseTreasuryRedeemer({
134
+ Collect: {
135
+ assetToCollect: assetToPay,
136
+ amountToCollect: amountToPay,
137
+ extraLovelaces: extraLovelaces,
138
+ actionInputOref: {
139
+ txHash: fromHex(actionOref.txHash),
140
+ outputIndex: BigInt(actionOref.outputIndex),
141
+ },
142
+ },
143
+ }),
144
+ )
53
145
  .pay.ToContract(
54
- createScriptAddress(
55
- lucid.config().network!,
56
- sysParams.validatorHashes.treasuryHash,
57
- stakeCredential,
146
+ mkTreasuryAddr(lucid, sysParams),
147
+ {
148
+ kind: 'inline',
149
+ value: serialiseTreasuryDatum({
150
+ treasuryInputOref: {
151
+ txHash: fromHex(treasuryOref.txHash),
152
+ outputIndex: BigInt(treasuryOref.outputIndex),
153
+ },
154
+ actionInputOref: {
155
+ txHash: fromHex(actionOref.txHash),
156
+ outputIndex: BigInt(actionOref.outputIndex),
157
+ },
158
+ }),
159
+ },
160
+ addAssets(
161
+ treasuryUtxo.assets,
162
+ mkAssetsOf(assetToPay, amountToPay),
163
+ mkLovelacesOf(extraLovelaces),
164
+ ),
165
+ );
166
+ }
167
+
168
+ export async function treasuryMerge(
169
+ treasuryOutRefs: OutRef[],
170
+ lucid: LucidEvolution,
171
+ sysParams: SystemParams,
172
+ ): Promise<TxBuilder> {
173
+ const treasuryScriptRefUtxo = matchSingle(
174
+ await lucid.utxosByOutRef([
175
+ fromSystemParamsScriptRef(
176
+ sysParams.scriptReferences.treasuryValidatorRef,
177
+ ),
178
+ ]),
179
+ (_) => new Error('Expected a single treasury Ref Script UTXO'),
180
+ );
181
+
182
+ const treasuryUtxos = await lucid.utxosByOutRef(treasuryOutRefs);
183
+
184
+ const totalAssets = F.pipe(
185
+ treasuryUtxos,
186
+ A.reduce<UTxO, Assets>({}, (acc, utxo) => addAssets(acc, utxo.assets)),
187
+ );
188
+
189
+ return lucid
190
+ .newTx()
191
+ .readFrom([treasuryScriptRefUtxo])
192
+ .collectFrom(treasuryUtxos, serialiseTreasuryRedeemer('Merge'))
193
+ .pay.ToContract(
194
+ mkTreasuryAddr(lucid, sysParams),
195
+ { kind: 'inline', value: Data.void() },
196
+ totalAssets,
197
+ );
198
+ }
199
+
200
+ export async function treasurySplit(
201
+ treasuryOutRef: OutRef,
202
+ lucid: LucidEvolution,
203
+ sysParams: SystemParams,
204
+ ): Promise<TxBuilder> {
205
+ const treasuryScriptRefUtxo = matchSingle(
206
+ await lucid.utxosByOutRef([
207
+ fromSystemParamsScriptRef(
208
+ sysParams.scriptReferences.treasuryValidatorRef,
209
+ ),
210
+ ]),
211
+ (_) => new Error('Expected a single treasury Ref Script UTXO'),
212
+ );
213
+
214
+ const treasuryUtxo = matchSingle(
215
+ await lucid.utxosByOutRef([treasuryOutRef]),
216
+ (_) => new Error('Expected a single treasury UTXO'),
217
+ );
218
+
219
+ const assets = Object.keys(treasuryUtxo.assets);
220
+
221
+ const tx = lucid
222
+ .newTx()
223
+ .collectFrom([treasuryUtxo], serialiseTreasuryRedeemer('Split'))
224
+ .readFrom([treasuryScriptRefUtxo]);
225
+
226
+ for (const asset of assets) {
227
+ tx.pay.ToContract(
228
+ mkTreasuryAddr(lucid, sysParams),
229
+ { kind: 'inline', value: Data.void() },
230
+ { [asset]: treasuryUtxo.assets[asset] },
231
+ );
232
+ }
233
+
234
+ return tx;
235
+ }
236
+
237
+ export async function treasuryPrepareWithdrawal(
238
+ treasuryOutRefs: OutRef[],
239
+ upgradeOutRef: OutRef,
240
+ lucid: LucidEvolution,
241
+ sysParams: SystemParams,
242
+ ): Promise<TxBuilder> {
243
+ const treasuryScriptRefUtxo = matchSingle(
244
+ await lucid.utxosByOutRef([
245
+ fromSystemParamsScriptRef(
246
+ sysParams.scriptReferences.treasuryValidatorRef,
58
247
  ),
248
+ ]),
249
+ (_) => new Error('Expected a single treasury Ref Script UTXO'),
250
+ );
251
+
252
+ const treasuryUtxos = await lucid.utxosByOutRef(treasuryOutRefs);
253
+
254
+ const upgradeUtxo = matchSingle(
255
+ await lucid.utxosByOutRef([upgradeOutRef]),
256
+ (_) => new Error('Expected a single upgrade UTXO'),
257
+ );
258
+
259
+ const treasuryAddress = mkTreasuryAddr(lucid, sysParams);
260
+
261
+ const totalAssets = F.pipe(
262
+ treasuryUtxos,
263
+ A.reduce<UTxO, Assets>({}, (acc, utxo) => addAssets(acc, utxo.assets)),
264
+ );
265
+ const executeDatum = parseExecuteDatumOrThrow(
266
+ getInlineDatumOrThrow(upgradeUtxo),
267
+ );
268
+ if (!executeDatum.treasuryWithdrawal)
269
+ throw new Error('Expected a treasury withdrawal in the execute datum');
270
+ const withdrawalVal = createValueFromWithdrawal(
271
+ executeDatum.treasuryWithdrawal,
272
+ );
273
+ const change = addAssets(totalAssets, negateAssets(withdrawalVal));
274
+
275
+ const tx = lucid
276
+ .newTx()
277
+ .collectFrom(treasuryUtxos, serialiseTreasuryRedeemer('PrepareWithdraw'))
278
+ .readFrom([treasuryScriptRefUtxo, upgradeUtxo])
279
+ .pay.ToContract(
280
+ treasuryAddress,
59
281
  { kind: 'inline', value: Data.void() },
60
- addAssets(treasuryUtxo.assets, mkLovelacesOf(fee)),
282
+ withdrawalVal,
283
+ )
284
+ .pay.ToContract(
285
+ treasuryAddress,
286
+ { kind: 'inline', value: Data.void() },
287
+ change,
61
288
  );
289
+
290
+ return tx;
62
291
  }
@@ -0,0 +1,69 @@
1
+ import { TSchema, Data } from '@evolution-sdk/evolution';
2
+ import {
3
+ AssetClassSchema,
4
+ OutputReferenceSchema,
5
+ } from '@3rd-eye-labs/cardano-offchain-common';
6
+ import { DEFAULT_SCHEMA_OPTIONS } from '../../types/evolution-schema-options';
7
+
8
+ const WithdrawalOutputDatumSchema = TSchema.Tuple([
9
+ TSchema.ByteArray,
10
+ OutputReferenceSchema,
11
+ ]);
12
+
13
+ export type WithdrawalOutputDatum = typeof WithdrawalOutputDatumSchema.Type;
14
+
15
+ const TreasuryDatumSchema = TSchema.Struct(
16
+ {
17
+ treasuryInputOref: OutputReferenceSchema,
18
+ actionInputOref: OutputReferenceSchema,
19
+ },
20
+ { flatFields: true },
21
+ );
22
+
23
+ export type TreasuryDatum = typeof TreasuryDatumSchema.Type;
24
+
25
+ const TreasuryRedeemerSchema = TSchema.Union(
26
+ TSchema.Literal('Withdraw', { flatInUnion: true }),
27
+ TSchema.Literal('PrepareWithdraw', { flatInUnion: true }),
28
+ TSchema.Literal('Split', { flatInUnion: true }),
29
+ TSchema.Literal('Merge', { flatInUnion: true }),
30
+ TSchema.Struct(
31
+ {
32
+ Collect: TSchema.Struct(
33
+ {
34
+ assetToCollect: AssetClassSchema,
35
+ amountToCollect: TSchema.Integer,
36
+ extraLovelaces: TSchema.Integer,
37
+ actionInputOref: OutputReferenceSchema,
38
+ },
39
+ { flatFields: true },
40
+ ),
41
+ },
42
+ { flatInUnion: true },
43
+ ),
44
+ TSchema.Literal('UpgradeVersion', { flatInUnion: true }),
45
+ );
46
+
47
+ export type TreasuryRedeemer = typeof TreasuryRedeemerSchema.Type;
48
+
49
+ export function serialiseTreasuryRedeemer(r: TreasuryRedeemer): string {
50
+ return Data.withSchema(
51
+ TreasuryRedeemerSchema,
52
+ DEFAULT_SCHEMA_OPTIONS,
53
+ ).toCBORHex(r);
54
+ }
55
+
56
+ export function serialiseWithdrawalOutputDatum(
57
+ d: WithdrawalOutputDatum,
58
+ ): string {
59
+ return Data.withSchema(
60
+ WithdrawalOutputDatumSchema,
61
+ DEFAULT_SCHEMA_OPTIONS,
62
+ ).toCBORHex(d);
63
+ }
64
+
65
+ export function serialiseTreasuryDatum(d: TreasuryDatum): string {
66
+ return Data.withSchema(TreasuryDatumSchema, DEFAULT_SCHEMA_OPTIONS).toCBORHex(
67
+ d,
68
+ );
69
+ }
@@ -1,9 +1,5 @@
1
- import { Data, Datum, Redeemer } from '@lucid-evolution/lucid';
2
- import {
3
- AssetClassSchema,
4
- OutputReferenceSchema,
5
- StakeCredentialSchema,
6
- } from '../../types/generic';
1
+ import { Data } from '@lucid-evolution/lucid';
2
+ import { AssetClassSchema, StakeCredentialSchema } from '../../types/generic';
7
3
 
8
4
  export const TreasuryParamsSchema = Data.Object({
9
5
  upgradeToken: AssetClassSchema,
@@ -13,43 +9,6 @@ export const TreasuryParamsSchema = Data.Object({
13
9
  export type TreasuryParams = Data.Static<typeof TreasuryParamsSchema>;
14
10
  export const TreasuryParams = TreasuryParamsSchema as unknown as TreasuryParams;
15
11
 
16
- const TreasuryRedeemerSchema = Data.Enum([
17
- Data.Literal('Withdraw'),
18
- Data.Literal('PrepareWithdraw'),
19
- Data.Literal('Split'),
20
- Data.Literal('Merge'),
21
- Data.Literal('CollectAda'),
22
- Data.Literal('UpgradeVersion'),
23
- ]);
24
- export type TreasuryRedeemer = Data.Static<typeof TreasuryRedeemerSchema>;
25
- const TreasuryRedeemer = TreasuryRedeemerSchema as unknown as TreasuryRedeemer;
26
-
27
- const WithdrawalOutputDatumSchema = Data.Tuple([
28
- Data.Bytes(),
29
- OutputReferenceSchema,
30
- ]);
31
- export type WithdrawalOutputDatum = Data.Static<
32
- typeof WithdrawalOutputDatumSchema
33
- >;
34
- const WithdrawalOutputDatum =
35
- WithdrawalOutputDatumSchema as unknown as WithdrawalOutputDatum;
36
-
37
- export function serialiseWithdrawalOutputDatum(
38
- d: WithdrawalOutputDatum,
39
- ): Datum {
40
- return Data.to<WithdrawalOutputDatum>(d, WithdrawalOutputDatum);
41
- }
42
-
43
- export function serialiseTreasuryRedeemer(
44
- redeemer: TreasuryRedeemer,
45
- ): Redeemer {
46
- return Data.to<TreasuryRedeemer>(redeemer, TreasuryRedeemer);
47
- }
48
-
49
- export function serialiseTreasuryDatum(): Datum {
50
- return Data.void();
51
- }
52
-
53
12
  export function castTreasuryParams(params: TreasuryParams): Data {
54
13
  return Data.castTo(params, TreasuryParams);
55
14
  }