@indigo-labs/indigo-sdk 0.5.4 → 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.
- package/dist/index.d.mts +11 -1
- package/dist/index.d.ts +11 -1
- package/dist/index.js +17 -0
- package/dist/index.mjs +15 -0
- package/package.json +11 -1
- package/.claude/settings.local.json +0 -7
- package/.github/workflows/ci.yml +0 -66
- package/.husky/pre-commit +0 -1
- package/.nvmrc +0 -1
- package/.prettierignore +0 -1
- package/.prettierrc +0 -6
- package/eslint.config.mjs +0 -42
- package/scripts/bench.sh +0 -62
- package/scripts/run-repeat.sh +0 -22
- package/src/contracts/cdp/helpers.ts +0 -173
- package/src/contracts/cdp/scripts.ts +0 -70
- package/src/contracts/cdp/transactions.ts +0 -1584
- package/src/contracts/cdp/types-new.ts +0 -277
- package/src/contracts/cdp/types.ts +0 -43
- package/src/contracts/cdp-creator/queries.ts +0 -30
- package/src/contracts/cdp-creator/scripts.ts +0 -45
- package/src/contracts/cdp-creator/types-new.ts +0 -50
- package/src/contracts/cdp-creator/types.ts +0 -24
- package/src/contracts/collector/scripts.ts +0 -32
- package/src/contracts/collector/transactions.ts +0 -50
- package/src/contracts/collector/types-new.ts +0 -17
- package/src/contracts/execute/scripts.ts +0 -57
- package/src/contracts/execute/types-new.ts +0 -44
- package/src/contracts/execute/types.ts +0 -24
- package/src/contracts/gov/helpers.ts +0 -351
- package/src/contracts/gov/scripts.ts +0 -41
- package/src/contracts/gov/transactions.ts +0 -1607
- package/src/contracts/gov/types-new.ts +0 -268
- package/src/contracts/gov/types.ts +0 -22
- package/src/contracts/iasset/helpers.ts +0 -170
- package/src/contracts/iasset/queries.ts +0 -102
- package/src/contracts/iasset/scripts.ts +0 -38
- package/src/contracts/iasset/types.ts +0 -154
- package/src/contracts/initialize/actions.ts +0 -789
- package/src/contracts/initialize/helpers.ts +0 -616
- package/src/contracts/initialize/types.ts +0 -116
- package/src/contracts/interest-collection/helpers.ts +0 -19
- package/src/contracts/interest-collection/queries.ts +0 -66
- package/src/contracts/interest-collection/scripts.ts +0 -44
- package/src/contracts/interest-collection/transactions.ts +0 -433
- package/src/contracts/interest-collection/types-new.ts +0 -50
- package/src/contracts/interest-collection/types.ts +0 -26
- package/src/contracts/interest-oracle/helpers.ts +0 -61
- package/src/contracts/interest-oracle/scripts.ts +0 -18
- package/src/contracts/interest-oracle/transactions.ts +0 -151
- package/src/contracts/interest-oracle/types-new.ts +0 -32
- package/src/contracts/interest-oracle/types.ts +0 -16
- package/src/contracts/one-shot/scripts.ts +0 -12
- package/src/contracts/one-shot/transactions.ts +0 -66
- package/src/contracts/one-shot/types.ts +0 -22
- package/src/contracts/poll/helpers.ts +0 -37
- package/src/contracts/poll/scripts.ts +0 -71
- package/src/contracts/poll/types-poll-manager.ts +0 -20
- package/src/contracts/poll/types-poll-new.ts +0 -196
- package/src/contracts/poll/types-poll-shard.ts +0 -16
- package/src/contracts/price-oracle/helpers.ts +0 -30
- package/src/contracts/price-oracle/scripts.ts +0 -18
- package/src/contracts/price-oracle/transactions.ts +0 -115
- package/src/contracts/price-oracle/types-new.ts +0 -50
- package/src/contracts/price-oracle/types.ts +0 -16
- package/src/contracts/pyth-feed/helpers.ts +0 -82
- package/src/contracts/pyth-feed/scripts.ts +0 -15
- package/src/contracts/pyth-feed/types.ts +0 -181
- package/src/contracts/rob/helpers.ts +0 -666
- package/src/contracts/rob/scripts.ts +0 -35
- package/src/contracts/rob/transactions.ts +0 -423
- package/src/contracts/rob/types-new.ts +0 -128
- package/src/contracts/rob/types.ts +0 -16
- package/src/contracts/rob-leverage/helpers.ts +0 -422
- package/src/contracts/rob-leverage/transactions.ts +0 -368
- package/src/contracts/stability-pool/helpers.ts +0 -798
- package/src/contracts/stability-pool/queries.ts +0 -132
- package/src/contracts/stability-pool/scripts.ts +0 -51
- package/src/contracts/stability-pool/transactions.ts +0 -921
- package/src/contracts/stability-pool/types-new.ts +0 -385
- package/src/contracts/stability-pool/types.ts +0 -25
- package/src/contracts/stableswap/helpers.ts +0 -507
- package/src/contracts/stableswap/scripts.ts +0 -37
- package/src/contracts/stableswap/transactions.ts +0 -475
- package/src/contracts/stableswap/types-new.ts +0 -131
- package/src/contracts/stableswap/types.ts +0 -17
- package/src/contracts/staking/helpers.ts +0 -136
- package/src/contracts/staking/scripts.ts +0 -41
- package/src/contracts/staking/transactions.ts +0 -349
- package/src/contracts/staking/types-new.ts +0 -137
- package/src/contracts/staking/types.ts +0 -17
- package/src/contracts/treasury/helpers.ts +0 -21
- package/src/contracts/treasury/queries.ts +0 -100
- package/src/contracts/treasury/scripts.ts +0 -27
- package/src/contracts/treasury/transactions.ts +0 -334
- package/src/contracts/treasury/types-new.ts +0 -69
- package/src/contracts/treasury/types.ts +0 -14
- package/src/contracts/version-registry/scripts.ts +0 -29
- package/src/contracts/version-registry/types-new.ts +0 -18
- package/src/contracts/version-registry/types.ts +0 -17
- package/src/index.ts +0 -93
- package/src/scripts/always-fail-validator.ts +0 -7
- package/src/scripts/auth-token-policy.ts +0 -24
- package/src/scripts/iasset-policy.ts +0 -23
- package/src/types/evolution-schema-options.ts +0 -16
- package/src/types/generic.ts +0 -52
- package/src/types/multisig.ts +0 -48
- package/src/types/on-chain-decimal.ts +0 -57
- package/src/types/rational.ts +0 -61
- package/src/types/system-params.ts +0 -463
- package/src/utils/array-utils.ts +0 -108
- package/src/utils/bigint-utils.ts +0 -35
- package/src/utils/helper-txs.ts +0 -31
- package/src/utils/indigo-helpers.ts +0 -17
- package/src/utils/lucid-utils.ts +0 -125
- package/src/utils/oracle-helpers.ts +0 -62
- package/src/utils/pyth/decode.ts +0 -223
- package/src/utils/pyth/encode.ts +0 -262
- package/src/utils/pyth/index.ts +0 -14
- package/src/utils/pyth/types.ts +0 -87
- package/src/utils/time-helpers.ts +0 -4
- package/src/utils/utils.ts +0 -25
- package/src/validators/always-succeed-validator.ts +0 -6
- package/src/validators/cdp-creator-validator.ts +0 -7
- package/src/validators/cdp-redeem-validator.ts +0 -7
- package/src/validators/cdp-validator.ts +0 -7
- package/src/validators/collector-validator.ts +0 -7
- package/src/validators/execute-validator.ts +0 -7
- package/src/validators/governance-validator.ts +0 -7
- package/src/validators/iasset-validator.ts +0 -7
- package/src/validators/interest-collection-validator.ts +0 -7
- package/src/validators/interest-oracle-validator.ts +0 -7
- package/src/validators/one-shot-policy.ts +0 -7
- package/src/validators/poll-manager-validator.ts +0 -7
- package/src/validators/poll-shard-validator.ts +0 -7
- package/src/validators/price-oracle-validator.ts +0 -7
- package/src/validators/pyth-feed-validator.ts +0 -7
- package/src/validators/rob-validator.ts +0 -7
- package/src/validators/stability-pool-validator.ts +0 -7
- package/src/validators/stableswap-validator.ts +0 -7
- package/src/validators/staking-validator.ts +0 -7
- package/src/validators/treasury-validator.ts +0 -7
- package/src/validators/version-record-policy.ts +0 -7
- package/src/validators/version-registry-validator.ts +0 -7
- package/tests/always-succeed/script.ts +0 -7
- package/tests/array-utils.test.ts +0 -92
- package/tests/bigint-utils.test.ts +0 -41
- package/tests/cdp/actions.ts +0 -680
- package/tests/cdp/cdp-helpers.ts +0 -55
- package/tests/cdp/cdp-queries.ts +0 -423
- package/tests/cdp/cdp.test.ts +0 -6730
- package/tests/cdp/transactions-mutated.ts +0 -1733
- package/tests/data/system-params.json +0 -1155
- package/tests/datums.test.ts +0 -302
- package/tests/endpoints/initialize.ts +0 -78
- package/tests/endpoints/interest-collector.ts +0 -37
- package/tests/endpoints/treasury.ts +0 -70
- package/tests/gov/actions.ts +0 -381
- package/tests/gov/gov.test.ts +0 -5147
- package/tests/gov/governance-queries.ts +0 -37
- package/tests/hash-checks.test.ts +0 -112
- package/tests/indigo-test-helpers.ts +0 -99
- package/tests/initialize.test.ts +0 -79
- package/tests/interest-calculations.test.ts +0 -120
- package/tests/interest-collection/interest-collection.test.ts +0 -843
- package/tests/interest-collection/transactions-mutated.ts +0 -260
- package/tests/interest-oracle.test.ts +0 -93
- package/tests/mock/assets-mock.ts +0 -243
- package/tests/mock/protocol-params-mock.ts +0 -21
- package/tests/price-oracle/actions.ts +0 -161
- package/tests/price-oracle/price-oracle-queries.ts +0 -12
- package/tests/price-oracle/price-oracle.test.ts +0 -228
- package/tests/price-oracle/transactions-mutated.ts +0 -59
- package/tests/pyth/endpoints.ts +0 -96
- package/tests/pyth/helpers.ts +0 -37
- package/tests/pyth/pyth-encoding.test.ts +0 -376
- package/tests/pyth/pyth-feeds.test.ts +0 -943
- package/tests/pyth/pyth-indigo.test.ts +0 -505
- package/tests/pyth/pyth.test.ts +0 -299
- package/tests/queries/collector-queries.ts +0 -27
- package/tests/queries/execute-queries.ts +0 -47
- package/tests/queries/iasset-queries.ts +0 -149
- package/tests/queries/interest-oracle-queries.ts +0 -12
- package/tests/queries/poll-queries.ts +0 -97
- package/tests/queries/staking-queries.ts +0 -54
- package/tests/rob/actions.ts +0 -56
- package/tests/rob/rob-leverage.test.ts +0 -2555
- package/tests/rob/rob-queries.ts +0 -95
- package/tests/rob/rob.test.ts +0 -3677
- package/tests/rob/transactions-mutated.ts +0 -844
- package/tests/script-size.test.ts +0 -240
- package/tests/setup.ts +0 -135
- package/tests/stability-pool/actions.ts +0 -229
- package/tests/stability-pool.test.ts +0 -6491
- package/tests/stableswap/stableswap-actions.ts +0 -83
- package/tests/stableswap/stableswap-queries.ts +0 -98
- package/tests/stableswap/stableswap.test.ts +0 -4471
- package/tests/stableswap/transactions-mutated.ts +0 -348
- package/tests/staking.test.ts +0 -172
- package/tests/test-helpers.ts +0 -94
- package/tests/treasury/actions.ts +0 -67
- package/tests/treasury/treasury.test.ts +0 -320
- package/tests/utils/asserts.ts +0 -87
- package/tests/utils/benchmark-utils.ts +0 -130
- package/tests/utils/index.ts +0 -168
- package/tsconfig.build.json +0 -4
- package/tsconfig.json +0 -27
- package/vitest.config.ts +0 -11
|
@@ -1,1607 +0,0 @@
|
|
|
1
|
-
// import { OutRef } from '@lucid-evolution/lucid';
|
|
2
|
-
import {
|
|
3
|
-
addAssets,
|
|
4
|
-
Assets,
|
|
5
|
-
fromHex,
|
|
6
|
-
fromText,
|
|
7
|
-
LucidEvolution,
|
|
8
|
-
OutRef,
|
|
9
|
-
paymentCredentialOf,
|
|
10
|
-
toHex,
|
|
11
|
-
TxBuilder,
|
|
12
|
-
UTxO,
|
|
13
|
-
} from '@lucid-evolution/lucid';
|
|
14
|
-
|
|
15
|
-
import { matchSingle } from '../../utils/utils';
|
|
16
|
-
import {
|
|
17
|
-
createScriptAddress,
|
|
18
|
-
getInlineDatumOrThrow,
|
|
19
|
-
} from '../../utils/lucid-utils';
|
|
20
|
-
|
|
21
|
-
import {
|
|
22
|
-
adaAssetClass,
|
|
23
|
-
assetClassValueOf,
|
|
24
|
-
isAssetsZero,
|
|
25
|
-
isSameAssetClass,
|
|
26
|
-
lovelacesAmt,
|
|
27
|
-
mkAssetsOf,
|
|
28
|
-
mkLovelacesOf,
|
|
29
|
-
negateAssets,
|
|
30
|
-
} from '@3rd-eye-labs/cardano-offchain-common';
|
|
31
|
-
import { Data } from '@lucid-evolution/lucid';
|
|
32
|
-
import { pipe } from 'fp-ts/lib/function';
|
|
33
|
-
import { array as A, option as O, function as F } from 'fp-ts';
|
|
34
|
-
import { match, P } from 'ts-pattern';
|
|
35
|
-
import {
|
|
36
|
-
fromSysParamsCredential,
|
|
37
|
-
fromSystemParamsAsset,
|
|
38
|
-
fromSystemParamsScriptRef,
|
|
39
|
-
SystemParams,
|
|
40
|
-
} from '../../types/system-params';
|
|
41
|
-
import { ONE_SECOND } from '../../utils/time-helpers';
|
|
42
|
-
import {
|
|
43
|
-
parseStakingPositionOrThrow,
|
|
44
|
-
serialiseStakingDatum,
|
|
45
|
-
serialiseStakingRedeemer,
|
|
46
|
-
StakingPosLockedAmt,
|
|
47
|
-
} from '../staking/types-new';
|
|
48
|
-
import { updateStakingLockedAmount } from '../staking/helpers';
|
|
49
|
-
import { pollPassQuorum } from '../poll/helpers';
|
|
50
|
-
import {
|
|
51
|
-
collateralAssetCreationDatumHelper,
|
|
52
|
-
createValueFromWithdrawal,
|
|
53
|
-
findFirstCollateralAsset,
|
|
54
|
-
findRelativeCollateralAssetForInsertion,
|
|
55
|
-
findRelativeIAssetForInsertion,
|
|
56
|
-
iassetCreationDatumHelper,
|
|
57
|
-
proposalDeposit,
|
|
58
|
-
} from './helpers';
|
|
59
|
-
import { initSpState } from '../stability-pool/helpers';
|
|
60
|
-
import { serialiseVersionRecordDatum } from '../version-registry/types-new';
|
|
61
|
-
import {
|
|
62
|
-
parseGovDatumOrThrow,
|
|
63
|
-
ProposalContent,
|
|
64
|
-
serialiseGovDatum,
|
|
65
|
-
serialiseGovRedeemer,
|
|
66
|
-
TreasuryWithdrawal,
|
|
67
|
-
} from './types-new';
|
|
68
|
-
import { serialiseStabilityPoolDatum } from '../stability-pool/types-new';
|
|
69
|
-
import {
|
|
70
|
-
parsePollManagerOrThrow,
|
|
71
|
-
parsePollShardOrThrow,
|
|
72
|
-
PollShardContent,
|
|
73
|
-
PollStatus,
|
|
74
|
-
serialisePollDatum,
|
|
75
|
-
serialisePollManagerRedeemer,
|
|
76
|
-
serialisePollShardRedeemer,
|
|
77
|
-
VoteOption,
|
|
78
|
-
} from '../poll/types-poll-new';
|
|
79
|
-
import {
|
|
80
|
-
parseExecuteDatumOrThrow,
|
|
81
|
-
serialiseExecuteDatum,
|
|
82
|
-
} from '../execute/types-new';
|
|
83
|
-
import {
|
|
84
|
-
addressFromBech32,
|
|
85
|
-
addressToBech32,
|
|
86
|
-
} from '@3rd-eye-labs/cardano-offchain-common';
|
|
87
|
-
import {
|
|
88
|
-
serialiseTreasuryRedeemer,
|
|
89
|
-
serialiseWithdrawalOutputDatum,
|
|
90
|
-
} from '../treasury/types-new';
|
|
91
|
-
import {
|
|
92
|
-
parseCollateralAssetDatumOrThrow,
|
|
93
|
-
parseIAssetDatumOrThrow,
|
|
94
|
-
serialiseIAssetDatum,
|
|
95
|
-
serialiseIAssetRedeemer,
|
|
96
|
-
} from '../iasset/types';
|
|
97
|
-
import { serialiseCDPCreatorRedeemer } from '../cdp-creator/types-new';
|
|
98
|
-
import {
|
|
99
|
-
serialiseCdpRedeemer,
|
|
100
|
-
serialiseStableswapPoolDatum,
|
|
101
|
-
} from '../cdp/types-new';
|
|
102
|
-
|
|
103
|
-
/**
|
|
104
|
-
* Returns the new PollId.
|
|
105
|
-
*/
|
|
106
|
-
export async function createProposal(
|
|
107
|
-
proposalContent: ProposalContent,
|
|
108
|
-
treasuryWithdrawal: TreasuryWithdrawal | null,
|
|
109
|
-
sysParams: SystemParams,
|
|
110
|
-
lucid: LucidEvolution,
|
|
111
|
-
govOref: OutRef,
|
|
112
|
-
/**
|
|
113
|
-
* This has to be passed only in case of createAsset proposal
|
|
114
|
-
*/
|
|
115
|
-
allIAssetOrefs: OutRef[] | undefined,
|
|
116
|
-
): Promise<[TxBuilder, bigint]> {
|
|
117
|
-
const network = lucid.config().network!;
|
|
118
|
-
const currentTime = BigInt(lucid.slotToUnixTime(lucid.currentSlot()));
|
|
119
|
-
|
|
120
|
-
const ownAddr = await lucid.wallet().address();
|
|
121
|
-
const pkh = paymentCredentialOf(ownAddr);
|
|
122
|
-
|
|
123
|
-
const govRefScriptUtxo = matchSingle(
|
|
124
|
-
await lucid.utxosByOutRef([
|
|
125
|
-
fromSystemParamsScriptRef(
|
|
126
|
-
sysParams.scriptReferences.governanceValidatorRef,
|
|
127
|
-
),
|
|
128
|
-
]),
|
|
129
|
-
(_) => new Error('Expected a single Gov Ref Script UTXO'),
|
|
130
|
-
);
|
|
131
|
-
const pollAuthTokenPolicyRefScriptUtxo = matchSingle(
|
|
132
|
-
await lucid.utxosByOutRef([
|
|
133
|
-
fromSystemParamsScriptRef(
|
|
134
|
-
sysParams.scriptReferences.authTokenPolicies.pollManagerTokenRef,
|
|
135
|
-
),
|
|
136
|
-
]),
|
|
137
|
-
(_) =>
|
|
138
|
-
new Error('Expected a single poll auth token policy ref Script UTXO'),
|
|
139
|
-
);
|
|
140
|
-
const govUtxo = matchSingle(
|
|
141
|
-
await lucid.utxosByOutRef([govOref]),
|
|
142
|
-
(_) => new Error('Expected a single Gov UTXO'),
|
|
143
|
-
);
|
|
144
|
-
|
|
145
|
-
const govDatum = parseGovDatumOrThrow(getInlineDatumOrThrow(govUtxo));
|
|
146
|
-
|
|
147
|
-
const votingEndTime = currentTime + govDatum.protocolParams.votingPeriod;
|
|
148
|
-
const expirationTime =
|
|
149
|
-
votingEndTime + govDatum.protocolParams.expirationPeriod;
|
|
150
|
-
const proposingEndTime =
|
|
151
|
-
currentTime + govDatum.protocolParams.proposingPeriod;
|
|
152
|
-
|
|
153
|
-
const pollNftValue = mkAssetsOf(
|
|
154
|
-
fromSystemParamsAsset(sysParams.govParams.pollToken),
|
|
155
|
-
1n,
|
|
156
|
-
);
|
|
157
|
-
|
|
158
|
-
const newPollId = govDatum.currentProposal + 1n;
|
|
159
|
-
|
|
160
|
-
const tx = lucid.newTx();
|
|
161
|
-
|
|
162
|
-
// Add iAsset ref input when Propose asset proposal
|
|
163
|
-
await match(proposalContent)
|
|
164
|
-
.with({ ProposeIAsset: { asset: P.select() } }, async (newAsset) => {
|
|
165
|
-
if (allIAssetOrefs === undefined) {
|
|
166
|
-
throw new Error('Missing iAsset orefs');
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
const relativeIAsset = await findRelativeIAssetForInsertion(
|
|
170
|
-
newAsset,
|
|
171
|
-
allIAssetOrefs,
|
|
172
|
-
lucid,
|
|
173
|
-
);
|
|
174
|
-
|
|
175
|
-
pipe(
|
|
176
|
-
relativeIAsset,
|
|
177
|
-
O.match(
|
|
178
|
-
() => {
|
|
179
|
-
if (govDatum.iassetsCount !== 0n) {
|
|
180
|
-
throw new Error(
|
|
181
|
-
'Has to find relative iAsset when there are iAssets.',
|
|
182
|
-
);
|
|
183
|
-
}
|
|
184
|
-
},
|
|
185
|
-
(relative) => {
|
|
186
|
-
tx.readFrom([relative.utxo]);
|
|
187
|
-
},
|
|
188
|
-
),
|
|
189
|
-
);
|
|
190
|
-
})
|
|
191
|
-
.otherwise(() => {});
|
|
192
|
-
|
|
193
|
-
return [
|
|
194
|
-
tx
|
|
195
|
-
.mintAssets(pollNftValue, Data.void())
|
|
196
|
-
// Ref scripts
|
|
197
|
-
.readFrom([govRefScriptUtxo, pollAuthTokenPolicyRefScriptUtxo])
|
|
198
|
-
.collectFrom(
|
|
199
|
-
[govUtxo],
|
|
200
|
-
serialiseGovRedeemer({
|
|
201
|
-
CreatePoll: {
|
|
202
|
-
content: proposalContent,
|
|
203
|
-
currentTime: currentTime,
|
|
204
|
-
proposalOwner: fromHex(pkh.hash),
|
|
205
|
-
treasuryWithdrawal: treasuryWithdrawal,
|
|
206
|
-
},
|
|
207
|
-
}),
|
|
208
|
-
)
|
|
209
|
-
.pay.ToContract(
|
|
210
|
-
govUtxo.address,
|
|
211
|
-
{
|
|
212
|
-
kind: 'inline',
|
|
213
|
-
value: serialiseGovDatum({
|
|
214
|
-
...govDatum,
|
|
215
|
-
currentProposal: govDatum.currentProposal + 1n,
|
|
216
|
-
activeProposals: govDatum.activeProposals + 1n,
|
|
217
|
-
}),
|
|
218
|
-
},
|
|
219
|
-
govUtxo.assets,
|
|
220
|
-
)
|
|
221
|
-
.pay.ToContract(
|
|
222
|
-
createScriptAddress(network, sysParams.validatorHashes.pollManagerHash),
|
|
223
|
-
{
|
|
224
|
-
kind: 'inline',
|
|
225
|
-
value: serialisePollDatum({
|
|
226
|
-
PollManager: {
|
|
227
|
-
pollId: newPollId,
|
|
228
|
-
pollOwner: fromHex(pkh.hash),
|
|
229
|
-
content: proposalContent,
|
|
230
|
-
treasuryWithdrawal: treasuryWithdrawal,
|
|
231
|
-
status: { yesVotes: 0n, noVotes: 0n },
|
|
232
|
-
votingEndTime: votingEndTime,
|
|
233
|
-
createdShardsCount: 0n,
|
|
234
|
-
talliedShardsCount: 0n,
|
|
235
|
-
totalShardsCount: govDatum.protocolParams.totalShards,
|
|
236
|
-
proposingEndTime: proposingEndTime,
|
|
237
|
-
expirationTime: expirationTime,
|
|
238
|
-
protocolVersion: govDatum.currentVersion,
|
|
239
|
-
minimumQuorum: govDatum.protocolParams.minimumQuorum,
|
|
240
|
-
},
|
|
241
|
-
}),
|
|
242
|
-
},
|
|
243
|
-
addAssets(
|
|
244
|
-
pollNftValue,
|
|
245
|
-
mkAssetsOf(
|
|
246
|
-
fromSystemParamsAsset(sysParams.govParams.indyAsset),
|
|
247
|
-
proposalDeposit(
|
|
248
|
-
govDatum.protocolParams.proposalDeposit,
|
|
249
|
-
govDatum.activeProposals,
|
|
250
|
-
),
|
|
251
|
-
),
|
|
252
|
-
),
|
|
253
|
-
)
|
|
254
|
-
.validFrom(Number(currentTime) - ONE_SECOND)
|
|
255
|
-
.validTo(
|
|
256
|
-
Number(currentTime) +
|
|
257
|
-
Number(sysParams.govParams.gBiasTime) -
|
|
258
|
-
ONE_SECOND,
|
|
259
|
-
)
|
|
260
|
-
.addSigner(ownAddr),
|
|
261
|
-
newPollId,
|
|
262
|
-
];
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
/**
|
|
266
|
-
* Builds transaction creating shards of count chunk size.
|
|
267
|
-
*/
|
|
268
|
-
export async function createShardsChunks(
|
|
269
|
-
/**
|
|
270
|
-
* This gets automatically capped to total shards count.
|
|
271
|
-
*/
|
|
272
|
-
chunkSize: bigint,
|
|
273
|
-
pollManagerOref: OutRef,
|
|
274
|
-
sysParams: SystemParams,
|
|
275
|
-
lucid: LucidEvolution,
|
|
276
|
-
): Promise<TxBuilder> {
|
|
277
|
-
const network = lucid.config().network!;
|
|
278
|
-
const currentTime = BigInt(lucid.slotToUnixTime(lucid.currentSlot()));
|
|
279
|
-
|
|
280
|
-
const ownAddr = await lucid.wallet().address();
|
|
281
|
-
|
|
282
|
-
const pollManagerUtxo = matchSingle(
|
|
283
|
-
await lucid.utxosByOutRef([pollManagerOref]),
|
|
284
|
-
(_) => new Error('Expected a single Poll manager UTXO'),
|
|
285
|
-
);
|
|
286
|
-
|
|
287
|
-
const pollManager = parsePollManagerOrThrow(
|
|
288
|
-
getInlineDatumOrThrow(pollManagerUtxo),
|
|
289
|
-
);
|
|
290
|
-
|
|
291
|
-
if (pollManager.createdShardsCount >= pollManager.totalShardsCount) {
|
|
292
|
-
throw new Error('All shards already created.');
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
const pollAuthTokenPolicyRefScriptUtxo = matchSingle(
|
|
296
|
-
await lucid.utxosByOutRef([
|
|
297
|
-
fromSystemParamsScriptRef(
|
|
298
|
-
sysParams.scriptReferences.authTokenPolicies.pollManagerTokenRef,
|
|
299
|
-
),
|
|
300
|
-
]),
|
|
301
|
-
(_) =>
|
|
302
|
-
new Error('Expected a single poll auth token policy ref Script UTXO'),
|
|
303
|
-
);
|
|
304
|
-
const pollManagerRefScriptUtxo = matchSingle(
|
|
305
|
-
await lucid.utxosByOutRef([
|
|
306
|
-
fromSystemParamsScriptRef(
|
|
307
|
-
sysParams.scriptReferences.pollManagerValidatorRef,
|
|
308
|
-
),
|
|
309
|
-
]),
|
|
310
|
-
(_) =>
|
|
311
|
-
new Error('Expected a single poll auth token policy ref Script UTXO'),
|
|
312
|
-
);
|
|
313
|
-
|
|
314
|
-
const shardsCount = BigInt(
|
|
315
|
-
Math.min(
|
|
316
|
-
Number(chunkSize),
|
|
317
|
-
Number(pollManager.totalShardsCount - pollManager.createdShardsCount),
|
|
318
|
-
),
|
|
319
|
-
);
|
|
320
|
-
|
|
321
|
-
const pollNft = fromSystemParamsAsset(sysParams.govParams.pollToken);
|
|
322
|
-
|
|
323
|
-
const tx = lucid
|
|
324
|
-
.newTx()
|
|
325
|
-
.validFrom(Number(currentTime) - ONE_SECOND)
|
|
326
|
-
.validTo(
|
|
327
|
-
Number(currentTime) +
|
|
328
|
-
Number(sysParams.pollManagerParams.pBiasTime) -
|
|
329
|
-
ONE_SECOND,
|
|
330
|
-
)
|
|
331
|
-
.mintAssets(mkAssetsOf(pollNft, shardsCount), Data.void())
|
|
332
|
-
// Ref scripts
|
|
333
|
-
.readFrom([pollAuthTokenPolicyRefScriptUtxo, pollManagerRefScriptUtxo])
|
|
334
|
-
.collectFrom(
|
|
335
|
-
[pollManagerUtxo],
|
|
336
|
-
serialisePollManagerRedeemer({ CreateShards: { currentTime } }),
|
|
337
|
-
)
|
|
338
|
-
.pay.ToContract(
|
|
339
|
-
pollManagerUtxo.address,
|
|
340
|
-
{
|
|
341
|
-
kind: 'inline',
|
|
342
|
-
value: serialisePollDatum({
|
|
343
|
-
PollManager: {
|
|
344
|
-
...pollManager,
|
|
345
|
-
createdShardsCount: pollManager.createdShardsCount + shardsCount,
|
|
346
|
-
},
|
|
347
|
-
}),
|
|
348
|
-
},
|
|
349
|
-
pollManagerUtxo.assets,
|
|
350
|
-
)
|
|
351
|
-
.addSigner(ownAddr);
|
|
352
|
-
|
|
353
|
-
for (let idx = 0; idx < shardsCount; idx++) {
|
|
354
|
-
tx.pay.ToContract(
|
|
355
|
-
createScriptAddress(network, sysParams.validatorHashes.pollShardHash),
|
|
356
|
-
{
|
|
357
|
-
kind: 'inline',
|
|
358
|
-
value: serialisePollDatum({
|
|
359
|
-
PollShard: {
|
|
360
|
-
pollId: pollManager.pollId,
|
|
361
|
-
status: { yesVotes: 0n, noVotes: 0n },
|
|
362
|
-
votingEndTime: pollManager.votingEndTime,
|
|
363
|
-
managerAddress: addressFromBech32(pollManagerUtxo.address),
|
|
364
|
-
},
|
|
365
|
-
}),
|
|
366
|
-
},
|
|
367
|
-
mkAssetsOf(pollNft, 1n),
|
|
368
|
-
);
|
|
369
|
-
}
|
|
370
|
-
|
|
371
|
-
return tx;
|
|
372
|
-
}
|
|
373
|
-
|
|
374
|
-
/**
|
|
375
|
-
* Updates both locked amount and poll status based on the vote.
|
|
376
|
-
*/
|
|
377
|
-
function voteHelper(
|
|
378
|
-
stakingPosLockedAmt: StakingPosLockedAmt,
|
|
379
|
-
pollShard: PollShardContent,
|
|
380
|
-
voteOption: VoteOption,
|
|
381
|
-
currentTime: bigint,
|
|
382
|
-
indyStakedAmt: bigint,
|
|
383
|
-
): [StakingPosLockedAmt, PollStatus] {
|
|
384
|
-
const newPollStatus = match(voteOption)
|
|
385
|
-
.returnType<PollStatus>()
|
|
386
|
-
.with('Yes', () => ({
|
|
387
|
-
...pollShard.status,
|
|
388
|
-
yesVotes: pollShard.status.yesVotes + indyStakedAmt,
|
|
389
|
-
}))
|
|
390
|
-
.with('No', () => ({
|
|
391
|
-
...pollShard.status,
|
|
392
|
-
noVotes: pollShard.status.noVotes + indyStakedAmt,
|
|
393
|
-
}))
|
|
394
|
-
.exhaustive();
|
|
395
|
-
|
|
396
|
-
const newLockedAmt: StakingPosLockedAmt = [
|
|
397
|
-
...updateStakingLockedAmount(stakingPosLockedAmt, currentTime),
|
|
398
|
-
[
|
|
399
|
-
BigInt(pollShard.pollId),
|
|
400
|
-
{ voteAmt: indyStakedAmt, votingEnd: pollShard.votingEndTime },
|
|
401
|
-
],
|
|
402
|
-
];
|
|
403
|
-
|
|
404
|
-
return [newLockedAmt, newPollStatus];
|
|
405
|
-
}
|
|
406
|
-
|
|
407
|
-
export async function vote(
|
|
408
|
-
voteOption: VoteOption,
|
|
409
|
-
pollShardOref: OutRef,
|
|
410
|
-
stakingPositionOref: OutRef,
|
|
411
|
-
sysParams: SystemParams,
|
|
412
|
-
lucid: LucidEvolution,
|
|
413
|
-
): Promise<TxBuilder> {
|
|
414
|
-
const currentTime = BigInt(lucid.slotToUnixTime(lucid.currentSlot()));
|
|
415
|
-
|
|
416
|
-
const ownAddr = await lucid.wallet().address();
|
|
417
|
-
|
|
418
|
-
const pollShardRefScriptUtxo = matchSingle(
|
|
419
|
-
await lucid.utxosByOutRef([
|
|
420
|
-
fromSystemParamsScriptRef(
|
|
421
|
-
sysParams.scriptReferences.pollShardValidatorRef,
|
|
422
|
-
),
|
|
423
|
-
]),
|
|
424
|
-
(_) => new Error('Expected a single poll shard ref Script UTXO'),
|
|
425
|
-
);
|
|
426
|
-
const stakingRefScriptUtxo = matchSingle(
|
|
427
|
-
await lucid.utxosByOutRef([
|
|
428
|
-
fromSystemParamsScriptRef(sysParams.scriptReferences.stakingValidatorRef),
|
|
429
|
-
]),
|
|
430
|
-
(_) => new Error('Expected a single staking ref Script UTXO'),
|
|
431
|
-
);
|
|
432
|
-
|
|
433
|
-
const pollShardUtxo = matchSingle(
|
|
434
|
-
await lucid.utxosByOutRef([pollShardOref]),
|
|
435
|
-
(_) => new Error('Expected a single Poll shard UTXO'),
|
|
436
|
-
);
|
|
437
|
-
const pollShardDatum = parsePollShardOrThrow(
|
|
438
|
-
getInlineDatumOrThrow(pollShardUtxo),
|
|
439
|
-
);
|
|
440
|
-
|
|
441
|
-
const stakingPosUtxo = matchSingle(
|
|
442
|
-
await lucid.utxosByOutRef([stakingPositionOref]),
|
|
443
|
-
(_) => new Error('Expected a single staking position UTXO'),
|
|
444
|
-
);
|
|
445
|
-
const stakingPosDatum = parseStakingPositionOrThrow(
|
|
446
|
-
getInlineDatumOrThrow(stakingPosUtxo),
|
|
447
|
-
);
|
|
448
|
-
|
|
449
|
-
const indyStakedAmt = assetClassValueOf(
|
|
450
|
-
stakingPosUtxo.assets,
|
|
451
|
-
fromSystemParamsAsset(sysParams.govParams.indyAsset),
|
|
452
|
-
);
|
|
453
|
-
|
|
454
|
-
const validityFrom = Number(currentTime) - ONE_SECOND;
|
|
455
|
-
|
|
456
|
-
if (
|
|
457
|
-
stakingPosDatum.lockedAmount.find((x) => x[0] === pollShardDatum.pollId) !==
|
|
458
|
-
undefined
|
|
459
|
-
) {
|
|
460
|
-
throw new Error('Already voted for that proposal.');
|
|
461
|
-
}
|
|
462
|
-
|
|
463
|
-
const [newLockedAmt, newVoteStatus] = voteHelper(
|
|
464
|
-
stakingPosDatum.lockedAmount,
|
|
465
|
-
pollShardDatum,
|
|
466
|
-
voteOption,
|
|
467
|
-
BigInt(validityFrom),
|
|
468
|
-
indyStakedAmt,
|
|
469
|
-
);
|
|
470
|
-
|
|
471
|
-
return lucid
|
|
472
|
-
.newTx()
|
|
473
|
-
.validFrom(validityFrom)
|
|
474
|
-
.validTo(
|
|
475
|
-
Math.min(
|
|
476
|
-
validityFrom +
|
|
477
|
-
Number(sysParams.pollManagerParams.pBiasTime) -
|
|
478
|
-
ONE_SECOND,
|
|
479
|
-
Number(pollShardDatum.votingEndTime) - ONE_SECOND,
|
|
480
|
-
),
|
|
481
|
-
)
|
|
482
|
-
.readFrom([stakingRefScriptUtxo, pollShardRefScriptUtxo])
|
|
483
|
-
.collectFrom([stakingPosUtxo], serialiseStakingRedeemer('Lock'))
|
|
484
|
-
.collectFrom(
|
|
485
|
-
[pollShardUtxo],
|
|
486
|
-
serialisePollShardRedeemer({ Vote: voteOption }),
|
|
487
|
-
)
|
|
488
|
-
.pay.ToContract(
|
|
489
|
-
pollShardUtxo.address,
|
|
490
|
-
{
|
|
491
|
-
kind: 'inline',
|
|
492
|
-
value: serialisePollDatum({
|
|
493
|
-
PollShard: {
|
|
494
|
-
...pollShardDatum,
|
|
495
|
-
status: newVoteStatus,
|
|
496
|
-
},
|
|
497
|
-
}),
|
|
498
|
-
},
|
|
499
|
-
pollShardUtxo.assets,
|
|
500
|
-
)
|
|
501
|
-
.pay.ToContract(
|
|
502
|
-
stakingPosUtxo.address,
|
|
503
|
-
{
|
|
504
|
-
kind: 'inline',
|
|
505
|
-
value: serialiseStakingDatum({
|
|
506
|
-
...stakingPosDatum,
|
|
507
|
-
lockedAmount: newLockedAmt,
|
|
508
|
-
}),
|
|
509
|
-
},
|
|
510
|
-
stakingPosUtxo.assets,
|
|
511
|
-
)
|
|
512
|
-
.addSigner(ownAddr);
|
|
513
|
-
}
|
|
514
|
-
|
|
515
|
-
export async function mergeShards(
|
|
516
|
-
pollManagerOref: OutRef,
|
|
517
|
-
shardsOutRefs: OutRef[],
|
|
518
|
-
sysParams: SystemParams,
|
|
519
|
-
lucid: LucidEvolution,
|
|
520
|
-
): Promise<TxBuilder> {
|
|
521
|
-
const currentTime = BigInt(lucid.slotToUnixTime(lucid.currentSlot()));
|
|
522
|
-
|
|
523
|
-
const ownAddr = await lucid.wallet().address();
|
|
524
|
-
|
|
525
|
-
const pollShardRefScriptUtxo = matchSingle(
|
|
526
|
-
await lucid.utxosByOutRef([
|
|
527
|
-
fromSystemParamsScriptRef(
|
|
528
|
-
sysParams.scriptReferences.pollShardValidatorRef,
|
|
529
|
-
),
|
|
530
|
-
]),
|
|
531
|
-
(_) => new Error('Expected a single poll shard ref Script UTXO'),
|
|
532
|
-
);
|
|
533
|
-
const pollManagerRefScriptUtxo = matchSingle(
|
|
534
|
-
await lucid.utxosByOutRef([
|
|
535
|
-
fromSystemParamsScriptRef(
|
|
536
|
-
sysParams.scriptReferences.pollManagerValidatorRef,
|
|
537
|
-
),
|
|
538
|
-
]),
|
|
539
|
-
(_) => new Error('Expected a single poll shard ref Script UTXO'),
|
|
540
|
-
);
|
|
541
|
-
|
|
542
|
-
const pollAuthTokenPolicyRefScriptUtxo = matchSingle(
|
|
543
|
-
await lucid.utxosByOutRef([
|
|
544
|
-
fromSystemParamsScriptRef(
|
|
545
|
-
sysParams.scriptReferences.authTokenPolicies.pollManagerTokenRef,
|
|
546
|
-
),
|
|
547
|
-
]),
|
|
548
|
-
(_) =>
|
|
549
|
-
new Error('Expected a single poll auth token policy ref Script UTXO'),
|
|
550
|
-
);
|
|
551
|
-
|
|
552
|
-
const pollManagerUtxo = matchSingle(
|
|
553
|
-
await lucid.utxosByOutRef([pollManagerOref]),
|
|
554
|
-
(_) => new Error('Expected a single Poll manager UTXO'),
|
|
555
|
-
);
|
|
556
|
-
|
|
557
|
-
const pollManagerDatum = parsePollManagerOrThrow(
|
|
558
|
-
getInlineDatumOrThrow(pollManagerUtxo),
|
|
559
|
-
);
|
|
560
|
-
|
|
561
|
-
const shardUtxos = await lucid.utxosByOutRef(shardsOutRefs);
|
|
562
|
-
|
|
563
|
-
const aggregatedStatus: PollStatus = F.pipe(
|
|
564
|
-
shardUtxos,
|
|
565
|
-
A.map((utxo) => parsePollShardOrThrow(getInlineDatumOrThrow(utxo))),
|
|
566
|
-
A.reduce<PollShardContent, PollStatus>(
|
|
567
|
-
pollManagerDatum.status,
|
|
568
|
-
(acc, shard) => ({
|
|
569
|
-
yesVotes: acc.yesVotes + shard.status.yesVotes,
|
|
570
|
-
noVotes: acc.noVotes + shard.status.noVotes,
|
|
571
|
-
}),
|
|
572
|
-
),
|
|
573
|
-
);
|
|
574
|
-
|
|
575
|
-
const shardsAggregatedAda = A.reduce<UTxO, Assets>({}, (acc, utxo) =>
|
|
576
|
-
addAssets(acc, mkLovelacesOf(lovelacesAmt(utxo.assets))),
|
|
577
|
-
)(shardUtxos);
|
|
578
|
-
|
|
579
|
-
const pollNft = fromSystemParamsAsset(sysParams.govParams.pollToken);
|
|
580
|
-
|
|
581
|
-
return lucid
|
|
582
|
-
.newTx()
|
|
583
|
-
.validFrom(Number(currentTime) - ONE_SECOND)
|
|
584
|
-
.validTo(
|
|
585
|
-
Number(currentTime) +
|
|
586
|
-
Number(sysParams.pollManagerParams.pBiasTime) -
|
|
587
|
-
ONE_SECOND,
|
|
588
|
-
)
|
|
589
|
-
.readFrom([
|
|
590
|
-
pollShardRefScriptUtxo,
|
|
591
|
-
pollManagerRefScriptUtxo,
|
|
592
|
-
pollAuthTokenPolicyRefScriptUtxo,
|
|
593
|
-
])
|
|
594
|
-
.mintAssets(mkAssetsOf(pollNft, -BigInt(shardsOutRefs.length)), Data.void())
|
|
595
|
-
.collectFrom(
|
|
596
|
-
[pollManagerUtxo],
|
|
597
|
-
serialisePollManagerRedeemer({
|
|
598
|
-
MergeShardsManager: { currentTime: currentTime },
|
|
599
|
-
}),
|
|
600
|
-
)
|
|
601
|
-
.collectFrom(
|
|
602
|
-
shardUtxos,
|
|
603
|
-
serialisePollShardRedeemer({
|
|
604
|
-
MergeShards: {
|
|
605
|
-
currentTime: currentTime,
|
|
606
|
-
pollManagerRef: {
|
|
607
|
-
outputIndex: BigInt(pollManagerUtxo.outputIndex),
|
|
608
|
-
txHash: fromHex(pollManagerUtxo.txHash),
|
|
609
|
-
},
|
|
610
|
-
},
|
|
611
|
-
}),
|
|
612
|
-
)
|
|
613
|
-
.pay.ToContract(
|
|
614
|
-
pollManagerUtxo.address,
|
|
615
|
-
{
|
|
616
|
-
kind: 'inline',
|
|
617
|
-
value: serialisePollDatum({
|
|
618
|
-
PollManager: {
|
|
619
|
-
...pollManagerDatum,
|
|
620
|
-
talliedShardsCount:
|
|
621
|
-
pollManagerDatum.talliedShardsCount +
|
|
622
|
-
BigInt(shardsOutRefs.length),
|
|
623
|
-
status: aggregatedStatus,
|
|
624
|
-
},
|
|
625
|
-
}),
|
|
626
|
-
},
|
|
627
|
-
addAssets(pollManagerUtxo.assets, shardsAggregatedAda),
|
|
628
|
-
)
|
|
629
|
-
.addSigner(ownAddr);
|
|
630
|
-
}
|
|
631
|
-
|
|
632
|
-
export async function endProposal(
|
|
633
|
-
pollManagerOref: OutRef,
|
|
634
|
-
govOref: OutRef,
|
|
635
|
-
sysParams: SystemParams,
|
|
636
|
-
lucid: LucidEvolution,
|
|
637
|
-
): Promise<TxBuilder> {
|
|
638
|
-
const network = lucid.config().network!;
|
|
639
|
-
const currentTime = BigInt(lucid.slotToUnixTime(lucid.currentSlot()));
|
|
640
|
-
|
|
641
|
-
const ownAddr = await lucid.wallet().address();
|
|
642
|
-
|
|
643
|
-
const pollManagerRefScriptUtxo = matchSingle(
|
|
644
|
-
await lucid.utxosByOutRef([
|
|
645
|
-
fromSystemParamsScriptRef(
|
|
646
|
-
sysParams.scriptReferences.pollManagerValidatorRef,
|
|
647
|
-
),
|
|
648
|
-
]),
|
|
649
|
-
(_) => new Error('Expected a single poll shard ref Script UTXO'),
|
|
650
|
-
);
|
|
651
|
-
const govRefScriptUtxo = matchSingle(
|
|
652
|
-
await lucid.utxosByOutRef([
|
|
653
|
-
fromSystemParamsScriptRef(
|
|
654
|
-
sysParams.scriptReferences.governanceValidatorRef,
|
|
655
|
-
),
|
|
656
|
-
]),
|
|
657
|
-
(_) => new Error('Expected a single Gov Ref Script UTXO'),
|
|
658
|
-
);
|
|
659
|
-
const pollAuthTokenPolicyRefScriptUtxo = matchSingle(
|
|
660
|
-
await lucid.utxosByOutRef([
|
|
661
|
-
fromSystemParamsScriptRef(
|
|
662
|
-
sysParams.scriptReferences.authTokenPolicies.pollManagerTokenRef,
|
|
663
|
-
),
|
|
664
|
-
]),
|
|
665
|
-
(_) =>
|
|
666
|
-
new Error('Expected a single poll auth token policy ref Script UTXO'),
|
|
667
|
-
);
|
|
668
|
-
const upgradeTokenPolicyRefScriptUtxo = matchSingle(
|
|
669
|
-
await lucid.utxosByOutRef([
|
|
670
|
-
fromSystemParamsScriptRef(
|
|
671
|
-
sysParams.scriptReferences.authTokenPolicies.upgradeTokenRef,
|
|
672
|
-
),
|
|
673
|
-
]),
|
|
674
|
-
(_) =>
|
|
675
|
-
new Error('Expected a single upgrade auth token policy ref Script UTXO'),
|
|
676
|
-
);
|
|
677
|
-
|
|
678
|
-
const pollManagerUtxo = matchSingle(
|
|
679
|
-
await lucid.utxosByOutRef([pollManagerOref]),
|
|
680
|
-
(_) => new Error('Expected a single Poll manager UTXO'),
|
|
681
|
-
);
|
|
682
|
-
const pollManager = parsePollManagerOrThrow(
|
|
683
|
-
getInlineDatumOrThrow(pollManagerUtxo),
|
|
684
|
-
);
|
|
685
|
-
|
|
686
|
-
const govUtxo = matchSingle(
|
|
687
|
-
await lucid.utxosByOutRef([govOref]),
|
|
688
|
-
(_) => new Error('Expected a single Gov UTXO'),
|
|
689
|
-
);
|
|
690
|
-
const govDatum = parseGovDatumOrThrow(getInlineDatumOrThrow(govUtxo));
|
|
691
|
-
|
|
692
|
-
const pollNft = fromSystemParamsAsset(sysParams.govParams.pollToken);
|
|
693
|
-
const indyAsset = fromSystemParamsAsset(
|
|
694
|
-
sysParams.pollManagerParams.indyAsset,
|
|
695
|
-
);
|
|
696
|
-
|
|
697
|
-
const proposalExpired = currentTime > pollManager.expirationTime;
|
|
698
|
-
const proposalPassed =
|
|
699
|
-
!proposalExpired &&
|
|
700
|
-
pollPassQuorum(
|
|
701
|
-
pollManager.status,
|
|
702
|
-
govDatum.protocolParams.electorate,
|
|
703
|
-
pollManager.minimumQuorum,
|
|
704
|
-
);
|
|
705
|
-
|
|
706
|
-
const upgradeTokenVal = mkAssetsOf(
|
|
707
|
-
fromSystemParamsAsset(sysParams.govParams.upgradeToken),
|
|
708
|
-
1n,
|
|
709
|
-
);
|
|
710
|
-
|
|
711
|
-
const tx = lucid
|
|
712
|
-
.newTx()
|
|
713
|
-
.validFrom(Number(currentTime) - ONE_SECOND)
|
|
714
|
-
.validTo(
|
|
715
|
-
Number(currentTime) +
|
|
716
|
-
Number(sysParams.pollManagerParams.pBiasTime) -
|
|
717
|
-
ONE_SECOND,
|
|
718
|
-
)
|
|
719
|
-
.readFrom([
|
|
720
|
-
pollManagerRefScriptUtxo,
|
|
721
|
-
govRefScriptUtxo,
|
|
722
|
-
pollAuthTokenPolicyRefScriptUtxo,
|
|
723
|
-
upgradeTokenPolicyRefScriptUtxo,
|
|
724
|
-
])
|
|
725
|
-
.collectFrom(
|
|
726
|
-
[pollManagerUtxo],
|
|
727
|
-
serialisePollManagerRedeemer({ EndPoll: { currentTime: currentTime } }),
|
|
728
|
-
)
|
|
729
|
-
.collectFrom(
|
|
730
|
-
[govUtxo],
|
|
731
|
-
serialiseGovRedeemer({ WitnessEndPoll: { currentTime: currentTime } }),
|
|
732
|
-
)
|
|
733
|
-
.mintAssets(mkAssetsOf(pollNft, -1n), Data.void())
|
|
734
|
-
.pay.ToContract(
|
|
735
|
-
govUtxo.address,
|
|
736
|
-
{
|
|
737
|
-
kind: 'inline',
|
|
738
|
-
value: serialiseGovDatum({
|
|
739
|
-
...govDatum,
|
|
740
|
-
activeProposals: govDatum.activeProposals - 1n,
|
|
741
|
-
}),
|
|
742
|
-
},
|
|
743
|
-
govUtxo.assets,
|
|
744
|
-
)
|
|
745
|
-
.addSigner(ownAddr);
|
|
746
|
-
|
|
747
|
-
if (proposalPassed) {
|
|
748
|
-
tx.pay
|
|
749
|
-
.ToContract(
|
|
750
|
-
createScriptAddress(network, sysParams.validatorHashes.executeHash),
|
|
751
|
-
{
|
|
752
|
-
kind: 'inline',
|
|
753
|
-
value: serialiseExecuteDatum({
|
|
754
|
-
id: pollManager.pollId,
|
|
755
|
-
content: pollManager.content,
|
|
756
|
-
passedTime: currentTime,
|
|
757
|
-
votingEndTime: pollManager.votingEndTime,
|
|
758
|
-
protocolVersion: pollManager.protocolVersion,
|
|
759
|
-
treasuryWithdrawal: pollManager.treasuryWithdrawal,
|
|
760
|
-
}),
|
|
761
|
-
},
|
|
762
|
-
upgradeTokenVal,
|
|
763
|
-
)
|
|
764
|
-
.mintAssets(upgradeTokenVal, Data.void());
|
|
765
|
-
} else {
|
|
766
|
-
tx.pay.ToContract(
|
|
767
|
-
createScriptAddress(network, sysParams.validatorHashes.treasuryHash),
|
|
768
|
-
{ kind: 'inline', value: Data.void() },
|
|
769
|
-
mkAssetsOf(
|
|
770
|
-
indyAsset,
|
|
771
|
-
assetClassValueOf(pollManagerUtxo.assets, indyAsset),
|
|
772
|
-
),
|
|
773
|
-
);
|
|
774
|
-
}
|
|
775
|
-
|
|
776
|
-
return tx;
|
|
777
|
-
}
|
|
778
|
-
|
|
779
|
-
export async function executeProposal(
|
|
780
|
-
executeOref: OutRef,
|
|
781
|
-
govOref: OutRef,
|
|
782
|
-
treasuryWithdrawalOref: OutRef | null,
|
|
783
|
-
allIAssetOrefs: OutRef[] | null,
|
|
784
|
-
/**
|
|
785
|
-
* This for either Propose IAsset, Modify IAsset or Add Collateral asset proposals.
|
|
786
|
-
*/
|
|
787
|
-
iAssetOref: OutRef | null,
|
|
788
|
-
/**
|
|
789
|
-
* This is when adding new collateral asset. It should be all collateral assets of the given iAsset
|
|
790
|
-
* where it's adding the new collateral asset.
|
|
791
|
-
*/
|
|
792
|
-
allCollateralAssetsOrefsOfIAsset: OutRef[] | null,
|
|
793
|
-
collateralAssetOref: OutRef | null,
|
|
794
|
-
stableswapPoolOrCdpCreatorOref: OutRef | null,
|
|
795
|
-
sysParams: SystemParams,
|
|
796
|
-
lucid: LucidEvolution,
|
|
797
|
-
): Promise<TxBuilder> {
|
|
798
|
-
const network = lucid.config().network!;
|
|
799
|
-
const currentTime = BigInt(lucid.slotToUnixTime(lucid.currentSlot()));
|
|
800
|
-
|
|
801
|
-
const ownAddr = await lucid.wallet().address();
|
|
802
|
-
|
|
803
|
-
const govUtxo = matchSingle(
|
|
804
|
-
await lucid.utxosByOutRef([govOref]),
|
|
805
|
-
(_) => new Error('Expected a single gov UTXO'),
|
|
806
|
-
);
|
|
807
|
-
|
|
808
|
-
const govDatum = parseGovDatumOrThrow(getInlineDatumOrThrow(govUtxo));
|
|
809
|
-
|
|
810
|
-
const govRefScriptUtxo = matchSingle(
|
|
811
|
-
await lucid.utxosByOutRef([
|
|
812
|
-
fromSystemParamsScriptRef(
|
|
813
|
-
sysParams.scriptReferences.governanceValidatorRef,
|
|
814
|
-
),
|
|
815
|
-
]),
|
|
816
|
-
(_) => new Error('Expected a single Gov Ref Script UTXO'),
|
|
817
|
-
);
|
|
818
|
-
const executeRefScriptUtxo = matchSingle(
|
|
819
|
-
await lucid.utxosByOutRef([
|
|
820
|
-
fromSystemParamsScriptRef(sysParams.scriptReferences.executeValidatorRef),
|
|
821
|
-
]),
|
|
822
|
-
(_) => new Error('Expected a single execute Ref Script UTXO'),
|
|
823
|
-
);
|
|
824
|
-
|
|
825
|
-
const upgradeTokenPolicyRefScriptUtxo = matchSingle(
|
|
826
|
-
await lucid.utxosByOutRef([
|
|
827
|
-
fromSystemParamsScriptRef(
|
|
828
|
-
sysParams.scriptReferences.authTokenPolicies.upgradeTokenRef,
|
|
829
|
-
),
|
|
830
|
-
]),
|
|
831
|
-
(_) =>
|
|
832
|
-
new Error('Expected a single upgrade auth token policy ref Script UTXO'),
|
|
833
|
-
);
|
|
834
|
-
|
|
835
|
-
const executeUtxo = matchSingle(
|
|
836
|
-
await lucid.utxosByOutRef([executeOref]),
|
|
837
|
-
(_) => new Error('Expected a single execute UTXO'),
|
|
838
|
-
);
|
|
839
|
-
|
|
840
|
-
const executeDatum = parseExecuteDatumOrThrow(
|
|
841
|
-
getInlineDatumOrThrow(executeUtxo),
|
|
842
|
-
);
|
|
843
|
-
|
|
844
|
-
const tx = lucid.newTx();
|
|
845
|
-
|
|
846
|
-
// Handle treasury withdrawal
|
|
847
|
-
await pipe(
|
|
848
|
-
O.fromNullable(executeDatum.treasuryWithdrawal),
|
|
849
|
-
O.match(
|
|
850
|
-
() => {
|
|
851
|
-
if (treasuryWithdrawalOref) {
|
|
852
|
-
throw new Error('Cannot provide withdrawal oref when no withdrawal.');
|
|
853
|
-
}
|
|
854
|
-
return Promise.resolve();
|
|
855
|
-
},
|
|
856
|
-
async (withdrawal) => {
|
|
857
|
-
if (!treasuryWithdrawalOref) {
|
|
858
|
-
throw new Error('Have to provide withdrawal oref when withdrawal.');
|
|
859
|
-
}
|
|
860
|
-
|
|
861
|
-
const treasuryRefScriptUtxo = matchSingle(
|
|
862
|
-
await lucid.utxosByOutRef([
|
|
863
|
-
fromSystemParamsScriptRef(
|
|
864
|
-
sysParams.scriptReferences.treasuryValidatorRef,
|
|
865
|
-
),
|
|
866
|
-
]),
|
|
867
|
-
(_) => new Error('Expected a single Treasury Ref Script UTXO'),
|
|
868
|
-
);
|
|
869
|
-
|
|
870
|
-
const treasuryWithdrawalUtxo = matchSingle(
|
|
871
|
-
await lucid.utxosByOutRef([treasuryWithdrawalOref]),
|
|
872
|
-
(_) => new Error('Expected a single withdrawal UTXO'),
|
|
873
|
-
);
|
|
874
|
-
|
|
875
|
-
const withdrawalVal = createValueFromWithdrawal(withdrawal);
|
|
876
|
-
const withdrawalChangeVal = addAssets(
|
|
877
|
-
treasuryWithdrawalUtxo.assets,
|
|
878
|
-
negateAssets(withdrawalVal),
|
|
879
|
-
);
|
|
880
|
-
|
|
881
|
-
if (!isAssetsZero(withdrawalChangeVal)) {
|
|
882
|
-
tx.pay.ToContract(
|
|
883
|
-
createScriptAddress(
|
|
884
|
-
network,
|
|
885
|
-
sysParams.validatorHashes.treasuryHash,
|
|
886
|
-
sysParams.treasuryParams.treasuryUtxosStakeCredential
|
|
887
|
-
? fromSysParamsCredential(
|
|
888
|
-
sysParams.treasuryParams.treasuryUtxosStakeCredential,
|
|
889
|
-
)
|
|
890
|
-
: undefined,
|
|
891
|
-
),
|
|
892
|
-
{ kind: 'inline', value: Data.void() },
|
|
893
|
-
withdrawalChangeVal,
|
|
894
|
-
);
|
|
895
|
-
}
|
|
896
|
-
|
|
897
|
-
tx.readFrom([treasuryRefScriptUtxo])
|
|
898
|
-
.collectFrom(
|
|
899
|
-
[treasuryWithdrawalUtxo],
|
|
900
|
-
serialiseTreasuryRedeemer('Withdraw'),
|
|
901
|
-
)
|
|
902
|
-
.pay.ToAddressWithData(
|
|
903
|
-
addressToBech32(withdrawal.destination, lucid.config().network!),
|
|
904
|
-
{
|
|
905
|
-
kind: 'inline',
|
|
906
|
-
value: serialiseWithdrawalOutputDatum([
|
|
907
|
-
fromHex(fromText('IndigoTreasuryWithdrawal')),
|
|
908
|
-
{
|
|
909
|
-
txHash: fromHex(executeUtxo.txHash),
|
|
910
|
-
outputIndex: BigInt(executeUtxo.outputIndex),
|
|
911
|
-
},
|
|
912
|
-
]),
|
|
913
|
-
},
|
|
914
|
-
withdrawalVal,
|
|
915
|
-
);
|
|
916
|
-
},
|
|
917
|
-
),
|
|
918
|
-
);
|
|
919
|
-
|
|
920
|
-
await match(executeDatum.content)
|
|
921
|
-
.with({ ProposeIAsset: P.select() }, async (proposeContent) => {
|
|
922
|
-
const iassetTokenPolicyRefScriptUtxo = matchSingle(
|
|
923
|
-
await lucid.utxosByOutRef([
|
|
924
|
-
fromSystemParamsScriptRef(
|
|
925
|
-
sysParams.scriptReferences.authTokenPolicies.iAssetAuthTokenRef,
|
|
926
|
-
),
|
|
927
|
-
]),
|
|
928
|
-
(_) =>
|
|
929
|
-
new Error(
|
|
930
|
-
'Expected a single iasset auth token policy ref Script UTXO',
|
|
931
|
-
),
|
|
932
|
-
);
|
|
933
|
-
const stabilityPoolTokenPolicyRefScriptUtxo = matchSingle(
|
|
934
|
-
await lucid.utxosByOutRef([
|
|
935
|
-
fromSystemParamsScriptRef(
|
|
936
|
-
sysParams.scriptReferences.authTokenPolicies
|
|
937
|
-
.stabilityPoolAuthTokenRef,
|
|
938
|
-
),
|
|
939
|
-
]),
|
|
940
|
-
(_) =>
|
|
941
|
-
new Error('Expected a single SP auth token policy ref Script UTXO'),
|
|
942
|
-
);
|
|
943
|
-
|
|
944
|
-
const iassetRefScriptUtxo = matchSingle(
|
|
945
|
-
await lucid.utxosByOutRef([
|
|
946
|
-
fromSystemParamsScriptRef(
|
|
947
|
-
sysParams.scriptReferences.iassetValidatorRef,
|
|
948
|
-
),
|
|
949
|
-
]),
|
|
950
|
-
(_) => new Error('Expected a single IAsset Ref Script UTXO'),
|
|
951
|
-
);
|
|
952
|
-
|
|
953
|
-
if (!allIAssetOrefs) {
|
|
954
|
-
throw new Error('Have to provide all iasset orefs when propose asset.');
|
|
955
|
-
}
|
|
956
|
-
|
|
957
|
-
const iassetToReference = await findRelativeIAssetForInsertion(
|
|
958
|
-
proposeContent.asset,
|
|
959
|
-
allIAssetOrefs,
|
|
960
|
-
lucid,
|
|
961
|
-
);
|
|
962
|
-
|
|
963
|
-
const { newIAsset, newReferencedIAsset } = iassetCreationDatumHelper(
|
|
964
|
-
proposeContent,
|
|
965
|
-
F.pipe(
|
|
966
|
-
iassetToReference,
|
|
967
|
-
O.map((i) => i.datum),
|
|
968
|
-
),
|
|
969
|
-
);
|
|
970
|
-
|
|
971
|
-
const iassetAuthVal = mkAssetsOf(
|
|
972
|
-
fromSystemParamsAsset(sysParams.executeParams.iAssetToken),
|
|
973
|
-
1n,
|
|
974
|
-
);
|
|
975
|
-
const spAuthVal = mkAssetsOf(
|
|
976
|
-
fromSystemParamsAsset(sysParams.executeParams.stabilityPoolToken),
|
|
977
|
-
1n,
|
|
978
|
-
);
|
|
979
|
-
|
|
980
|
-
tx.readFrom([
|
|
981
|
-
govRefScriptUtxo,
|
|
982
|
-
iassetRefScriptUtxo,
|
|
983
|
-
iassetTokenPolicyRefScriptUtxo,
|
|
984
|
-
stabilityPoolTokenPolicyRefScriptUtxo,
|
|
985
|
-
])
|
|
986
|
-
.mintAssets(spAuthVal, Data.void())
|
|
987
|
-
.mintAssets(iassetAuthVal, Data.void())
|
|
988
|
-
.collectFrom([govUtxo], serialiseGovRedeemer('UpgradeGov'))
|
|
989
|
-
.pay.ToContract(
|
|
990
|
-
govUtxo.address,
|
|
991
|
-
{
|
|
992
|
-
kind: 'inline',
|
|
993
|
-
value: serialiseGovDatum({
|
|
994
|
-
...govDatum,
|
|
995
|
-
iassetsCount: govDatum.iassetsCount + 1n,
|
|
996
|
-
}),
|
|
997
|
-
},
|
|
998
|
-
govUtxo.assets,
|
|
999
|
-
)
|
|
1000
|
-
.pay.ToContract(
|
|
1001
|
-
createScriptAddress(network, sysParams.validatorHashes.iassetHash),
|
|
1002
|
-
{
|
|
1003
|
-
kind: 'inline',
|
|
1004
|
-
value: serialiseIAssetDatum({ IAssetContent: newIAsset }),
|
|
1005
|
-
},
|
|
1006
|
-
iassetAuthVal,
|
|
1007
|
-
)
|
|
1008
|
-
.pay.ToContract(
|
|
1009
|
-
createScriptAddress(
|
|
1010
|
-
network,
|
|
1011
|
-
sysParams.validatorHashes.stabilityPoolHash,
|
|
1012
|
-
),
|
|
1013
|
-
{
|
|
1014
|
-
kind: 'inline',
|
|
1015
|
-
value: serialiseStabilityPoolDatum(
|
|
1016
|
-
{
|
|
1017
|
-
StabilityPool: {
|
|
1018
|
-
iasset: proposeContent.asset,
|
|
1019
|
-
state: initSpState,
|
|
1020
|
-
assetStates: [],
|
|
1021
|
-
},
|
|
1022
|
-
},
|
|
1023
|
-
true,
|
|
1024
|
-
),
|
|
1025
|
-
},
|
|
1026
|
-
spAuthVal,
|
|
1027
|
-
);
|
|
1028
|
-
|
|
1029
|
-
F.pipe(
|
|
1030
|
-
iassetToReference,
|
|
1031
|
-
O.match(
|
|
1032
|
-
() => {
|
|
1033
|
-
// no action
|
|
1034
|
-
},
|
|
1035
|
-
(i) =>
|
|
1036
|
-
F.pipe(
|
|
1037
|
-
newReferencedIAsset,
|
|
1038
|
-
O.match(
|
|
1039
|
-
() => {
|
|
1040
|
-
throw new Error('Expected some referenced iasset.');
|
|
1041
|
-
},
|
|
1042
|
-
(newRefIAsset) => {
|
|
1043
|
-
tx.collectFrom([i.utxo], {
|
|
1044
|
-
kind: 'selected',
|
|
1045
|
-
makeRedeemer: (inputIndices) =>
|
|
1046
|
-
serialiseIAssetRedeemer({
|
|
1047
|
-
UpdateOrInsertAsset: {
|
|
1048
|
-
executeInputIdx: inputIndices[0],
|
|
1049
|
-
},
|
|
1050
|
-
}),
|
|
1051
|
-
inputs: [executeUtxo],
|
|
1052
|
-
}).pay.ToContract(
|
|
1053
|
-
i.utxo.address,
|
|
1054
|
-
{
|
|
1055
|
-
kind: 'inline',
|
|
1056
|
-
value: serialiseIAssetDatum({
|
|
1057
|
-
IAssetContent: newRefIAsset,
|
|
1058
|
-
}),
|
|
1059
|
-
},
|
|
1060
|
-
i.utxo.assets,
|
|
1061
|
-
);
|
|
1062
|
-
},
|
|
1063
|
-
),
|
|
1064
|
-
),
|
|
1065
|
-
),
|
|
1066
|
-
);
|
|
1067
|
-
})
|
|
1068
|
-
.with({ ModifyIAsset: P.select() }, async (modifyContent) => {
|
|
1069
|
-
const iassetRefScriptUtxo = matchSingle(
|
|
1070
|
-
await lucid.utxosByOutRef([
|
|
1071
|
-
fromSystemParamsScriptRef(
|
|
1072
|
-
sysParams.scriptReferences.iassetValidatorRef,
|
|
1073
|
-
),
|
|
1074
|
-
]),
|
|
1075
|
-
(_) => new Error('Expected a single CDP Ref Script UTXO'),
|
|
1076
|
-
);
|
|
1077
|
-
|
|
1078
|
-
if (!iAssetOref) {
|
|
1079
|
-
throw new Error('Have to provide iasset oref when modify asset.');
|
|
1080
|
-
}
|
|
1081
|
-
|
|
1082
|
-
const iassetUtxo = matchSingle(
|
|
1083
|
-
await lucid.utxosByOutRef([iAssetOref]),
|
|
1084
|
-
(_) => new Error('Expected a single iasset UTXO'),
|
|
1085
|
-
);
|
|
1086
|
-
|
|
1087
|
-
const iassetDatum = parseIAssetDatumOrThrow(
|
|
1088
|
-
getInlineDatumOrThrow(iassetUtxo),
|
|
1089
|
-
);
|
|
1090
|
-
|
|
1091
|
-
tx.readFrom([iassetRefScriptUtxo])
|
|
1092
|
-
.collectFrom([iassetUtxo], {
|
|
1093
|
-
kind: 'selected',
|
|
1094
|
-
makeRedeemer: (inputIndices) =>
|
|
1095
|
-
serialiseIAssetRedeemer({
|
|
1096
|
-
UpdateOrInsertAsset: {
|
|
1097
|
-
executeInputIdx: inputIndices[0],
|
|
1098
|
-
},
|
|
1099
|
-
}),
|
|
1100
|
-
inputs: [executeUtxo],
|
|
1101
|
-
})
|
|
1102
|
-
.pay.ToContract(
|
|
1103
|
-
createScriptAddress(network, sysParams.validatorHashes.iassetHash),
|
|
1104
|
-
{
|
|
1105
|
-
kind: 'inline',
|
|
1106
|
-
value: serialiseIAssetDatum({
|
|
1107
|
-
IAssetContent: {
|
|
1108
|
-
assetName: iassetDatum.assetName,
|
|
1109
|
-
collateralAssetsCount: iassetDatum.collateralAssetsCount,
|
|
1110
|
-
debtMintingFeeRatio: modifyContent.newDebtMintingFeeRatio,
|
|
1111
|
-
liquidationProcessingFeeRatio:
|
|
1112
|
-
modifyContent.newLiquidationProcessingFeeRatio,
|
|
1113
|
-
stabilityPoolWithdrawalFeeRatio:
|
|
1114
|
-
modifyContent.newStabilityPoolWithdrawalFeeRatio,
|
|
1115
|
-
redemptionReimbursementRatio:
|
|
1116
|
-
modifyContent.newRedemptionReimbursementRatio,
|
|
1117
|
-
redemptionProcessingFeeRatio:
|
|
1118
|
-
modifyContent.newRedemptionProcessingFeeRatio,
|
|
1119
|
-
firstIAsset: iassetDatum.firstIAsset,
|
|
1120
|
-
nextIAsset: iassetDatum.nextIAsset,
|
|
1121
|
-
},
|
|
1122
|
-
}),
|
|
1123
|
-
},
|
|
1124
|
-
iassetUtxo.assets,
|
|
1125
|
-
)
|
|
1126
|
-
.readFrom([govUtxo])
|
|
1127
|
-
.setMinFee(900_000n);
|
|
1128
|
-
})
|
|
1129
|
-
.with({ AddCollateralAsset: P.select() }, async (addCollateralContent) => {
|
|
1130
|
-
const collateralAssetTokenPolicyRefScriptUtxo = matchSingle(
|
|
1131
|
-
await lucid.utxosByOutRef([
|
|
1132
|
-
fromSystemParamsScriptRef(
|
|
1133
|
-
sysParams.scriptReferences.authTokenPolicies
|
|
1134
|
-
.collateralAssetTokenRef,
|
|
1135
|
-
),
|
|
1136
|
-
]),
|
|
1137
|
-
(_) =>
|
|
1138
|
-
new Error(
|
|
1139
|
-
'Expected a single collateral asset auth token policy ref Script UTXO',
|
|
1140
|
-
),
|
|
1141
|
-
);
|
|
1142
|
-
|
|
1143
|
-
const iassetRefScriptUtxo = matchSingle(
|
|
1144
|
-
await lucid.utxosByOutRef([
|
|
1145
|
-
fromSystemParamsScriptRef(
|
|
1146
|
-
sysParams.scriptReferences.iassetValidatorRef,
|
|
1147
|
-
),
|
|
1148
|
-
]),
|
|
1149
|
-
(_) => new Error('Expected a single iasset Ref Script UTXO'),
|
|
1150
|
-
);
|
|
1151
|
-
|
|
1152
|
-
if (!iAssetOref) {
|
|
1153
|
-
throw new Error(
|
|
1154
|
-
'Have to provide iasset oref when add collateral asset.',
|
|
1155
|
-
);
|
|
1156
|
-
}
|
|
1157
|
-
|
|
1158
|
-
const iassetUtxo = matchSingle(
|
|
1159
|
-
await lucid.utxosByOutRef([iAssetOref]),
|
|
1160
|
-
(_) => new Error('Expected a single iasset UTXO'),
|
|
1161
|
-
);
|
|
1162
|
-
const iassetDatum = parseIAssetDatumOrThrow(
|
|
1163
|
-
getInlineDatumOrThrow(iassetUtxo),
|
|
1164
|
-
);
|
|
1165
|
-
|
|
1166
|
-
if (!allCollateralAssetsOrefsOfIAsset) {
|
|
1167
|
-
throw new Error(
|
|
1168
|
-
'Have to provide all collateral asset orefs when propose asset.',
|
|
1169
|
-
);
|
|
1170
|
-
}
|
|
1171
|
-
|
|
1172
|
-
const collateralAssetToReference =
|
|
1173
|
-
await findRelativeCollateralAssetForInsertion(
|
|
1174
|
-
addCollateralContent.collateralAsset,
|
|
1175
|
-
allCollateralAssetsOrefsOfIAsset,
|
|
1176
|
-
lucid,
|
|
1177
|
-
);
|
|
1178
|
-
|
|
1179
|
-
const { newCollateralAsset, newReferencedCollateralAsset } =
|
|
1180
|
-
collateralAssetCreationDatumHelper(
|
|
1181
|
-
addCollateralContent,
|
|
1182
|
-
F.pipe(
|
|
1183
|
-
collateralAssetToReference,
|
|
1184
|
-
O.map((i) => i.datum),
|
|
1185
|
-
),
|
|
1186
|
-
);
|
|
1187
|
-
|
|
1188
|
-
const collateralAssetAuthVal = mkAssetsOf(
|
|
1189
|
-
fromSystemParamsAsset(sysParams.executeParams.collateralAssetToken),
|
|
1190
|
-
1n,
|
|
1191
|
-
);
|
|
1192
|
-
|
|
1193
|
-
const firstCollateralAsset = await findFirstCollateralAsset(
|
|
1194
|
-
allCollateralAssetsOrefsOfIAsset,
|
|
1195
|
-
lucid,
|
|
1196
|
-
);
|
|
1197
|
-
|
|
1198
|
-
tx.readFrom([
|
|
1199
|
-
iassetRefScriptUtxo,
|
|
1200
|
-
collateralAssetTokenPolicyRefScriptUtxo,
|
|
1201
|
-
]);
|
|
1202
|
-
tx.readFrom([govUtxo])
|
|
1203
|
-
.mintAssets(collateralAssetAuthVal, Data.void())
|
|
1204
|
-
.collectFrom([iassetUtxo], {
|
|
1205
|
-
kind: 'selected',
|
|
1206
|
-
makeRedeemer: (inputIndices) =>
|
|
1207
|
-
serialiseIAssetRedeemer({
|
|
1208
|
-
UpdateOrInsertAsset: {
|
|
1209
|
-
executeInputIdx: inputIndices[0],
|
|
1210
|
-
},
|
|
1211
|
-
}),
|
|
1212
|
-
inputs: [executeUtxo],
|
|
1213
|
-
})
|
|
1214
|
-
.pay.ToContract(
|
|
1215
|
-
createScriptAddress(network, sysParams.validatorHashes.iassetHash),
|
|
1216
|
-
{
|
|
1217
|
-
kind: 'inline',
|
|
1218
|
-
value: serialiseIAssetDatum({
|
|
1219
|
-
IAssetContent: {
|
|
1220
|
-
...iassetDatum,
|
|
1221
|
-
collateralAssetsCount: iassetDatum.collateralAssetsCount + 1n,
|
|
1222
|
-
},
|
|
1223
|
-
}),
|
|
1224
|
-
},
|
|
1225
|
-
iassetUtxo.assets,
|
|
1226
|
-
)
|
|
1227
|
-
.pay.ToContract(
|
|
1228
|
-
createScriptAddress(network, sysParams.validatorHashes.iassetHash),
|
|
1229
|
-
{
|
|
1230
|
-
kind: 'inline',
|
|
1231
|
-
value: serialiseIAssetDatum({
|
|
1232
|
-
CollateralAssetContent: newCollateralAsset,
|
|
1233
|
-
}),
|
|
1234
|
-
},
|
|
1235
|
-
collateralAssetAuthVal,
|
|
1236
|
-
);
|
|
1237
|
-
|
|
1238
|
-
F.pipe(
|
|
1239
|
-
collateralAssetToReference,
|
|
1240
|
-
O.match(
|
|
1241
|
-
() => {
|
|
1242
|
-
// no action
|
|
1243
|
-
},
|
|
1244
|
-
(i) => {
|
|
1245
|
-
F.pipe(
|
|
1246
|
-
newReferencedCollateralAsset,
|
|
1247
|
-
O.match(
|
|
1248
|
-
() => {
|
|
1249
|
-
throw new Error('Expected some referenced collateral asset');
|
|
1250
|
-
},
|
|
1251
|
-
(newRefCollateralAsset) => {
|
|
1252
|
-
tx.collectFrom([i.utxo], {
|
|
1253
|
-
kind: 'selected',
|
|
1254
|
-
makeRedeemer: (inputIndices) =>
|
|
1255
|
-
serialiseIAssetRedeemer({
|
|
1256
|
-
UpdateOrInsertAsset: {
|
|
1257
|
-
executeInputIdx: inputIndices[0],
|
|
1258
|
-
},
|
|
1259
|
-
}),
|
|
1260
|
-
inputs: [executeUtxo],
|
|
1261
|
-
}).pay.ToContract(
|
|
1262
|
-
createScriptAddress(
|
|
1263
|
-
network,
|
|
1264
|
-
sysParams.validatorHashes.iassetHash,
|
|
1265
|
-
),
|
|
1266
|
-
{
|
|
1267
|
-
kind: 'inline',
|
|
1268
|
-
value: serialiseIAssetDatum({
|
|
1269
|
-
CollateralAssetContent: newRefCollateralAsset,
|
|
1270
|
-
}),
|
|
1271
|
-
},
|
|
1272
|
-
i.utxo.assets,
|
|
1273
|
-
);
|
|
1274
|
-
},
|
|
1275
|
-
),
|
|
1276
|
-
);
|
|
1277
|
-
},
|
|
1278
|
-
),
|
|
1279
|
-
);
|
|
1280
|
-
|
|
1281
|
-
// Provide proof of existence or non-existence of ADA collateral.
|
|
1282
|
-
if (
|
|
1283
|
-
!isSameAssetClass(newCollateralAsset.collateralAsset, adaAssetClass)
|
|
1284
|
-
) {
|
|
1285
|
-
F.pipe(
|
|
1286
|
-
newReferencedCollateralAsset,
|
|
1287
|
-
O.match(
|
|
1288
|
-
() => {},
|
|
1289
|
-
(referencedAsset) =>
|
|
1290
|
-
referencedAsset.firstCollateralAsset
|
|
1291
|
-
? {}
|
|
1292
|
-
: F.pipe(
|
|
1293
|
-
firstCollateralAsset,
|
|
1294
|
-
O.match(
|
|
1295
|
-
() => {},
|
|
1296
|
-
(firstAsset) => {
|
|
1297
|
-
tx.readFrom([firstAsset.utxo]);
|
|
1298
|
-
},
|
|
1299
|
-
),
|
|
1300
|
-
),
|
|
1301
|
-
),
|
|
1302
|
-
);
|
|
1303
|
-
}
|
|
1304
|
-
})
|
|
1305
|
-
.with(
|
|
1306
|
-
{ UpdateCollateralAsset: P.select() },
|
|
1307
|
-
async (updateCollateralContent) => {
|
|
1308
|
-
const iassetRefScriptUtxo = matchSingle(
|
|
1309
|
-
await lucid.utxosByOutRef([
|
|
1310
|
-
fromSystemParamsScriptRef(
|
|
1311
|
-
sysParams.scriptReferences.iassetValidatorRef,
|
|
1312
|
-
),
|
|
1313
|
-
]),
|
|
1314
|
-
(_) => new Error('Expected a single iasset Ref Script UTXO'),
|
|
1315
|
-
);
|
|
1316
|
-
|
|
1317
|
-
if (!collateralAssetOref) {
|
|
1318
|
-
throw new Error(
|
|
1319
|
-
'Have to provide collateral asset oref when add collateral asset.',
|
|
1320
|
-
);
|
|
1321
|
-
}
|
|
1322
|
-
|
|
1323
|
-
const collateralAssetUtxo = matchSingle(
|
|
1324
|
-
await lucid.utxosByOutRef([collateralAssetOref]),
|
|
1325
|
-
(_) => new Error('Expected a single collateral asset UTXO'),
|
|
1326
|
-
);
|
|
1327
|
-
const collateralAssetDatum = parseCollateralAssetDatumOrThrow(
|
|
1328
|
-
getInlineDatumOrThrow(collateralAssetUtxo),
|
|
1329
|
-
);
|
|
1330
|
-
|
|
1331
|
-
if (
|
|
1332
|
-
!isSameAssetClass(
|
|
1333
|
-
collateralAssetDatum.collateralAsset,
|
|
1334
|
-
updateCollateralContent.collateralAsset,
|
|
1335
|
-
) ||
|
|
1336
|
-
toHex(collateralAssetDatum.iasset) !==
|
|
1337
|
-
toHex(updateCollateralContent.correspondingIAsset)
|
|
1338
|
-
) {
|
|
1339
|
-
throw new Error('Wrong collateral asset.');
|
|
1340
|
-
}
|
|
1341
|
-
|
|
1342
|
-
tx.readFrom([iassetRefScriptUtxo])
|
|
1343
|
-
.collectFrom([collateralAssetUtxo], {
|
|
1344
|
-
kind: 'selected',
|
|
1345
|
-
makeRedeemer: (inputIndices) =>
|
|
1346
|
-
serialiseIAssetRedeemer({
|
|
1347
|
-
UpdateOrInsertAsset: {
|
|
1348
|
-
executeInputIdx: inputIndices[0],
|
|
1349
|
-
},
|
|
1350
|
-
}),
|
|
1351
|
-
inputs: [executeUtxo],
|
|
1352
|
-
})
|
|
1353
|
-
.pay.ToContract(
|
|
1354
|
-
createScriptAddress(network, sysParams.validatorHashes.iassetHash),
|
|
1355
|
-
{
|
|
1356
|
-
kind: 'inline',
|
|
1357
|
-
value: serialiseIAssetDatum({
|
|
1358
|
-
CollateralAssetContent: {
|
|
1359
|
-
...collateralAssetDatum,
|
|
1360
|
-
priceInfo: updateCollateralContent.newAssetPriceInfo,
|
|
1361
|
-
interestOracleNft:
|
|
1362
|
-
updateCollateralContent.newInterestOracleNft,
|
|
1363
|
-
redemptionRatio: updateCollateralContent.newRedemptionRatio,
|
|
1364
|
-
maintenanceRatio: updateCollateralContent.newMaintenanceRatio,
|
|
1365
|
-
liquidationRatio: updateCollateralContent.newLiquidationRatio,
|
|
1366
|
-
minCollateralAmt: updateCollateralContent.newMinCollateralAmt,
|
|
1367
|
-
},
|
|
1368
|
-
}),
|
|
1369
|
-
},
|
|
1370
|
-
collateralAssetUtxo.assets,
|
|
1371
|
-
)
|
|
1372
|
-
.readFrom([govUtxo])
|
|
1373
|
-
.setMinFee(900_000n);
|
|
1374
|
-
},
|
|
1375
|
-
)
|
|
1376
|
-
.with({ TextProposal: P.any }, () => {
|
|
1377
|
-
tx.readFrom([govUtxo]);
|
|
1378
|
-
})
|
|
1379
|
-
.with({ ProposeStableswapPool: P.select() }, async (proposeContent) => {
|
|
1380
|
-
const cdpCreatorRefScriptUtxo = matchSingle(
|
|
1381
|
-
await lucid.utxosByOutRef([
|
|
1382
|
-
fromSystemParamsScriptRef(
|
|
1383
|
-
sysParams.scriptReferences.cdpCreatorValidatorRef,
|
|
1384
|
-
),
|
|
1385
|
-
]),
|
|
1386
|
-
(_) => new Error('Expected a single CDP Creator Ref Script UTXO'),
|
|
1387
|
-
);
|
|
1388
|
-
|
|
1389
|
-
const cdpTokenPolicyRefScriptUtxo = matchSingle(
|
|
1390
|
-
await lucid.utxosByOutRef([
|
|
1391
|
-
fromSystemParamsScriptRef(
|
|
1392
|
-
sysParams.scriptReferences.authTokenPolicies.cdpAuthTokenRef,
|
|
1393
|
-
),
|
|
1394
|
-
]),
|
|
1395
|
-
(_) =>
|
|
1396
|
-
new Error('Expected a single CDP auth token policy ref Script UTXO'),
|
|
1397
|
-
);
|
|
1398
|
-
|
|
1399
|
-
if (!iAssetOref) {
|
|
1400
|
-
throw new Error(
|
|
1401
|
-
'Have to provide iasset oref when proposing stableswap pool.',
|
|
1402
|
-
);
|
|
1403
|
-
}
|
|
1404
|
-
|
|
1405
|
-
const iassetUtxo = matchSingle(
|
|
1406
|
-
await lucid.utxosByOutRef([iAssetOref]),
|
|
1407
|
-
(_) => new Error('Expected a single iasset UTXO'),
|
|
1408
|
-
);
|
|
1409
|
-
|
|
1410
|
-
const cdpAuthVal = mkAssetsOf(
|
|
1411
|
-
fromSystemParamsAsset(sysParams.executeParams.cdpToken),
|
|
1412
|
-
1n,
|
|
1413
|
-
);
|
|
1414
|
-
|
|
1415
|
-
if (!stableswapPoolOrCdpCreatorOref) {
|
|
1416
|
-
throw new Error(
|
|
1417
|
-
'Have to provide cdp creator oref when propose stableswap pool.',
|
|
1418
|
-
);
|
|
1419
|
-
}
|
|
1420
|
-
|
|
1421
|
-
const cdpCreatorUtxo = matchSingle(
|
|
1422
|
-
await lucid.utxosByOutRef([stableswapPoolOrCdpCreatorOref]),
|
|
1423
|
-
(_) => new Error('Expected a single iasset UTXO'),
|
|
1424
|
-
);
|
|
1425
|
-
|
|
1426
|
-
tx.readFrom([
|
|
1427
|
-
cdpCreatorRefScriptUtxo,
|
|
1428
|
-
cdpTokenPolicyRefScriptUtxo,
|
|
1429
|
-
govUtxo,
|
|
1430
|
-
iassetUtxo,
|
|
1431
|
-
])
|
|
1432
|
-
.mintAssets(cdpAuthVal, Data.void())
|
|
1433
|
-
.collectFrom(
|
|
1434
|
-
[cdpCreatorUtxo],
|
|
1435
|
-
serialiseCDPCreatorRedeemer('CreateStableswapPool'),
|
|
1436
|
-
)
|
|
1437
|
-
.pay.ToContract(
|
|
1438
|
-
createScriptAddress(
|
|
1439
|
-
network,
|
|
1440
|
-
sysParams.validatorHashes.cdpCreatorHash,
|
|
1441
|
-
),
|
|
1442
|
-
{ kind: 'inline', value: Data.void() },
|
|
1443
|
-
cdpCreatorUtxo.assets,
|
|
1444
|
-
)
|
|
1445
|
-
.pay.ToContract(
|
|
1446
|
-
createScriptAddress(network, sysParams.validatorHashes.cdpHash),
|
|
1447
|
-
{
|
|
1448
|
-
kind: 'inline',
|
|
1449
|
-
value: serialiseStableswapPoolDatum({
|
|
1450
|
-
iasset: proposeContent.iasset,
|
|
1451
|
-
collateralAsset: proposeContent.collateralAsset,
|
|
1452
|
-
collateralToIassetRatio: proposeContent.collateralToIassetRatio,
|
|
1453
|
-
mintingFeeRatio: proposeContent.mintingFeeRatio,
|
|
1454
|
-
redemptionFeeRatio: proposeContent.redemptionFeeRatio,
|
|
1455
|
-
mintingEnabled: true,
|
|
1456
|
-
redemptionEnabled: true,
|
|
1457
|
-
feeManager: proposeContent.feeManager,
|
|
1458
|
-
minMintOrderAmount: proposeContent.minMintingAmount,
|
|
1459
|
-
minRedemptionOrderAmount: proposeContent.minRedemptionAmount,
|
|
1460
|
-
stableswapValHash: proposeContent.stableswapValHash,
|
|
1461
|
-
}),
|
|
1462
|
-
},
|
|
1463
|
-
cdpAuthVal,
|
|
1464
|
-
);
|
|
1465
|
-
})
|
|
1466
|
-
.with({ ModifyStableswapPool: P.select() }, async (modifyContent) => {
|
|
1467
|
-
const cdpRefScriptUtxo = matchSingle(
|
|
1468
|
-
await lucid.utxosByOutRef([
|
|
1469
|
-
fromSystemParamsScriptRef(sysParams.scriptReferences.cdpValidatorRef),
|
|
1470
|
-
]),
|
|
1471
|
-
(_) => new Error('Expected a single CDP Ref Script UTXO'),
|
|
1472
|
-
);
|
|
1473
|
-
|
|
1474
|
-
if (!stableswapPoolOrCdpCreatorOref) {
|
|
1475
|
-
throw new Error(
|
|
1476
|
-
'Have to provide stableswap pool oref when propose stableswap pool.',
|
|
1477
|
-
);
|
|
1478
|
-
}
|
|
1479
|
-
|
|
1480
|
-
const stableswapPoolUtxo = matchSingle(
|
|
1481
|
-
await lucid.utxosByOutRef([stableswapPoolOrCdpCreatorOref]),
|
|
1482
|
-
(_) => new Error('Expected a single stableswap pool UTXO'),
|
|
1483
|
-
);
|
|
1484
|
-
|
|
1485
|
-
tx.readFrom([govRefScriptUtxo, cdpRefScriptUtxo, govUtxo])
|
|
1486
|
-
.collectFrom([stableswapPoolUtxo], {
|
|
1487
|
-
kind: 'selected',
|
|
1488
|
-
makeRedeemer: (inputIndices) =>
|
|
1489
|
-
serialiseCdpRedeemer({
|
|
1490
|
-
Stableswap: {
|
|
1491
|
-
forwardingInputIndex: inputIndices[0],
|
|
1492
|
-
},
|
|
1493
|
-
}),
|
|
1494
|
-
inputs: [executeUtxo],
|
|
1495
|
-
})
|
|
1496
|
-
.pay.ToContract(
|
|
1497
|
-
createScriptAddress(network, sysParams.validatorHashes.cdpHash),
|
|
1498
|
-
{
|
|
1499
|
-
kind: 'inline',
|
|
1500
|
-
value: serialiseStableswapPoolDatum({
|
|
1501
|
-
iasset: modifyContent.iasset,
|
|
1502
|
-
collateralAsset: modifyContent.collateralAsset,
|
|
1503
|
-
collateralToIassetRatio: modifyContent.newCollateralToIassetRatio,
|
|
1504
|
-
mintingFeeRatio: modifyContent.newMintingFeeRatio,
|
|
1505
|
-
redemptionFeeRatio: modifyContent.newRedemptionFeeRatio,
|
|
1506
|
-
mintingEnabled: modifyContent.newMintingEnabled,
|
|
1507
|
-
redemptionEnabled: modifyContent.newRedemptionEnabled,
|
|
1508
|
-
feeManager: modifyContent.newFeeManager,
|
|
1509
|
-
minMintOrderAmount: modifyContent.newMinMintingAmount,
|
|
1510
|
-
minRedemptionOrderAmount: modifyContent.newMinRedemptionAmount,
|
|
1511
|
-
stableswapValHash: modifyContent.newStableswapValHash,
|
|
1512
|
-
}),
|
|
1513
|
-
},
|
|
1514
|
-
stableswapPoolUtxo.assets,
|
|
1515
|
-
)
|
|
1516
|
-
// This has to be added as otherwise there is the following error:
|
|
1517
|
-
// TxBuilderError: { Complete: RedeemerBuilder: Coin selection had to be updated
|
|
1518
|
-
// after building redeemers, possibly leading to incorrect indices. Try setting
|
|
1519
|
-
// a minimum fee of 1416197 lovelaces. }
|
|
1520
|
-
.setMinFee(1416197n);
|
|
1521
|
-
})
|
|
1522
|
-
.with(
|
|
1523
|
-
{ ModifyProtocolParams: { newParams: P.select() } },
|
|
1524
|
-
(newProtocolParams) => {
|
|
1525
|
-
tx.readFrom([govRefScriptUtxo])
|
|
1526
|
-
.collectFrom([govUtxo], serialiseGovRedeemer('UpgradeGov'))
|
|
1527
|
-
.pay.ToContract(
|
|
1528
|
-
govUtxo.address,
|
|
1529
|
-
{
|
|
1530
|
-
kind: 'inline',
|
|
1531
|
-
value: serialiseGovDatum({
|
|
1532
|
-
...govDatum,
|
|
1533
|
-
protocolParams: newProtocolParams,
|
|
1534
|
-
}),
|
|
1535
|
-
},
|
|
1536
|
-
govUtxo.assets,
|
|
1537
|
-
);
|
|
1538
|
-
},
|
|
1539
|
-
)
|
|
1540
|
-
.with({ UpgradeProtocol: P.select() }, async (d) => {
|
|
1541
|
-
const versionRecordTokenPolicyRefScriptUtxo = matchSingle(
|
|
1542
|
-
await lucid.utxosByOutRef([
|
|
1543
|
-
fromSystemParamsScriptRef(
|
|
1544
|
-
sysParams.scriptReferences.authTokenPolicies
|
|
1545
|
-
.versionRecordTokenPolicyRef,
|
|
1546
|
-
),
|
|
1547
|
-
]),
|
|
1548
|
-
(_) =>
|
|
1549
|
-
new Error(
|
|
1550
|
-
'Expected a single version record token policy ref Script UTXO',
|
|
1551
|
-
),
|
|
1552
|
-
);
|
|
1553
|
-
|
|
1554
|
-
const versionRecordNftVal = mkAssetsOf(
|
|
1555
|
-
fromSystemParamsAsset(sysParams.executeParams.versionRecordToken),
|
|
1556
|
-
1n,
|
|
1557
|
-
);
|
|
1558
|
-
|
|
1559
|
-
tx.readFrom([govRefScriptUtxo, versionRecordTokenPolicyRefScriptUtxo])
|
|
1560
|
-
.mintAssets(versionRecordNftVal, Data.void())
|
|
1561
|
-
.pay.ToContract(
|
|
1562
|
-
createScriptAddress(
|
|
1563
|
-
network,
|
|
1564
|
-
sysParams.validatorHashes.versionRegistryHash,
|
|
1565
|
-
),
|
|
1566
|
-
{
|
|
1567
|
-
kind: 'inline',
|
|
1568
|
-
value: serialiseVersionRecordDatum({
|
|
1569
|
-
upgradeId: d.content.upgradeId,
|
|
1570
|
-
upgradePaths: d.content.upgradePaths.map(([h1, h2]) => [
|
|
1571
|
-
h1,
|
|
1572
|
-
h2.upgradeSymbol,
|
|
1573
|
-
]),
|
|
1574
|
-
}),
|
|
1575
|
-
},
|
|
1576
|
-
versionRecordNftVal,
|
|
1577
|
-
)
|
|
1578
|
-
.collectFrom([govUtxo], serialiseGovRedeemer('UpgradeGov'))
|
|
1579
|
-
.pay.ToContract(
|
|
1580
|
-
govUtxo.address,
|
|
1581
|
-
{
|
|
1582
|
-
kind: 'inline',
|
|
1583
|
-
value: serialiseGovDatum({
|
|
1584
|
-
...govDatum,
|
|
1585
|
-
currentVersion: govDatum.currentVersion + 1n,
|
|
1586
|
-
}),
|
|
1587
|
-
},
|
|
1588
|
-
govUtxo.assets,
|
|
1589
|
-
);
|
|
1590
|
-
})
|
|
1591
|
-
.otherwise(() => {});
|
|
1592
|
-
|
|
1593
|
-
tx.readFrom([upgradeTokenPolicyRefScriptUtxo, executeRefScriptUtxo])
|
|
1594
|
-
.validFrom(Number(currentTime) - ONE_SECOND)
|
|
1595
|
-
.validTo(
|
|
1596
|
-
Number(currentTime) + Number(sysParams.govParams.gBiasTime) - ONE_SECOND,
|
|
1597
|
-
)
|
|
1598
|
-
.collectFrom([executeUtxo], Data.void())
|
|
1599
|
-
.mintAssets(
|
|
1600
|
-
mkAssetsOf(fromSystemParamsAsset(sysParams.govParams.upgradeToken), -1n),
|
|
1601
|
-
Data.void(),
|
|
1602
|
-
)
|
|
1603
|
-
.addSigner(ownAddr)
|
|
1604
|
-
.setMinFee(922932n);
|
|
1605
|
-
|
|
1606
|
-
return tx;
|
|
1607
|
-
}
|