From 394bf43a2e83961e329e767ffe2ea4cc60fcaf22 Mon Sep 17 00:00:00 2001 From: Ken Lau Date: Thu, 26 Jun 2025 11:37:26 +0800 Subject: [PATCH 1/2] feat: certificate . . --- lib/cocktail.ak | 31 ++++++++ lib/cocktail/vodka_certificate.ak | 122 ++++++++++++++++++++++++++++++ 2 files changed, 153 insertions(+) create mode 100644 lib/cocktail/vodka_certificate.ak diff --git a/lib/cocktail.ak b/lib/cocktail.ak index f67cb2c..67994cd 100644 --- a/lib/cocktail.ak +++ b/lib/cocktail.ak @@ -16,6 +16,7 @@ //// - `Redeemers` - [Documentation](./cocktail/vodka_redeemers.html) use cocktail/vodka_address +use cocktail/vodka_certificate use cocktail/vodka_converter use cocktail/vodka_extra_signatories use cocktail/vodka_inputs @@ -159,3 +160,33 @@ pub const value_policy_info = vodka_value.value_policy_info /// Documentation please refer to [`vodka_value`](./cocktail/vodka_value.html) pub const value_tokens = vodka_value.value_tokens + +// Certificate + +/// Documentation please refer to [`vodka_certificate`](./cocktail/vodka_certificate.html) +pub const register_stake_certificate = + vodka_certificate.register_stake_certificate + +/// Documentation please refer to [`vodka_certificate`](./cocktail/vodka_certificate.html) +pub const unregister_stake_certificate = + vodka_certificate.unregister_stake_certificate + +/// Documentation please refer to [`vodka_certificate`](./cocktail/vodka_certificate.html) +pub const register_drep_certificate = + vodka_certificate.register_drep_certificate + +/// Documentation please refer to [`vodka_certificate`](./cocktail/vodka_certificate.html) +pub const unregister_drep_certificate = + vodka_certificate.unregister_drep_certificate + +/// Documentation please refer to [`vodka_certificate`](./cocktail/vodka_certificate.html) +pub const delegate_stake_certificate = + vodka_certificate.delegate_stake_certificate + +/// Documentation please refer to [`vodka_certificate`](./cocktail/vodka_certificate.html) +pub const delegate_vote_certificate = + vodka_certificate.delegate_vote_certificate + +/// Documentation please refer to [`vodka_certificate`](./cocktail/vodka_certificate.html) +pub const delegate_stake_and_vote_certificate = + vodka_certificate.delegate_stake_and_vote_certificate diff --git a/lib/cocktail/vodka_certificate.ak b/lib/cocktail/vodka_certificate.ak new file mode 100644 index 0000000..05f0363 --- /dev/null +++ b/lib/cocktail/vodka_certificate.ak @@ -0,0 +1,122 @@ +use aiken/collection/list +use cardano/address.{Credential} +use cardano/assets.{Lovelace} +use cardano/certificate.{ + Certificate, DelegateBlockProduction, DelegateBoth, DelegateCredential, + DelegateRepresentative, DelegateVote, RegisterCredential, + RegisterDelegateRepresentative, StakePoolId, UnregisterCredential, + UnregisterDelegateRepresentative, +} + +/// Check if a certain stake registration certificate exists in certificates. +/// ```aiken +/// let is_stake_registerd = register_stake_certificate(certificates, stake_credential) +/// ``` +pub fn register_stake_certificate( + certificates: List, + credential: Credential, +) { + list.has(certificates, RegisterCredential { credential, deposit: Never }) +} + +/// Check if a certain stake unregistration certificate exists in certificates. +/// ```aiken +/// let is_stake_unregisterd = unregister_stake_certificate(certificates, stake_credential) +/// ``` +pub fn unregister_stake_certificate( + certificates: List, + credential: Credential, +) { + list.has(certificates, UnregisterCredential { credential, refund: Never }) +} + +/// Check if a certain drep registration with specified deposit certificate exists in certificates. +/// ```aiken +/// let is_drep_registerd = register_drep_certificate(certificates, stake_credential, deposit) +/// ``` +pub fn register_drep_certificate( + certificates: List, + credential: Credential, + deposit: Lovelace, +) { + list.has( + certificates, + RegisterDelegateRepresentative { + delegate_representative: credential, + deposit, + }, + ) +} + +/// Check if a certain drep unregistration with specified refund certificate exists in certificates. +/// ```aiken +/// let is_drep_unregisterd = unregister_drep_certificate(certificates, stake_credential, refund) +/// ``` +pub fn unregister_drep_certificate( + certificates: List, + credential: Credential, + refund: Lovelace, +) { + list.has( + certificates, + UnregisterDelegateRepresentative { + delegate_representative: credential, + refund, + }, + ) +} + +/// Check if a certain stake delegation to a specificed pool certificate exists in certificates. +/// ```aiken +/// let is_stake_delegated = delegate_stake_certificate(certificates, stake_credential, stake_pool) +/// ``` +pub fn delegate_stake_certificate( + certificates: List, + credential: Credential, + stake_pool: StakePoolId, +) { + list.has( + certificates, + DelegateCredential { + credential, + delegate: DelegateBlockProduction { stake_pool }, + }, + ) +} + +/// Check if a certain voting power delegation to a specificed drep certificate exists in certificates. +/// ```aiken +/// let is_vote_delegated = delegate_vote_certificate(certificates, stake_credential, delegate_representative) +/// ``` +pub fn delegate_vote_certificate( + certificates: List, + credential: Credential, + delegate_representative: DelegateRepresentative, +) { + list.has( + certificates, + DelegateCredential { + credential, + delegate: DelegateVote { delegate_representative }, + }, + ) +} + +/// Check if a certain delegation to a specificed pool and drep certificate exists in certificates. +/// ```aiken +/// let is_stake_and_vote_delegated = delegate_stake_and_vote_certificate(certificates, stake_credential, stake_pool, delegate_representative) +/// ``` +pub fn delegate_stake_and_vote_certificate( + certificates: List, + credential: Credential, + stake_pool: StakePoolId, + delegate_representative: DelegateRepresentative, +) { + list.has( + certificates, + DelegateCredential { + credential, + delegate: DelegateBoth { stake_pool, delegate_representative }, + }, + ) +} From dee88246b483f5645b8c079f886beceb9dffd48f Mon Sep 17 00:00:00 2001 From: Ken Lau Date: Mon, 30 Jun 2025 16:18:06 +0800 Subject: [PATCH 2/2] added: test --- lib/cocktail/vodka_certificate.ak | 368 +++++++++++++++++++++++++++++- 1 file changed, 366 insertions(+), 2 deletions(-) diff --git a/lib/cocktail/vodka_certificate.ak b/lib/cocktail/vodka_certificate.ak index 05f0363..174f380 100644 --- a/lib/cocktail/vodka_certificate.ak +++ b/lib/cocktail/vodka_certificate.ak @@ -1,10 +1,10 @@ use aiken/collection/list -use cardano/address.{Credential} +use cardano/address.{Credential, Script} use cardano/assets.{Lovelace} use cardano/certificate.{ Certificate, DelegateBlockProduction, DelegateBoth, DelegateCredential, DelegateRepresentative, DelegateVote, RegisterCredential, - RegisterDelegateRepresentative, StakePoolId, UnregisterCredential, + RegisterDelegateRepresentative, Registered, StakePoolId, UnregisterCredential, UnregisterDelegateRepresentative, } @@ -19,6 +19,26 @@ pub fn register_stake_certificate( list.has(certificates, RegisterCredential { credential, deposit: Never }) } +test register_stake_certificate_found() { + let certificates = + [ + RegisterCredential { credential: Script(""), deposit: Never }, + UnregisterCredential { credential: Script(""), refund: Never }, + ] + + register_stake_certificate(certificates, Script("")) == True +} + +test register_stake_certificate_not_found() { + let certificates = + [ + RegisterCredential { credential: Script("Not Found"), deposit: Never }, + UnregisterCredential { credential: Script("Not Found"), refund: Never }, + ] + + register_stake_certificate(certificates, Script("")) == False +} + /// Check if a certain stake unregistration certificate exists in certificates. /// ```aiken /// let is_stake_unregisterd = unregister_stake_certificate(certificates, stake_credential) @@ -30,6 +50,26 @@ pub fn unregister_stake_certificate( list.has(certificates, UnregisterCredential { credential, refund: Never }) } +test unregister_stake_certificate_found() { + let certificates = + [ + RegisterCredential { credential: Script(""), deposit: Never }, + UnregisterCredential { credential: Script(""), refund: Never }, + ] + + unregister_stake_certificate(certificates, Script("")) == True +} + +test unregister_stake_certificate_not_found() { + let certificates = + [ + RegisterCredential { credential: Script("Not Found"), deposit: Never }, + UnregisterCredential { credential: Script("Not Found"), refund: Never }, + ] + + unregister_stake_certificate(certificates, Script("")) == False +} + /// Check if a certain drep registration with specified deposit certificate exists in certificates. /// ```aiken /// let is_drep_registerd = register_drep_certificate(certificates, stake_credential, deposit) @@ -48,6 +88,54 @@ pub fn register_drep_certificate( ) } +test register_drep_certificate_found() { + let certificates = + [ + RegisterDelegateRepresentative { + delegate_representative: Script(""), + deposit: 10, + }, + UnregisterDelegateRepresentative { + delegate_representative: Script(""), + refund: 10, + }, + ] + + register_drep_certificate(certificates, Script(""), 10) == True +} + +test register_drep_certificate_not_found_with_diff_cred() { + let certificates = + [ + RegisterDelegateRepresentative { + delegate_representative: Script("Not Found"), + deposit: 10, + }, + UnregisterDelegateRepresentative { + delegate_representative: Script("Not Found"), + refund: 10, + }, + ] + + register_drep_certificate(certificates, Script(""), 10) == False +} + +test register_drep_certificate_not_found_with_diff_deposit() { + let certificates = + [ + RegisterDelegateRepresentative { + delegate_representative: Script("Not Found"), + deposit: 0, + }, + UnregisterDelegateRepresentative { + delegate_representative: Script("Not Found"), + refund: 0, + }, + ] + + register_drep_certificate(certificates, Script(""), 10) == False +} + /// Check if a certain drep unregistration with specified refund certificate exists in certificates. /// ```aiken /// let is_drep_unregisterd = unregister_drep_certificate(certificates, stake_credential, refund) @@ -66,6 +154,54 @@ pub fn unregister_drep_certificate( ) } +test unregister_drep_certificate_found() { + let certificates = + [ + RegisterDelegateRepresentative { + delegate_representative: Script(""), + deposit: 10, + }, + UnregisterDelegateRepresentative { + delegate_representative: Script(""), + refund: 10, + }, + ] + + unregister_drep_certificate(certificates, Script(""), 10) == True +} + +test unregister_drep_certificate_not_found_with_diff_cred() { + let certificates = + [ + RegisterDelegateRepresentative { + delegate_representative: Script("Not Found"), + deposit: 10, + }, + UnregisterDelegateRepresentative { + delegate_representative: Script("Not Found"), + refund: 10, + }, + ] + + unregister_drep_certificate(certificates, Script(""), 10) == False +} + +test unregister_drep_certificate_not_found_with_diff_refund() { + let certificates = + [ + RegisterDelegateRepresentative { + delegate_representative: Script("Not Found"), + deposit: 0, + }, + UnregisterDelegateRepresentative { + delegate_representative: Script("Not Found"), + refund: 0, + }, + ] + + unregister_drep_certificate(certificates, Script(""), 10) == False +} + /// Check if a certain stake delegation to a specificed pool certificate exists in certificates. /// ```aiken /// let is_stake_delegated = delegate_stake_certificate(certificates, stake_credential, stake_pool) @@ -84,6 +220,60 @@ pub fn delegate_stake_certificate( ) } +test delegate_stake_certificate_found() { + let certificates = + [ + DelegateCredential { + credential: Script(""), + delegate: DelegateBlockProduction { stake_pool: "" }, + }, + DelegateCredential { + credential: Script(""), + delegate: DelegateVote { + delegate_representative: Registered(Script("")), + }, + }, + ] + + delegate_stake_certificate(certificates, Script(""), "") == True +} + +test delegate_stake_certificate_not_found_with_diff_cred() { + let certificates = + [ + DelegateCredential { + credential: Script("Not Found"), + delegate: DelegateBlockProduction { stake_pool: "" }, + }, + DelegateCredential { + credential: Script("Not Found"), + delegate: DelegateVote { + delegate_representative: Registered(Script("")), + }, + }, + ] + + delegate_stake_certificate(certificates, Script(""), "") == False +} + +test delegate_stake_certificate_not_found_with_diff_pool() { + let certificates = + [ + DelegateCredential { + credential: Script(""), + delegate: DelegateBlockProduction { stake_pool: "Not Found" }, + }, + DelegateCredential { + credential: Script(""), + delegate: DelegateVote { + delegate_representative: Registered(Script("")), + }, + }, + ] + + delegate_stake_certificate(certificates, Script(""), "") == False +} + /// Check if a certain voting power delegation to a specificed drep certificate exists in certificates. /// ```aiken /// let is_vote_delegated = delegate_vote_certificate(certificates, stake_credential, delegate_representative) @@ -102,6 +292,60 @@ pub fn delegate_vote_certificate( ) } +test delegate_vote_certificate_found() { + let certificates = + [ + DelegateCredential { + credential: Script(""), + delegate: DelegateBlockProduction { stake_pool: "" }, + }, + DelegateCredential { + credential: Script(""), + delegate: DelegateVote { + delegate_representative: Registered(Script("")), + }, + }, + ] + + delegate_vote_certificate(certificates, Script(""), Registered(Script(""))) == True +} + +test delegate_vote_certificate_not_found_with_diff_cred() { + let certificates = + [ + DelegateCredential { + credential: Script("Not Found"), + delegate: DelegateBlockProduction { stake_pool: "" }, + }, + DelegateCredential { + credential: Script("Not Found"), + delegate: DelegateVote { + delegate_representative: Registered(Script("")), + }, + }, + ] + + delegate_vote_certificate(certificates, Script(""), Registered(Script(""))) == False +} + +test delegate_vote_certificate_not_found_with_diff_drep() { + let certificates = + [ + DelegateCredential { + credential: Script(""), + delegate: DelegateBlockProduction { stake_pool: "Not Found" }, + }, + DelegateCredential { + credential: Script(""), + delegate: DelegateVote { + delegate_representative: Registered(Script("Not Found")), + }, + }, + ] + + delegate_vote_certificate(certificates, Script(""), Registered(Script(""))) == False +} + /// Check if a certain delegation to a specificed pool and drep certificate exists in certificates. /// ```aiken /// let is_stake_and_vote_delegated = delegate_stake_and_vote_certificate(certificates, stake_credential, stake_pool, delegate_representative) @@ -120,3 +364,123 @@ pub fn delegate_stake_and_vote_certificate( }, ) } + +test delegate_stake_and_vote_certificate_found() { + let certificates = + [ + DelegateCredential { + credential: Script(""), + delegate: DelegateBlockProduction { stake_pool: "" }, + }, + DelegateCredential { + credential: Script(""), + delegate: DelegateVote { + delegate_representative: Registered(Script("")), + }, + }, + DelegateCredential { + credential: Script(""), + delegate: DelegateBoth { + stake_pool: "", + delegate_representative: Registered(Script("")), + }, + }, + ] + + delegate_stake_and_vote_certificate( + certificates, + Script(""), + "", + Registered(Script("")), + ) == True +} + +test delegate_stake_and_vote_certificate_not_found_with_diff_cred() { + let certificates = + [ + DelegateCredential { + credential: Script("Not Found"), + delegate: DelegateBlockProduction { stake_pool: "" }, + }, + DelegateCredential { + credential: Script("Not Found"), + delegate: DelegateVote { + delegate_representative: Registered(Script("")), + }, + }, + DelegateCredential { + credential: Script("Not Found"), + delegate: DelegateBoth { + stake_pool: "", + delegate_representative: Registered(Script("")), + }, + }, + ] + + delegate_stake_and_vote_certificate( + certificates, + Script(""), + "", + Registered(Script("")), + ) == False +} + +test delegate_stake_and_vote_certificate_not_found_with_diff_drep() { + let certificates = + [ + DelegateCredential { + credential: Script(""), + delegate: DelegateBlockProduction { stake_pool: "" }, + }, + DelegateCredential { + credential: Script(""), + delegate: DelegateVote { + delegate_representative: Registered(Script("")), + }, + }, + DelegateCredential { + credential: Script(""), + delegate: DelegateBoth { + stake_pool: "", + delegate_representative: Registered(Script("Not Found")), + }, + }, + ] + + delegate_stake_and_vote_certificate( + certificates, + Script(""), + "", + Registered(Script("")), + ) == False +} + +test delegate_stake_and_vote_certificate_not_found_with_diff_pool() { + let certificates = + [ + DelegateCredential { + credential: Script(""), + delegate: DelegateBlockProduction { stake_pool: "" }, + }, + DelegateCredential { + credential: Script(""), + delegate: DelegateVote { + delegate_representative: Registered(Script("")), + }, + }, + DelegateCredential { + credential: Script(""), + delegate: DelegateBoth { + stake_pool: "Not Found", + delegate_representative: Registered(Script("")), + }, + }, + ] + + delegate_stake_and_vote_certificate( + certificates, + Script(""), + "", + Registered(Script("")), + ) == False +}