Skip to content

Commit

Permalink
feat(core): hoist computeImplicitCoin from wallet package
Browse files Browse the repository at this point in the history
  • Loading branch information
mkazlauskas committed Jan 21, 2022
1 parent 9ff5735 commit d991f73
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
33 changes: 33 additions & 0 deletions packages/core/src/Cardano/util/computeImplicitCoin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { BigIntMath, Cardano, ProtocolParametersRequiredByWallet } from '../../';

/**
* Implementation is the same as in CSL.get_implicit_input() and CSL.get_deposit().
*/
export const computeImplicitCoin = (
{ stakeKeyDeposit, poolDeposit }: ProtocolParametersRequiredByWallet,
{ certificates, withdrawals }: Pick<Cardano.TxBodyAlonzo, 'certificates' | 'withdrawals'>
): Cardano.ImplicitCoin => {
const stakeKeyDepositBigint = stakeKeyDeposit && BigInt(stakeKeyDeposit);
const poolDepositBigint = poolDeposit && BigInt(poolDeposit);
const deposit = BigIntMath.sum(
certificates?.map(
(cert) =>
(cert.__typename === Cardano.CertificateType.StakeKeyRegistration && stakeKeyDepositBigint) ||
(cert.__typename === Cardano.CertificateType.PoolRegistration && poolDepositBigint) ||
0n
) || []
);
const withdrawalsTotal = (withdrawals && BigIntMath.sum(withdrawals.map(({ quantity }) => quantity))) || 0n;
const reclaimTotal = BigIntMath.sum(
certificates?.map(
(cert) =>
(cert.__typename === Cardano.CertificateType.StakeKeyDeregistration && stakeKeyDepositBigint) ||
(cert.__typename === Cardano.CertificateType.PoolRetirement && poolDepositBigint) ||
0n
) || []
);
return {
deposit,
input: withdrawalsTotal + reclaimTotal
};
};
1 change: 1 addition & 0 deletions packages/core/src/Cardano/util/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './coalesceValueQuantities';
export * from './computeMinUtxoValue';
export * from './computeImplicitCoin';
export * from './primitives';
export * as metadatum from './metadatum';
28 changes: 28 additions & 0 deletions packages/core/test/Cardano/util/computeImplicitCoin.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Cardano, ProtocolParametersRequiredByWallet } from '@cardano-sdk/core';

describe('Cardano.util.computeImplicitCoin', () => {
it('sums registrations for deposit, withdrawals and deregistrations for input', async () => {
const protocolParameters = { poolDeposit: 3, stakeKeyDeposit: 2 } as ProtocolParametersRequiredByWallet;
const rewardAccount = Cardano.RewardAccount('stake_test1uqfu74w3wh4gfzu8m6e7j987h4lq9r3t7ef5gaw497uu85qsqfy27');
const certificates: Cardano.Certificate[] = [
{ __typename: Cardano.CertificateType.StakeKeyRegistration, rewardAccount },
{ __typename: Cardano.CertificateType.StakeKeyDeregistration, rewardAccount },
{ __typename: Cardano.CertificateType.StakeKeyRegistration, rewardAccount },
{
__typename: Cardano.CertificateType.PoolRetirement,
epoch: 500,
poolId: Cardano.PoolId('pool1zuevzm3xlrhmwjw87ec38mzs02tlkwec9wxpgafcaykmwg7efhh')
},
{
__typename: Cardano.CertificateType.StakeDelegation,
epoch: 500,
poolId: Cardano.PoolId('pool1zuevzm3xlrhmwjw87ec38mzs02tlkwec9wxpgafcaykmwg7efhh'),
rewardAccount
}
];
const withdrawals: Cardano.Withdrawal[] = [{ quantity: 5n, stakeAddress: rewardAccount }];
const coin = Cardano.util.computeImplicitCoin(protocolParameters, { certificates, withdrawals });
expect(coin.deposit).toBe(2n + 2n);
expect(coin.input).toBe(2n + 3n + 5n);
});
});

0 comments on commit d991f73

Please sign in to comment.