implement staking contract + tests

This commit is contained in:
Iuri Matias 2019-06-18 08:14:29 -04:00
parent 0f85c8ce19
commit 4b04258289
18 changed files with 4086 additions and 0 deletions

150
config/blockchain.js Normal file
View File

@ -0,0 +1,150 @@
module.exports = {
// applies to all environments
default: {
enabled: true,
rpcHost: "localhost", // HTTP-RPC server listening interface (default: "localhost")
rpcPort: 8545, // HTTP-RPC server listening port (default: 8545)
rpcCorsDomain: { // Domains from which to accept cross origin requests (browser enforced). This can also be a comma separated list
auto: true, // When "auto" is true, Embark will automatically set the cors to the address of the webserver
additionalCors: [] // Additional CORS domains to add to the list. If "auto" is false, only those will be added
},
wsRPC: true, // Enable the WS-RPC server
wsOrigins: { // Same thing as "rpcCorsDomain", but for WS origins
auto: true,
additionalCors: []
},
wsHost: "localhost", // WS-RPC server listening interface (default: "localhost")
wsPort: 8546 // WS-RPC server listening port (default: 8546)
// Accounts to use as node accounts
// The order here corresponds to the order of `web3.eth.getAccounts`, so the first one is the `defaultAccount`
/*,accounts: [
{
nodeAccounts: true, // Accounts use for the node
numAddresses: "1", // Number of addresses/accounts (defaults to 1)
password: "config/development/devpassword" // Password file for the accounts
},
// Below are additional accounts that will count as `nodeAccounts` in the `deployment` section of your contract config
// Those will not be unlocked in the node itself
{
privateKey: "your_private_key"
},
{
privateKeyFile: "path/to/file", // Either a keystore or a list of keys, separated by , or ;
password: "passwordForTheKeystore" // Needed to decrypt the keystore file
},
{
mnemonic: "12 word mnemonic",
addressIndex: "0", // Optional. The index to start getting the address
numAddresses: "1", // Optional. The number of addresses to get
hdpath: "m/44'/60'/0'/0/" // Optional. HD derivation path
}
]*/
},
// default environment, merges with the settings in default
// assumed to be the intended environment by `embark run` and `embark blockchain`
development: {
ethereumClientName: "geth", // Can be geth or parity (default:geth)
//ethereumClientBin: "geth", // path to the client binary. Useful if it is not in the global PATH
networkType: "custom", // Can be: testnet, rinkeby, livenet or custom, in which case, it will use the specified networkId
networkId: 1337, // Network id used when networkType is custom
isDev: true, // Uses and ephemeral proof-of-authority network with a pre-funded developer account, mining enabled
datadir: ".embark/development/datadir", // Data directory for the databases and keystore (Geth 1.8.15 and Parity 2.0.4 can use the same base folder, till now they does not conflict with each other)
mineWhenNeeded: true, // Uses our custom script (if isDev is false) to mine only when needed
nodiscover: true, // Disables the peer discovery mechanism (manual peer addition)
maxpeers: 0, // Maximum number of network peers (network disabled if set to 0) (default: 25)
proxy: true, // Proxy is used to present meaningful information about transactions
targetGasLimit: 8000000, // Target gas limit sets the artificial target gas floor for the blocks to mine
simulatorBlocktime: 0 // Specify blockTime in seconds for automatic mining. Default is 0 and no auto-mining.
},
// merges with the settings in default
// used with "embark run privatenet" and/or "embark blockchain privatenet"
privatenet: {
networkType: "custom",
networkId: 1337,
isDev: false,
datadir: ".embark/privatenet/datadir",
// -- mineWhenNeeded --
// This options is only valid when isDev is false.
// Enabling this option uses our custom script to mine only when needed.
// Embark creates a development account for you (using `geth account new`) and funds the account. This account can be used for
// development (and even imported in to MetaMask). To enable correct usage, a password for this account must be specified
// in the `account > password` setting below.
// NOTE: once `mineWhenNeeded` is enabled, you must run an `embark reset` on your dApp before running
// `embark blockchain` or `embark run` for the first time.
mineWhenNeeded: true,
// -- genesisBlock --
// This option is only valid when mineWhenNeeded is true (which is only valid if isDev is false).
// When enabled, geth uses POW to mine transactions as it would normally, instead of using POA as it does in --dev mode.
// On the first `embark blockchain or embark run` after this option is enabled, geth will create a new chain with a
// genesis block, which can be configured using the `genesisBlock` configuration option below.
genesisBlock: "config/privatenet/genesis.json", // Genesis block to initiate on first creation of a development node
nodiscover: true,
maxpeers: 0,
proxy: true,
accounts: [
{
nodeAccounts: true,
password: "config/privatenet/password" // Password to unlock the account
}
],
targetGasLimit: 8000000,
simulatorBlocktime: 0
},
privateparitynet: {
ethereumClientName: "parity",
networkType: "custom",
networkId: 1337,
isDev: false,
genesisBlock: "config/privatenet/genesis-parity.json", // Genesis block to initiate on first creation of a development node
datadir: ".embark/privatenet/datadir",
mineWhenNeeded: false,
nodiscover: true,
maxpeers: 0,
proxy: true,
accounts: [
{
nodeAccounts: true,
password: "config/privatenet/password"
}
],
targetGasLimit: 8000000,
simulatorBlocktime: 0
},
// merges with the settings in default
// used with "embark run testnet" and/or "embark blockchain testnet"
testnet: {
networkType: "testnet",
syncMode: "light",
accounts: [
{
nodeAccounts: true,
password: "config/testnet/password"
}
]
},
// merges with the settings in default
// used with "embark run livenet" and/or "embark blockchain livenet"
livenet: {
networkType: "livenet",
syncMode: "light",
rpcCorsDomain: "http://localhost:8000",
wsOrigins: "http://localhost:8000",
accounts: [
{
nodeAccounts: true,
password: "config/livenet/password"
}
]
}
// you can name an environment with specific settings and then specify with
// "embark run custom_name" or "embark blockchain custom_name"
//custom_name: {
//}
};

