Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Librarization #12

Closed
wants to merge 16 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
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: 0 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +0,0 @@
[submodule "lib/ds-test"]
path = lib/ds-test
url = https://github.com/dapphub/ds-test
2 changes: 0 additions & 2 deletions Makefile

This file was deleted.

74 changes: 74 additions & 0 deletions contracts/DSMath.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/// math.sol -- mixin for inline numerical wizardry

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

pragma solidity 0.4.24;

import "./DSMathL.sol";

contract DSMath {
using DSMathL for uint;
using DSMathL for int;

uint constant WAD = 10 ** 18;
uint constant RAY = 10 ** 27;

function add(uint x, uint y) public pure returns (uint z) {
return x.ds_add(y);
}

function sub(uint x, uint y) public pure returns (uint z) {
return x.ds_sub(y);
}

function mul(uint x, uint y) public pure returns (uint z) {
return x.ds_mul(y);
}

function min(uint x, uint y) public pure returns (uint z) {
return x.ds_min(y);
}

function max(uint x, uint y) public pure returns (uint z) {
return x.ds_max(y);
}

function imin(int x, int y) public pure returns (int z) {
return x.ds_imin(y);
}

function imax(int x, int y) public pure returns (int z) {
return x.ds_imax(y);
}

function wmul(uint x, uint y) public pure returns (uint) {
return x.ds_wmul(y);
}

function rmul(uint x, uint y) public pure returns (uint) {
return x.ds_rmul(y);
}

function wdiv(uint x, uint y) public pure returns (uint) {
return x.ds_wdiv(y);
}

function rdiv(uint x, uint y) public pure returns (uint) {
return x.ds_rdiv(y);
}

function rpow(uint x, uint n) public pure returns (uint) {
return x.ds_rpow(n);
}
}
40 changes: 20 additions & 20 deletions src/math.sol → contracts/DSMathL.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,46 +13,46 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

pragma solidity >0.4.13;
pragma solidity 0.4.24;

