Skip to content

Commit

Permalink
Problem: Missing examples of using the cpp sdk (fix #103)
Browse files Browse the repository at this point in the history
Solution:
- Add examples.
  • Loading branch information
damoncro committed Jun 15, 2022
1 parent f37aa33 commit fe25ade
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 0 deletions.
10 changes: 10 additions & 0 deletions demo/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@ x86_64_build: prepare_x86_64 easywsclient.o.x86_64
-std=c++14 \
$(FLAGS)

examples:
g++ -o new_wallet new_wallet.cc lib/libplay_cpp_sdk.a -std=c++14 $(FLAGS)
g++ -o restore_wallet restore_wallet.cc lib/libplay_cpp_sdk.a -std=c++14 $(FLAGS)
g++ -o get_erc20_transfer_history_blocking get_erc20_transfer_history_blocking.cc lib/libplay_cpp_sdk.a -std=c++14 $(FLAGS)
g++ -o get_erc721_transfer_history_blocking get_erc721_transfer_history_blocking.cc lib/libplay_cpp_sdk.a -std=c++14 $(FLAGS)
./new_wallet
./restore_wallet
. .env && ./get_erc20_transfer_history_blocking
. .env && ./get_erc721_transfer_history_blocking

run_static:
./demostatic

Expand Down
60 changes: 60 additions & 0 deletions demo/get_erc20_transfer_history_blocking.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include "include/extra-cpp-bindings/src/lib.rs.h"
#include "include/rust/cxx.h"
#include <iostream>
using namespace com::crypto::game_sdk;
using namespace rust;
using namespace std;

// Read CronoScan api key in env
const String CRONOSCAN_API_KEY = getenv("CRONOSCAN_API_KEY");

int main(int argc, char *argv[]) {
// Get a list of "CRC20 - Token Transfer Events" by Address
// Returns up to a maximum of the last 10000 transactions only
// https://cronoscan.com/tokentxns?a=0xa9b34a4b568e640d5e5d1e6e13101025e1262864
Vec<RawTxDetail> erc20_txs = get_erc20_transfer_history_blocking(
"0xa9b34a4b568e640d5e5d1e6e13101025e1262864", "", QueryOption::ByAddress,
CRONOSCAN_API_KEY);

for (Vec<RawTxDetail>::iterator ptr = erc20_txs.begin();
ptr < erc20_txs.end(); ptr++) {
cout << "hash: " << ptr->hash << " ";
cout << "to: " << ptr->to_address << " ";
cout << "from: " << ptr->from_address << " ";
cout << "value:" << ptr->value << " ";
cout << "block_no: " << ptr->block_no << " ";
cout << "timestamp: " << ptr->timestamp << " ";
cout << "contract: " << ptr->contract_address << " " << endl;
}

cout << "A total of " << erc20_txs.size() << " transactions" << endl;

// Get a list of "CRC20 - Token Transfer Events" by ByAddressAndContract
// Returns up to a maximum of the last 10000 transactions only
// https://cronoscan.com/token/0x2d03bece6747adc00e1a131bba1469c15fd11e03?a=0xa9b34a4b568e640d5e5d1e6e13101025e1262864
erc20_txs = get_erc20_transfer_history_blocking(
"0xa9b34a4b568e640d5e5d1e6e13101025e1262864",
"0x2D03bECE6747ADC00E1a131BBA1469C15fD11e03",
QueryOption::ByAddressAndContract, CRONOSCAN_API_KEY);

for (Vec<RawTxDetail>::iterator ptr = erc20_txs.begin();
ptr < erc20_txs.end(); ptr++) {
cout << "hash: " << ptr->hash << " ";
cout << "to: " << ptr->to_address << " ";
cout << "from: " << ptr->from_address << " ";
cout << "value:" << ptr->value << " ";
cout << "block_no: " << ptr->block_no << " ";
cout << "timestamp: " << ptr->timestamp << " ";
cout << "contract: " << ptr->contract_address << " " << endl;
}
cout << "A total of " << erc20_txs.size() << " transactions" << endl;

// Get a list of "CRC20 - Token Transfer Events" by ByContract
// Returns up to a maximum of the last 10000 transactions only
erc20_txs = get_erc20_transfer_history_blocking(
"", "0x66e428c3f67a68878562e79A0234c1F83c208770", QueryOption::ByContract,
CRONOSCAN_API_KEY);

cout << "A total of " << erc20_txs.size() << " transactions" << endl;
return 0;
}
71 changes: 71 additions & 0 deletions demo/get_erc721_transfer_history_blocking.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#include "include/extra-cpp-bindings/src/lib.rs.h"
#include "include/rust/cxx.h"
#include <iostream>
using namespace com::crypto::game_sdk;
using namespace rust;
using namespace std;

// Read CronoScan api key in env
const String CRONOSCAN_API_KEY = getenv("CRONOSCAN_API_KEY");

int main(int argc, char *argv[]) {
// Get a list of "ERC721 - Token Transfer Events" by Address
// Returns up to a maximum of the last 10000 transactions only
// https://cronoscan.com/tokentxns-nft?a=0x668f126b87936df4f9a98f18c44eb73868fffea0
Vec<RawTxDetail> erc721_txs = get_erc721_transfer_history_blocking(
"0x668f126b87936df4f9a98f18c44eb73868fffea0", "", QueryOption::ByAddress,
CRONOSCAN_API_KEY);

for (Vec<RawTxDetail>::iterator ptr = erc721_txs.begin();
ptr < erc721_txs.end(); ptr++) {
cout << "hash: " << ptr->hash << " ";
cout << "to: " << ptr->to_address << " ";
cout << "from: " << ptr->from_address << " ";
cout << "TokenID:" << ptr->value << " ";
cout << "block_no: " << ptr->block_no << " ";
cout << "timestamp: " << ptr->timestamp << " ";
cout << "contract: " << ptr->contract_address << " " << endl;
}

cout << "A total of " << erc721_txs.size() << " transactions" << endl;

// Get a list of "ERC721 - Token Transfer Events" ByAddressAndContract
// Returns up to a maximum of the last 10000 transactions only
// https://cronoscan.com/token/0x562f021423d75a1636db5be1c4d99bc005ccebfe?a=0x668f126b87936df4f9a98f18c44eb73868fffea0
erc721_txs = get_erc721_transfer_history_blocking(
"0x668f126b87936df4f9a98f18c44eb73868fffea0",
"0x562F021423D75A1636DB5bE1C4D99Bc005ccebFe",
QueryOption::ByAddressAndContract, CRONOSCAN_API_KEY);

for (Vec<RawTxDetail>::iterator ptr = erc721_txs.begin();
ptr < erc721_txs.end(); ptr++) {
cout << "hash: " << ptr->hash << " ";
cout << "to: " << ptr->to_address << " ";
cout << "from: " << ptr->from_address << " ";
cout << "TokenID:" << ptr->value << " ";
cout << "block_no: " << ptr->block_no << " ";
cout << "timestamp: " << ptr->timestamp << " ";
cout << "contract: " << ptr->contract_address << " " << endl;
}
cout << "A total of " << erc721_txs.size() << " transactions" << endl;

// Get a list of "ERC721 - Token Transfer Events" ByContract
// Returns up to a maximum of the last 10000 transactions only
// https://cronoscan.com/token/0x18b73d1f9e2d97057dec3f8d6ea9e30fcadb54d7
erc721_txs = get_erc721_transfer_history_blocking(
"", "0x18b73D1f9e2d97057deC3f8D6ea9e30FCADB54D7", QueryOption::ByContract,
CRONOSCAN_API_KEY);
for (Vec<RawTxDetail>::iterator ptr = erc721_txs.begin();
ptr < erc721_txs.end(); ptr++) {
cout << "hash: " << ptr->hash << " ";
cout << "to: " << ptr->to_address << " ";
cout << "from: " << ptr->from_address << " ";
cout << "TokenID:" << ptr->value << " ";
cout << "block_no: " << ptr->block_no << " ";
cout << "timestamp: " << ptr->timestamp << " ";
cout << "contract: " << ptr->contract_address << " " << endl;
}

cout << "A total of " << erc721_txs.size() << " transactions" << endl;
return 0;
}
14 changes: 14 additions & 0 deletions demo/new_wallet.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include "include/defi-wallet-core-cpp/src/lib.rs.h"
#include "include/rust/cxx.h"
#include <iostream>
using namespace org::defi_wallet_core;
using namespace std;

int main(int argc, char *argv[]) {
auto wallet = new_wallet("", MnemonicWordCount::TwentyFour);
cout << wallet->get_default_address(CoinType::CronosMainnet) << endl;
cout << wallet->get_address(CoinType::CronosMainnet, 0) << endl;
cout << wallet->get_eth_address(0) << endl;
auto private_key = wallet->get_key("m/44'/60/0'/0/0");
return 0;
}
14 changes: 14 additions & 0 deletions demo/restore_wallet.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include "include/defi-wallet-core-cpp/src/lib.rs.h"
#include "include/rust/cxx.h"
#include <iostream>
using namespace org::defi_wallet_core;
using namespace std;

int main(int argc, char *argv[]) {
auto wallet = restore_wallet("shed crumble dismiss loyal latin million oblige gesture shrug still oxygen custom remove ribbon disorder palace addict again blanket sad flock consider obey popular", "");
cout << wallet->get_default_address(CoinType::CronosMainnet) << endl;
cout << wallet->get_address(CoinType::CronosMainnet, 0) << endl;
cout << wallet->get_eth_address(0) << endl;
auto private_key = wallet->get_key("m/44'/60/0'/0/0");
return 0;
}

0 comments on commit fe25ade

Please sign in to comment.