0

What is the standard way of making the link between our CoreContract and the ERC20Token contract used by the Core Contract? Are people used to use inheritance like: MyCoreContract is MyERC20Contract { ... } or do they deploy an instance of the ERC20 token within the core contract like: MyCoreContract { var ERC20token = new(MyERC20token) } . Or maybe another alternative is better ?

Best Regards

  • The answer to you question depends on your purpose, and more generally, on your system architecture. – goodvibration May 8 at 10:40
0

First of all, inheritance does not link contracts together. It produces single contract that has logic from both, base and inherited contracts. For actual linking, there are several common ways:

First way: hardcoded addresses

contract A {
  ...
}

contract B {
  A a = A (/* address of deployed contract A here */);
}

This way is the cheapest in terms of gas, but does not allow using the same bytecode of contract B in different networks, say in devnet, testnet, and mainnet.

Second way: constructor parameter

contract A {
  ...
}

contract B {
  A a;

  constructor (A _a) public {
    a = _a;
  }
}

Third way: deploy in constructor

This way bi-directional link may easily be established.

contract A {
  B b;

  constructor () public {
    b = B (msg.sender);
  }
}

contract B {
  A a;

  constructor () public {
    a = new A ();
  }
}

Fourth way: service registry

This way allows deploying smart contracts in any order and even modifying link after deployment.

contract A {
  ServiceRegistry serviceRegistry;

  constructor (ServiceRegistry _serviceRegistry) public {
    serviceRegistry = _serviceRegistry;
    serviceRegistry.register ("A", address (this));
  }

  function getB () internal view returns (B) {
    return B (serviceRegistry.get ("B"));
  }
}

contract B {
  ServiceRegistry serviceRegistry;

  constructor (ServiceRegistry _serviceRegistry) public {
    serviceRegistry = _serviceRegistry;
    serviceRegistry.register ("B", address (this));
  }

  function getA () internal view returns (A) {
    return A (serviceRegistry.get ("A"));
  }
}

etc.

  • Ok thanks for your reply! I think inheritance will be the best option for my use case. As you said it will produce a single contract but as long as the ERC20 interface is available I don't see any drawbacks using inheritance and not having two separate contracts. Best regards – Kevin Wad May 8 at 11:14
  • Whether there will be any drawback or not depends on what your core contract does. – Mikhail Vladimirov May 8 at 11:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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