Smart contracts and JS API for Kosmos Kredits https://wiki.kosmos.org/Kredits
Go to file
Basti f2fc9fd161 Add empty Ganache DB directory 2018-04-17 13:13:52 +02:00
.ganache-db Add empty Ganache DB directory 2018-04-17 13:13:52 +02:00
config Adjust seeds to new proposal storage 2018-04-15 15:37:11 +02:00
contracts Merge pull request #23 from 67P/refactor/contributor-naming-convention 2018-04-10 14:08:26 +02:00
lib Add getContributorById 2018-04-07 20:10:55 +02:00
migrations Import/Migrate existing kredits contracts 2018-03-31 16:18:17 +02:00
scripts Make the CLI a real reply 2018-04-09 01:52:51 +02:00
.gitignore Persist ganache DB and use fixed Mnemonic 2018-04-15 21:03:12 +02:00
README.mdown readme 2018-04-16 11:01:43 +02:00
package-lock.json Add script to add new contirbutors 2018-04-02 14:44:46 +02:00
package.json Persist ganache DB and use fixed Mnemonic 2018-04-15 21:03:12 +02:00
truffle.js Upgradable experiments 2018-03-11 23:12:29 +01:00

README.mdown

Kredits

This repository contains all the contracts for Kredits as a truffle framework project.

Development

Installation

$ npm install

Requirements

All requirements are defined in package.json.

Those can be installed globally for convenience:

We use following solidity contract libraries:

For local development it is recommended to use ganache-cli (or the ganache GUI to run a local development chain.
Using the ganache simulator no full Ethereum node is required.

We default to:

  • port 7545 for development to not get in conflict with the default Ethereum RPC port.
  • network ID 100 to stay on the same network id
  • store ganache data in .ganache-db to presist the chain data across restarts
  • use a fixed Mnemonic code to get the same accounts across restarts

Have a look at ganache-cli for more configuration options.

Run your ganache simulator before using Kredits locally:

$ npm run ganache (which is: ganache-cli -p 7545 -i 100 --db=./.ganache-db -m kredits)

Truffle console

Truffle comes with a simple REPL to interact with the Smart Contracts. Have a look at the documentation here

NOTE: There are promisses, have a look at the examples:

Token.deployed().then(function(token) { 
  token.totalSupply.call().then(function(value) {
    console.log(value.toString());
  })
});

Also please be aware of the differences between web3.js 0.2x.x and 1.x.x - web3 repo

Contract Deployment

Truffle uses migration scripts to deploy contract to various networks. Have a look at the migrations folder for those.
The Ethereum nodes for the different networks need to be configured in truffle.js.

Run the truffle migration scripts:

$ truffle deploy 
$ truffle deploy --network=<network config from truffle.js>

Truffle keeps track of already executed migration scripts. To reset the migration use the --reset option

$ truffle migrate --reset

Migration scripts can also be run from within truffle console or truffle develop

To initially bootstrap a local development chain in ganache you can use the bootstrap script:

$ npm run bootstrap (= `truffle migrate --reset && truffle exec scripts/seeds.js && npm run build-json`)

Helper scripts

scripts/ contains some helper scripts to interact with the contracts from the CLI. At some point these should be moved into a real nice CLI.

To run these scripts use truffle exec. For example: truffle exec scripts/add-proposal.js

cli.js

Call any function on any contract:

$ truffle exec scripts/cli.js <Contract Name> <Function> [<optional> <arguments>]

For example: 
$ truffle exec scripts/cli.js Operator proposalsCount

Please note that the contract name and the function are case sensitive.

add-contributor.js

Adds a new core contributor, creates a proposal for the new contributor and votes for that one.

$ truffle exec scripts/add-contributor.js <ethereum address> [<profile IPFS hash>]

add-proposal.js

Adds a new proposal for an existing contributor

$ truffle exec scripts/add-proposal.js <ethereum address> [<proposal IPFS hash>]

send-funds.js

Sends funds to an address. Helpful in development mode to for example fund a metamask account.

$ truffle exec scripts/send-funds.js <ethereum address>

seeds.js

Run seeds defined in config/seeds.js.

$ truffle exec scripts/seeds.js 
or 
$ npm run seeds

Upgradeable contracts

Some of the contracts use upgradability ideas from zeppelinos (see contracts/upgradable).

The basic idea is to have a Registry contract that knows about the current implementations and a Proxy contract that uses delegatecall to call the current implementation.
That means the Proxy contract holds the storage and the address of that one does not change but the actuall implemenation is managed through the Registry.

To deploy a new version a new contract is deployed then the version is registered (addVersion()) in the Registry and on the Proxy contract is "upgraded" (upgrade()) to the new version.

The Registry knows about all the different contracts and implementations. Versions are stored as uint and automatically incremented for every added implementation.

Example:

Deployment is best done using the truffle deployer

  1. Setup
    1. deploy the Registry
    2. deploy the contract
    3. register the contract at the Registry: registry.addVersion('Token', Token.address)
    4. create the Proxy: registry.createProxy('Token', 1)
  2. Update
    1. deploy a new Version of the contract
    2. register the new version at the Registry: registry.addVersion('Token', NewToken.address)
    3. set the new implementation address on the Proxy contract: registry.upgrade('Token', 2)

Known Issues

When resetting ganache Metamask might have an invalid transaction nonce and transactions get rejected. Nonces in Ethereum must be incrementing and have no gap.

To solve this reset the metamask account (Account -> Settings -> Reset Account)