I am currently trying to setup a project where I intend to have multiple contracts call on each other. Since I am using the openbrush standard (mostly to implement an NFT using PSP34) I thought it would make sense to adhere to the recommended project structure by the openbrush team, since it also makes it easier to cross contract call, compared to what I have seen before when only ink! is used.
However, I now have some problems actually implementing it. For starters (and mostly as an example), I just wanted to implement a simple contract which prints an NFT when deployed and allows the caller to print new ones, if the caller already owns an NFT. So you would need to be "invited" by someone who already has an NFT.
I looked at the example-project-structure which is provided on github and tried to model my approach after that. However, I do not understand a few things, especially in the 'impls' folder, which is supposed to contain the actual implementations of the contract methods.
I get bombarded with a lot of Error messages, some of them I could fix, but some of them I could not figure out the problem.
Here is the code:
pub use crate::traits::nft::*;
use ink_prelude::{
vec::Vec
};
use openbrush::{
contracts::traits::psp34::{
PSP34Ref,
Id
},
traits::{
AccountId,
}
};
impl <T> Buy for T {
default fn _emit_nft_bought(&mut self, _owner: AccountId, _id: Vec<u8>){}
default fn buy(
&mut self,
to: AccountId,
id: Vec<u8>
) -> Result<(), BuyError> {
if id_exists(&self){
PSP34Ref::_mint_to(to, Id::Bytes(id.clone()))?;
self._emit_nft_bought(to, id);
Ok(())
}else{
return Err(BuyError::NoBalance)
}
}
}
/// this function just serves as an example
fn id_exists <T> (instance: &T) -> bool{
if PSP34Ref::balance_of(&instance, Self::env().caller()) > 0{
true
}else{
false
}
}
And here the errors:
error[E0433]: failed to resolve: use of undeclared type `Self` --> contracts/impls/nft/buy.rs:43:40 | 43 | if PSP34Ref::balance_of(&instance, Self::env().caller()) > 0{ | ^^^^ use of undeclared type `Self` error[E0599]: no associated item named `_mint_to` found for trait object `(dyn PSP34Wrapper + 'static)` in the current scope --> contracts/impls/nft/buy.rs:29:23 | 29 | PSP34Ref::_mint_to(to, Id::Bytes(id.clone()))?; | ^^^^^^^^ associated item not found in `(dyn PSP34Wrapper + 'static)` error[E0277]: the trait bound `&T: PSP34Wrapper` is not satisfied --> contracts/impls/nft/buy.rs:43:29 | 43 | if PSP34Ref::balance_of(&instance, Self::env().caller()) > 0{ | ^^^^^^^^^ the trait `PSP34Wrapper` is not implemented for `&T` | = help: the trait `PSP34Wrapper` is implemented for `<DefaultEnvironment as Environment>::AccountId` = note: required for the cast to the object type `dyn PSP34Wrapper`
In the example provided by the team behind openbrush, they are using Self::env().caller(), so I don't really understand why this is not accepted here. The other two errors I am a bit lost, where the origin of the error lies and what is necessary to fix them.