Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ members = [
"examples/erc20-wrapper",
"examples/erc721",
"examples/erc721-consecutive",
"examples/erc721-holder",
"examples/erc721-metadata",
"examples/erc721-wrapper",
"examples/erc1155",
Expand Down Expand Up @@ -48,6 +49,7 @@ default-members = [
"examples/erc20-wrapper",
"examples/erc721",
"examples/erc721-consecutive",
"examples/erc721-holder",
"examples/erc721-metadata",
"examples/erc721-wrapper",
"examples/erc1155",
Expand Down
30 changes: 30 additions & 0 deletions examples/erc721-holder/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
[package]
name = "erc721-holder-example"
edition.workspace = true
license.workspace = true
repository.workspace = true
publish = false
version.workspace = true

[dependencies]
openzeppelin-stylus.workspace = true
alloy-primitives.workspace = true
alloy-sol-types.workspace = true
stylus-sdk.workspace = true

[dev-dependencies]
alloy.workspace = true
e2e.workspace = true
tokio.workspace = true
eyre.workspace = true

[lib]
crate-type = ["lib", "cdylib"]

[features]
e2e = []
export-abi = ["openzeppelin-stylus/export-abi"]

[[bin]]
name = "erc721-holder-example"
path = "src/main.rs"
42 changes: 42 additions & 0 deletions examples/erc721-holder/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#![cfg_attr(not(any(test, feature = "export-abi")), no_main)]
extern crate alloc;

use openzeppelin_stylus::token::erc721::{
receiver::IErc721Receiver, utils::Erc721Holder,
};
use stylus_sdk::{
abi::Bytes,
alloy_primitives::{aliases::B32, Address, U256},
prelude::*,
};

#[entrypoint]
#[storage]
struct Erc721HolderExample {
holder: Erc721Holder,
}

#[public]
#[implements(IErc721Receiver<Error = Vec<u8>>)]
impl Erc721HolderExample {
#[constructor]
pub fn constructor(&mut self) -> Result<(), Vec<u8>> {
Ok(())
}
}

#[public]
impl IErc721Receiver for Erc721HolderExample {
type Error = Vec<u8>;

#[selector(name = "onERC721Received")]
fn on_erc721_received(
&mut self,
operator: Address,
from: Address,
token_id: U256,
data: Bytes,
) -> Result<B32, Self::Error> {
self.holder.on_erc721_received(operator, from, token_id, data)
}
}
10 changes: 10 additions & 0 deletions examples/erc721-holder/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#![cfg_attr(not(any(test, feature = "export-abi")), no_main)]

#[cfg(not(any(test, feature = "export-abi")))]
#[no_mangle]
pub extern "C" fn main() {}

#[cfg(feature = "export-abi")]
fn main() {
erc721_holder_example::print_from_args();
}
14 changes: 14 additions & 0 deletions examples/erc721-holder/tests/abi/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#![allow(dead_code)]
use alloy::sol;

sol!(
#[sol(rpc)]
contract Erc721HolderExample {
function onERC721Received(
address operator,
address from,
uint256 tokenId,
bytes calldata data
) external returns (bytes4);
}
);
51 changes: 51 additions & 0 deletions examples/erc721-holder/tests/erc721-holder.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#![cfg(feature = "e2e")]

use abi::Erc721HolderExample;
use alloy::primitives::{Bytes, U256};
use e2e::{constructor, Account};
use eyre::Result;
use openzeppelin_stylus::token::erc721::RECEIVER_FN_SELECTOR;

mod abi;

#[e2e::test]
async fn returns_correct_selector(alice: Account) -> Result<()> {
let contract_addr = alice
.as_deployer()
.with_constructor(constructor!())
.deploy()
.await?
.contract_address;

let contract = Erc721HolderExample::new(contract_addr, &alice.wallet);

// call without data.
let interface_selector = contract
.onERC721Received(
alice.address(),
alice.address(),
U256::from(1),
Bytes::new(),
)
.call()
.await?
._0;

assert_eq!(RECEIVER_FN_SELECTOR, interface_selector);

// call with data.
let interface_selector = contract
.onERC721Received(
alice.address(),
alice.address(),
U256::from(42),
Bytes::from(vec![0xde, 0xad, 0xbe, 0xef]),
)
.call()
.await?
._0;

assert_eq!(RECEIVER_FN_SELECTOR, interface_selector);

Ok(())
}
Loading