Skip to content

Commit

Permalink
Problem: Missing transaction receipt support (fix #317)
Browse files Browse the repository at this point in the history
Add GetEthTransactionReceipt and WaitForTransactionReceipt
  • Loading branch information
damoncro committed May 25, 2023
1 parent 51f9ae6 commit 0858bc2
Show file tree
Hide file tree
Showing 21 changed files with 355 additions and 140 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ UNAME := $(shell uname)
PWD = $(shell pwd)

# Set the play cpp sdk version
PLAYCPPSDK=v0.0.19-alpha
PLAYCPPSDK=v0.0.20-alpha
# Set the play-cpp-sdk cache path
PLAYCPPSDK_CACHE_DIR=./install/$(PLAYCPPSDK)
# Set the play-cpp-sdk target path
Expand Down
60 changes: 60 additions & 0 deletions Source/CronosPlayUnreal/Private/DefiWalletCoreActor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "PlayCppSdkLibrary/Include/defi-wallet-core-cpp/src/lib.rs.h"
#include "PlayCppSdkLibrary/Include/rust/cxx.h"
#include "TxBuilder.h"
#include "Utlis.h"

#define SECURE_STORAGE_CLASS "com/cronos/play/SecureStorage"

Expand Down Expand Up @@ -2180,3 +2181,62 @@ void zeroize_buffer(char *dst, char value, int length) {
dst[i] = value; // NOLINT
}
}

void ADefiWalletCoreActor::GetEthTransactionReceipt(
TArray<uint8> txHash, FTransactionReceiptDelegate Out) {

AsyncTask(ENamedThreads::AnyHiPriThreadNormalTask, [this, Out, txHash]() {
FString tx_receipt;
FString result;
try {
if (NULL == _coreWallet) {
result = TEXT("Invalid Wallet");
} else {
std::string mycronosrpc = TCHAR_TO_UTF8(*myCronosRpc);
rust::Vec<uint8_t> dst_tx_hash;
UUtlis::copyTArrayToVec(txHash, dst_tx_hash);
tx_receipt = UTF8_TO_TCHAR(get_eth_transaction_receipt_blocking(
dst_tx_hash, mycronosrpc)
.c_str());
}
} catch (const std::exception &e) {
result = FString::Printf(
TEXT("CronosPlayUnreal WaitForTransactionReceipt Error: %s"),
UTF8_TO_TCHAR(e.what()));
}

AsyncTask(ENamedThreads::GameThread, [Out, tx_receipt, result]() {
Out.ExecuteIfBound(tx_receipt, result);
});
});
}

void ADefiWalletCoreActor::WaitForTransactionReceipt(
TArray<uint8> txHash, FTransactionReceiptDelegate Out) {

AsyncTask(ENamedThreads::AnyHiPriThreadNormalTask, [this, Out, txHash]() {
FString tx_receipt;
FString result;
try {
if (NULL == _coreWallet) {
result = TEXT("Invalid Wallet");
} else {
std::string mycronosrpc = TCHAR_TO_UTF8(*myCronosRpc);
rust::Vec<uint8_t> dst_tx_hash;
UUtlis::copyTArrayToVec(txHash, dst_tx_hash);
tx_receipt =
UTF8_TO_TCHAR(wait_for_transaction_receipt_blocking(
dst_tx_hash, mycronosrpc)
.c_str());
}
} catch (const std::exception &e) {
result = FString::Printf(
TEXT("CronosPlayUnreal WaitForTransactionReceipt Error: %s"),
UTF8_TO_TCHAR(e.what()));
}

AsyncTask(ENamedThreads::GameThread, [Out, tx_receipt, result]() {
Out.ExecuteIfBound(tx_receipt, result);
});
});
}
18 changes: 18 additions & 0 deletions Source/CronosPlayUnreal/Private/Utlis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include "Utlis.h"
#include "Containers/UnrealString.h"

using namespace rust;

FString UUtlis::ToHex(TArray<uint8> address) {
return BytesToHex(address.GetData(), address.Num());
}
Expand All @@ -17,3 +19,19 @@ std::array<std::uint8_t, 20> UUtlis::ToArray(TArray<uint8> address) {
}
return std_array;
}

