Examples

Examples

Here are some examples of how to integrate with SuiNS on-chain.

Transferring any object to a SuiNS name fully on-chain

The following demo module demonstrates how to transfer an object of any type to a SuiNS name. This is a basic example that demonstrates how to interact with SuiNS on-chain.

module demo::demo {
    use std::string::String;
    use sui::clock::Clock;
 
    /// Our SuiNS dependency is imported here.
    use suins::{ 
        suins::SuiNS,
        registry::Registry,
        domain
    };
 
    /// Different custom error messages
    const ENameNotFound: u64 = 0;
    const ENameNotPointingToAddress: u64 = 1;
    const ENameExpired: u64 = 2;
 
    /// A function to transfer an object of any type T to a name (e.g `example.sui`)
    public fun send_to_name<T: key + store>(suins: &SuiNS, obj: T, name: String, clock: &Clock) {
        // Lookup the name on the registry
        let mut optional = suins.registry<Registry>().lookup(domain::new(name));
        // Check that the name indeed exists
        assert!(optional.is_some(), ENameNotFound);
 
        let name_record = optional.extract();
        // Check that name has not expired. 
        // We could skip that, but it's recommended we do indeed check for this.
        assert!(name_record.has_expired(clock), ENameExpired);
        // Check that the name has a target address set.
        assert!(name_record.target_address().is_some(), ENameNotPointingToAddress);
 
        // Transfer the object to that name!
        transfer::public_transfer(obj, name_record.target_address().extract())
    }
}