Derive Macro stellar_axelar_std_derive::Upgradable

source ·
#[derive(Upgradable)]
{
    // Attributes available to this derive:
    #[migratable]
}
Expand description

Implements the Upgradable and Migratable interfaces for a Soroban contract.

A ContractError error type must be defined in scope, and have a MigrationNotAllowed variant. A default migration implementation is automatically provided. If custom migration code is required, the #[migratable] attribute can be applied to the contract struct. In that case, the contract must implement the CustomMigratableInterface trait. The associated Error type must implement the Into<ContractError> trait. The ContractError type itself implements it implicitly, so that is an easy way to use it.

§Example

use stellar_axelar_std_derive::{Ownable, Upgradable};

#[contract]
#[derive(Ownable, Upgradable)]
#[migratable]
pub struct Contract;

#[contractimpl]
impl Contract {
    pub fn __constructor(env: &Env, owner: Address) {
        stellar_axelar_std::interfaces::set_owner(env, &owner);
    }
}

impl CustomMigratableInterface for Contract {
    type MigrationData = Address;
    type Error = ContractError;

    fn __migrate(env: &Env, new_owner: Self::MigrationData) -> Result<(), Self::Error> {
        Self::transfer_ownership(env, new_owner);
        Ok(())
    }
}