Skip to content

Commit 21e1863

Browse files
committed
feat: add proposal procedures deposit to compute implicit coins
1 parent baebef7 commit 21e1863

File tree

5 files changed

+54
-10
lines changed

5 files changed

+54
-10
lines changed

packages/core/src/Cardano/util/computeImplicitCoin.ts

+17-7
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ const computeShellyDeposits = (
7777
const computeConwayDeposits = (
7878
certificates: Cardano.Certificate[],
7979
rewardAccounts: Cardano.RewardAccount[],
80-
dRepKeyHash?: Crypto.Ed25519KeyHashHex
80+
dRepKeyHash?: Crypto.Ed25519KeyHashHex,
81+
proposalProcedures?: Cardano.ProposalProcedure[]
8182
): { deposit: Cardano.Lovelace; reclaimDeposit: Cardano.Lovelace } => {
8283
let deposit = 0n;
8384
let reclaimDeposit = 0n;
@@ -108,6 +109,8 @@ const computeConwayDeposits = (
108109
}
109110
}
110111

112+
if (proposalProcedures) for (const proposal of proposalProcedures) deposit += proposal.deposit;
113+
111114
return {
112115
deposit,
113116
reclaimDeposit
@@ -119,17 +122,19 @@ const getTxDeposits = (
119122
{ stakeKeyDeposit, poolDeposit }: Pick<Cardano.ProtocolParameters, 'stakeKeyDeposit' | 'poolDeposit'>,
120123
certificates: Cardano.Certificate[],
121124
rewardAccounts: Cardano.RewardAccount[] = [],
122-
dRepKeyHash?: Crypto.Ed25519KeyHashHex
125+
dRepKeyHash?: Crypto.Ed25519KeyHashHex,
126+
proposalProcedures?: Cardano.ProposalProcedure[]
123127
): { deposit: Cardano.Lovelace; reclaimDeposit: Cardano.Lovelace } => {
124-
if (certificates.length === 0) return { deposit: 0n, reclaimDeposit: 0n };
128+
if (certificates.length === 0 && (!proposalProcedures || proposalProcedures.length === 0))
129+
return { deposit: 0n, reclaimDeposit: 0n };
125130

126131
const depositParams = {
127132
poolDeposit: poolDeposit ? BigInt(poolDeposit) : 0n,
128133
stakeKeyDeposit: BigInt(stakeKeyDeposit)
129134
};
130135

131136
const shelleyDeposits = computeShellyDeposits(depositParams, certificates, rewardAccounts);
132-
const conwayDeposits = computeConwayDeposits(certificates, rewardAccounts, dRepKeyHash);
137+
const conwayDeposits = computeConwayDeposits(certificates, rewardAccounts, dRepKeyHash, proposalProcedures);
133138

134139
return {
135140
deposit: shelleyDeposits.deposit + conwayDeposits.deposit,
@@ -150,19 +155,24 @@ const getTxDeposits = (
150155
* On the other hand, the transaction summary/display could receive a transaction from a dApp,
151156
* and can have mixed certificates (foreign and ours), so we need the list of reward accounts and drepKeyHash
152157
* to be able to distinguish the deposits that are going to our rewardAccounts from the ones that could
153-
* potentially go to a different reward accounts that we dont control (same with reclaims).
158+
* potentially go to a different reward accounts that we don't control (same with reclaims).
154159
*/
155160
export const computeImplicitCoin = (
156161
{ stakeKeyDeposit, poolDeposit }: Pick<Cardano.ProtocolParameters, 'stakeKeyDeposit' | 'poolDeposit'>,
157-
{ certificates, withdrawals }: Pick<HydratedTxBody, 'certificates' | 'withdrawals'>,
162+
{
163+
certificates,
164+
proposalProcedures,
165+
withdrawals
166+
}: Pick<HydratedTxBody, 'certificates' | 'proposalProcedures' | 'withdrawals'>,
158167
rewardAccounts?: Cardano.RewardAccount[],
159168
dRepKeyHash?: Crypto.Ed25519KeyHashHex
160169
): ImplicitCoin => {
161170
const { deposit, reclaimDeposit } = getTxDeposits(
162171
{ poolDeposit, stakeKeyDeposit },
163172
certificates ?? [],
164173
rewardAccounts,
165-
dRepKeyHash
174+
dRepKeyHash,
175+
proposalProcedures
166176
);
167177

168178
const withdrawalsTotal = (withdrawals && BigIntMath.sum(withdrawals.map(({ quantity }) => quantity))) || 0n;

packages/core/test/Cardano/util/computeImplicitCoin.test.ts

+31
Original file line numberDiff line numberDiff line change
@@ -198,4 +198,35 @@ describe('Cardano.util.computeImplicitCoin', () => {
198198
expect(coin.input).toBe(withdrawals[0].quantity + expectedReclaim);
199199
expect(coin.withdrawals).toBe(withdrawals[0].quantity);
200200
});
201+
202+
it('sums certificates and proposal procedures for deposit', () => {
203+
const protocolParameters = { governanceActionDeposit: 4, stakeKeyDeposit: 2 } as Cardano.ProtocolParameters;
204+
const governanceActionDeposit = BigInt(protocolParameters.governanceActionDeposit);
205+
const stakeKeyDeposit = BigInt(protocolParameters.stakeKeyDeposit);
206+
207+
const anchor = {
208+
dataHash: '3e33018e8293d319ef5b3ac72366dd28006bd315b715f7e7cfcbd3004129b80d' as Crypto.Hash32ByteBase16,
209+
url: 'https://testing.this'
210+
};
211+
const certificates: Cardano.Certificate[] = [
212+
{ __typename: Cardano.CertificateType.Registration, deposit: stakeKeyDeposit, stakeCredential }
213+
];
214+
const proposalProcedures: Cardano.ProposalProcedure[] = [
215+
{
216+
anchor,
217+
deposit: governanceActionDeposit,
218+
governanceAction: { __typename: Cardano.GovernanceActionType.info_action },
219+
rewardAccount
220+
}
221+
];
222+
223+
const coin = Cardano.util.computeImplicitCoin(
224+
protocolParameters,
225+
{ certificates, proposalProcedures },
226+
[rewardAccount],
227+
dRepKeyHash
228+
);
229+
230+
expect(coin.deposit).toBe(stakeKeyDeposit + governanceActionDeposit);
231+
});
201232
});

packages/tx-construction/src/tx-builder/TxBuilder.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@ export class GenericTxBuilder implements TxBuilder {
260260
customizeCb: this.#customizeCb,
261261
handleResolutions: this.#handleResolutions,
262262
outputs: new Set(this.partialTxBody.outputs || []),
263+
proposalProcedures: this.partialTxBody.proposalProcedures,
263264
signingOptions: partialSigningOptions,
264265
witness: { extraSigners }
265266
},
@@ -361,15 +362,15 @@ export class GenericTxBuilder implements TxBuilder {
361362
);
362363

363364
// Reward accounts already delegated to the correct pool. Change must be distributed accordingly
364-
for (const accnt of rewardAccounts.filter(
365+
for (const account of rewardAccounts.filter(
365366
(rewardAccount) =>
366367
rewardAccount.keyStatus === Cardano.StakeKeyStatus.Registered &&
367368
rewardAccount.delegatee?.nextNextEpoch &&
368369
this.#requestedPortfolio?.some(({ id }) => id === rewardAccount.delegatee?.nextNextEpoch?.id)
369370
))
370371
rewardAccountsWithWeights.set(
371-
accnt.address,
372-
this.#requestedPortfolio!.find(({ id }) => id === accnt.delegatee?.nextNextEpoch?.id)!.weight
372+
account.address,
373+
this.#requestedPortfolio!.find(({ id }) => id === account.delegatee?.nextNextEpoch?.id)!.weight
373374
);
374375

375376
// Reward accounts which don't have the stake key registered or that were delegated but should not be anymore

packages/tx-construction/src/tx-builder/initializeTx.ts

+1
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ export const initializeTx = async (
8888

8989
const implicitCoin = Cardano.util.computeImplicitCoin(protocolParameters, {
9090
certificates: bodyPreInputSelection.certificates,
91+
proposalProcedures: bodyPreInputSelection.proposalProcedures,
9192
withdrawals: bodyPreInputSelection.withdrawals
9293
});
9394

packages/tx-construction/src/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ export interface InitializeTxProps {
4848
witness?: InitializeTxWitness;
4949
signingOptions?: Pick<SignTransactionOptions, 'additionalKeyPaths'>;
5050
handleResolutions?: HandleResolution[];
51+
proposalProcedures?: Cardano.ProposalProcedure[];
5152
/** callback function that allows updating the transaction before input selection */
5253
customizeCb?: CustomizeCb;
5354
}

0 commit comments

Comments
 (0)