Skip to content

Commit

Permalink
cli: support invoicing with fractional token amounts
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-orlovsky committed Sep 22, 2024
1 parent 955c52f commit 94e48de
Showing 1 changed file with 42 additions and 16 deletions.
58 changes: 42 additions & 16 deletions cli/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ use rgb::validation::Validity;
use rgb::vm::{RgbIsa, WitnessOrd};
use rgb::{
Allocation, BundleId, ContractId, DescriptorRgb, GenesisSeal, GraphSeal, Identity, OpId,
OutputSeal, RgbDescr, RgbKeychain, RgbWallet, StateType, TokenIndex, TransferParams,
WalletError, WalletProvider, XChain, XOutpoint, XWitnessId,
OutputSeal, OwnedFraction, RgbDescr, RgbKeychain, RgbWallet, StateType, TokenIndex,
TransferParams, WalletError, WalletProvider, XChain, XOutpoint, XWitnessId,
};
use rgbstd::interface::{AllocatedState, ContractIface, OwnedIface};
use rgbstd::persistence::{MemContractState, StockError};
Expand Down Expand Up @@ -189,8 +189,17 @@ pub enum Command {
/// Contract identifier
contract_id: ContractId,

/// Value (for fungible token) or token ID (for NFT) to transfer
value: Option<u64>,
/// Amount of tokens (in the smallest unit) to transfer
#[arg(short, long)]
amount: Option<u64>,

/// Token index for NFT transfer
#[arg(long)]
token_index: Option<TokenIndex>,

/// Fraction of an NFT token to transfer
#[arg(long, requires = "token_index")]
token_fraction: Option<OwnedFraction>,
},

/// Prepare PSBT file for transferring RGB assets
Expand Down Expand Up @@ -800,7 +809,9 @@ impl Exec for RgbArgs {
state,

Check warning on line 809 in cli/src/command.rs

View check run for this annotation

Codecov / codecov/patch

cli/src/command.rs#L808-L809

Added lines #L808 - L809 were not covered by tests
contract_id,
iface,
value,
amount,
token_index,
token_fraction,

Check warning on line 814 in cli/src/command.rs

View check run for this annotation

Codecov / codecov/patch

cli/src/command.rs#L812-L814

Added lines #L812 - L814 were not covered by tests
} => {
let mut wallet = self.rgb_wallet(&config)?;

Expand Down Expand Up @@ -892,26 +903,42 @@ impl Exec for RgbArgs {
}
}

Check warning on line 904 in cli/src/command.rs

View check run for this annotation

Codecov / codecov/patch

cli/src/command.rs#L897-L904

Added lines #L897 - L904 were not covered by tests

match (assign_iface.owned_state, value) {
match (assign_iface.owned_state, amount, token_index.map(|i| (i, token_fraction))) {

Check warning on line 906 in cli/src/command.rs

View check run for this annotation

Codecov / codecov/patch

cli/src/command.rs#L906

Added line #L906 was not covered by tests
(
OwnedIface::Rights
| OwnedIface::Amount
| OwnedIface::AnyData
| OwnedIface::Data(_),
None,
None,
) => {
// There is no state which has to be added to the invoice
}

Check warning on line 916 in cli/src/command.rs

View check run for this annotation

Codecov / codecov/patch

cli/src/command.rs#L914-L916

Added lines #L914 - L916 were not covered by tests
(OwnedIface::Rights, Some(_)) => {
(OwnedIface::Rights, Some(_), None | Some(_))
| (OwnedIface::Rights, None, Some(_)) => {
return Err(WalletError::Invoicing(format!(
"state {state_name} in interface {iface_name} defines a right and it \
can't has a value"
can't has a value or a token information"
)));

Check warning on line 922 in cli/src/command.rs

View check run for this annotation

Codecov / codecov/patch

cli/src/command.rs#L919-L922

Added lines #L919 - L922 were not covered by tests
}
(OwnedIface::Amount, Some(amount)) => {
(OwnedIface::Amount, _, Some(_)) => {
return Err(WalletError::Invoicing(format!(
"state {state_name} in interface {iface_name} defines a fungible \
state, while a non-fungible token index is provided for the invoice. \
Please use only --amount argument"
)));

Check warning on line 929 in cli/src/command.rs

View check run for this annotation

Codecov / codecov/patch

cli/src/command.rs#L925-L929

Added lines #L925 - L929 were not covered by tests
}
(OwnedIface::Amount, Some(amount), None) => {
builder = builder.set_amount_raw(*amount);
}

Check warning on line 933 in cli/src/command.rs

View check run for this annotation

Codecov / codecov/patch

cli/src/command.rs#L931-L933

Added lines #L931 - L933 were not covered by tests
(OwnedIface::Data(sem_id), Some(_))
(OwnedIface::Data(_) | OwnedIface::AnyData, Some(_), _) => {
return Err(WalletError::Invoicing(format!(
"state {state_name} in interface {iface_name} defines a non-fungible \
state, while a fungible amount is provided for the invoice. Please \
use only --token-index and --token-fraction arguments"
)));

Check warning on line 939 in cli/src/command.rs

View check run for this annotation

Codecov / codecov/patch

cli/src/command.rs#L935-L939

Added lines #L935 - L939 were not covered by tests
}
(OwnedIface::Data(sem_id), None, Some(_))
if sem_id
!= rgb_contract_stl()
.types
Expand All @@ -924,21 +951,20 @@ impl Exec for RgbArgs {
be used with a non-fungible state allocation"
)));

Check warning on line 952 in cli/src/command.rs

View check run for this annotation

Codecov / codecov/patch

cli/src/command.rs#L941-L952

Added lines #L941 - L952 were not covered by tests
}
(OwnedIface::AnyData | OwnedIface::Data(_), Some(value)) => {
(OwnedIface::AnyData | OwnedIface::Data(_), None, Some((index, fraction))) => {
builder = builder.set_allocation_raw(Allocation::with(
TokenIndex::from(*value as u32),
// TODO: Support fractional NFT invoicing
0,
index,
fraction.unwrap_or(OwnedFraction::from(0)),
))

Check warning on line 958 in cli/src/command.rs

View check run for this annotation

Codecov / codecov/patch

cli/src/command.rs#L954-L958

Added lines #L954 - L958 were not covered by tests
}

(OwnedIface::Any, _) => {
(OwnedIface::Any, _, _) => {
return Err(WalletError::Invoicing(format!(
"state {state_name} in interface {iface_name} can be of any type; \
adding it to the invoice is impossible"
)));

Check warning on line 965 in cli/src/command.rs

View check run for this annotation

Codecov / codecov/patch

cli/src/command.rs#L962-L965

Added lines #L962 - L965 were not covered by tests
}
(OwnedIface::AnyAttach, _) => {
(OwnedIface::AnyAttach, _, _) => {
return Err(WalletError::Invoicing(s!(
"invoicing with attachments is not yet supported"
)));

Check warning on line 970 in cli/src/command.rs

View check run for this annotation

Codecov / codecov/patch

cli/src/command.rs#L968-L970

Added lines #L968 - L970 were not covered by tests
Expand Down

0 comments on commit 94e48de

Please sign in to comment.