Skip to content

Commit

Permalink
Merge pull request #16 from MrVeit/dev
Browse files Browse the repository at this point in the history
Added a function to load the balance of a particular token from the master address or from the vault and more
  • Loading branch information
MrVeit authored Dec 21, 2024
2 parents 9e6c960 + 4e316cf commit bf986d2
Show file tree
Hide file tree
Showing 20 changed files with 381 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ MonoBehaviour:
m_Name: Jetton_DOGS
m_EditorClassIdentifier:
<Type>k__BackingField: 2
<Symbol>k__BackingField: DOGS
<MasterAddress>k__BackingField: 0:afc49cb8786f21c87045b19ede78fc6b46c51048513f8e9a6d44060199c1bf0c
<IconUrl>k__BackingField: https://cache.tonapi.io/imgproxy/6Pb0sBFy_AzW6l39EIHGs-Iz4eLbbZUh8AYY_Xq-rcg/rs:fill:200:200:1/g:no/aHR0cHM6Ly9jZG4uZG9ncy5kZXYvZG9ncy5wbmc.webp
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ MonoBehaviour:
m_Name: Jetton_GRAM
m_EditorClassIdentifier:
<Type>k__BackingField: 0
<Symbol>k__BackingField: GRAM
<MasterAddress>k__BackingField: 0:b8ef4f77a17e5785bd31ba4da50abd91852f2b8febee97ad6ee16d941f939198
<IconUrl>k__BackingField: https://cache.tonapi.io/imgproxy/lNoY3YdNeBug53ixjK6hxT6XIX3_xoIYNqv-ykIQ1Aw/rs:fill:200:200:1/g:no/aHR0cHM6Ly9ncmFtY29pbi5vcmcvaW1nL2ljb24ucG5n.webp
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ MonoBehaviour:
m_Name: Jetton_NOT
m_EditorClassIdentifier:
<Type>k__BackingField: 1
<Symbol>k__BackingField: NOT
<MasterAddress>k__BackingField: 0:2f956143c461769579baef2e32cc2d7bc18283f40d20bb03e432cd603ac33ffc
<IconUrl>k__BackingField: https://cache.tonapi.io/imgproxy/4KCMNm34jZLXt0rqeFm4rH-BK4FoK76EVX9r0cCIGDg/rs:fill:200:200:1/g:no/aHR0cHM6Ly9jZG4uam9pbmNvbW11bml0eS54eXovY2xpY2tlci9ub3RfbG9nby5wbmc.webp
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ MonoBehaviour:
m_Name: Jetton_USDT
m_EditorClassIdentifier:
<Type>k__BackingField: 3
<Symbol>k__BackingField: "USD\u20AE"
<MasterAddress>k__BackingField: 0:b113a994b5024a16719f69139328eb759596c38a25f59028b146fecdc3621dfe
<IconUrl>k__BackingField: https://cache.tonapi.io/imgproxy/T3PB4s7oprNVaJkwqbGg54nexKE0zzKhcrPv8jcWYzU/rs:fill:200:200:1/g:no/aHR0cHM6Ly90ZXRoZXIudG8vaW1hZ2VzL2xvZ29DaXJjbGUucG5n.webp
3 changes: 2 additions & 1 deletion Uniton Connect/Runtime/Common/JettonTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ public enum JettonTypes
GRAM,
NOT,
DOGS,
USDT
USDT,
Custom
}
}
8 changes: 8 additions & 0 deletions Uniton Connect/Runtime/Common/TransactionTypes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace UnitonConnect.Core.Common
{
public enum TransactionTypes
{
Sent,
Received
}
}
10 changes: 10 additions & 0 deletions Uniton Connect/Runtime/Consts/ClassicJettonNames.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace UnitonConnect.Core.Common
{
public static class ClassicJettonNames
{
public const string USDT_NAME = "Tether USD";
public const string GRAM_NAME = "Gram";
public const string NOT_NAME = "Notcoin";
public const string DOGS_NAME = "Dogs";
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
using System.Collections.Generic;
using UnitonConnect.Core.Data;

namespace UnitonConnect.Core.Common
{
public interface IUnitonConnectJettonCallbacks
{
delegate void OnJettonBalanceLoaded(decimal balance,
string jettonName, string masterAddress);

delegate void OnJettonAddressParsed(string jettonAddress);

delegate void OnLastJettonTransactionsLoaded(TransactionTypes type,
List<JettonTransactionData> transactions);

delegate void OnJettonTransactionSend(string masterAddress,
SuccessTransactionData transactionData);
delegate void OnJettonTransactionSendFail(string masterAddress,
string errorMessage);

event OnJettonBalanceLoaded OnBalanceLoaded;
event OnJettonAddressParsed OnAddressParsed;

event OnLastJettonTransactionsLoaded OnLastTransactionsLoaded;

event OnJettonTransactionSend OnTransactionSended;
event OnJettonTransactionSendFail OnTransactionSendFailed;
}
Expand Down
61 changes: 35 additions & 26 deletions Uniton Connect/Runtime/Core/Bridges/TonApiBridge.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,6 @@ internal static IEnumerator GetAssetIcon(

internal static IEnumerator GetBalance(Action<long> walletBalanceClaimed)
{
if (!UNITON_CONNECT.IsWalletConnected)
{
UnitonConnectLogger.LogWarning("Failed to request the balance," +
" connect the wallet and repeat the operation later");

walletBalanceClaimed?.Invoke(0);

yield break;
}

var userEncodedAddress = ConvertAddressToEncodeURL(_walletAddress);
var targetUrl = GetUserWalletUrl(userEncodedAddress);

Expand Down Expand Up @@ -173,14 +163,6 @@ internal static IEnumerator GetTransactionPayload(decimal amount,
internal static IEnumerator GetTransactionData(string transactionHash, float awaitDelay,
Action<SuccessTransactionData> dataClaimed, Action<string> fetchDataFailed)
{
if (!UNITON_CONNECT.IsWalletConnected)
{
UnitonConnectLogger.LogWarning("Failed to request the balance," +
" connect the wallet and repeat the operation later");

yield break;
}

yield return new WaitForSeconds(awaitDelay);

var encodedTransactionHash = EscapeQueryParam(transactionHash);
Expand Down Expand Up @@ -250,14 +232,6 @@ internal static class NFT
internal static IEnumerator GetCollections(string apiURL,
Action<NftCollectionData> collectionsClaimed)
{
if (!UNITON_CONNECT.IsWalletConnected)
{
UnitonConnectLogger.LogWarning("Failed to load user nft" +
" collections, wallet is not connected.");

yield break;
}

using (UnityWebRequest request = UnityWebRequest.Get(apiURL))
{
yield return request.SendWebRequest();
Expand Down Expand Up @@ -300,7 +274,42 @@ internal static string GetAllCollectionsUrl(

internal sealed class Jetton
{
internal static IEnumerator GetBalance(string tonAddress,
string masterJettonAddress, Action<JettonBalanceData> jettonBalanceLoaded)
{
var targetUrl = GetBalanceUrl(tonAddress, masterJettonAddress);

using (UnityWebRequest request = UnityWebRequest.Get(targetUrl))
{
yield return request.SendWebRequest();

var responseJson = request.downloadHandler.text;

if (request.result != WebRequestUtils.SUCCESS)
{
UnitonConnectLogger.LogError($"Failed to fetch jetton balance, reason: {responseJson}");

jettonBalanceLoaded?.Invoke(null);

yield break;
}

var responseData = JsonConvert.DeserializeObject<JettonBalanceData>(responseJson);

jettonBalanceLoaded?.Invoke(responseData);

UnitonConnectLogger.Log($"Loaded jetton {responseData.Configuration.Name} " +
$"with balance in nano: {responseData.BalanceInNano}");

yield break;
}
}

internal static string GetBalanceUrl(string tonAddress,
string masterJettonAddress)
{
return $"https://tonapi.io/v2/accounts/{tonAddress}/jettons/{masterJettonAddress}";
}
}
}
}
49 changes: 49 additions & 0 deletions Uniton Connect/Runtime/Core/Bridges/TonCenterApiBridge.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.Networking;
using Newtonsoft.Json;
using UnitonConnect.Core;
Expand Down Expand Up @@ -71,11 +72,59 @@ internal static IEnumerator GetJettonWalletByOwner(string tonAddress,
}
}

internal static IEnumerator GetLastTransactions(string ownerAddress,
string directionTag, int limit, Action<List<JettonTransactionData>> transactionsLoaded)
{
var targetUrl = GetLastJettonTransactionsUrl(ownerAddress, directionTag, limit);

using (UnityWebRequest request = UnityWebRequest.Get(targetUrl))
{
yield return request.SendWebRequest();

var responseResult = request.downloadHandler.text;

UnitonConnectLogger.Log($"Parsed response for loading " +
$"last jetton transactions: {responseResult}");

if (request.result == WebRequestUtils.SUCCESS)
{
var loadedTransactionsList = JsonConvert.DeserializeObject<JettonTransactionsListData>(responseResult);

if (loadedTransactionsList.Transfers == null ||
loadedTransactionsList.Transfers.Count == 0)
{
UnitonConnectLogger.Log($"Jetton transactions is not detected at wallet: {ownerAddress}");

transactionsLoaded?.Invoke(null);

yield break;
}

transactionsLoaded?.Invoke(loadedTransactionsList.Transfers);

yield break;
}

var errorData = JsonConvert.DeserializeObject<TonCenterResponseErrorData>(responseResult);

UnitonConnectLogger.LogError($"Failed to load last jetton transactions, reason: {errorData.Message}");

transactionsLoaded?.Invoke(null);
}
}

internal static string GetJettonWalletUlr(string tonAddress, string jettonMaster)
{
return $"https://toncenter.com/api/v3/jetton/wallets?owner_address={tonAddress}&" +
$"jetton_address={jettonMaster}&exclude_zero_balance=false&limit=50&offset=0";
}

internal static string GetLastJettonTransactionsUrl(string ownerAddress,
string directionTag, int limit)
{
return $"https://toncenter.com/api/v3/jetton/transfers?owner_address={ownerAddress}" +
$"&direction={directionTag}&limit={limit}&offset=0";
}
}
}
}
Loading

0 comments on commit bf986d2

Please sign in to comment.