etherep

GointoMigrationABI

[{"constant":true,"inputs":[{"name":"who","type":"address"}],"name":"getPermissions","outputs":[{"name":"","type":"bool"},{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"adminAddress","type":"address"}],"name":"removeAdmin","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"manAddress","type":"address"}],"name":"addManager","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"key","type":"string"}],"name":"getContract","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"key","type":"string"},{"name":"contractAddress","type":"address"}],"name":"setContract","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"adminAddress","type":"address"}],"name":"addAdmin","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"manAddress","type":"address"}],"name":"removeManager","outputs":[],"payable":false,"type":"function"},{"inputs":[{"name":"originalAdmin","type":"address"}],"payable":false,"type":"constructor"},{"anonymous":false,"inputs":[{"indexed":false,"name":"by","type":"address"},{"indexed":false,"name":"key","type":"string"},{"indexed":false,"name":"contractAddress","type":"address"}],"name":"EventSetContract","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"by","type":"address"},{"indexed":false,"name":"admin","type":"address"}],"name":"EventAddAdmin","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"by","type":"address"},{"indexed":false,"name":"admin","type":"address"}],"name":"EventRemoveAdmin","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"by","type":"address"},{"indexed":false,"name":"manager","type":"address"}],"name":"EventAddManager","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"by","type":"address"},{"indexed":false,"name":"manager","type":"address"}],"name":"EventRemoveManager","type":"event"}]

Etherep ABI

[{"constant":false,"inputs":[{"name":"d","type":"bool"}],"name":"setDebug","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"newFee","type":"uint256"}],"name":"setFee","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"drain","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"who","type":"address"}],"name":"getScoreAndCount","outputs":[{"name":"score","type":"int256"},{"name":"ratings","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"getDelay","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"getFee","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"who","type":"address"}],"name":"setManager","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"getDebug","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"who","type":"address"}],"name":"getScore","outputs":[{"name":"score","type":"int256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"getManager","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_delay","type":"uint256"}],"name":"setDelay","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"who","type":"address"},{"name":"rating","type":"int256"}],"name":"rate","outputs":[],"payable":true,"type":"function"},{"inputs":[{"name":"_manager","type":"address"},{"name":"_fee","type":"uint256"},{"name":"_storageAddress","type":"address"},{"name":"_wait","type":"uint256"}],"payable":false,"type":"constructor"},{"anonymous":false,"inputs":[{"indexed":false,"name":"sender","type":"address"},{"indexed":false,"name":"message","type":"string"}],"name":"Error","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"message","type":"string"}],"name":"Debug","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"message","type":"int256"}],"name":"DebugInt","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"message","type":"uint256"}],"name":"DebugUint","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"by","type":"address"},{"indexed":false,"name":"who","type":"address"},{"indexed":false,"name":"rating","type":"int256"}],"name":"Rating","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"f","type":"uint256"}],"name":"FeeChanged","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"d","type":"uint256"}],"name":"DelayChanged","type":"event"}];

Dapp Integration with web3.js

Simple Use Case

Dapp integration with web3.js is fairly simple and only requires a few lines of code. Let's say you want to add on Etherep ratings to your Q&A forum as a symbol of users who are known to give quality answers to questions.

let etherep = web3.eth.contract(ETHEREP_ABI).at(ETHEREP_ADDR);
let returned = etherep.getScoreAndCount("0xfbb469c9c442d45ca7fa8a2161e788f5eb99573f");
console.log(`0xfbb469c9c442d45ca7fa8a2161e788f5eb99573f has a rating of ${returned[0] / 100} and was rated ${returned[1]} times.`);

However, that's the most simplistic use case and does not use our migration contract, nor make any ratings. Let's try a more complex example.

Full Use Case

First and foremost, we want to get the most current address of the Etherep contract. While it should not change often, it's possible that the interface may be upgraded, or bugs fixed. So we're going to use the GointoMigration contract to get the address we need.

let migration = web3.eth.contract(MIGRATION_ABI).at(MIGRATION_ADDR);
let etherepAddress = migration.getContract("etherep");

Now we have Etherep's current address in etherepAddress and can get the most current instance.

let etherep = web3.eth.contract(ETHEREP_ABI).at(etherepAddress);

Let's check the score of Etherep.

let returned = etherep.getScoreAndCount("0xfbb469c9c442d45ca7fa8a2161e788f5eb99573f");
let score = returned[0] / 100;
let count = returned[1];

getScoreAndCount returns 2 numbers. The total cumulative score for an address, and the number of ratings that have been made. The score is divided by zero because the EVM returns uint256, but we're working with a floating point number with 3 significant digits. For example, 500 = 5 and -125 == -1.25. You can display it however you want, however.

Now, let's make a rating. Etherep is awesome, so let's give it a rating of 5. And we must include the current fee.

let fee = etherep.getFee();
let tx = etherep.rate("0xfbb469c9c442d45ca7fa8a2161e788f5eb99573f", 5, {from: web3.eth.defaultAccount, value: fee, gas: 300000});

First we got the current fee, which is usually around 0.2 finney. This may change as time goes on to combat spam, so it's best to check with the contract first. Then we make our rating, and get the transaction hash back. You can do whatever you need to to verify this transaction from here, but assuming the correct fee was provided and enough gas, it should have gone through without issue.

Simple as that!

Contract Integration With Solidity

You can also integrate Etherep directly into your contract. Let's say you have an auction contract and will not accept bids from people with a rating below 0. You deem these other people as untrustworthy.

Here's an example contract with lots of irrelevant bits trimmed out.

pragma solidity ^0.4.11;
import "./etherep.sol";

contract MyAuction {
    address internal etherepAddress;
    [...snip...]

    modifier goodRatingOnly() {
        // Get contract interface
        Etherep etherep = Etherep(etherepAddress);

        // Make sure they have a rating of 0 or better
        require(etherep.getScore() <= 0);
        _;
    }

    function MyAuction(address _etherepAddress, [...snip...]) {
        etherepAddress = _etherepAddress;
        [...snip...]
    }

    function bid(uint _itemId, uint bidAmount) goodRatingOnly {
        [...snip...]
    }
}

In this example, we are using the modifier goodRatingOnly to make sure that the bidding address has a rating of 0 or better before allowing them to place the bid.

You should also add a function to change the Etherep address in case we ever migrate ours. Alternatively, you can also integrate the GointoMigration contract, but for the sake of saving gas, it's probably better to just check from time to time and update the address only when necessary.

To compile your contract, you'll need a copy of Etherep. You can get Etherep's contract source from GitHub.