tondynasty-contracts icon indicating copy to clipboard operation
tondynasty-contracts copied to clipboard

Feature - Upgradable trait

Open bymoses opened this issue 8 months ago • 8 comments

Disclaimer: Hi guys, I'm relative new to TON ecosystem but AFAIK there's no standardized approach for upgradable contracts in Tact.

Motivation

Motivation for this issue is to discuss some approaches for making contract upgrades here because tondynasty is relatively popular and it will be easier to find this information for newcomers.

Solution

A couple weeks ago @wedvjin created proof of concept repo. Using his example and tondynasty's traits approach I've created a basic Upgradable trait:

@name(set_code)
native setCode(code: Cell);

@name(set_data)
native setData(d: Cell);

message UpgradeContract {
  code: Cell;
  data: Cell?;
}

trait Upgradable {
  owner: Address;

  // @dev  Upgrade
  receive(msg: UpgradeContract) {
    let ctx: Context = context();
    self._upgrade_validate(ctx, msg);
    self._upgrade(ctx, msg);
  }

  // @dev  _upgrade_validate conduct some custom validating before upgrade
  virtual inline fun _upgrade_validate(ctx: Context, msg: UpgradeContract) {
    require(ctx.sender == self.owner, "Upgradable: Sender is not a contract owner");
  }

  // @dev  _upgrade
  virtual inline fun _upgrade(ctx: Context, msg: UpgradeContract) {
    setCode(msg.code);
    if (msg.data != null) {
      setData(msg.data)
    }
  }
}

Limitations

https://github.com/wedvjin/ton-tact-contract-upgrade?tab=readme-ov-file#issue

@alan890104 @howardpen9 please take a look, maybe you have some different approaches for this

bymoses avatar Jun 02 '24 14:06 bymoses