-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): hoist computeImplicitCoin from wallet package
- Loading branch information
1 parent
9ff5735
commit d991f73
Showing
3 changed files
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
28
packages/core/test/Cardano/util/computeImplicitCoin.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |