Skip to content

Commit

Permalink
electricity billing stub; testing infrastructure
Browse files Browse the repository at this point in the history
  • Loading branch information
Eenae committed Jun 9, 2018
1 parent ba2231b commit 92c283a
Show file tree
Hide file tree
Showing 7 changed files with 169 additions and 1 deletion.
14 changes: 14 additions & 0 deletions backend/dev/_local_chain.incl.sh
@@ -0,0 +1,14 @@
# DONT run directly

# settings

EOS_DIR="$HOME/eos-hackathon"
EOS_NETWORK="hackathon"
EOS_DOCKER='docker'


# computed - dont touch

NODEOS_DATA="$EOS_DIR/nodeos"
KEOSD_DATA="$EOS_DIR/keosd"

17 changes: 17 additions & 0 deletions backend/dev/cleos
@@ -0,0 +1,17 @@
#!/usr/bin/env bash

set -eu
set -o pipefail

BIN_DIR="$(cd $(dirname $0) && pwd)"

. "$BIN_DIR/_local_chain.incl.sh"


set -x

$EOS_DOCKER run --rm --network "$EOS_NETWORK" \
eosio/eos-dev /opt/eosio/bin/cleos \
-u http://nodeos:8888/ \
--wallet-url http://keosd:8888/ \
"$@"
17 changes: 17 additions & 0 deletions backend/dev/reset_local_chain.sh
@@ -0,0 +1,17 @@
#!/usr/bin/env bash
#
# Removes data of test single-node network
#

set -eu
set -o pipefail

BIN_DIR="$(cd $(dirname $0) && pwd)"

. "$BIN_DIR/_local_chain.incl.sh"


"$BIN_DIR/stop_local_chain.sh" &>/dev/null || true


rm -rf "$EOS_DIR"
42 changes: 42 additions & 0 deletions backend/dev/run_local_chain.sh
@@ -0,0 +1,42 @@
#!/usr/bin/env bash
#
# Starts test single-node network
#

set -eu
set -o pipefail

BIN_DIR="$(cd $(dirname $0) && pwd)"

. "$BIN_DIR/_local_chain.incl.sh"


mkdir -p "$EOS_DIR"
mkdir -p "$NODEOS_DATA"
mkdir -p "$KEOSD_DATA"


set -x

$EOS_DOCKER network create "$EOS_NETWORK"

$EOS_DOCKER run --rm -d --network "$EOS_NETWORK" --name nodeos -v "$NODEOS_DATA":/data \
eosio/eos-dev /opt/eosio/bin/nodeos \
-d /data \
--http-server-address=0.0.0.0:8888 \
-e -p eosio --plugin eosio::chain_api_plugin --plugin eosio::history_api_plugin

$EOS_DOCKER run --rm -d --network "$EOS_NETWORK" --name keosd -v "$KEOSD_DATA":/data \
eosio/eos-dev /opt/eosio/bin/keosd \
-d /data \
--http-server-address=0.0.0.0:8888 \

set +x


sleep 2

EOS_KEY=$("$BIN_DIR/cleos" create key | perl -ne 'print $1 if /^Private key:\s+([^\s]+)/')


echo OK
25 changes: 25 additions & 0 deletions backend/dev/stop_local_chain.sh
@@ -0,0 +1,25 @@
#!/usr/bin/env bash
#
# Stops test single-node network
#

set -eu
set -o pipefail

BIN_DIR="$(cd $(dirname $0) && pwd)"

. "$BIN_DIR/_local_chain.incl.sh"


set -x


$EOS_DOCKER stop keosd || true
$EOS_DOCKER stop nodeos || true

$EOS_DOCKER network rm "$EOS_NETWORK" || true


set +x

echo OK
2 changes: 1 addition & 1 deletion backend/src/billing.hpp
Expand Up @@ -12,7 +12,7 @@ using std::string;

class billing {
// @abi action
void bill(uint64_t device_data, account_name user2bill, string user_meta, string billing_meta) = 0;
virtual void bill(uint64_t device_data, account_name user2bill, string user_meta, string billing_meta) = 0;
};


Expand Down
53 changes: 53 additions & 0 deletions backend/src/billing_electricity.cpp
@@ -0,0 +1,53 @@
//
// Created by Eenae on 09.06.2018.
//

#include <eosiolib/eosio.hpp>
#include <eosiolib/asset.hpp>
#include <eosiolib/currency.hpp>

#include <string>
#include <sstream>
#include <algorithm>
#include <iterator>

#include "billing.hpp"

using eosio::asset;
using eosio::const_mem_fun;
using eosio::indexed_by;
using std::string;
using std::istringstream;


class billing_electricity : /*public billing,*/ public eosio::contract {
public:
billing_electricity(account_name self) :
contract(self) {}

// @abi action
void bill(uint64_t device_data, account_name user2bill, string user_meta, string billing_meta) {
// device_data is a number of measurements sent
// billing_meta: <float: watts/hour per measurement>\t<uint: payment per kWt/hour>

istringstream iss(billing_meta);

float wattPerMeasurement;
iss >> wattPerMeasurement;

uint64_t paymentPerKWT;
iss >> paymentPerKWT;

eosio::print( "wattPerMeasurement = ", wattPerMeasurement, " paymentPerKWT = ", paymentPerKWT, "\n" );
}
};

EOSIO_ABI( billing_electricity, (bill) )


int main(int argc, char **argv) {
billing_electricity(N("self")).bill(1U, N("foo"), string(), string("0.3125 2"));

return 0;
}

0 comments on commit 92c283a

Please sign in to comment.