Published 18 July 2019

Credits High Level API Demo

https://github.com/CREDITSCOM/examples

https://github.com/CREDITSCOM/examples/tree/master/API/CreditsHighLevelAPI

It consists:

Wrapper around low-level API

class client

{

private:

    std::shared_ptr<apache::thrift::transport::TSocket> m_socket;

    std::shared_ptr<apache::thrift::transport::TTransport> m_transport;

    std::shared_ptr<apache::thrift::protocol::TProtocol> m_protocol;

    std::shared_ptr<api::APIClient> m_api;

 

    std::unique_ptr<keys> m_keys;

 

    void connect();

    void disconnect();

    short fee(double value);

    std::unique_ptr<api::Transaction> make_transaction_with_smart_contract(std::string code, double fee_value);

    std::unique_ptr<api::Transaction> make_transaction(int32_t integral, int64_t fraction, double fee_value);

    template<class T>

    void cp(std::vector<byte>& arr, T& value, int16_t size, bool reverse);

 

public:

    client(std::string ip, int port);

    ~client();

 

    void set_keys(const std::string& publicKey, const std::string& privateKey, const std::string& targetKey);

 

    std::unique_ptr<api::WalletBalanceGetResult> wallet_balance_get();

    void transfer_coins(int32_t integral, int64_t fraction, double fee_value);

    void deploy_smart(std::string code, double fee_value);

};




Server 

void GetBalance(hlapi::api::Amount& _return, const std::string& publicKey)

 

void TransferCoins(std::string& _return, const std::string& publicKey, const std::string& privateKey, const std::string& targetKey, const int32_t integral, const int64_t fraction, const double fee)

 

void DeploySmartContract(std::string& _return, const std::string& publicKey, const std::string& privateKey, const std::string& targetKey, const std::string& code, const double fee)

 

Client

class client

{

private:

std::shared_ptr<apache::thrift::transport::TSocket> m_socket;

std::shared_ptr<apache::thrift::transport::TTransport> m_transport;

std::shared_ptr<apache::thrift::protocol::TProtocol> m_protocol;

std::shared_ptr<hlapi::api::ApiClient> m_api;

 

keys m_keys;

 

void connect();

void disconnect();

 

public:

client(std::string ip, int port);

~client();

 

void set_keys(const keys& ks);

 

void wallet_balance();

void transfer_coins(int32_t integral, int32_t fraction, double fee_value);

void deploy_smart_contract(std::string code, double fee_value);

};

 

Using

int main(int argc, char* argv[])

{

    if (argc != 6)

    {

        cout << "Usage: main.exe NodeIpAddress NodePort YourPublicKey YourPrivateKey TargetPublicKey" << std::endl;

        return 1;

    }

 

    auto c = make_unique<client>(argv[1], atoi(argv[2]));

    c->set_keys(argv[3], argv[4], argv[5]);

 

    cout << " ****** Credits API Demo C++ ******" << endl;

 

    cout << "****** Wallet Get Balance ******" << endl;

    c->wallet_balance_get();

    cout << endl << " ****** End ******" << endl << endl;

 

    cout << "****** Transfer Coins ******" << endl;

    c->transfer_coins(1, 0, 0.9);

    cout << endl << " ****** End ******" << endl << endl;

 

    cout << endl << "****** Deploy Smart Contract ******" << endl;

    c->deploy_smart("", 1);

    cout << endl << " ****** End ******" << endl << endl;

}

 

Thrift file

struct Amount

{

    1: i32 integral,

    2: i64 fraction,

    3: string message

}

 

service Api

{

    string Hello()

    Amount GetBalance(1: string publicKey)

    string TransferCoins(1: string publicKey, 2: string privateKey, 3: string targetKey, 4: i32 integral, 5: i64 fraction, 6: double fee)

    string DeploySmartContract(1: string publicKey, 2: string privateKey, 3: string targetKey, 4: string code, 5: double fee)

}

 

Votes 0, average rating 0

Contents