46
config/communication.js Normal file
View File

@ -0,0 +1,46 @@
module.exports = {
// default applies to all environments
default: {
enabled: true,
provider: "whisper", // Communication provider. Currently, Embark only supports whisper
available_providers: ["whisper"], // Array of available providers
},
// default environment, merges with the settings in default
// assumed to be the intended environment by `embark run`
development: {
connection: {
host: "localhost", // Host of the blockchain node
port: 8546, // Port of the blockchain node
type: "ws" // Type of connection (ws or rpc)
}
},
// merges with the settings in default
// used with "embark run privatenet"
privatenet: {
},
// merges with the settings in default
// used with "embark run testnet"
testnet: {
},
// merges with the settings in default
// used with "embark run livenet"
livenet: {
},
// you can name an environment with specific settings and then specify with
// "embark run custom_name"
//custom_name: {
//}
// Use this section when you need a specific symmetric or private keys in whisper
/*
,keys: {
symmetricKey: "your_symmetric_key",// Symmetric key for message decryption
privateKey: "your_private_key" // Private Key to be used as a signing key and for message decryption
}
*/
//}
};

91
config/contracts.js Normal file
View File

@ -0,0 +1,91 @@
module.exports = {
// default applies to all environments
default: {
// Blockchain node to deploy the contracts
deployment: {
host: "localhost", // Host of the blockchain node
port: 8546, // Port of the blockchain node
type: "ws" // Type of connection (ws or rpc),
// Accounts to use instead of the default account to populate your wallet
// The order here corresponds to the order of `web3.eth.getAccounts`, so the first one is the `defaultAccount`
/*,accounts: [
{
privateKey: "your_private_key",
balance: "5 ether" // You can set the balance of the account in the dev environment
// Balances are in Wei, but you can specify the unit with its name
},
{
privateKeyFile: "path/to/file", // Either a keystore or a list of keys, separated by , or ;
password: "passwordForTheKeystore" // Needed to decrypt the keystore file
},
{
mnemonic: "12 word mnemonic",
addressIndex: "0", // Optional. The index to start getting the address
numAddresses: "1", // Optional. The number of addresses to get
hdpath: "m/44'/60'/0'/0/" // Optional. HD derivation path
},
{
"nodeAccounts": true // Uses the Ethereum node's accounts
}
]*/
},
// order of connections the dapp should connect to
dappConnection: [
"$WEB3", // uses pre existing web3 object if available (e.g in Mist)
"ws://localhost:8546",
"http://localhost:8545"
],
// Automatically call `ethereum.enable` if true.
// If false, the following code must run before sending any transaction: `await EmbarkJS.enableEthereum();`
// Default value is true.
// dappAutoEnable: true,
gas: "auto",
// Strategy for the deployment of the contracts:
// - implicit will try to deploy all the contracts located inside the contracts directory
// or the directory configured for the location of the contracts. This is default one
// when not specified
// - explicit will only attempt to deploy the contracts that are explicitly specified inside the
// contracts section.
//strategy: 'implicit',
contracts: {
// example:
//SimpleStorage: {
// args: [ 100 ]
//}
}
},
// default environment, merges with the settings in default
// assumed to be the intended environment by `embark run`
development: {
dappConnection: [
"ws://localhost:8546",
"http://localhost:8545",
"$WEB3" // uses pre existing web3 object if available (e.g in Mist)
]
},
// merges with the settings in default
// used with "embark run privatenet"
privatenet: {
},
// merges with the settings in default
// used with "embark run testnet"
testnet: {
},
// merges with the settings in default
// used with "embark run livenet"
livenet: {
},
// you can name an environment with specific settings and then specify with
// "embark run custom_name" or "embark blockchain custom_name"
//custom_name: {
//}
};

