Skip to content

Commit

Permalink
Add construction helpers for CIS2 contract.
Browse files Browse the repository at this point in the history
  • Loading branch information
abizjak committed Sep 26, 2023
1 parent d5d87bd commit 1462970
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 17 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
`make_revoke_credential_other` to the CIS4 client. These are like the methods
without the `make_` prefix, except that they only construct the transaction,
they do not send it.
- Add `make_transfer` and `make_update_operator` functions to the CIS2 client.
These are like the methods without the `make_`, except that they only
construct the transaction.

## 3.0.1

Expand Down
86 changes: 76 additions & 10 deletions src/cis2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::{contract_client::*, types as sdk_types, v2::IntoBlockIdentifier};
use concordium_base::{
base::Energy,
contracts_common::{Address, Amount},
transactions::{AccountTransaction, EncodedPayload},
};
use sdk_types::{smart_contracts, transactions};
use smart_contracts::concordium_contracts_common;
Expand Down Expand Up @@ -165,7 +166,7 @@ impl Cis2Contract {
self.transfer_dry_run(bi, sender, vec![transfer]).await
}

/// Construct and send a CIS2 transfer smart contract update transaction
/// Construct **and send** a CIS2 transfer smart contract update transaction
/// given a list of CIS2 transfers. Returns a Result with the
/// transaction hash.
///
Expand All @@ -181,11 +182,31 @@ impl Cis2Contract {
transaction_metadata: Cis2TransactionMetadata,
transfers: Vec<Transfer>,
) -> Result<sdk_types::hashes::TransactionHash, Cis2TransactionError> {
let transfer = self.make_transfer(signer, transaction_metadata, transfers)?;
let hash = self.client.send_account_transaction(transfer).await?;
Ok(hash)
}

/// Construct a CIS2 transfer smart contract update transaction
/// given a list of CIS2 transfers. Returns a Result with the
/// transaction hash.
///
/// # Arguments
///
/// * `signer` - The account keys to use for signing the smart contract
/// update transaction.
/// * `transaction_metadata` - Metadata for constructing the transaction.
/// * `transfers` - A list of CIS2 token transfers to execute.
pub fn make_transfer(
&self,
signer: &impl transactions::ExactSizeTransactionSigner,
transaction_metadata: Cis2TransactionMetadata,
transfers: Vec<Transfer>,
) -> Result<AccountTransaction<EncodedPayload>, Cis2TransactionError> {
let parameter = TransferParams::new(transfers)?;
let message = smart_contracts::OwnedParameter::from_serial(&parameter)
.map_err(|_| Cis2TransactionError::InvalidTransferParams(NewTransferParamsError))?;
self.update_raw(signer, &transaction_metadata, "transfer", message)
.await
self.make_update_raw(signer, &transaction_metadata, "transfer", message)
}

/// Like [`transfer`](Self::transfer), except it is more ergonomic
Expand All @@ -200,6 +221,17 @@ impl Cis2Contract {
.await
}

/// Like [`make_transfer`](Self::make_transfer), except it is more ergonomic
/// when transferring a single token.
pub fn make_transfer_single(
&self,
signer: &impl transactions::ExactSizeTransactionSigner,
transaction_metadata: Cis2TransactionMetadata,
transfer: Transfer,
) -> Result<AccountTransaction<EncodedPayload>, Cis2TransactionError> {
self.make_transfer(signer, transaction_metadata, vec![transfer])
}

/// Dry run a CIS2 updateOperator transaction. This is analogous to
/// [`update_operator`](Self::update_operator), except that it does not send
/// a transaction to the chain, and just simulates the transaction.
Expand Down Expand Up @@ -247,8 +279,7 @@ impl Cis2Contract {
.await
}

