Skip to content

royalty_info hardcodes Base::, so it reverts for 9 of every 10 tokens under ContractType = Consecutive #845

Closed

Description

@yigitcangokmen

What happens

NonFungibleRoyalties::royalty_info calls Base::royalty_info directly instead of dispatching through Self::ContractType:

https://github.com/OpenZeppelin/stellar-contracts/blob/main/packages/tokens/src/non_fungible/extensions/royalties/mod.rs#L144-L146

fn royalty_info(e: &Env, token_id: u32, sale_price: i128) -> (Address, i128) {
    Base::royalty_info(e, token_id, sale_price)
}

Base::royalty_info establishes existence with Base::owner_of:

pub fn royalty_info(e: &Env, token_id: u32, sale_price: i128) -> (Address, i128) {
    // Verify token exists by checking owner
    let _ = Base::owner_of(e, token_id);

Under Consecutive, ownership is stored sparsely: only the batch boundary token gets a materialised Owner entry, and Consecutive::owner_of walks back through the bucket to resolve the rest. Base::owner_of does a direct lookup, so it panics #200 NonExistentToken for every token that is not a boundary.

The result is that a contract using Consecutive together with the royalties extension has a royalty_info that reverts for almost every token it owns.

This deviates from the library's own documented convention

packages/tokens/src/non_fungible/overrides.rs#L21 states that default bodies should call Self::ContractType::{function_name}, and every sibling follows it:

// non_fungible/mod.rs
fn balance(e: &Env, account: Address) -> u32     { Self::ContractType::balance(e, &account) }
fn owner_of(e: &Env, token_id: u32) -> Address   { Self::ContractType::owner_of(e, token_id) }
fn transfer(e: &Env, from: Address, ...)         { Self::ContractType::transfer(e, &from, &to, token_id); }

royalty_info is the exception. It looks like it was missed when the default bodies were added in #604.

Reproduction

Two contracts wired identically, differing only in ContractType. Full source at the bottom.

CONTROL  ContractType = Base        : 10/10 token ids answer royalty_info
SUBJECT  ContractType = Consecutive : 10/10 token ids answer owner_of
SUBJECT  ContractType = Consecutive :  1/10 token ids answer royalty_info
         failing token ids: [0, 1, 2, 3, 4, 5, 6, 7, 8]

The middle line is the important control. Consecutive::owner_of resolves all ten tokens correctly, so the tokens exist and the extension is working as designed. Only royalty_info fails, and it fails because of how it dispatches, not because of anything wrong with the token.

Only token 9 succeeds, because 9 is the batch boundary and therefore the one id with a materialised Owner entry.

Verified against tag v0.7.2 and against main (the line is unchanged on main).

The fix, and the design question in it

The straightforward fix routes the existence check through Self::ContractType::owner_of, which is how every other default body in this library dispatches, and moves the lookup into a Base::royalty_info_unchecked so Base::royalty_info behaves exactly as before.

That adds public API to a published crate, so it is a real decision rather than a mechanical change. The alternative is to declare royalty_info on ContractOverrides with the Base behaviour as the default and override it in Consecutive, which grows the trait instead of the Base surface. I have a working patch for the first shape and will open it as a PR against this issue, but I do not have a strong view and would rather you chose.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

Relationships

None yet

Development

No branches or pull requests

Issue actions