-
Notifications
You must be signed in to change notification settings - Fork 7
Tutorial #1. (Cpp version) Basic plugin usage.
In this tutorial we'll show you how to use basic functionality of Stratis Unreal plugin: create a wallet, check your balance and unspent transaction outputs (UTXOs), and send coins to another address.
You need to setup your project and Stratis Unreal Plugin within it. If you looking for a tutorial about basic project setup, please check our tutorial #0.
First of all, we need to setup StratisUnrealManager to be able to use all of the API methods provided by plugin.
Let's open Source path of your project and create new header named StratisHandler.h:
#pragma once
#include "CoreMinimal.h"
#include "UStratisUnrealManager.h"
#include "StratisHandler.generated.h"
UCLASS(minimalapi)
class UStratisHandler : public UObject {
GENERATED_BODY()
public:
UStratisHandler();
UPROPERTY()
UStratisUnrealManager *stratisManager;
UWorld *GetWorld() const override;
UFUNCTION(BlueprintCallable, Category = "StratisHandler")
void RunSomeLogic();
};We'll use this class to setup and hold UStratisUnrealManager instance and to run some code inside RunSomeLogic method later.
Okay, now we're going to implement StratisHandler class we defined above:
#include "StratisHandler.h"
UStratisHandler::UStratisHandler() {
stratisManager =
CreateDefaultSubobject<UStratisUnrealManager>(TEXT("StratisManager"));
//TODO: Initialize stratisManager
}
UWorld *UStratisHandler::GetWorld() const { return GetOuter()->GetWorld(); }
void UStratisHandler::RunSomeLogic() {
//TODO: print balance, utxos etc
}Now we're going to set up base URL, network and wallet mnemonic.
Let's walk through the steps:
-
Set base url. To use Stratis Unreal Plugin, you need a full node to be running locally or remotely. Find your node's address (IP or domain) if you're running remote node, or use
http://localhost:44336if you're running your node locally. -
Set network you want to operate on. Use
ENetwork::CIRRUSandENetwork::CIRRUS_TESTfor production and testing respectively. -
Set mnemonic for your wallet. Mnemonic is a sequence of words used to define private key of your wallet. You can create mnemonic using just a pen, a papper and a dice, or using a various of a hardware & software mnemonic generators.
UStratisHandler::UStratisHandler() {
stratisManager =
CreateDefaultSubobject<UStratisUnrealManager>(TEXT("StratisManager"));
stratisManager->setBaseUrl(TEXT("http://148.251.15.126:44336"));
stratisManager->setPredefinedNetwork(ENetwork::CIRRUS_TEST);
stratisManager->setMnemonic(TEXT("legal door leopard "
"fire attract stove "
"similar response photo "
"prize seminar frown"));
}Now, let's write something interesting inside of RunSomeLogic method - print current wallet's address to log console:
void UStratisHandler::RunSomeLogic() {
UE_LOG(LogTemp, Display, TEXT("Address: %s"),
*(stratisManager->getAddress()));
}The last thing we need to do is to make use of our UStratisHandler.
Open ...Character.h and add lines below to the class declaration:
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, meta = (AllowPrivateAccess = "true"))
UStratisHandler* stratisHandler;And now add object initialization code in to the character's class constructor definition:
stratisHandler = CreateDefaultSubobject<UStratisHandler>(TEXT("StratisHandler"));Add RunSomeLogic call to the end of OnFire event definition:
stratisHandler->RunSomeLogic();Now we need to compile code and run the game. An address of our wallet will be printed on every shot we make.
Now let's learn how we can get a balance of our wallet.
Put the code below inside of RunSomeLogic method:
stratisManager->getBalance([](const auto &result) {
if (result::isSuccessful(result)) {
UE_LOG(LogTemp, Display, TEXT("Balance in satoshis: %llu"),
result::getValue(result));
} else {
UE_LOG(LogTemp, Error, TEXT("%s"), *(result::getError(result).message));
}
});In this example we're using getBalance method with TFunction parameter. You can also use another overloaded version of this method which takes delegates as parameters.
This code will print your balance if call is succeed, and print error otherwise.
Okay, now we will try to find unpsent transaction outputs for our wallet.
Put the code below inside of RunSomeLogic method:
stratisManager->getCoins([](const auto &result) {
if (result::isSuccessful(result)) {
const auto &utxos = result::getValue(result);
for (const auto &utxo : utxos) {
UE_LOG(LogTemp, Display, TEXT("UTXO #%i, hash: %s, satoshis: %llu"),
utxo.n, *(utxo.hash), utxo.satoshis);
}
} else {
UE_LOG(LogTemp, Error, TEXT("%s"), *(result::getError(result).message));
}
});Here we're usign range-based for loop to iterate over TArray of FUTXO items.
This code will print all of yours utxos to log console one-by-one if call is succeed, and print error otherwise.
Now let's try to implement a more complex logic: send some coins and await for transaction's receipt.
At first, define a couple of variables:
-
destinationAddress: in this example we're usingtD5aDZSu4Go4A23R7VsjuJTL51YMyeoLySfor Cirrus Test network -
amount: amount of satoshis we want to send. Let's send 10.000 satoshis (= 0.0001 STRAX).
FString destinationAddress(TEXT("tD5aDZSu4Go4A23R7VsjuJTL51YMyeoLyS"));
int64 amount = 10000;Now, send amount of coins to destinationAddress with code shown below:
stratisManager->sendCoinsTransaction(
destinationAddress, amount, [this](const auto &result) {
if (result::isSuccessful(result)) {
const auto &transactionID = result::getValue(result);
// TODO: now we need to await receipt
} else {
UE_LOG(LogTemp, Error, TEXT("%s"),
*(result::getError(result).message));
}
});Well, now we want to known when receipt for this transaction is available. To achive this, use the code shown below:
this->stratisManager->waitTillReceiptAvailable(
transactionID, [transactionID](const auto &result) {
if (result::isSuccessful(result)) {
UE_LOG(LogTemp, Display,
TEXT("Coins had been sent successfuly, transaction "
"id: %s"),
*transactionID);
} else {
UE_LOG(LogTemp, Error, TEXT("%s"),
*(result::getError(result).message));
}
});In this tutorial we've learned how to use some core plugin's functions: get balance, send coins and wait for receipt. In next tutorial we'll cover more advanced functionality of plugin: sending no-op transactions and interacting with smart contracts.
If you found a problem, you can open an issue on project's Github page. If you still have a questions, feel free to ask them in our Discord channel.
Stay tuned!