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

Upgrade migration #44

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
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
2 changes: 1 addition & 1 deletion contracts/HumanStandardToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Machine-based, rapid creation of many tokens would not necessarily need these ex
3) Optional approveAndCall() functionality to notify a contract if an approval() has occurred.

.*/
pragma solidity ^0.4.4;
pragma solidity ^0.4.6;

import "./StandardToken.sol";

Expand Down
62 changes: 62 additions & 0 deletions contracts/HumanStandardTokenFactory.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import "./HumanStandardToken.sol";

pragma solidity ^0.4.6;

contract HumanStandardTokenFactory {

mapping(address => address[]) public created;
mapping(address => bool) public isHumanToken; //verify without having to do a bytecode check.
bytes public humanStandardByteCode;

function HumanStandardTokenFactory() {
//upon creation of the factory, deploy a HumanStandardToken (parameters are meaningless) and store the bytecode provably.
address verifiedToken = createHumanStandardToken(10000, "Verify Token", 3, "VTX");
humanStandardByteCode = codeAt(verifiedToken);
}

//verifies if a contract that has been deployed is a Human Standard Token.
//NOTE: This is a very expensive function, and should only be used in an eth_call. ~800k gas
function verifyHumanStandardToken(address _tokenContract) returns (bool) {
bytes memory fetchedTokenByteCode = codeAt(_tokenContract);

if (fetchedTokenByteCode.length != humanStandardByteCode.length) {
return false; //clear mismatch
}

//starting iterating through it if lengths match
for (uint i = 0; i < fetchedTokenByteCode.length; i ++) {
if (fetchedTokenByteCode[i] != humanStandardByteCode[i]) {
return false;
}
}

return true;
}

//for now, keeping this internal. Ideally there should also be a live version of this that any contract can use, lib-style.
//retrieves the bytecode at a specific address.
function codeAt(address _addr) internal returns (bytes o_code) {
assembly {
// retrieve the size of the code, this needs assembly
let size := extcodesize(_addr)
// allocate output byte array - this could also be done without assembly
// by using o_code = new bytes(size)
o_code := mload(0x40)
// new "memory end" including padding
mstore(0x40, add(o_code, and(add(add(size, 0x20), 0x1f), not(0x1f))))
// store length in memory
mstore(o_code, size)
// actually retrieve the code, this needs assembly
extcodecopy(_addr, add(o_code, 0x20), 0, size)
}
}

function createHumanStandardToken(uint256 _initialAmount, string _name, uint8 _decimals, string _symbol) returns (address) {

HumanStandardToken newToken = (new HumanStandardToken(_initialAmount, _name, _decimals, _symbol));
created[msg.sender].push(address(newToken));
isHumanToken[address(newToken)] = true;
newToken.transfer(msg.sender, _initialAmount); //the factory will own the created tokens. You must transfer them.
return address(newToken);
}
}
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.6;

contract Migrations {
address public owner;
uint public last_completed_migration;

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

function Migrations() {
owner = msg.sender;
}

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

function upgrade(address new_address) restricted {
Migrations upgraded = Migrations(new_address);
upgraded.setCompleted(last_completed_migration);
}
}
2 changes: 1 addition & 1 deletion contracts/StandardToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ If you deploy this, you won't have anything useful.
Implements ERC 20 Token standard: https://github.com/ethereum/EIPs/issues/20
.*/

pragma solidity ^0.4.4;
pragma solidity ^0.4.6;

import "./Token.sol";

Expand Down
2 changes: 1 addition & 1 deletion contracts/Token.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pragma solidity ^0.4.4;
pragma solidity ^0.4.6;

contract Token {

Expand Down
15 changes: 15 additions & 0 deletions contracts/TokenTester.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
pragma solidity ^0.4.6;

import "./HumanStandardTokenFactory.sol";

//commented out for now as Factory > 3m gas to deploy, causing this to OOG.
contract TokenTester {
address public tokenContractAddress;

/*function TokenTester() {
address factoryAddr = address(new HumanStandardTokenFactory());
HumanStandardTokenFactory tokenFactory = HumanStandardTokenFactory(factoryAddr);

tokenContractAddress = tokenFactory.createHumanStandardToken(10000, 'Simon Bucks', 1, 'SBX');
}*/
}
5 changes: 5 additions & 0 deletions migrations/1_initial_migration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
var Migrations = artifacts.require("./Migrations.sol");

module.exports = function(deployer) {
deployer.deploy(Migrations);
};
7 changes: 7 additions & 0 deletions migrations/2_deploy_contracts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
var HumanStandardTokenFactory = artifacts.require("./HumanStandardTokenFactory.sol");
var HumanStandardToken = artifacts.require("./HumanStandardToken.sol");

module.exports = function(deployer) {
deployer.deploy(HumanStandardTokenFactory);
deployer.deploy(HumanStandardToken);
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"reflux-tx": "0.2.4",
"sass-loader": "^3.1.2",
"style-loader": "^0.13.0",
"truffle-hdwallet-provider": "0.0.3",
"uport-lib": "^1.3.1",
"web3": "0.16.0",
"webpack": "^1.12.12"
Expand Down
Loading