/// Send a CIS2 updateOperator transaction.
/// Construct and send a CIS2 updateOperator smart contract update
/// Construct a CIS2 updateOperator smart contract update
/// transaction given a list of CIS2 UpdateOperators. Returns a Result
/// with the transaction hash.
///
Expand All @@ -258,18 +289,38 @@ impl Cis2Contract {
/// update transaction.
/// * `transaction_metadata` - Metadata for constructing the transaction.
/// * `updates` - A list of CIS2 UpdateOperators to update.
pub async fn update_operator(
&mut self,
pub fn make_update_operator(
&self,
signer: &impl transactions::ExactSizeTransactionSigner,
transaction_metadata: Cis2TransactionMetadata,
updates: Vec<UpdateOperator>,
) -> anyhow::Result<sdk_types::hashes::TransactionHash, Cis2TransactionError> {
) -> anyhow::Result<AccountTransaction<EncodedPayload>, Cis2TransactionError> {
let parameter = UpdateOperatorParams::new(updates)?;
let message = smart_contracts::OwnedParameter::from_serial(&parameter).map_err(|_| {
Cis2TransactionError::InvalidUpdateOperatorParams(NewUpdateOperatorParamsError)
})?;
self.update_raw(signer, &transaction_metadata, "updateOperator", message)
.await
self.make_update_raw(signer, &transaction_metadata, "updateOperator", message)
}

/// Construct **and send** a CIS2 updateOperator smart contract update
/// transaction given a list of CIS2 UpdateOperators. Returns a Result
/// with the transaction hash.
///
/// # Arguments
///
/// * `signer` - The account keys to use for signing the smart contract
/// update transaction.
/// * `transaction_metadata` - Metadata for constructing the transaction.
/// * `updates` - A list of CIS2 UpdateOperators to update.
pub async fn update_operator(
&mut self,
signer: &impl transactions::ExactSizeTransactionSigner,
transaction_metadata: Cis2TransactionMetadata,
updates: Vec<UpdateOperator>,
) -> anyhow::Result<sdk_types::hashes::TransactionHash, Cis2TransactionError> {
let update = self.make_update_operator(signer, transaction_metadata, updates)?;
let hash = self.client.send_account_transaction(update).await?;
Ok(hash)
}

/// Like [`update_operator`](Self::update_operator), but more ergonomic
Expand All @@ -288,6 +339,21 @@ impl Cis2Contract {
.await
}

/// Like [`make_update_operator`](Self::make_update_operator), but more
/// ergonomic when updating a single operator.
pub fn make_update_operator_single(
&self,
signer: &impl transactions::ExactSizeTransactionSigner,
transaction_metadata: Cis2TransactionMetadata,
operator: Address,
update: OperatorUpdate,
) -> anyhow::Result<AccountTransaction<EncodedPayload>, Cis2TransactionError> {
self.make_update_operator(signer, transaction_metadata, vec![UpdateOperator {
update,
operator,
}])
}

/// Invoke the CIS2 balanceOf query given a list of BalanceOfQuery.
///
/// Note: the query is executed locally by the node and does not produce a
Expand Down
8 changes: 4 additions & 4 deletions src/cis4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ impl Cis4Contract {
/// Construct a transaction for registering a new credential.
/// Note that this **does not** send the transaction.c
pub fn make_register_credential(
&mut self,
&self,
signer: &impl transactions::ExactSizeTransactionSigner,
metadata: &Cis4TransactionMetadata,
cred_info: &CredentialInfo,
Expand Down Expand Up @@ -189,7 +189,7 @@ impl Cis4Contract {

/// Construct a transaction to revoke a credential as an issuer.
pub fn make_revoke_credential_as_issuer(
&mut self,
&self,
signer: &impl transactions::ExactSizeTransactionSigner,
metadata: &Cis4TransactionMetadata,
cred_id: CredentialHolderId,
Expand Down Expand Up @@ -238,7 +238,7 @@ impl Cis4Contract {
/// contract. The signature on this revocation message is set to expire at
/// the same time as the transaction.
pub fn make_revoke_credential_as_holder(
&mut self,
&self,
signer: &impl transactions::ExactSizeTransactionSigner,
metadata: &Cis4TransactionMetadata,
web3signer: impl Web3IdSigner, // the holder
Expand Down Expand Up @@ -304,7 +304,7 @@ impl Cis4Contract {
/// The signature on this revocation message is set to expire at
/// the same time as the transaction.
pub fn make_revoke_credential_other(
&mut self,
&self,
signer: &impl transactions::ExactSizeTransactionSigner,
metadata: &Cis4TransactionMetadata,
revoker: impl Web3IdSigner, // the revoker.
Expand Down
4 changes: 2 additions & 2 deletions src/contract_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ impl<Type> ContractClient<Type> {

/// Make the payload of a contract update with the specified parameter.
pub fn make_update<P: contracts_common::Serial, E>(
&mut self,
&self,
signer: &impl transactions::ExactSizeTransactionSigner,
metadata: &ContractTransactionMetadata,
entrypoint: &str,
Expand Down Expand Up @@ -246,7 +246,7 @@ impl<Type> ContractClient<Type> {
/// Like [`make_update`](Self::make_update) but expects a serialized
/// parameter.
pub fn make_update_raw<E>(
&mut self,
&self,
signer: &impl transactions::ExactSizeTransactionSigner,
metadata: &ContractTransactionMetadata,
entrypoint: &str,
Expand Down

0 comments on commit 1462970

Please sign in to comment.