Skip to content

Commit

Permalink
feat: new pool delegation and stake registration factory methods adde…
Browse files Browse the repository at this point in the history
…d to core package
  • Loading branch information
AngelCastilloB committed Jun 20, 2023
1 parent 164be41 commit 82d95af
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 35 deletions.
32 changes: 32 additions & 0 deletions packages/core/src/Cardano/types/Certificate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,35 @@ export type Certificate =
| StakeDelegationCertificate
| MirCertificate
| GenesisKeyDelegationCertificate;

/**
* Creates a stake key registration certificate from a given reward account.
*
* @param rewardAccount The reward account to be registered.
*/
export const createStakeKeyRegistrationCert = (rewardAccount: RewardAccount): Certificate => ({
__typename: CertificateType.StakeKeyRegistration,
stakeKeyHash: RewardAccount.toHash(rewardAccount)
});

/**
* Creates a stake key de-registration certificate from a given reward account.
*
* @param rewardAccount The reward account to be de-registered.
*/
export const createStakeKeyDeregistrationCert = (rewardAccount: RewardAccount): Certificate => ({
__typename: CertificateType.StakeKeyDeregistration,
stakeKeyHash: RewardAccount.toHash(rewardAccount)
});

/**
* Creates a delegation certificate from a given reward account and a pool id.
*
* @param rewardAccount The reward account to be registered.
* @param poolId The id of the pool that we are delegating to.
*/
export const createDelegationCert = (rewardAccount: RewardAccount, poolId: PoolId): Certificate => ({
__typename: CertificateType.StakeDelegation,
poolId,
stakeKeyHash: RewardAccount.toHash(rewardAccount)
});
45 changes: 45 additions & 0 deletions packages/core/test/Cardano/types/Certificates.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import {
CertificateType,
PoolId,
RewardAccount,
createDelegationCert,
createStakeKeyDeregistrationCert,
createStakeKeyRegistrationCert
} from '../../../src/Cardano';

const rewardAccount = RewardAccount('stake1u89sasnfyjtmgk8ydqfv3fdl52f36x3djedfnzfc9rkgzrcss5vgr');
const stakeKeyHash = 'cb0ec2692497b458e46812c8a5bfa2931d1a2d965a99893828ec810f';
const poolId = PoolId('pool1ev8vy6fyj7693ergzty2t0azjvw35tvkt2vcjwpgajqs7z6u2vn');

describe('Certificate', () => {
describe('createStakeKeyRegistrationCert', () => {
it('can create a stake key registration certificate', () => {
const cert = createStakeKeyRegistrationCert(rewardAccount);
expect(cert).toEqual({
__typename: CertificateType.StakeKeyRegistration,
stakeKeyHash
});
});
});

describe('createStakeKeyDeregistrationCert', () => {
it('can create a stake key de-registration certificate', () => {
const cert = createStakeKeyDeregistrationCert(rewardAccount);
expect(cert).toEqual({
__typename: CertificateType.StakeKeyDeregistration,
stakeKeyHash
});
});
});

describe('createDelegationCert', () => {
it('can get the policy ID component from the asset id', () => {
const cert = createDelegationCert(rewardAccount, poolId);
expect(cert).toEqual({
__typename: CertificateType.StakeDelegation,
poolId: 'pool1ev8vy6fyj7693ergzty2t0azjvw35tvkt2vcjwpgajqs7z6u2vn',
stakeKeyHash
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -110,17 +110,6 @@ const deregisterAllStakeKeys = async (wallet: PersonalWallet): Promise<void> =>
}
};

const createStakeKeyRegistrationCert = (rewardAccount: Cardano.RewardAccount): Cardano.Certificate => ({
__typename: Cardano.CertificateType.StakeKeyRegistration,
stakeKeyHash: Cardano.RewardAccount.toHash(rewardAccount)
});

const createDelegationCert = (rewardAccount: Cardano.RewardAccount, poolId: Cardano.PoolId): Cardano.Certificate => ({
__typename: Cardano.CertificateType.StakeDelegation,
poolId,
stakeKeyHash: Cardano.RewardAccount.toHash(rewardAccount)
});

const getPoolIds = async (wallet: PersonalWallet, count: number): Promise<Cardano.StakePool[]> => {
const activePools = await wallet.stakePoolProvider.queryStakePools({
filters: { status: [Cardano.StakePoolStatus.Active] },
Expand All @@ -134,11 +123,11 @@ const delegateToMultiplePools = async (wallet: PersonalWallet) => {
// Delegating to multiple pools should be added in TxBuilder. Doing it manually for now.
// Prepare stakeKey registration certificates
const rewardAccounts = await firstValueFrom(wallet.delegation.rewardAccounts$);
const stakeKeyRegCertificates = rewardAccounts.map(({ address }) => createStakeKeyRegistrationCert(address));
const stakeKeyRegCertificates = rewardAccounts.map(({ address }) => Cardano.createStakeKeyRegistrationCert(address));

const poolIds = await getPoolIds(wallet, rewardAccounts.length);
const delegationCertificates = rewardAccounts.map(({ address }, index) =>
createDelegationCert(address, poolIds[index].id)
Cardano.createDelegationCert(address, poolIds[index].id)
);

logger.debug(
Expand Down
25 changes: 3 additions & 22 deletions packages/tx-construction/src/tx-builder/TxBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import * as Crypto from '@cardano-sdk/crypto';
import { Cardano, HandleProvider, HandleResolution } from '@cardano-sdk/core';
import { Logger } from 'ts-log';
import {
Expand Down Expand Up @@ -250,7 +249,6 @@ export class GenericTxBuilder implements TxBuilder {
this.partialTxBody = { ...this.partialTxBody, certificates: [] };

for (const rewardAccount of rewardAccounts) {
const stakeKeyHash = Cardano.RewardAccount.toHash(rewardAccount.address);
if (this.#delegateConfig.type === 'deregister') {
// Deregister scenario
if (rewardAccount.keyStatus === Cardano.StakeKeyStatus.Unregistered) {
Expand All @@ -260,10 +258,7 @@ export class GenericTxBuilder implements TxBuilder {
rewardAccount.keyStatus
);
} else {
this.partialTxBody.certificates!.push({
__typename: Cardano.CertificateType.StakeKeyDeregistration,
stakeKeyHash
});
this.partialTxBody.certificates!.push(Cardano.createStakeKeyDeregistrationCert(rewardAccount.address));
}
} else if (this.#delegateConfig.type === 'delegate') {
// Register and delegate scenario
Expand All @@ -274,27 +269,13 @@ export class GenericTxBuilder implements TxBuilder {
rewardAccount.keyStatus
);
} else {
this.partialTxBody.certificates!.push({
__typename: Cardano.CertificateType.StakeKeyRegistration,
stakeKeyHash
});
this.partialTxBody.certificates!.push(Cardano.createStakeKeyRegistrationCert(rewardAccount.address));
}

this.partialTxBody.certificates!.push(
GenericTxBuilder.#createDelegationCert(this.#delegateConfig.poolId, stakeKeyHash)
Cardano.createDelegationCert(rewardAccount.address, this.#delegateConfig.poolId)
);
}
}
}

static #createDelegationCert(
poolId: Cardano.PoolId,
stakeKeyHash: Crypto.Ed25519KeyHashHex
): Cardano.StakeDelegationCertificate {
return {
__typename: Cardano.CertificateType.StakeDelegation,
poolId,
stakeKeyHash
};
}
}

0 comments on commit 82d95af

Please sign in to comment.