Skip to content

Commit

Permalink
add setrate
Browse files Browse the repository at this point in the history
  • Loading branch information
DenisCarriere committed Aug 10, 2019
1 parent 2290a39 commit f85a40c
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 64 deletions.
1 change: 1 addition & 0 deletions deploy.sh
Expand Up @@ -19,6 +19,7 @@ cleos -u $ENDPOINT set contract $TOKEN ./ $TOKEN.wasm $TOKEN.abi
cleos -u $ENDPOINT set contract eosio.token ./ $TOKEN.wasm $TOKEN.abi

cleos push action $TOKEN create '["xy", "1000000000.0000 XY"]' -p $TOKEN
cleos push action $TOKEN create '["xy", "1000000000.0000 EOSXY"]' -p $TOKEN
cleos push action $TOKEN issue '["xy", "1.0000 XY", "init"]' -p $CODE

cleos push action eosio.token create '["eosio.token", "1000000000.0000 EOS"]' -p eosio.token
Expand Down
23 changes: 15 additions & 8 deletions include/xy/xy.hpp
Expand Up @@ -11,7 +11,7 @@

#include <xy/structs.hpp>
#include <token.xy/token.xy.hpp>
// #include <eosio.token/eosio.token.hpp>
#include <eosio.system/exchange_state.hpp>
#include <mapbox/geometry.hpp>

using namespace eosio;
Expand Down Expand Up @@ -142,6 +142,19 @@ class [[eosio::contract("xy")]] xy : public contract {
const uint64_t id,
const vector<tag> tags );

/**
* ACTION `setrate`
*
* Set rate of <chain>XY token
*
* @param {asset} rate - rate value
* @example
*
* cleos push action xy setrate '["0.1000 EOSXY""]'
*/
[[eosio::action]]
void setrate( const asset rate );

/**
* Notify contract when eosio.token deposits core symbol
*
Expand All @@ -153,13 +166,6 @@ class [[eosio::contract("xy")]] xy : public contract {
const asset& quantity,
const string& memo );

/**
* PRIVATE ACTION `clean`
*
* Removes all rows in tables
*/
[[eosio::action]] void clean();

