From fe25adea6d8753807bd132f6211b32d8dc776619 Mon Sep 17 00:00:00 2001 From: Damon Chen Date: Wed, 15 Jun 2022 13:00:10 +0800 Subject: [PATCH] Problem: Missing examples of using the cpp sdk (fix #103) Solution: - Add examples. --- demo/Makefile | 10 +++ demo/get_erc20_transfer_history_blocking.cc | 60 +++++++++++++++++ demo/get_erc721_transfer_history_blocking.cc | 71 ++++++++++++++++++++ demo/new_wallet.cc | 14 ++++ demo/restore_wallet.cc | 14 ++++ 5 files changed, 169 insertions(+) create mode 100644 demo/get_erc20_transfer_history_blocking.cc create mode 100644 demo/get_erc721_transfer_history_blocking.cc create mode 100644 demo/new_wallet.cc create mode 100644 demo/restore_wallet.cc diff --git a/demo/Makefile b/demo/Makefile index 760390bb..06084283 100644 --- a/demo/Makefile +++ b/demo/Makefile @@ -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 diff --git a/demo/get_erc20_transfer_history_blocking.cc b/demo/get_erc20_transfer_history_blocking.cc new file mode 100644 index 00000000..e6352dc3 --- /dev/null +++ b/demo/get_erc20_transfer_history_blocking.cc @@ -0,0 +1,60 @@ +#include "include/extra-cpp-bindings/src/lib.rs.h" +#include "include/rust/cxx.h" +#include +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 erc20_txs = get_erc20_transfer_history_blocking( + "0xa9b34a4b568e640d5e5d1e6e13101025e1262864", "", QueryOption::ByAddress, + CRONOSCAN_API_KEY); + + for (Vec::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::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; +} diff --git a/demo/get_erc721_transfer_history_blocking.cc b/demo/get_erc721_transfer_history_blocking.cc new file mode 100644 index 00000000..28506c18 --- /dev/null +++ b/demo/get_erc721_transfer_history_blocking.cc @@ -0,0 +1,71 @@ +#include "include/extra-cpp-bindings/src/lib.rs.h" +#include "include/rust/cxx.h" +#include +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 erc721_txs = get_erc721_transfer_history_blocking( + "0x668f126b87936df4f9a98f18c44eb73868fffea0", "", QueryOption::ByAddress, + CRONOSCAN_API_KEY); + + for (Vec::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::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::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; +} diff --git a/demo/new_wallet.cc b/demo/new_wallet.cc new file mode 100644 index 00000000..4914ba35 --- /dev/null +++ b/demo/new_wallet.cc @@ -0,0 +1,14 @@ +#include "include/defi-wallet-core-cpp/src/lib.rs.h" +#include "include/rust/cxx.h" +#include +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; +} diff --git a/demo/restore_wallet.cc b/demo/restore_wallet.cc new file mode 100644 index 00000000..067161cc --- /dev/null +++ b/demo/restore_wallet.cc @@ -0,0 +1,14 @@ +#include "include/defi-wallet-core-cpp/src/lib.rs.h" +#include "include/rust/cxx.h" +#include +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; +}