contract DSMath {
function add(uint x, uint y) internal pure returns (uint z) {
library DSMathL {
function ds_add(uint x, uint y) public pure returns (uint z) {
require((z = x + y) >= x, "ds-math-add-overflow");
}
function sub(uint x, uint y) internal pure returns (uint z) {
function ds_sub(uint x, uint y) public pure returns (uint z) {
require((z = x - y) <= x, "ds-math-sub-underflow");
}
function mul(uint x, uint y) internal pure returns (uint z) {
function ds_mul(uint x, uint y) public pure returns (uint z) {
require(y == 0 || (z = x * y) / y == x, "ds-math-mul-overflow");
}

function min(uint x, uint y) internal pure returns (uint z) {
function ds_min(uint x, uint y) public pure returns (uint z) {
return x <= y ? x : y;
}
function max(uint x, uint y) internal pure returns (uint z) {
function ds_max(uint x, uint y) public pure returns (uint z) {
return x >= y ? x : y;
}
function imin(int x, int y) internal pure returns (int z) {
function ds_imin(int x, int y) public pure returns (int z) {
return x <= y ? x : y;
}
function imax(int x, int y) internal pure returns (int z) {
function ds_imax(int x, int y) public pure returns (int z) {
return x >= y ? x : y;
}

uint constant WAD = 10 ** 18;
uint constant RAY = 10 ** 27;

function wmul(uint x, uint y) internal pure returns (uint z) {
z = add(mul(x, y), WAD / 2) / WAD;
function ds_wmul(uint x, uint y) public pure returns (uint z) {
z = ds_add(ds_mul(x, y), WAD / 2) / WAD;
}
function rmul(uint x, uint y) internal pure returns (uint z) {
z = add(mul(x, y), RAY / 2) / RAY;
function ds_rmul(uint x, uint y) public pure returns (uint z) {
z = ds_add(ds_mul(x, y), RAY / 2) / RAY;
}
function wdiv(uint x, uint y) internal pure returns (uint z) {
z = add(mul(x, WAD), y / 2) / y;
function ds_wdiv(uint x, uint y) public pure returns (uint z) {
z = ds_add(ds_mul(x, WAD), y / 2) / y;
}
function rdiv(uint x, uint y) internal pure returns (uint z) {
z = add(mul(x, RAY), y / 2) / y;
function ds_rdiv(uint x, uint y) public pure returns (uint z) {
z = ds_add(ds_mul(x, RAY), y / 2) / y;
}

// This famous algorithm is called "exponentiation by squaring"
Expand All @@ -70,14 +70,14 @@ contract DSMath {
// Also, EVM division is flooring and
// floor[(n-1) / 2] = floor[n / 2].
//
function rpow(uint x, uint n) internal pure returns (uint z) {
function ds_rpow(uint x, uint n) public pure returns (uint z) {
z = n % 2 != 0 ? x : RAY;

for (n /= 2; n != 0; n /= 2) {
x = rmul(x, x);
x = ds_rmul(x, x);

if (n % 2 != 0) {
z = rmul(z, x);
z = ds_rmul(z, x);
}
}
}
Expand Down
23 changes: 23 additions & 0 deletions contracts/Migrations.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
pragma solidity 0.4.24;

contract Migrations {
address public owner;
uint public last_completed_migration;

constructor() public {
owner = msg.sender;
}

modifier restricted() {
if (msg.sender == owner) _;
}

function setCompleted(uint completed) public restricted {
last_completed_migration = completed;
}

function upgrade(address new_address) public restricted {
Migrations upgraded = Migrations(new_address);
upgraded.setCompleted(last_completed_migration);
}
}
6 changes: 3 additions & 3 deletions src/math.t.sol → contracts/ds-test/DSMathTest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

pragma solidity >0.4.13;
pragma solidity 0.4.24;

import "ds-test/test.sol";
import "./math.sol";
import "./DSTest.sol";
import "../DSMath.sol";

contract DSMathTest is DSTest, DSMath {
function testFail_add() public pure {
Expand Down
139 changes: 139 additions & 0 deletions contracts/ds-test/DSTest.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

pragma solidity 0.4.24;

contract DSTest {
event eventListener (address target, bool exact);
event logs (bytes);
event log_bytes32 (bytes32);
event log_named_address (bytes32 key, address val);
event log_named_bytes32 (bytes32 key, bytes32 val);
event log_named_decimal_int (bytes32 key, int val, uint decimals);
event log_named_decimal_uint (bytes32 key, uint val, uint decimals);
event log_named_int (bytes32 key, int val);
event log_named_uint (bytes32 key, uint val);

bool public IS_TEST;
bool public failed;
bool SUPPRESS_SETUP_WARNING; // hack for solc pure restriction warning

constructor() internal {
IS_TEST = true;
}

function setUp() public {
SUPPRESS_SETUP_WARNING = true; // totally unused by anything
}

function fail() internal {
failed = true;
}

function expectEventsExact(address target) internal {
emit eventListener(target, true);
}

modifier logs_gas() {
uint startGas = gasleft();
_;
uint endGas = gasleft();
emit log_named_uint("gas", startGas - endGas);
}

function assertTrue(bool condition) internal {
if (!condition) {
emit log_bytes32("Assertion failed");
fail();
}
}

function assertEq(address a, address b) internal {
if (a != b) {
emit log_bytes32("Error: Wrong `address' value");
emit log_named_address(" Expected", b);
emit log_named_address(" Actual", a);
fail();
}
}

function assertEq32(bytes32 a, bytes32 b) internal {
assertEq(a, b);
}

function assertEq(bytes32 a, bytes32 b) internal {
if (a != b) {
emit log_bytes32("Error: Wrong `bytes32' value");
emit log_named_bytes32(" Expected", b);
emit log_named_bytes32(" Actual", a);
fail();
}
}

function assertEqDecimal(int a, int b, uint decimals) internal {
if (a != b) {
emit log_bytes32("Error: Wrong fixed-point decimal");
emit log_named_decimal_int(" Expected", b, decimals);
emit log_named_decimal_int(" Actual", a, decimals);
fail();
}
}

function assertEqDecimal(uint a, uint b, uint decimals) internal {
if (a != b) {
emit log_bytes32("Error: Wrong fixed-point decimal");
emit log_named_decimal_uint(" Expected", b, decimals);
emit log_named_decimal_uint(" Actual", a, decimals);
fail();
}
}

function assertEq(int a, int b) internal {
if (a != b) {
emit log_bytes32("Error: Wrong `int' value");
emit log_named_int(" Expected", b);
emit log_named_int(" Actual", a);
fail();
}
}

function assertEq(uint a, uint b) internal {
if (a != b) {
emit log_bytes32("Error: Wrong `uint' value");
emit log_named_uint(" Expected", b);
emit log_named_uint(" Actual", a);
fail();
}
}

function assertEq0(bytes memory a, bytes memory b) internal {
bool ok = true;

if (a.length == b.length) {
for (uint i = 0; i < a.length; i++) {
if (a[i] != b[i]) {
ok = false;
}
}
} else {
ok = false;
}

if (!ok) {
emit log_bytes32("Error: Wrong `bytes' value");
emit log_named_bytes32(" Expected", "[cannot show `bytes' value]");
emit log_named_bytes32(" Actual", "[cannot show `bytes' value]");
fail();
}
}
}
5 changes: 0 additions & 5 deletions default.nix

This file was deleted.

1 change: 0 additions & 1 deletion lib/ds-test
Submodule ds-test deleted from b24714
7 changes: 7 additions & 0 deletions migrations/1_initial_migration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
var Migrations = artifacts.require("./Migrations.sol");

module.exports = function(deployer) {
deployer.then(async () => { //fake async await support: https://github.com/trufflesuite/truffle/issues/501
await deployer.deploy(Migrations)
})
};
14 changes: 14 additions & 0 deletions migrations/2_initial_migration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
var DSMathL = artifacts.require("./DSMathL.sol");
var DSMath = artifacts.require("./DSMath.sol");
var DSMathTest = artifacts.require("./DSMathTest.sol");

module.exports = function(deployer) {
deployer.then(async () => { //fake async await support: https://github.com/trufflesuite/truffle/issues/501
//deploy library
await deployer.deploy(DSMathL)

//link library
await deployer.link(DSMathL, DSMath)
await deployer.link(DSMathL, DSMathTest)
})
}
Loading