using createnode_action = eosio::action_wrapper<"createnode"_n, &xy::createnode>;
using createway_action = eosio::action_wrapper<"createway"_n, &xy::createway>;
using createrel_action = eosio::action_wrapper<"createrel"_n, &xy::createrel>;
Expand All @@ -175,6 +181,7 @@ class [[eosio::contract("xy")]] xy : public contract {
*/
struct [[eosio::table("global")]] global_row {
uint64_t available_primary_key = 0;
asset rate = asset{10000, symbol{"EOSXY", 4}};
};

/**
Expand Down
20 changes: 0 additions & 20 deletions src/clean.cpp

This file was deleted.

10 changes: 10 additions & 0 deletions src/get_trx_id.cpp
@@ -0,0 +1,10 @@
#include <eosio/transaction.hpp>
#include <eosio/crypto.hpp>

checksum256 xy::get_trx_id() {
size_t size = transaction_size();
char buf[size];
size_t read = read_transaction( buf, size );
check( size == read, "read_transaction failed");
return sha256( buf, read );
}
26 changes: 26 additions & 0 deletions src/global.cpp
@@ -0,0 +1,26 @@
uint64_t xy::global_available_primary_key() {
auto global = _global.get_or_default();
uint64_t available_primary_key = global.available_primary_key;
global.available_primary_key += 1;
_global.set(global, get_self());
return available_primary_key;
}

/**
* ACTION setrate
*/
void xy::setrate( asset rate )
{
require_auth( get_self() );

auto sym_code = rate.symbol.code();
const asset supply = token::get_supply( TOKEN_CONTRACT, sym_code );

check( rate.is_valid(), "invalid rate" );
check( rate.amount > 0, "must set rate positive rate" );
check( rate.symbol == supply.symbol, "symbol precision mismatch" );

auto global = _global.get_or_default();
global.rate = rate;
_global.set(global, get_self());
}
13 changes: 9 additions & 4 deletions src/on_notify.cpp
@@ -1,3 +1,5 @@
#include <math.h> /* pow */

[[eosio::on_notify("eosio.token::transfer")]]
void xy::transfer( const name& from,
const name& to,
Expand All @@ -10,11 +12,14 @@ void xy::transfer( const name& from,
// authenticate incoming `from` account
require_auth( from );

asset swap_quantity = asset{10000, symbol{"XY", 4}};
asset rate = _global.get_or_default().rate;
symbol sym = symbol{quantity.symbol.code().to_string() + "XY", 4};
int64_t amount = quantity.amount / (rate.amount / pow(10, rate.symbol.precision()) );
asset convert_quantity = asset{amount, sym};

token::issue_action issue(TOKEN_CONTRACT, {get_self(), "active"_n});
issue.send(get_self(), swap_quantity, "token swap");
issue.send(get_self(), convert_quantity, "convert");

token::transfer_action transfer(TOKEN_CONTRACT, {get_self(), "active"_n});
transfer.send(get_self(), from, swap_quantity, "token swap");
}
transfer.send(get_self(), from, convert_quantity, "convert");
}
16 changes: 0 additions & 16 deletions src/utils.cpp
@@ -1,19 +1,3 @@
#include <eosio/transaction.hpp>
#include <eosio/crypto.hpp>

checksum256 xy::get_trx_id() {
size_t size = transaction_size();
char buf[size];
size_t read = read_transaction( buf, size );
check( size == read, "read_transaction failed");
return sha256( buf, read );
}

uint64_t xy::global_available_primary_key() {
uint64_t available_primary_key = _global.get_or_default().available_primary_key;
_global.set(global_row{ available_primary_key + 1 }, get_self());
return available_primary_key;
}

name xy::get_owner( uint64_t id ) {
if (node_exists( id )) {
Expand Down
3 changes: 2 additions & 1 deletion src/xy.cpp
Expand Up @@ -4,8 +4,9 @@
#include "way.cpp"
#include "relation.cpp"
#include "tag.cpp"
#include "clean.cpp"
#include "erase.cpp"
#include "utils.cpp"
#include "consume.cpp"
#include "on_notify.cpp"
#include "get_trx_id.cpp"
#include "global.cpp"
32 changes: 17 additions & 15 deletions test.sh
Expand Up @@ -6,24 +6,26 @@ USER=bob

ENDPOINT=http://localhost:8888

cleos push action $CODE setrate '["0.1000 EOSXY"]' -p $CODE

cleos -u $ENDPOINT transfer $USER $CODE "1.0000 EOS"

cleos -u $ENDPOINT push action $CODE createnode \
"[${USER},[45.123, 150.123], [{\"k\":\"second\",\"v\":\"value0\"}]]" \
-p $USER
# cleos -u $ENDPOINT push action $CODE createnode \
# "[${USER},[45.123, 150.123], [{\"k\":\"second\",\"v\":\"value0\"}]]" \
# -p $USER

cleos -u $ENDPOINT push action $CODE createway \
"[${USER},[[-25, -45], [65, 180]], [{\"k\":\"second\",\"v\":\"value0\"}]]" \
-p $USER
# cleos -u $ENDPOINT push action $CODE createway \
# "[${USER},[[-25, -45], [65, 180]], [{\"k\":\"second\",\"v\":\"value0\"}]]" \
# -p $USER

cleos -u $ENDPOINT push action $CODE createrel \
"[${USER},[{\"type\": \"way\", \"ref\": 1, \"role\": \"\"}, {\"type\": \"node\", \"ref\": 0, \"role\": \"\"}], [{\"k\":\"foo\", \"v\": \"bar\"}]]" \
-p $USER
# cleos -u $ENDPOINT push action $CODE createrel \
# "[${USER},[{\"type\": \"way\", \"ref\": 1, \"role\": \"\"}, {\"type\": \"node\", \"ref\": 0, \"role\": \"\"}], [{\"k\":\"foo\", \"v\": \"bar\"}]]" \
# -p $USER

cleos -u $ENDPOINT push action $CODE move \
"[${USER},0, [20, 50]]" \
-p $USER
# cleos -u $ENDPOINT push action $CODE move \
# "[${USER},0, [20, 50]]" \
# -p $USER

cleos -u $ENDPOINT push action $CODE modify \
"[${USER}, 1, [{\"k\":\"second\", \"v\": \"updated\"}]]" \
-p $USER
# cleos -u $ENDPOINT push action $CODE modify \
# "[${USER}, 1, [{\"k\":\"second\", \"v\": \"updated\"}]]" \
# -p $USER

0 comments on commit f85a40c

Please sign in to comment.