void UUtlis::copyTArrayToVec(const TArray<uint8> &src, rust::Vec<uint8_t> &dst) {
dst.clear();
for (int i = 0; i < src.Num(); i++) {
dst.push_back(src[i]);
}
assert(dst.size() == src.Num());
}
void UUtlis::copyVecToTArray(const rust::Vec<uint8_t> &src,
TArray<uint8> &dst) {
dst.Empty();
for (int i = 0; i < src.size(); i++) {
dst.Add(src[i]);
}
assert(dst.Num() == src.size());
}
29 changes: 29 additions & 0 deletions Source/CronosPlayUnreal/Public/DefiWalletCoreActor.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ DECLARE_DYNAMIC_DELEGATE_TwoParams(FErc20ApproveDelegate,
FCronosTransactionReceiptRaw, TxResult,
FString, Result);

DECLARE_DYNAMIC_DELEGATE_TwoParams(FTransactionReceiptDelegate,
FString, TxReceipt,
FString, Result);

/**
Cosmos NFT Denom
*/
Expand Down Expand Up @@ -1026,6 +1030,31 @@ class CRONOSPLAYUNREAL_API ADefiWalletCoreActor : public AActor {
int32 walletindex, bool &success,
FString &output_message);

/**
* Get eth transaction receipt
* @param txHash transaction hash
* @param Out event delegate which is triggered after getting eth transaction receipt
*/
UFUNCTION(BlueprintCallable,
meta = (DisplayName = "GetEthTransactionReceipt",
Keywords = "Wallet"),
Category = "CronosPlayUnreal")
void GetEthTransactionReceipt(TArray<uint8> txHash,
FTransactionReceiptDelegate Out);

/**
* Wait for transaction receipt
* @param txHash transaction hash
* @param Out event delegate which is triggered after waiting eth
* transaction receipt
*/
UFUNCTION(BlueprintCallable,
meta = (DisplayName = "WaitForTransactionReceipt",
Keywords = "Wallet"),
Category = "CronosPlayUnreal")
void WaitForTransactionReceipt(TArray<uint8> txHash,
FTransactionReceiptDelegate Out);

/**
* Grpc address
* for example: http://127.0.0.1:1316
Expand Down
4 changes: 4 additions & 0 deletions Source/CronosPlayUnreal/Public/Utlis.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "CoreMinimal.h"
#include "Kismet/BlueprintFunctionLibrary.h"
#include "PlayCppSdkLibrary/Include/rust/cxx.h"
#include "Utlis.generated.h"

/**
Expand Down Expand Up @@ -33,4 +34,7 @@ class CRONOSPLAYUNREAL_API UUtlis : public UBlueprintFunctionLibrary {
*
*/
static std::array<std::uint8_t, 20> ToArray(TArray<uint8> address);

static void copyTArrayToVec(const TArray<uint8> &src, rust::Vec<uint8_t> &dst);
static void copyVecToTArray(const rust::Vec<uint8_t> &src, TArray<uint8> &dst);
};
2 changes: 1 addition & 1 deletion Source/ThirdParty/PlayCppSdkLibrary/Include/android.cc
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,4 @@ rust::String secureStorageRead(rust::String userkey) {

} // namespace defi_wallet_core
} // namespace org
#endif
#endif
2 changes: 1 addition & 1 deletion Source/ThirdParty/PlayCppSdkLibrary/Include/android.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ int secureStorageWrite(rust::String userkey, rust::String uservalue);
rust::String secureStorageRead(rust::String userkey);
} // namespace defi_wallet_core
} // namespace org
#endif
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ class String final {
static String lossy(const char16_t *) noexcept;
static String lossy(const char16_t *, std::size_t) noexcept;

String &operator=(const String &) &noexcept;
String &operator=(String &&) &noexcept;
String &operator=(const String &) & noexcept;
String &operator=(String &&) & noexcept;

explicit operator std::string() const;

Expand Down Expand Up @@ -110,8 +110,8 @@ template <> struct copy_assignable_if<false> {
copy_assignable_if() noexcept = default;
copy_assignable_if(const copy_assignable_if &) noexcept = default;
copy_assignable_if &
operator=(const copy_assignable_if &) &noexcept = delete;
copy_assignable_if &operator=(copy_assignable_if &&) &noexcept = default;
operator=(const copy_assignable_if &) & noexcept = delete;
copy_assignable_if &operator=(copy_assignable_if &&) & noexcept = default;
};
} // namespace detail