View File

@ -0,0 +1 @@
dev_password

39
config/namesystem.js Normal file
View File

@ -0,0 +1,39 @@
module.exports = {
// default applies to all environments
default: {
enabled: true,
available_providers: ["ens"],
provider: "ens"
},
// default environment, merges with the settings in default
// assumed to be the intended environment by `embark run`
development: {
register: {
rootDomain: "embark.eth",
subdomains: {
'status': '0x1a2f3b98e434c02363f3dac3174af93c1d690914'
}
}
},
// merges with the settings in default
// used with "embark run privatenet"
privatenet: {
},
// merges with the settings in default
// used with "embark run testnet"
testnet: {
},
// merges with the settings in default
// used with "embark run livenet"
livenet: {
},
// you can name an environment with specific settings and then specify with
// "embark run custom_name" or "embark blockchain custom_name"
//custom_name: {
//}
};

27
config/pipeline.js Normal file
View File

@ -0,0 +1,27 @@
// Embark has support for Flow enabled by default in its built-in webpack
// config: type annotations will automatically be stripped out of DApp sources
// without any additional configuration. Note that type checking is not
// performed during builds.
// To enable Flow type checking refer to the preconfigured template:
// https://github.com/embark-framework/embark-flow-template
// A new DApp can be created from that template with:
// embark new --template flow
module.exports = {
typescript: false,
// Setting `typescript: true` in this config will disable Flow support in
// Embark's default webpack config and enable TypeScript support: .ts and
// .tsx sources will automatically be transpiled into JavaScript without any
// additional configuration. Note that type checking is not performed during
// builds.
// To enable TypeScript type checking refer to the preconfigured template:
// https://github.com/embark-framework/embark-typescript-template
// A new DApp can be created from that template with:
// embark new --template typescript
enabled: true
// Setting `enabled: false` in this config will disable Embark's built-in Webpack
// pipeline. The developer will need to use a different frontend build tool, such as
// `create-react-app` or Angular CLI to build their dapp
};

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,20 @@
{
"config": {
"homesteadBlock": 0,
"byzantiumBlock": 0,
"eip155Block": 0,
"eip158Block": 0,
"daoForkSupport": true
},
"nonce": "0x0000000000000042",
"difficulty": "0x0",
"alloc": {
"0x3333333333333333333333333333333333333333": {"balance": "15000000000000000000"}
},
"mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"coinbase": "0x3333333333333333333333333333333333333333",
"timestamp": "0x00",
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"extraData": "0x",
"gasLimit": "0x7a1200"
}

View File

@ -0,0 +1 @@
dev_password

59
config/storage.js Normal file
View File

@ -0,0 +1,59 @@
module.exports = {
// default applies to all environments
default: {
enabled: true,
ipfs_bin: "ipfs",
available_providers: ["ipfs"],
upload: {
provider: "ipfs",
host: "localhost",
port: 5001
},
dappConnection: [
{
provider: "ipfs",
host: "localhost",
port: 5001,
getUrl: "http://localhost:8080/ipfs/"
}
]
// Configuration to start Swarm in the same terminal as `embark run`
/*,account: {
address: "YOUR_ACCOUNT_ADDRESS", // Address of account accessing Swarm
password: "PATH/TO/PASSWORD/FILE" // File containing the password of the account
},
swarmPath: "PATH/TO/SWARM/EXECUTABLE" // Path to swarm executable (default: swarm)*/
},
// default environment, merges with the settings in default
// assumed to be the intended environment by `embark run`
development: {
enabled: true,
upload: {
provider: "ipfs",
host: "localhost",
port: 5001,
getUrl: "http://localhost:8080/ipfs/"
}
},
// merges with the settings in default
// used with "embark run privatenet"
privatenet: {
},
// merges with the settings in default
// used with "embark run testnet"
testnet: {
},
// merges with the settings in default
// used with "embark run livenet"
livenet: {
},
// you can name an environment with specific settings and then specify with
// "embark run custom_name"
//custom_name: {
//}
};

