Skip to content

Commit

Permalink
use new message for each call
Browse files Browse the repository at this point in the history
  • Loading branch information
Maar-io committed Apr 4, 2023
1 parent 780ec61 commit 046b146
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 70 deletions.
107 changes: 54 additions & 53 deletions crates/rmrk/src/batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ use crate::traits::{
NestingRef,
};

use ink::prelude::vec::Vec;
use ink::{
env::debug_println,
prelude::vec::Vec,
};
use openbrush::{
contracts::psp34::extensions::enumerable::*,
traits::AccountId,
Expand All @@ -24,80 +27,78 @@ pub const MAX_BATCH_TOKENS_PER_ASSET: usize = 50;
pub const MAX_BATCH_ADD_CHILDREN: usize = 50;
pub const MAX_BATCH_TOKEN_TRANSFERS: usize = 50;

fn nested_deep_result_unwrap_or_default<T: Default>(
res: Result<Result<Result<T, Error>, ink::LangError>, ink::env::Error>,
) -> T {
match res {
Ok(Ok(Ok(v))) => v,
_ => Default::default(),
}
}

#[openbrush::wrapper]
pub type BatchCallsRef = dyn BatchCalls;

#[openbrush::trait_definition]
pub trait BatchCalls {
/// Add a list of parent-child token pairs. The child NFT is from a different collection.
#[ink(message)]

fn add_many_children(
&mut self,
parent_contract: AccountId,
child_contract: AccountId,
parent_child_pair: Vec<(Id, Id)>,
) -> Result<(), RmrkError> {
ensure!(
parent_child_pair.len() <= MAX_BATCH_ADD_CHILDREN,
RmrkError::InputVectorTooBig
);
for (parent_id, child_id) in parent_child_pair {
_ = NestingRef::add_child_builder(&parent_contract, parent_id, (child_contract, child_id)).try_invoke();
}

Ok(())
fn add_single_asset_to_token(&mut self, contract: AccountId, token_id: Id, asset_id: AssetId) {
let _ = MultiAssetRef::add_asset_to_token_builder(&contract, token_id, asset_id, None)
.try_invoke();
}

/// Used to add an asset to a vector of tokens.
/// tokenId - ID of the token to add the asset to
/// assetId - ID of the asset to add to the token
#[ink(message)]
fn add_asset_to_many_tokens(
&mut self,
contract: AccountId,
tokens: Vec<Id>,
asset_id: AssetId,
) -> Result<(), RmrkError> {
ensure!(
tokens.len() <= MAX_BATCH_TOKENS_PER_ASSET,
RmrkError::InputVectorTooBig
);

) {
for token_id in tokens {
let _ = nested_deep_result_unwrap_or_default(
MultiAssetRef::add_asset_to_token_builder(&contract, token_id, asset_id, None).try_invoke(),
);

self.add_single_asset_to_token(contract, token_id, asset_id);
}
}

Ok(())
/// Add the child NFT.
#[ink(message)]
fn add_single_child(
&mut self,
parent_contract: AccountId,
parent_id: Id,
child_contract: AccountId,
child_id: Id,
) {
_ = NestingRef::add_child_builder(&parent_contract, parent_id, (child_contract, child_id))
.try_invoke();
}

/// Transfer many tokens to specified addresses
/// Add a list of parent-child token pairs. The child NFT is from a different collection.
#[ink(message)]
fn transfer_many(
fn add_many_children(
&mut self,
contract: AccountId,
token_to_destination: Vec<(Id, AccountId)>,
) -> Result<(), RmrkError> {
ensure!(
token_to_destination.len() <= MAX_BATCH_TOKEN_TRANSFERS,
RmrkError::InputVectorTooBig
);
for (token_id, destination) in token_to_destination {
_ = MintingRef::transfer_token_builder(&contract, destination, token_id, Vec::new()).try_invoke();
parent_contract: AccountId,
child_contract: AccountId,
parent_child_pair: Vec<(Id, Id)>,
) {
// -> Result<(), RmrkError> {
// ensure!(
// parent_child_pair.len() <= MAX_BATCH_ADD_CHILDREN,
// RmrkError::InputVectorTooBig
// );
for (parent_id, child_id) in parent_child_pair {
self.add_single_child(parent_contract, parent_id, child_contract, child_id);
}

Ok(())
// Ok(())
}

// Transfer many tokens to specified addresses
// #[ink(message)]
// fn transfer_many(
// &mut self,
// contract: AccountId,
// token_to_destination: Vec<(Id, AccountId)>,
// ) {
// -> Result<(), RmrkError> {
// ensure!(
// token_to_destination.len() <= MAX_BATCH_TOKEN_TRANSFERS,
// RmrkError::InputVectorTooBig
// );
// for (token_id, destination) in token_to_destination {
// _ = MintingRef::transfer_token_builder(&contract, destination, token_id, Vec::new()).try_invoke();
// }

// Ok(())
// }
}
41 changes: 24 additions & 17 deletions tests/batch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ describe("RMRK Nesting tests", () => {
});

it("deployer mints and transfers many works", async () => {
const PARENT_TOKENS = 50;
const CHILD_TOKENS = 50;
const PARENT_TOKENS = 1;
const CHILD_TOKENS = 1;
const ASSET_ID1 = 1;

// add part for catalog
Expand Down Expand Up @@ -125,13 +125,20 @@ describe("RMRK Nesting tests", () => {
for (let i = 1; i <= CHILD_TOKENS; i++) {
tokenList.push({ u64: i });
}
// const res = await child.query.getAssets(child.address, ASSET_ID1);
console.log("tokenList", child.address, tokenList, ASSET_ID1)
// try{
const re1 = await child
.withSigner(deployer)
.query.addAssetToManyTokens(child.address, tokenList, ASSET_ID1);
console.log("addAssetToManyTokens", re1.value.unwrap());
// expect(
// (await child.query.totalTokenAssets({ u64: 1 }))?.value.unwrap().ok.toString()
// ).to.be.equal("1,0");
console.log("addAssetToManyTokens", re1);
// } catch (e) {
// console.log("addAssetToManyTokens", e);
// }

expect(
(await child.query.totalTokenAssets({ u64: 1 }))?.value.unwrap().ok.toString()
).to.be.equal("1,0");

// // deployer approves parent's Contract on child
// await approve(child, parent, deployer);
Expand Down Expand Up @@ -187,17 +194,17 @@ const mintMany = async (contract: Rmrk, signer: KeyringPair, mintAmount: number)
}

// helper function to add many children to many parents
const addManyChildren = async (contract: Rmrk, signer: KeyringPair, child: Rmrk, parentChildPair: any): Promise<void> => {
let addResult = await contract
.withSigner(signer)
.tx.addManyChildren(
contract.address,
child.address,
parentChildPair,
);

// console.log("addResult", addResult.value.unwrap().ok);
}
// const addManyChildren = async (contract: Rmrk, signer: KeyringPair, child: Rmrk, parentChildPair: any): Promise<void> => {
// let addResult = await contract
// .withSigner(signer)
// .tx.addManyChildren(
// contract.address,
// child.address,
// parentChildPair,
// );

// // console.log("addResult", addResult.value.unwrap().ok);
// }

// helper function to approve a token
const approve = async (child: Rmrk, parent: Rmrk, signer: KeyringPair): Promise<SignAndSendSuccessResponse> => {
Expand Down

0 comments on commit 046b146

Please sign in to comment.