Expand All @@ -124,8 +124,8 @@ class Slice final
Slice() noexcept;
Slice(T *, std::size_t count) noexcept;

Slice &operator=(const Slice<T> &) &noexcept = default;
Slice &operator=(Slice<T> &&) &noexcept = default;
Slice &operator=(const Slice<T> &) & noexcept = default;
Slice &operator=(Slice<T> &&) & noexcept = default;

T *data() const noexcept;
std::size_t size() const noexcept;
Expand Down Expand Up @@ -397,7 +397,7 @@ template <typename T> class Vec final {
Vec(Vec &&) noexcept;
~Vec() noexcept;

Vec &operator=(Vec &&) &noexcept;
Vec &operator=(Vec &&) & noexcept;
Vec &operator=(const Vec &) &;

std::size_t size() const noexcept;
Expand Down Expand Up @@ -463,7 +463,7 @@ template <typename T> Vec<T>::Vec(Vec &&other) noexcept : repr(other.repr) {

template <typename T> Vec<T>::~Vec() noexcept { this->drop(); }

template <typename T> Vec<T> &Vec<T>::operator=(Vec &&other) &noexcept {
template <typename T> Vec<T> &Vec<T>::operator=(Vec &&other) & noexcept {
this->drop();
this->repr = other.repr;
new (&other) Vec();
Expand Down Expand Up @@ -604,7 +604,7 @@ class Error final : public std::exception {
~Error() noexcept override;

Error &operator=(const Error &) &;
Error &operator=(Error &&) &noexcept;
Error &operator=(Error &&) & noexcept;

const char *what() const noexcept override;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ class String final {
static String lossy(const char16_t *) noexcept;
static String lossy(const char16_t *, std::size_t) noexcept;

String &operator=(const String &) &noexcept;
String &operator=(String &&) &noexcept;
String &operator=(const String &) & noexcept;
String &operator=(String &&) & noexcept;

explicit operator std::string() const;

Expand Down Expand Up @@ -110,8 +110,8 @@ template <> struct copy_assignable_if<false> {
copy_assignable_if() noexcept = default;
copy_assignable_if(const copy_assignable_if &) noexcept = default;
copy_assignable_if &
operator=(const copy_assignable_if &) &noexcept = delete;
copy_assignable_if &operator=(copy_assignable_if &&) &noexcept = default;
operator=(const copy_assignable_if &) & noexcept = delete;
copy_assignable_if &operator=(copy_assignable_if &&) & noexcept = default;
};
} // namespace detail

Expand All @@ -124,8 +124,8 @@ class Slice final
Slice() noexcept;
Slice(T *, std::size_t count) noexcept;

Slice &operator=(const Slice<T> &) &noexcept = default;
Slice &operator=(Slice<T> &&) &noexcept = default;
Slice &operator=(const Slice<T> &) & noexcept = default;
Slice &operator=(Slice<T> &&) & noexcept = default;

T *data() const noexcept;
std::size_t size() const noexcept;
Expand Down Expand Up @@ -397,7 +397,7 @@ template <typename T> class Vec final {
Vec(Vec &&) noexcept;
~Vec() noexcept;

Vec &operator=(Vec &&) &noexcept;
Vec &operator=(Vec &&) & noexcept;
Vec &operator=(const Vec &) &;

std::size_t size() const noexcept;
Expand Down Expand Up @@ -463,7 +463,7 @@ template <typename T> Vec<T>::Vec(Vec &&other) noexcept : repr(other.repr) {

template <typename T> Vec<T>::~Vec() noexcept { this->drop(); }

template <typename T> Vec<T> &Vec<T>::operator=(Vec &&other) &noexcept {
template <typename T> Vec<T> &Vec<T>::operator=(Vec &&other) & noexcept {
this->drop();
this->repr = other.repr;
new (&other) Vec();
Expand Down
Loading

0 comments on commit 0858bc2

Please sign in to comment.