Skip to main content

02 Sending transactions & retrieving data

This page lists the different methods you can use in your game.

in general

All write method calls such as minting a character or wearable incur gas fees to cover smart contract operations. Tickets are issued for these and approval needed via MetaMask.

All read data method calls such as retrieving a balance do NOT incur gas fees.

๐Ÿ•น ย Guided tutorialโ€‹

Send transactionโ€‹


The SendTransaction function interacts with the blockchain and incurs gas fees. Signing in MetaMask is required. When a call is made, the back end client sends a 'Ticket' in MetaMask if the call was successful.

URLโ€‹

http://45.77.189.28:5000/send/transaction

Parametersโ€‹

device_id, contract_address, abi_hash, method, args

Responseโ€‹

A ticket is the response.

The session saved during the GetClientcall is used to open MetaMask. MetaMask will popup to display gas fees for the transaction. Sign or confirm the transaction for that ticket.


void UAnkrClient::SendTransaction(FString contract, FString abi_hash, FString method, FString args, FAnkrTicket Ticket)
{
http = &FHttpModule::Get();

TSharedRef<IHttpRequest, ESPMode::ThreadSafe> Request = http->CreateRequest();
Request->OnProcessRequestComplete().BindLambda([Ticket, this](FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful)
{
TSharedPtr<FJsonObject> JsonObject;
TSharedRef<TJsonReader<>> Reader = TJsonReaderFactory<>::Create(Response->GetContentAsString());
FString data = Response->GetContentAsString();

if (FJsonSerializer::Deserialize(Reader, JsonObject))
{
FString ticketId = JsonObject->GetStringField("ticket");
data = ticketId;
}

Ticket.ExecuteIfBound(data);
});

AsyncTask(ENamedThreads::AnyBackgroundThreadNormalTask, [this, Request, contract, abi_hash, method, args]()
{
FString url = baseUrl + "send/transaction";

Request->SetURL(url);
Request->SetVerb("POST");
Request->SetHeader(TEXT("User-"), "X-MirageSDK-Agent");
Request->SetHeader("Content-Type", TEXT("application/json"));
Request->SetContentAsString("{\"device_id\": \"" + deviceId + "\", \"contract_address\": \"" + contract + "\", \"abi_hash\": \"" + abi_hash + "\", \"method\": \"" + method + "\", \"args\": \"" + args + "\"}");
Request->ProcessRequest();
});
#if PLATFORM_ANDROID
FPlatformProcess::LaunchURL(session.GetCharArray().GetData(), NULL, NULL);
#endif
}

Checking status of ticketโ€‹


Check status of tickets by calling GetTicketResult.

For example, if you come back to the game, check the status of the ticket with GetTicketResult having body parameters as device_id, ticket.

Returns the status of the result.

URLโ€‹

http://45.77.189.28:5000/send/GetTicketResult

Parametersโ€‹

device_id, ticket

Responseโ€‹

Successful or Unsuccessful.

Get Dataโ€‹


GetData is used to retrieve data associated with a particular wallet and user from a contract.

URLโ€‹

http://45.77.189.28:5000/wallet/call/method

Parametersโ€‹

device_id, contract_address, abi_hash, method, args

Responseโ€‹

The data for a particular wallet and user from a contract is returned.

void UAnkrClient::GetData(FString contract, FString abi_hash, FString method, FString args, FAnkrDelegate Result)
{
http = &FHttpModule::Get();

TSharedRef<IHttpRequest, ESPMode::ThreadSafe> Request = http->CreateRequest();
Request->OnProcessRequestComplete().BindLambda([Result, this](FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful)
{
TSharedPtr<FJsonObject> JsonObject;
TSharedRef<TJsonReader<>> Reader = TJsonReaderFactory<>::Create(Response->GetContentAsString());
Result.ExecuteIfBound(Response->GetContentAsString());
});

FString url = baseUrl + "call/method";

Request->SetURL(url);
Request->SetVerb("POST");
Request->SetHeader(TEXT("User-Agent"), "X-MirageSDK-Agent");
Request->SetHeader("Content-Type", TEXT("application/json"));
Request->SetContentAsString("{\"device_id\": \"" + deviceId + "\", \"contract_address\": \"" + contract + "\", \"abi_hash\": \"" + abi_hash + "\", \"method\": \"" + method + "\", \"args\": \"" + args + "\"}");
Request->ProcessRequest();
}

Send ABIโ€‹


SendABI is used to send a request and retrieve the ABI hash generated by the backend.

URLโ€‹

http://45.77.189.28:5000/abi

Parametersโ€‹

abi

Responseโ€‹

The ABI hash is returned.


