Created
October 13, 2018 14:13
-
-
Save nourharidy/93e778a82b28cdd4fd8793728a17bc97 to your computer and use it in GitHub Desktop.
Parasol code example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.4.24; | |
{{ strict = true }} // Activate strict mode. This will abort deployment on both errors and warnings. | |
/// @title {{= title }} | |
contract Token { | |
// Variables retrieved from parasol.js config file pre compilation | |
string private _name = "{{= tokenName}}"; | |
string private _symbol = "{{= tokenSymbol}}"; | |
uint8 private _decimals = {{= tokenDecimals}}; | |
uint256 private _totalSupply = {{= tokenSupply}}; | |
mapping (address => uint256) public _balances; | |
event Transfer(address indexed _from, address indexed _to, uint256 _value); | |
constructor() public { | |
_balances[msg.sender] = _totalSupply; | |
} | |
/** | |
* @dev Transfer token for a specified addresses | |
* @param _to The address to transfer to. | |
* @param _value The amount to be transferred. | |
*/ | |
function transfer(address _to, uint256 _value) public returns (bool success) { | |
require(_balances[msg.sender] >= _value); | |
_balances[msg.sender] -= _value; | |
_balances[_to] += _value; | |
emit Transfer(msg.sender, _to, _value); | |
return true; | |
} | |
{{ | |
// inline mocha unit test | |
test(this, 'Balance should change balance after transfer()', async function() { | |
var preBalance = await contracts["Token.psol:Token"].methods.balanceOf(accounts[0]).call() | |
await contracts["Token.psol:Token"].methods.transfer(accounts[1], 1).send() | |
var postBalance = await contracts["Token.psol:Token"].methods.balanceOf(accounts[0]).call() | |
assert.notEqual(preBalance, postBalance) | |
}) | |
}} | |
/// @author Nour Haridy | |
/// @dev Checks balance of address | |
/// @param _owner The queried address | |
/// @return balance of address | |
function balanceOf(address _owner) constant public returns (uint256 balance) { | |
return _balances[_owner]; | |
} | |
/** | |
* @return the name of the token. | |
*/ | |
function name() public view returns(string) { | |
return _name; | |
} | |
/** | |
* @return the symbol of the token. | |
*/ | |
function symbol() public view returns(string) { | |
return _symbol; | |
} | |
/** | |
* @return the number of decimals of the token. | |
*/ | |
function decimals() public view returns(uint8) { | |
return _decimals; | |
} | |
// Preprocessor directive. Only compiles if in 'dev' network | |
{{ if (network === "dev") { }} | |
/// @dev Force transfers tokens from any account to another. Only compiled in 'dev' network | |
/// @param _from The address of the transfer sender | |
/// @param _to The address of the transfer recipient | |
/// @param _value The amount of tokens to be transferred | |
/// @return true if tokens transferred successfully | |
function forceTransfer(address _from, address _to, uint256 _value) public returns (bool success) { | |
require(_balances[_from] >= _value); | |
_balances[_from] -= _value; | |
_balances[_to] += _value; | |
emit Transfer(_from, _to, _value); | |
return true; | |
} | |
{{ } }} | |
} | |
{{ | |
parasol.onCompiled(this, function(fileName){ | |
console.log("File: " + fileName + " compiled") | |
}) | |
parasol.onError(this, function(e){ | |
console.log("error: " + e) | |
}) | |
parasol.onDeployed(this, function(contract, contractName){ | |
console.log(contractName + ' deployed') | |
}) | |
}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment