Skip to content

Commit

Permalink
swap APY info
Browse files Browse the repository at this point in the history
  • Loading branch information
madMAx43v3r committed Dec 15, 2022
1 parent e8d3fb5 commit d780b7a
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 10 deletions.
8 changes: 6 additions & 2 deletions generated/include/mmx/swap_info_t.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ struct MMX_EXPORT swap_info_t {
std::array<::mmx::uint128, 2> fees_paid = {};
std::array<::mmx::uint128, 2> fees_claimed = {};
std::array<::mmx::uint128, 2> user_total = {};
std::array<vnx::float64_t, 2> avg_apy_1d = {};
std::array<vnx::float64_t, 2> avg_apy_7d = {};

static const vnx::Hash64 VNX_TYPE_HASH;
static const vnx::Hash64 VNX_CODE_HASH;
Expand Down Expand Up @@ -70,7 +72,7 @@ struct MMX_EXPORT swap_info_t {

template<typename T>
void swap_info_t::accept_generic(T& _visitor) const {
_visitor.template type_begin<swap_info_t>(8);
_visitor.template type_begin<swap_info_t>(10);
_visitor.type_field("name", 0); _visitor.accept(name);
_visitor.type_field("address", 1); _visitor.accept(address);
_visitor.type_field("tokens", 2); _visitor.accept(tokens);
Expand All @@ -79,7 +81,9 @@ void swap_info_t::accept_generic(T& _visitor) const {
_visitor.type_field("fees_paid", 5); _visitor.accept(fees_paid);
_visitor.type_field("fees_claimed", 6); _visitor.accept(fees_claimed);
_visitor.type_field("user_total", 7); _visitor.accept(user_total);
_visitor.template type_end<swap_info_t>(8);
_visitor.type_field("avg_apy_1d", 8); _visitor.accept(avg_apy_1d);
_visitor.type_field("avg_apy_7d", 9); _visitor.accept(avg_apy_7d);
_visitor.template type_end<swap_info_t>(10);
}


Expand Down
4 changes: 2 additions & 2 deletions generated/src/Node_get_swap_info_return.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace mmx {


const vnx::Hash64 Node_get_swap_info_return::VNX_TYPE_HASH(0x302116742171428ull);
const vnx::Hash64 Node_get_swap_info_return::VNX_CODE_HASH(0x506882e0fdd86d70ull);
const vnx::Hash64 Node_get_swap_info_return::VNX_CODE_HASH(0x985f6fd1925ad73eull);

vnx::Hash64 Node_get_swap_info_return::get_type_hash() const {
return VNX_TYPE_HASH;
Expand Down Expand Up @@ -114,7 +114,7 @@ std::shared_ptr<vnx::TypeCode> Node_get_swap_info_return::static_create_type_cod
auto type_code = std::make_shared<vnx::TypeCode>();
type_code->name = "mmx.Node.get_swap_info.return";
type_code->type_hash = vnx::Hash64(0x302116742171428ull);
type_code->code_hash = vnx::Hash64(0x506882e0fdd86d70ull);
type_code->code_hash = vnx::Hash64(0x985f6fd1925ad73eull);
type_code->is_native = true;
type_code->is_class = true;
type_code->is_return = true;
Expand Down
4 changes: 2 additions & 2 deletions generated/src/Node_get_swaps_return.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace mmx {


const vnx::Hash64 Node_get_swaps_return::VNX_TYPE_HASH(0x6c94172788fc0d28ull);
const vnx::Hash64 Node_get_swaps_return::VNX_CODE_HASH(0xf8ce02560bcd40b8ull);
const vnx::Hash64 Node_get_swaps_return::VNX_CODE_HASH(0x22f3be524a088a8dull);

vnx::Hash64 Node_get_swaps_return::get_type_hash() const {
return VNX_TYPE_HASH;
Expand Down Expand Up @@ -114,7 +114,7 @@ std::shared_ptr<vnx::TypeCode> Node_get_swaps_return::static_create_type_code()
auto type_code = std::make_shared<vnx::TypeCode>();
type_code->name = "mmx.Node.get_swaps.return";
type_code->type_hash = vnx::Hash64(0x6c94172788fc0d28ull);
type_code->code_hash = vnx::Hash64(0xf8ce02560bcd40b8ull);
type_code->code_hash = vnx::Hash64(0x22f3be524a088a8dull);
type_code->is_native = true;
type_code->is_class = true;
type_code->is_return = true;
Expand Down
49 changes: 45 additions & 4 deletions generated/src/swap_info_t.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace mmx {


const vnx::Hash64 swap_info_t::VNX_TYPE_HASH(0x7586be908f15ae8ull);
const vnx::Hash64 swap_info_t::VNX_CODE_HASH(0x6968eccd619bc7d6ull);
const vnx::Hash64 swap_info_t::VNX_CODE_HASH(0x49bbbd84c9e78bdbull);

vnx::Hash64 swap_info_t::get_type_hash() const {
return VNX_TYPE_HASH;
Expand Down Expand Up @@ -55,6 +55,8 @@ void swap_info_t::accept(vnx::Visitor& _visitor) const {
_visitor.type_field(_type_code->fields[5], 5); vnx::accept(_visitor, fees_paid);
_visitor.type_field(_type_code->fields[6], 6); vnx::accept(_visitor, fees_claimed);
_visitor.type_field(_type_code->fields[7], 7); vnx::accept(_visitor, user_total);
_visitor.type_field(_type_code->fields[8], 8); vnx::accept(_visitor, avg_apy_1d);
_visitor.type_field(_type_code->fields[9], 9); vnx::accept(_visitor, avg_apy_7d);
_visitor.type_end(*_type_code);
}

Expand All @@ -68,6 +70,8 @@ void swap_info_t::write(std::ostream& _out) const {
_out << ", \"fees_paid\": "; vnx::write(_out, fees_paid);
_out << ", \"fees_claimed\": "; vnx::write(_out, fees_claimed);
_out << ", \"user_total\": "; vnx::write(_out, user_total);
_out << ", \"avg_apy_1d\": "; vnx::write(_out, avg_apy_1d);
_out << ", \"avg_apy_7d\": "; vnx::write(_out, avg_apy_7d);
_out << "}";
}

Expand All @@ -88,13 +92,19 @@ vnx::Object swap_info_t::to_object() const {
_object["fees_paid"] = fees_paid;
_object["fees_claimed"] = fees_claimed;
_object["user_total"] = user_total;
_object["avg_apy_1d"] = avg_apy_1d;
_object["avg_apy_7d"] = avg_apy_7d;
return _object;
}

void swap_info_t::from_object(const vnx::Object& _object) {
for(const auto& _entry : _object.field) {
if(_entry.first == "address") {
_entry.second.to(address);
} else if(_entry.first == "avg_apy_1d") {
_entry.second.to(avg_apy_1d);
} else if(_entry.first == "avg_apy_7d") {
_entry.second.to(avg_apy_7d);
} else if(_entry.first == "balance") {
_entry.second.to(balance);
} else if(_entry.first == "fees_claimed") {
Expand Down Expand Up @@ -138,6 +148,12 @@ vnx::Variant swap_info_t::get_field(const std::string& _name) const {
if(_name == "user_total") {
return vnx::Variant(user_total);
}
if(_name == "avg_apy_1d") {
return vnx::Variant(avg_apy_1d);
}
if(_name == "avg_apy_7d") {
return vnx::Variant(avg_apy_7d);
}
return vnx::Variant();
}

Expand All @@ -158,6 +174,10 @@ void swap_info_t::set_field(const std::string& _name, const vnx::Variant& _value
_value.to(fees_claimed);
} else if(_name == "user_total") {
_value.to(user_total);
} else if(_name == "avg_apy_1d") {
_value.to(avg_apy_1d);
} else if(_name == "avg_apy_7d") {
_value.to(avg_apy_7d);
}
}

Expand Down Expand Up @@ -185,11 +205,11 @@ std::shared_ptr<vnx::TypeCode> swap_info_t::static_create_type_code() {
auto type_code = std::make_shared<vnx::TypeCode>();
type_code->name = "mmx.swap_info_t";
type_code->type_hash = vnx::Hash64(0x7586be908f15ae8ull);
type_code->code_hash = vnx::Hash64(0x6968eccd619bc7d6ull);
type_code->code_hash = vnx::Hash64(0x49bbbd84c9e78bdbull);
type_code->is_native = true;
type_code->native_size = sizeof(::mmx::swap_info_t);
type_code->create_value = []() -> std::shared_ptr<vnx::Value> { return std::make_shared<vnx::Struct<swap_info_t>>(); };
type_code->fields.resize(8);
type_code->fields.resize(10);
{
auto& field = type_code->fields[0];
field.is_extended = true;
Expand Down Expand Up @@ -238,6 +258,18 @@ std::shared_ptr<vnx::TypeCode> swap_info_t::static_create_type_code() {
field.name = "user_total";
field.code = {11, 2, 11, 16, 1};
}
{
auto& field = type_code->fields[8];
field.data_size = 16;
field.name = "avg_apy_1d";
field.code = {11, 2, 10};
}
{
auto& field = type_code->fields[9];
field.data_size = 16;
field.name = "avg_apy_7d";
field.code = {11, 2, 10};
}
type_code->build();
return type_code;
}
Expand Down Expand Up @@ -278,8 +310,14 @@ void read(TypeInput& in, ::mmx::swap_info_t& value, const TypeCode* type_code, c
}
}
}
in.read(type_code->total_field_size);
const char* const _buf = in.read(type_code->total_field_size);
if(type_code->is_matched) {
if(const auto* const _field = type_code->field_map[8]) {
vnx::read_value(_buf + _field->offset, value.avg_apy_1d, _field->code.data());
}
if(const auto* const _field = type_code->field_map[9]) {
vnx::read_value(_buf + _field->offset, value.avg_apy_7d, _field->code.data());
}
}
for(const auto* _field : type_code->ext_fields) {
switch(_field->native_index) {
Expand Down Expand Up @@ -309,6 +347,9 @@ void write(TypeOutput& out, const ::mmx::swap_info_t& value, const TypeCode* typ
else if(code && code[0] == CODE_STRUCT) {
type_code = type_code->depends[code[1]];
}
char* const _buf = out.write(32);
vnx::write_value(_buf + 0, value.avg_apy_1d);
vnx::write_value(_buf + 16, value.avg_apy_7d);
vnx::write(out, value.name, type_code, type_code->fields[0].code.data());
vnx::write(out, value.address, type_code, type_code->fields[1].code.data());
vnx::write(out, value.tokens, type_code, type_code->fields[2].code.data());
Expand Down
3 changes: 3 additions & 0 deletions interface/swap_info_t.vni
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ struct swap_info_t {
uint128 fees_claimed[2];
uint128 user_total[2];

double avg_apy_1d[2];
double avg_apy_7d[2];


double get_price() const;

Expand Down
16 changes: 16 additions & 0 deletions src/Node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1287,6 +1287,22 @@ swap_info_t Node::get_swap_info(const addr_t& address) const
for(size_t i = 0; i < 2 && i < user_total.size(); ++i) {
out.user_total[i] = to_uint(user_total[i]);
}
const auto height = get_height();
const auto ref_fees_paid = to_ref(data["fees_paid"]);
{
const auto prev_fees_paid = read_storage_array(address, ref_fees_paid, height - std::min(8640u, height));
for(size_t i = 0; i < 2; ++i) {
const uint256_t prev_i = i < prev_fees_paid.size() ? to_uint(prev_fees_paid[i]) : uint256_0;
out.avg_apy_1d[i] = uint128(365 * (out.fees_paid[i] - prev_i)).to_double() / out.user_total[i].to_double();
}
}
{
const auto prev_fees_paid = read_storage_array(address, ref_fees_paid, height - std::min(60480u, height));
for(size_t i = 0; i < 2; ++i) {
const uint256_t prev_i = i < prev_fees_paid.size() ? to_uint(prev_fees_paid[i]) : uint256_0;
out.avg_apy_7d[i] = uint128(52 * (out.fees_paid[i] - prev_i)).to_double() / out.user_total[i].to_double();
}
}
return out;
}

Expand Down
6 changes: 6 additions & 0 deletions www/web-gui/public/swap.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,8 @@ Vue.component('swap-info', {
<th></th>
<th>Pool Balance</th>
<th>Symbol</th>
<th>APY (last day)</th>
<th>APY (last 7 days)</th>
<th>Contract</th>
</tr>
</thead>
Expand All @@ -265,6 +267,8 @@ Vue.component('swap-info', {
<td class="key-cell">Token</td>
<td><b>{{ parseFloat( (data.balance[0].value).toPrecision(6) ) }}</b></td>
<td>{{data.symbols[0]}}</td>
<td>{{(data.avg_apy_1d[0] * 100).toFixed(2)}} %</td>
<td>{{(data.avg_apy_7d[0] * 100).toFixed(2)}} %</td>
<td>
<template v-if="data.symbols[0] != 'MMX'">
<router-link :to="'/explore/address/' + data.tokens[0]">{{data.tokens[0]}}</router-link>
Expand All @@ -275,6 +279,8 @@ Vue.component('swap-info', {
<td class="key-cell">Currency</td>
<td><b>{{ parseFloat( (data.balance[1].value).toPrecision(6) ) }}</b></td>
<td>{{data.symbols[1]}}</td>
<td>{{(data.avg_apy_1d[1] * 100).toFixed(2)}} %</td>
<td>{{(data.avg_apy_7d[1] * 100).toFixed(2)}} %</td>
<td>
<template v-if="data.symbols[1] != 'MMX'">
<router-link :to="'/explore/address/' + data.tokens[1]">{{data.tokens[1]}}</router-link>
Expand Down

0 comments on commit d780b7a

Please sign in to comment.