Skip to content

Instantly share code, notes, and snippets.

@wissalHaji
Last active December 6, 2020 12:11
Show Gist options
  • Save wissalHaji/50af2ffc141fdf8ed6f598c1f516e3f1 to your computer and use it in GitHub Desktop.
Save wissalHaji/50af2ffc141fdf8ed6f598c1f516e3f1 to your computer and use it in GitHub Desktop.
Gist for the new post in medium : Interacting with other contracts from within a contract
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.4.22 <0.8.0;
contract OwnedToken {
TokenCreator creator;
address owner;
bytes32 public name;
constructor(bytes32 _name) {
owner = msg.sender;
creator = TokenCreator(msg.sender);
name = _name;
}
function changeName(bytes32 newName) public {
if (msg.sender == address(creator))
name = newName;
}
function transfer(address newOwner) public {
if (msg.sender != owner) return;
if (creator.isTokenTransferOK(owner, newOwner))
owner = newOwner;
}
}
contract TokenCreator {
event TokenCreated(bytes32 name, address tokenAddress);
function createToken(bytes32 name)
public
returns (OwnedToken tokenAddress)
{
tokenAddress = new OwnedToken(name);
emit TokenCreated(name, address(tokenAddress));
}
function changeName(OwnedToken tokenAddress, bytes32 name) public {
tokenAddress.changeName(name);
}
function isTokenTransferOK(address currentOwner, address newOwner)
public
pure
returns (bool ok)
{
return keccak256(abi.encodePacked(currentOwner, newOwner))[0] == 0x7f;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment