Skip to content

Commit

Permalink
Accout locker claim non fungibles by amount
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiupuhalschi-rdx committed Sep 4, 2024
1 parent 284c16b commit 7e25aa8
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 75 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/sargon/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "sargon"
version = "1.1.9"
version = "1.1.10"
edition = "2021"
build = "build.rs"

Expand Down
24 changes: 8 additions & 16 deletions crates/sargon/fixtures/transaction/account_locker_claim.rtm
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,14 @@ CALL_METHOD
;
CALL_METHOD
Address("locker_rdx1drn4q2zk6dvljehytnhfah330xk7emfznv59rqlps5ayy52d7xkzzz")
"claim_non_fungibles"
"claim"
Address("account_rdx128y6j78mt0aqv6372evz28hrxp8mn06ccddkr7xppc88hyvynvjdwr")
Address("resource_rdx1nfyg2f68jw7hfdlg5hzvd8ylsa7e0kjl68t5t62v3ttamtejc9wlxa")
Array<NonFungibleLocalId>(
NonFungibleLocalId("#1#")
)
Decimal("10")
;
TAKE_NON_FUNGIBLES_FROM_WORKTOP
TAKE_FROM_WORKTOP
Address("resource_rdx1nfyg2f68jw7hfdlg5hzvd8ylsa7e0kjl68t5t62v3ttamtejc9wlxa")
Array<NonFungibleLocalId>(
NonFungibleLocalId("#1#")
)
Decimal("10")
Bucket("bucket2")
;
CALL_METHOD
Expand All @@ -55,18 +51,14 @@ CALL_METHOD
;
CALL_METHOD
Address("locker_rdx1drn4q2zk6dvljehytnhfah330xk7emfznv59rqlps5ayy52d7xkzzz")
"claim_non_fungibles"
"claim"
Address("account_rdx128y6j78mt0aqv6372evz28hrxp8mn06ccddkr7xppc88hyvynvjdwr")
Address("resource_rdx1n2ekdd2m0jsxjt9wasmu3p49twy2yfalpaa6wf08md46sk8dfmldnd")
Array<NonFungibleLocalId>(
NonFungibleLocalId("<foobar>")
)
Decimal("1")
;
TAKE_NON_FUNGIBLES_FROM_WORKTOP
TAKE_FROM_WORKTOP
Address("resource_rdx1n2ekdd2m0jsxjt9wasmu3p49twy2yfalpaa6wf08md46sk8dfmldnd")
Array<NonFungibleLocalId>(
NonFungibleLocalId("<foobar>")
)
Decimal("1")
Bucket("bucket4")
;
CALL_METHOD
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
CALL_METHOD
Address("locker_rdx1drn4q2zk6dvljehytnhfah330xk7emfznv59rqlps5ayy52d7xkzzz")
"claim"
Address("account_rdx128y6j78mt0aqv6372evz28hrxp8mn06ccddkr7xppc88hyvynvjdwr")
Address("resource_rdx1n2ekdd2m0jsxjt9wasmu3p49twy2yfalpaa6wf08md46sk8dfmldnd")
Decimal("50")
;
TAKE_FROM_WORKTOP
Address("resource_rdx1n2ekdd2m0jsxjt9wasmu3p49twy2yfalpaa6wf08md46sk8dfmldnd")
Decimal("50")
Bucket("bucket1")
;
CALL_METHOD
Address("account_rdx128y6j78mt0aqv6372evz28hrxp8mn06ccddkr7xppc88hyvynvjdwr")
"deposit"
Bucket("bucket1")
;
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use radix_common::prelude::ManifestExpression;
use radix_engine_interface::blueprints::locker::{
ACCOUNT_LOCKER_CLAIM_IDENT, ACCOUNT_LOCKER_CLAIM_NON_FUNGIBLES_IDENT,
};
use std::cmp::min;