void UAnkrClient::SendABI(FString abi, FAnkrDelegate Result)
{
http = &FHttpModule::Get();

TSharedRef<IHttpRequest, ESPMode::ThreadSafe> Request = http->CreateRequest();
Request->OnProcessRequestComplete().BindLambda([Result, this](FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful)
{
TSharedPtr<FJsonObject> JsonObject;
TSharedRef<TJsonReader<>> Reader = TJsonReaderFactory<>::Create(Response->GetContentAsString());
FString data = Response->GetContentAsString();

if (FJsonSerializer::Deserialize(Reader, JsonObject))
{
Result.ExecuteIfBound(JsonObject->GetStringField("abi"));
}
});

FString url = baseUrl + "abi";

Request->SetURL(url);
Request->SetVerb("POST");
Request->SetHeader(TEXT("User-Agent"), "X-MirageSDK-Agent");
Request->SetHeader("Content-Type", TEXT("application/json"));

const TCHAR* find = TEXT("\"");
const TCHAR* replace = TEXT("\\\"");
FString body = FString("{\"abi\": \"" + abi.Replace(find, replace, ESearchCase::IgnoreCase) + "\"}");

Request->SetContentAsString(*body);
Request->ProcessRequest();
}

Sign messageโ€‹


SignMessage is used to send a request that requires confirmation in MetaMask. The session saved during GetClient will be used to open MetaMask.

URLโ€‹

http://45.77.189.28:5000/sign/message

Parametersโ€‹

device_id, message

Responseโ€‹

A ticketid is returned.


void UAnkrClient::SignMessage(FString message, FAnkrDelegate Result)
{
http = &FHttpModule::Get();

TSharedRef<IHttpRequest, ESPMode::ThreadSafe> Request = http->CreateRequest();
Request->OnProcessRequestComplete().BindLambda([Result, this](FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful)
{
TSharedPtr<FJsonObject> JsonObject;
TSharedRef<TJsonReader<>> Reader = TJsonReaderFactory<>::Create(Response->GetContentAsString());
if (FJsonSerializer::Deserialize(Reader, JsonObject))
{
FString ticketId = JsonObject->GetStringField("ticket");

#if PLATFORM_ANDROID
FPlatformProcess::LaunchURL(session.GetCharArray().GetData(), NULL, NULL);
#endif
Result.ExecuteIfBound(ticketId);
}
});

FString url = baseUrl + "sign/message";

Request->SetURL(url);
Request->SetVerb("POST");
Request->SetHeader(TEXT("User-Agent"), "X-MirageSDK-Agent");
Request->SetHeader("Content-Type", TEXT("application/json"));
Request->SetContentAsString("{\"device_id\": \"" + deviceId + "\", \"message\":\"" + message + "\"}"); // erc20 abi
Request->ProcessRequest();
}

The Call is as follows:


FString url = baseUrl + "sign/message";

Get signatureโ€‹


GetSignature is used to verify a message by getting a signature data object.

URLโ€‹

http://45.77.189.28:5000/result

Parametersโ€‹

device_id, ticket

Responseโ€‹

A 'data' object with 'signature' string field.


void UAnkrClient::GetSignature(FString ticket, FAnkrDelegate Result)
{
http = &FHttpModule::Get();

TSharedRef<IHttpRequest, ESPMode::ThreadSafe> Request = http->CreateRequest();
Request->OnProcessRequestComplete().BindLambda([Result, this](FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful)
{
TSharedPtr<FJsonObject> JsonObject;
TSharedRef<TJsonReader<>> Reader = TJsonReaderFactory<>::Create(Response->GetContentAsString());
if (FJsonSerializer::Deserialize(Reader, JsonObject))
{
TSharedPtr<FJsonObject> data = JsonObject->GetObjectField("data");
Result.ExecuteIfBound(data->GetStringField("signature"));
}
});

FString url = baseUrl + "result";

Request->SetURL(url);
Request->SetVerb("POST");
Request->SetHeader(TEXT("User-Agent"), "X-MirageSDK-Agent");
Request->SetHeader("Content-Type", TEXT("application/json"));
Request->SetContentAsString("{\"device_id\": \"" + deviceId + "\", \"ticket\":\"" + ticket + "\"}");
Request->ProcessRequest();
}

Verify messageโ€‹


VerifyMessage is used to verify a signature and connect a player account with a connected wallet's public address.

URLโ€‹

http://45.77.189.28:5000/verify/message

Parametersโ€‹

device_id, message, signature

Responseโ€‹

Account 'address'. The account address for the connected wallet's public address.


void UAnkrClient::VerifyMessage(FString message, FString signature, FAnkrDelegate Result)
{
http = &FHttpModule::Get();

TSharedRef<IHttpRequest, ESPMode::ThreadSafe> Request = http->CreateRequest();
Request->OnProcessRequestComplete().BindLambda([Result, this](FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful)
{
TSharedPtr<FJsonObject> JsonObject;
TSharedRef<TJsonReader<>> Reader = TJsonReaderFactory<>::Create(Response->GetContentAsString());
if (FJsonSerializer::Deserialize(Reader, JsonObject))
{
Result.ExecuteIfBound(JsonObject->GetStringField("address"));
}
});

FString url = baseUrl + "verify/message";
Request->SetURL(url);
Request->SetVerb("POST");
Request->SetHeader(TEXT("User-Agent"), "X-MirageSDK-Agent");
Request->SetHeader("Content-Type", TEXT("application/json"));
Request->SetContentAsString("{\"device_id\": \"" + deviceId + "\", \"message\":\"" + message + "\", \"signature\":\"" + signature + "\"}");
Request->ProcessRequest();
}