1
config/testnet/password Normal file
View File

@ -0,0 +1 @@
test_password

6
config/webserver.js Normal file
View File

@ -0,0 +1,6 @@
module.exports = {
enabled: true,
host: "localhost",
openBrowser: true,
port: 8000
};

0
contracts/.gitkeep Normal file
View File

43
contracts/StakingPool.sol Normal file
View File

@ -0,0 +1,43 @@
pragma solidity ^0.5.0;
import "openzeppelin-solidity/contracts/token/ERC20/ERC20.sol";
import "openzeppelin-solidity/contracts/token/ERC20/ERC20Detailed.sol";
import "openzeppelin-solidity/contracts/token/ERC20/ERC20Burnable.sol";
import "math.sol";
contract StakingPool is ERC20, ERC20Detailed, ERC20Burnable, DSMath {
uint private INITIAL_SUPPLY = 0;
constructor () public ERC20Detailed("TellerStatus", "TSNT", 18) {
}
function exchangeRate (uint256 excludeAmount) public view returns (uint256) {
if (totalSupply() == 0) return 1000000000000000000;
return wdiv(address(this).balance - excludeAmount, totalSupply());
}
function estimatedTokens(uint256 value) public view returns (uint256) {
uint256 rate = exchangeRate(value);
return wdiv(value, wdiv(rate, 1000000000000000000));
}
function deposit () public payable {
uint256 rate = exchangeRate(msg.value);
_mint(msg.sender, estimatedTokens(msg.value));
}
function div () public returns (uint256) {
uint256 rate = exchangeRate(0);
return wdiv(rate, 1000000000000000000);
}
function withdraw (uint256 amount) public {
uint256 rate = exchangeRate(0);
burn(amount);
msg.sender.transfer(wmul(amount, wdiv(rate, 1000000000000000000)));
}
function() external payable {
}
}

85
contracts/math.sol Normal file
View File