impl From<AccountAddress> for ScryptoComponentAddress {
fn from(value: AccountAddress) -> ScryptoComponentAddress {
Expand All @@ -23,73 +24,76 @@ impl TransactionManifest {
Self::build_claimable_batch(claimable_resources, 50);

for claimable in claimable_resources.iter() {
match claimable.clone() {
let (resource_arg, amount_arg) = match claimable.clone() {
AccountLockerClaimableResource::Fungible {
resource_address,
amount,
} => {
let resource_arg: ScryptoResourceAddress =
resource_address.into();
let amount_arg: ScryptoDecimal192 = amount.into();

builder = builder.call_method(
locker_address,
ACCOUNT_LOCKER_CLAIM_IDENT,
(claimant_arg, resource_arg, amount_arg),
);

let bucket = &bucket_factory.next();

builder = builder.take_from_worktop(
resource_arg,
amount_arg,
bucket,
);
builder = builder.deposit(claimant_arg, bucket);
(resource_arg, amount_arg)
}
AccountLockerClaimableResource::NonFungible {
resource_address,
ids,
count,
} => {
let resource_arg: ScryptoResourceAddress =
resource_address.into();
let ids_arg: Vec<ScryptoNonFungibleLocalId> =
ids.iter().map(|id| id.clone().into()).collect();

builder = builder.call_method(
locker_address,
ACCOUNT_LOCKER_CLAIM_NON_FUNGIBLES_IDENT,
(claimant_arg, resource_arg, ids_arg.clone()),
);
let amount_arg: ScryptoDecimal192 = count.into();
(resource_arg, amount_arg)
}
};

let bucket = &bucket_factory.next();
builder = builder.call_method(
locker_address,
ACCOUNT_LOCKER_CLAIM_IDENT,
(claimant_arg, resource_arg, amount_arg),
);

builder = builder.take_non_fungibles_from_worktop(
resource_arg,
ids_arg,
bucket,
);
let bucket = &bucket_factory.next();

builder = builder.deposit(claimant_arg, bucket);
}
}
builder =
builder.take_from_worktop(resource_arg, amount_arg, bucket);
builder = builder.deposit(claimant_arg, bucket);
}

TransactionManifest::sargon_built(builder, claimant.network_id())
}

fn build_claimable_batch(
claimable_resources: Vec<AccountLockerClaimableResource>,
size: usize,
size: u64,
) -> IndexSet<AccountLockerClaimableResource> {
let mut current_batch_size = 0;
claimable_resources
.into_iter()
.take_while(|claimable| {
current_batch_size += claimable.resource_count();
current_batch_size <= size
})
.collect()
let mut result = IndexSet::new();

for claimable in claimable_resources {
let resource_count = claimable.resource_count();
if current_batch_size + resource_count > size {
if let AccountLockerClaimableResource::NonFungible {
resource_address,
..
} = claimable
{
let remaining_size = size - current_batch_size;
if remaining_size > 0 {
result.insert(
AccountLockerClaimableResource::NonFungible {
resource_address,
count: remaining_size,
},
);
}
break;
}
} else {
current_batch_size += resource_count;
result.insert(claimable);
}
}

result
}
}

Expand Down Expand Up @@ -128,15 +132,15 @@ mod tests {
},
AccountLockerClaimableResource::NonFungible {
resource_address: "resource_rdx1nfyg2f68jw7hfdlg5hzvd8ylsa7e0kjl68t5t62v3ttamtejc9wlxa".into(),
ids: vec![NonFungibleLocalId::integer(1)],
count: 10,
},
AccountLockerClaimableResource::Fungible {
resource_address: "resource_rdx1tknxxxxxxxxxradxrdxxxxxxxxx009923554798xxxxxxxxxradxrd".into(),
amount: 1500.into(),
},
AccountLockerClaimableResource::NonFungible {
resource_address: "resource_rdx1n2ekdd2m0jsxjt9wasmu3p49twy2yfalpaa6wf08md46sk8dfmldnd".into(),
ids: vec![NonFungibleLocalId::string("foobar").unwrap()],
count: 1,
},
],
);
Expand All @@ -146,15 +150,21 @@ mod tests {

#[test]
fn claim_limited_to_required_batch_size() {
let expected_manifest = include_str!(concat!(
env!("FIXTURES_TX"),
"account_locker_claim_max_nft_items.rtm"
));
let manifest = SUT::account_locker_claim(
&"locker_rdx1drn4q2zk6dvljehytnhfah330xk7emfznv59rqlps5ayy52d7xkzzz".into(),
&"account_rdx128y6j78mt0aqv6372evz28hrxp8mn06ccddkr7xppc88hyvynvjdwr".into(),
(0..100).map(|i| AccountLockerClaimableResource::NonFungible {
resource_address: "resource_rdx1n2ekdd2m0jsxjt9wasmu3p49twy2yfalpaa6wf08md46sk8dfmldnd".into(),
ids: vec![NonFungibleLocalId::integer(i)]
}).collect(),
vec![
AccountLockerClaimableResource::NonFungible {
resource_address: "resource_rdx1n2ekdd2m0jsxjt9wasmu3p49twy2yfalpaa6wf08md46sk8dfmldnd".into(),
count: 100,
}
],
);

assert_eq!(manifest.instructions().len(), 150)
manifest_eq(manifest, expected_manifest)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ pub enum AccountLockerClaimableResource {
resource_address: ResourceAddress,
amount: Decimal192,
},
/// A non-fungible resource with specific claimable IDs
/// A non-fungible resource with a specific claimable item count
NonFungible {
resource_address: ResourceAddress,
ids: Vec<NonFungibleLocalId>,
count: u64,
},
}

impl AccountLockerClaimableResource {
pub fn resource_count(&self) -> usize {
pub fn resource_count(&self) -> u64 {
match self {
Self::Fungible { .. } => 1,
Self::NonFungible { ids, .. } => ids.len(),
Self::NonFungible { count, .. } => *count,
}
}
}
Expand All @@ -35,10 +35,7 @@ impl HasSampleValues for AccountLockerClaimableResource {
fn sample_other() -> Self {
Self::NonFungible {
resource_address: ResourceAddress::sample_other(),
ids: vec![
NonFungibleLocalId::sample(),
NonFungibleLocalId::sample_other(),
],
count: 2,
}
}
}
Expand Down

0 comments on commit 7e25aa8

Please sign in to comment.