1

I've readed some code of ethereum Token contracts, such as this one

In this code, it does 2 things for upgradable:

  • a contract interface or something like a "abstract contract" called "UpgradeAgent" which new contract needs to implement by derive from it.
  • a function called "upgrade" in contract which may need to upgrade in future.

here is the code of function "upgrade"

  function upgrade(uint256 value) public {
      UpgradeState state = getUpgradeState();
      if(!(state == UpgradeState.ReadyToUpgrade || state == UpgradeState.Upgrading)) {
        // Called in a bad state
        throw;
      }

      // Validate input value.
      if (value == 0) throw;

      balances[msg.sender] = safeSub(balances[msg.sender], value);

      // Take tokens out from circulation
      totalSupply = safeSub(totalSupply, value);
      totalUpgraded = safeAdd(totalUpgraded, value);

      // Upgrade agent reissues the tokens
      upgradeAgent.upgradeFrom(msg.sender, value);
      Upgrade(msg.sender, upgradeAgent, value);
  }

It's seems like : Sub caller's balance which stored in old contract, and then add them in new contract by call the NewContract's function "upgradeFrom".

Now I have a confuse with this design:

Can anyone call NewContract's function "upgradeFrom" directly?

  • If they can, will they add thier balance in NewContract while hold their balance in OldContract?
  • If they can't, why?

1 Answer 1

0

Function "upgradeFrom" have to check that it is called by OldToken address. You'll have to implement this in your future NewContract.

2
  • Thanks for your reply, but how to make sure if the caller is OldToken address in method 'upgradeFrom'? will the value of msg.sender be the address of OldToken?
    – xocoder
    Mar 12, 2018 at 6:45
  • While deploying NewContract you have to pass and store OldToken address in it. And then check it there like this: function upgradeFrom(address _from, uint256 _value) public { require(msg.sender == oldTokenAddress); /* ... and so on, then deliver new tokens */ }
    – Nickolay
    Mar 12, 2018 at 10:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.