EOS Bancor 算法测试

2020-02-14 14:02:53

为了实际项目中,解决相对有限资源,异步动态价格交换的问题,测试下Bancor 算法

从EOS系统合约中,抽离出测试代码如下

#include <iostream>
#include <cmath>
#include <list>

typedef double real_type;
int64_t supply_amount = 10000000000000ll;                         // RAMCORE

struct connector {
    int64_t balance;
    double weight = .5;
};

int64_t convert_from_exchange( connector& reserve, const int64_t& tokens_amount )
{
    const double R0 = reserve.balance;
    const double S0 = supply_amount;
    const double dS = -tokens_amount; // dS < 0, tokens are subtracted from supply
    const double Fi = double(1) / reserve.weight;

    double dR = R0 * ( std::pow(1. + dS / S0, Fi) - 1. ); // dR < 0 since dS < 0
    if ( dR > 0 ) dR = 0; // rounding errors
    reserve.balance -= int64_t(-dR);
    supply_amount   -= tokens_amount;
    return int64_t(-dR);
}

int64_t convert_to_exchange( connector& reserve, const int64_t& tokens_amount )
{
    const double S0 = supply_amount;
    const double R0 = reserve.balance;
    const double dR = tokens_amount; // dS < 0, tokens are subtracted from supply
    const double F =  reserve.weight;

    double dS = S0 * ( std::pow(1. + dR / R0, F) - 1. );
    if ( dS < 0 ) dS = 0; // rounding errors
    reserve.balance += tokens_amount;
    supply_amount   += int64_t(dS);
    return int64_t(dS);
}

// RAM / EOS
int main() {
    struct connector base;           // RAM
    base.balance = 90091267903;
    struct connector quote;
    quote.balance = 56842445020ll;   // EOS

    int64_t ram_byte = 0;
    int64_t tokens_amount = 10000000000;
    int64_t total_tokens_amount = 0;
    int64_t total_ram_byte = 0;
    int total_num = 10;                 // 测试循环次数
    std::list <int64_t > list_ram_byte;
    // Buy EOS->RAM
    int64_t tmp = 0;
    for (int i = 0; i < total_num; ++i) {
        tmp = convert_to_exchange( quote, tokens_amount );
        ram_byte = convert_from_exchange( base, tmp );
        total_ram_byte += ram_byte;
        list_ram_byte.push_back(ram_byte);
        total_tokens_amount += tokens_amount;
        std::cout << "Buy EOS->RAM " << ram_byte << std::endl;
    }

    for (int i = 0; i < total_num; ++i) {
        tmp = convert_to_exchange( base, list_ram_byte.back() );
        list_ram_byte.pop_back();
        tokens_amount = convert_from_exchange( quote, tmp );
        std::cout << "One Sell RAM->EOS " << tokens_amount << std::endl;
    }
    // Sell RAM->EOS
    //tmp = convert_to_exchange( base, total_ram_byte );
    //tokens_amount = convert_from_exchange( quote, tmp );
    //std::cout << "One Sell RAM->EOS " << tokens_amount << std::endl;

    return 0;
}

测试结果如下

Buy EOS->RAM 13478152673
Buy EOS->RAM 9970155844
Buy EOS->RAM 7674007724
Buy EOS->RAM 6089163862
Buy EOS->RAM 4949324006
Buy EOS->RAM 4102144882
Buy EOS->RAM 3455335389
Buy EOS->RAM 2950326086
Buy EOS->RAM 2548490488
Buy EOS->RAM 2223515894


Sell RAM->EOS 9999999996
Sell RAM->EOS 10000000000
Sell RAM->EOS 9999999999
Sell RAM->EOS 10000000000
Sell RAM->EOS 9999999998
Sell RAM->EOS 10000000000
Sell RAM->EOS 9999999999
Sell RAM->EOS 10000000000
Sell RAM->EOS 10000000000
Sell RAM->EOS 10000000001

符合预期,剩余资源越少,价格越高的需求

参考

https://zhuanlan.zhihu.com/p/40162089

当前页面是本站的「Baidu MIP」版。发表评论请点击:完整版 »