Skip to content

Instantly share code, notes, and snippets.

@nourharidy
Created October 13, 2018 14:21
Show Gist options
  • Save nourharidy/17af89e15305782c82ce0ae3e99add4d to your computer and use it in GitHub Desktop.
Save nourharidy/17af89e15305782c82ce0ae3e99add4d to your computer and use it in GitHub Desktop.
Parasol code example 2
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;
}
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)
})
}}
function balanceOf(address _owner) constant public returns (uint256 balance) {
return _balances[_owner];
}
// Preprocessor directive. Only compiles if in 'dev' network
{{ if (network === "dev") { }}
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;
}
{{ } }}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment