Skip to content
This repository was archived by the owner on Jan 24, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions examples/linking-contracts/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@
/.zos.session
/zos.dev-*
.zos*

.openzeppelin/.session
.openzeppelin/dev-*.json
27 changes: 19 additions & 8 deletions examples/linking-contracts/.openzeppelin/project.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
{
"manifestVersion": "2.2",
"contracts": ["TokenExchange"],
"name": "example-linking-contracts",
"version": "1.0.0",
"contracts": {
"TokenExchange": "TokenExchange"
},
"dependencies": {
"openzeppelin-eth": "2.2.0"
"@openzeppelin/contracts-ethereum-package": "^3.0.0"
},
"name": "example-linking-contracts",
"version": "2.8.2",
"compiler": {
"manager": "zos",
"compilerSettings": {
"optimizer": {}
"optimizer": {
"enabled": false,
"runs": "200"
}
},
"solcVersion": "0.5.9"
}
"typechain": {
"enabled": false
},
"manager": "openzeppelin",
"solcVersion": "0.6.9",
"artifactsDir": "build/contracts",
"contractsDir": "contracts"
},
"telemetryOptIn": true
}
71 changes: 39 additions & 32 deletions examples/linking-contracts/contracts/TokenExchange.sol
Original file line number Diff line number Diff line change
@@ -1,37 +1,44 @@
pragma solidity ^0.5.0;
pragma solidity ^0.6.2;

// Import base Initializable contract
import "@openzeppelin/upgrades/contracts/Initializable.sol";

// TODO: Update these import paths to @openzeppelin/contracts-ethereum-package once it is released
import "openzeppelin-eth/contracts/token/ERC20/IERC20.sol";
import "openzeppelin-eth/contracts/math/SafeMath.sol";
// Import the IERC20 interface and and SafeMath library
import "@openzeppelin/contracts-ethereum-package/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts-ethereum-package/contracts/math/SafeMath.sol";


contract TokenExchange is Initializable {
using SafeMath for uint256;

uint256 public rate;
IERC20 public token;

function initialize(uint256 _rate, IERC20 _token) public initializer {
rate = _rate;
token = _token;
}

function() external payable {
uint256 tokens = msg.value.mul(rate);
token.transfer(msg.sender, tokens);
}

// Added on subsequent version!
address public owner;

function withdraw() public {
require(msg.sender == owner);
msg.sender.transfer(address(this).balance);
}

function setOwner(address _owner) public {
require(owner == address(0));
owner = _owner;
}
}
using SafeMath for uint256;

// Contract state: exchange rate and token
uint256 public rate;
IERC20 public token;
address public owner;

// Initializer function (replaces constructor)
function initialize(uint256 _rate, IERC20 _token) public initializer {
rate = _rate;
token = _token;
}

// Send tokens back to the sender using predefined exchange rate
receive() external payable {
uint256 tokens = msg.value.mul(rate);
token.transfer(msg.sender, tokens);
}

function withdraw() public {
require(
msg.sender == owner,
"Address not allowed to call this function"
);
msg.sender.transfer(address(this).balance);
}

// To be run during upgrade, ensuring it can never be called again
function setOwner(address _owner) public {
require(owner == address(0), "Owner already set, cannot modify!");
owner = _owner;
}
}
10 changes: 5 additions & 5 deletions examples/linking-contracts/package.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
{
"name": "example-linking-contracts",
"version": "2.8.0-rc.0",
"version": "2.8.2",
"private": true,
"description": "",
"main": "index.js",
"keywords": [],
"author": "Santiago Palladino <santiago@openzeppelin.com>",
"license": "MIT",
"dependencies": {
"@openzeppelin/upgrades": "2.8.0-rc.0",
"openzeppelin-eth": "^2.2.0"
"@openzeppelin/contracts-ethereum-package": "^3.0.0",
"@openzeppelin/upgrades": "2.8.0"
},
"devDependencies": {
"@openzeppelin/cli": "2.8.0-rc.0",
"ganache-cli": "^6.4.3"
"@openzeppelin/cli": "2.8.2",
"ganache-cli": "^6.9.1"
}
}