@ -0,0 +1,85 @@
// https://raw.githubusercontent.com/dapphub/ds-math/master/src/math.sol
/// 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.13;
contract DSMath {
function add(uint x, uint y) internal pure returns (uint z) {
require((z = x + y) >= x, "ds-math-add-overflow");
}
function sub(uint x, uint y) internal pure returns (uint z) {
require((z = x - y) <= x, "ds-math-sub-underflow");
}
function mul(uint x, uint y) internal 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) {
return x <= y ? x : y;
}
function max(uint x, uint y) internal pure returns (uint z) {
return x >= y ? x : y;
}
function imin(int x, int y) internal pure returns (int z) {
return x <= y ? x : y;
}
function imax(int x, int y) internal 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 rmul(uint x, uint y) internal pure returns (uint z) {
z = add(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 rdiv(uint x, uint y) internal pure returns (uint z) {
z = add(mul(x, RAY), y / 2) / y;
}
// This famous algorithm is called "exponentiation by squaring"
// and calculates x^n with x as fixed-point and n as regular unsigned.
//
// It's O(log n), instead of O(n) for naive repeated multiplication.
//
// These facts are why it works:
//
// If n is even, then x^n = (x^2)^(n/2).
// If n is odd, then x^n = x * x^(n-1),
// and applying the equation for even x gives
// x^n = x * (x^2)^((n-1) / 2).
//
// Also, EVM division is flooring and
// floor[(n-1) / 2] = floor[n / 2].
//
function rpow(uint x, uint n) internal pure returns (uint z) {
z = n % 2 != 0 ? x : RAY;
for (n /= 2; n != 0; n /= 2) {
x = rmul(x, x);
if (n % 2 != 0) {
z = rmul(z, x);
}
}
}
}

17
package.json Normal file
View File

@ -0,0 +1,17 @@
{
"name": "StakingPool",
"version": "0.0.1",
"description": "",
"scripts": {
"test": "embark test"
},
"keywords": [],
"author": "",
"license": "MIT",
"devDependencies": {
"embarkjs-connector-web3": "^4.1.0-beta.3"
},
"dependencies": {
"openzeppelin-solidity": "^2.3.0"
}
}

149
test/stakingPool_spec.js Normal file
View File

@ -0,0 +1,149 @@
// /*global contract, config, it, assert*/
const StakingPool = require('Embark/contracts/StakingPool');
let iuri, jonathan, richard;
// For documentation please see https://embark.status.im/docs/contracts_testing.html
config({
contracts: {
"StakingPool": {
}
}
}, (_err, accounts) => {
iuri = accounts[0];
jonathan = accounts[1];
richard = accounts[2];
});
// TODO: add asserts for balances
contract("StakingPool", function () {
this.timeout(0);
describe("initial state", () => {
it("initial exchangeRate should be 1", async function () {
let rate = await StakingPool.methods.exchangeRate(0).call();
assert.strictEqual(rate, "1000000000000000000");
});
it("initial token supply should be 0", async function () {
let rate = await StakingPool.methods.totalSupply().call();
assert.strictEqual(rate, "0");
});
it("initial balance should be 0", async function () {
let rate = await web3.eth.getBalance(StakingPool.address);
assert.strictEqual(rate, "0");
});
})
describe("depositing before contributions", () => {
before("deposit 11 ETH", async () => {
await StakingPool.methods.deposit().send({value: "11000000000000000000", from: jonathan})
})
it("exchangeRate should remain 1", async function () {
let rate = await StakingPool.methods.exchangeRate(0).call();
assert.strictEqual(rate, "1000000000000000000");
});
it("token supply should be 12", async function () {
let rate = await StakingPool.methods.totalSupply().call();
assert.strictEqual(rate, "11000000000000000000");
});
it("balance should be 12", async function () {
let rate = await web3.eth.getBalance(StakingPool.address);
assert.strictEqual(rate, "11000000000000000000");
});
});
describe("2nd person depositing before contributions", () => {
before("deposit 5 ETH", async () => {
await StakingPool.methods.deposit().send({value: "5000000000000000000", from: iuri})
})
it("exchangeRate should remain 1", async function () {
let rate = await StakingPool.methods.exchangeRate(0).call();
assert.strictEqual(rate, "1000000000000000000");
});
it("token supply should be 17", async function () {
let rate = await StakingPool.methods.totalSupply().call();
assert.strictEqual(rate, "16000000000000000000");
});
it("balance should be 17", async function () {
let rate = await web3.eth.getBalance(StakingPool.address);
assert.strictEqual(rate, "16000000000000000000");
});
});
describe("contributions", () => {
before("contribute 10 ETH", async () => {
await web3.eth.sendTransaction({value: "10000000000000000000", to: StakingPool.address})
})
it("exchangeRate should increase", async function () {
let rate = await StakingPool.methods.exchangeRate(0).call();
assert.strictEqual(rate, "1625000000000000000");
});
it("token supply should remain at 17", async function () {
let rate = await StakingPool.methods.totalSupply().call();
assert.strictEqual(rate, "16000000000000000000");
});
it("balance should be 27", async function () {
let rate = await web3.eth.getBalance(StakingPool.address);
assert.strictEqual(rate, "26000000000000000000");
});
});
describe("withdrawing 5 tokens after contributions", () => {
before("withdraw 5 tokens", async () => {
await StakingPool.methods.withdraw("5000000000000000000").send({from: jonathan})
})
it("exchangeRate should remain the same", async function () {
let rate = await StakingPool.methods.exchangeRate(0).call();
assert.strictEqual(rate, "1625000000000000000");
});
it("token supply should decrease to 11", async function () {
let rate = await StakingPool.methods.totalSupply().call();
assert.strictEqual(rate, "11000000000000000000");
});
it("balance should decrease by correct exchange rate", async function () {
let rate = await web3.eth.getBalance(StakingPool.address);
// 5000000000000000000 tokens x 1.625 rate
// => 8125000000000000000 ETH
// 26000000000000000000 - 8125000000000000000 = 17875000000000000000
assert.strictEqual(rate, "17875000000000000000");
});
});
describe("depositing after contributions", () => {
before("deposit 8 ETH", async () => {
await StakingPool.methods.deposit().send({value: "8000000000000000000", from: richard})
})
it("exchangeRate should remain the same", async function () {
let rate = await StakingPool.methods.exchangeRate(0).call();
assert.strictEqual(rate, "1625000000000000000");
});
it("token supply should increase by correct exchange rate", async function () {
let rate = await StakingPool.methods.totalSupply().call();
assert.strictEqual(rate, "15923076923076923077");
});
it("balance should increase", async function () {
let rate = await web3.eth.getBalance(StakingPool.address);
// 17875000000000000000 + 8000000000000000000
assert.strictEqual(rate, "25875000000000000000");
});
});
});

3204
yarn.lock Normal file

File diff suppressed because it is too large Load Diff