Skip to content

Commit

Permalink
refactor: APIs cleaned up, refactored and fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
talha-trili committed Nov 14, 2024
1 parent b3fc95e commit e27320c
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 256 deletions.
156 changes: 5 additions & 151 deletions Runtime/Scripts/API/TezosAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,6 @@ await UnityMainThreadDispatcher.Instance().EnqueueAsync(
return result;
}

public static UniTask DeployContract(DeployContractRequest deployContractRequest) => ProviderFactory.GetConnectedProviderController().DeployContract(deployContractRequest);

/// <summary>
/// Fetches the XTZ balance of a given wallet address asynchronously.
/// </summary>
Expand All @@ -173,155 +171,11 @@ public static UniTask<string> GetBalance()
return ProviderFactory.GetConnectedProviderController().GetBalance();
}

public static UniTask<T> ReadView<T>(string contractAddress, string view, string input) => _rpc.PostRequest<T>(EndPoints.GetRunViewEndPoint(contractAddress, view), input);

public static UniTask<IEnumerable<TokenBalance>> GetTokensForOwner(
string owner,
bool withMetadata,
long maxItems,
TokensForOwnerOrder orderBy
)
{
var sort = orderBy switch
{
TokensForOwnerOrder.Default byDefault => $"sort.asc=id&offset.cr={byDefault.lastId}",
TokensForOwnerOrder.ByLastTimeAsc byLastTimeAsc =>
$"sort.asc=lastLevel&offset.pg={byLastTimeAsc.page}",
TokensForOwnerOrder.ByLastTimeDesc byLastTimeDesc =>
$"sort.desc=lastLevel&offset.pg={byLastTimeDesc.page}",
_ => string.Empty
};
return _rpc.GetRequest<IEnumerable<TokenBalance>>(
EndPoints.GetTokensForOwnerEndPoint(
owner, withMetadata,
maxItems, sort
)
);
}

public static UniTask<IEnumerable<TokenBalance>> GetOwnersForToken(
string contractAddress,
uint tokenId,
long maxItems,
OwnersForTokenOrder orderBy
)
{
var sort = orderBy switch
{
OwnersForTokenOrder.Default byDefault => $"sort.asc=id&offset.cr={byDefault.lastId}",
OwnersForTokenOrder.ByBalanceAsc byBalanceAsc =>
$"sort.asc=balance&offset.pg={byBalanceAsc.page}",
OwnersForTokenOrder.ByBalanceDesc byBalanceDesc =>
$"sort.desc=balance&offset.pg={byBalanceDesc.page}",
OwnersForTokenOrder.ByLastTimeAsc byLastTimeAsc =>
$"sort.asc=lastLevel&offset.pg={byLastTimeAsc.page}",
OwnersForTokenOrder.ByLastTimeDesc byLastTimeDesc =>
$"sort.desc=lastLevel&offset.pg={byLastTimeDesc.page}",
_ => string.Empty
};
return _rpc.GetRequest<IEnumerable<TokenBalance>>(
EndPoints.GetOwnersForTokenEndPoint(
contractAddress,
tokenId, maxItems,
sort
)
);
}

public static UniTask<IEnumerable<TokenBalance>> GetOwnersForContract(
string contractAddress,
long maxItems,
OwnersForContractOrder orderBy
)
{
var sort = orderBy switch
{
OwnersForContractOrder.Default byDefault => $"sort.asc=id&offset.cr={byDefault.lastId}",
OwnersForContractOrder.ByLastTimeAsc byLastTimeAsc =>
$"sort.asc=lastLevel&offset.pg={byLastTimeAsc.page}",
OwnersForContractOrder.ByLastTimeDesc byLastTimeDesc =>
$"sort.desc=lastLevel&offset.pg={byLastTimeDesc.page}",
_ => string.Empty
};
return _rpc.GetRequest<IEnumerable<TokenBalance>>(
EndPoints.GetOwnersForContractEndPoint(
contractAddress,
maxItems, sort
)
);
}

public static UniTask<bool> IsHolderOfContract(string wallet, string contractAddress) => _rpc.GetRequest<bool>(EndPoints.GetIsHolderOfContractEndPoint(wallet, contractAddress));

public static UniTask<bool> IsHolderOfToken(string wallet, string contractAddress, uint tokenId) => _rpc.GetRequest<bool>(EndPoints.GetIsHolderOfTokenEndPoint(wallet, contractAddress, tokenId));

public static UniTask<JsonElement> GetTokenMetadata(
string contractAddress,
uint tokenId
) => _rpc.GetRequest<JsonElement>(EndPoints.GetTokenMetadataEndPoint(contractAddress, tokenId));

public static UniTask<JsonElement> GetContractMetadata(
string contractAddress
) => _rpc.GetRequest<JsonElement>(EndPoints.GetContractMetadataEndPoint(contractAddress));

public static UniTask<IEnumerable<TokenData>> GetTokensForContract(
string contractAddress,
bool withMetadata,
long maxItems,
TokensForContractOrder orderBy
)
{
TezosLogger.LogDebug($"Getting tokens for contract: {contractAddress}");
var sort = orderBy switch
{
TokensForContractOrder.Default byDefault => $"sort.asc=id&offset.cr={byDefault.lastId}",
TokensForContractOrder.ByLastTimeAsc byLastTimeAsc =>
$"sort.asc=lastLevel&offset.pg={byLastTimeAsc.page}",
TokensForContractOrder.ByLastTimeDesc byLastTimeDesc =>
$"sort.desc=lastLevel&offset.pg={byLastTimeDesc.page}",
TokensForContractOrder.ByHoldersCountAsc byHoldersCountAsc =>
$"sort.asc=holdersCount&offset.pg={byHoldersCountAsc.page}",
TokensForContractOrder.ByHoldersCountDesc byHoldersCountDesc =>
$"sort.desc=holdersCount&offset.pg={byHoldersCountDesc.page}",
_ => string.Empty
};
return _rpc.GetRequest<IEnumerable<TokenData>>(
EndPoints.GetTokensForContractEndPoint(
contractAddress,
withMetadata,
maxItems, sort
)
);
}

public static UniTask<bool> GetOperationStatus(string operationHash) => _rpc.GetRequest<bool>(EndPoints.GetOperationStatusEndPoint(operationHash));

public static UniTask<int> GetLatestBlockLevel() => _rpc.GetRequest<int>(EndPoints.GetLatestBlockLevelEndPoint());

public static UniTask<int> GetAccountCounter(string address) => _rpc.GetRequest<int>(EndPoints.GetAccountCounterEndPoint(address));

public static UniTask<IEnumerable<FA2Token>> GetOriginatedContractsForOwner(
string creator,
string codeHash,
long maxItems,
OriginatedContractsForOwnerOrder orderBy
)
{
TezosLogger.LogDebug($"API.GetOriginatedContractsForOwner: creator={creator}, codeHash={codeHash}");

var sort = orderBy switch
{
OriginatedContractsForOwnerOrder.Default byDefault => $"sort.asc=id&offset.cr={byDefault.lastId}",
OriginatedContractsForOwnerOrder.ByLastActivityTimeAsc byLastTimeAsc => $"sort.asc=lastActivity&offset.pg={byLastTimeAsc.page}",
OriginatedContractsForOwnerOrder.ByLastActivityTimeDesc byLastTimeDesc => $"sort.desc=lastActivity&offset.pg={byLastTimeDesc.page}",
_ => string.Empty
};

var url = $"contracts?creator={creator}&tzips.any=fa2&codeHash={codeHash}&" +
$"select=address,tokensCount as tokens_count,lastActivity,lastActivityTime as last_activity_time,id&{sort}&limit={maxItems}";

return _rpc.GetRequest<IEnumerable<FA2Token>>(url);
}
public static UniTask DeployContract(DeployContractRequest deployContractRequest) => ProviderFactory.GetConnectedProviderController().DeployContract(deployContractRequest);
public static UniTask<T> ReadView<T>(string contractAddress, string view, string input) => _rpc.PostRequest<T>(EndPoints.GetRunViewEndPoint(contractAddress, view), input);
public static UniTask<T> GetTokens<T>(string address, int limit = 100) => _rpc.GetRequest<T>(EndPoints.GetTokensEndPoint(address, limit));
public static UniTask<T> GetTokenMetadata<T>(string tokenId) => _rpc.GetRequest<T>(EndPoints.GetTokenMetadataEndPoint(tokenId));
public static UniTask<bool> GetOperationStatus(string operationHash) => _rpc.GetRequest<bool>(EndPoints.GetOperationStatusEndPoint(operationHash));

#endregion
}
Expand Down
76 changes: 0 additions & 76 deletions Runtime/Scripts/API/TokenAPI.cs

This file was deleted.

3 changes: 0 additions & 3 deletions Runtime/Scripts/API/TokenAPI.cs.meta

This file was deleted.

38 changes: 23 additions & 15 deletions Runtime/Scripts/Request/EndPoints/EndPoints.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System;
using System.IO;
using System.Web;
using Tezos.Configs;
using Tezos.MessageSystem;

Expand All @@ -11,19 +11,27 @@ public static class EndPoints

static EndPoints() => _baseUrl = ConfigGetter.GetOrCreateConfig<DataProviderConfig>().BaseUrl;

public static string GetBalanceEndPoint(string walletAddress) => Path.Combine(_baseUrl, "accounts", walletAddress, "balance");
public static string GetContractCodeEndPoint(string contract) => Path.Combine(_baseUrl, $"chains/main/blocks/head/context/contracts/{contract}/script/");
public static string GetRunViewEndPoint(string contract, string name) => Path.Combine(_baseUrl, $"helpers/view/{contract}/{name}");
public static string GetIsHolderOfContractEndPoint(string wallet, string contractAddress) => Path.Combine(_baseUrl, $"tokens/balances?account={wallet}&token.contract={contractAddress}&balance.ne=0&select=id");
public static string GetIsHolderOfTokenEndPoint(string wallet, string contractAddress, uint tokenId) => Path.Combine(_baseUrl, $"tokens/balances?account={wallet}&token.contract={contractAddress}&token.tokenId={tokenId}&balance.ne=0&select=id");
public static string GetTokenMetadataEndPoint(string contractAddress, uint tokenId) => Path.Combine(_baseUrl, $"tokens?contract={contractAddress}&tokenId={tokenId}&select=metadata");
public static string GetContractMetadataEndPoint(string contractAddress) => Path.Combine(_baseUrl, $"accounts/{contractAddress}?legacy=false");
public static string GetOperationStatusEndPoint(string operationHash) => Path.Combine(_baseUrl, $"operations/{operationHash}/status");
public static string GetLatestBlockLevelEndPoint() => Path.Combine(_baseUrl, $"blocks/{DateTime.UtcNow:yyyy-MM-ddTHH:mm:ssZ}/level");
public static string GetAccountCounterEndPoint(string address) => Path.Combine(_baseUrl, $"accounts/{address}/counter");
public static string GetTokensForOwnerEndPoint(string owner, bool withMetadata, long maxItems, string sort) => Path.Combine(_baseUrl, "tokens/balances?" + $"account={owner}&balance.ne=0&" + "select=account.address as owner,balance,token.contract as token_contract," + $"token.tokenId as token_id{(withMetadata ? ",token.metadata as token_metadata" : "")}," + "lastTime as last_time,id&" + $"{sort}&limit={maxItems}");
public static string GetOwnersForTokenEndPoint(string contractAddress, uint tokenId, long maxItems, string sort) => Path.Combine(_baseUrl, "tokens/balances?" + $"token.contract={contractAddress}&balance.ne=0&token.tokenId={tokenId}&" + "select=account.address as owner,balance,token.contract as token_contract," + "token.tokenId as token_id,lastTime as last_time,id&" + $"{sort}&limit={maxItems}");
public static string GetOwnersForContractEndPoint(string contractAddress, long maxItems, string sort) => Path.Combine(_baseUrl, "tokens/balances?" + $"token.contract={contractAddress}&balance.ne=0&" + "select=account.address as owner,balance,token.contract as token_contract," + "token.tokenId as token_id,id&" + $"{sort}&limit={maxItems}");
public static string GetTokensForContractEndPoint(string contractAddress, bool withMetadata, long maxItems, string sort) => Path.Combine(_baseUrl, $"tokens?contract={contractAddress}&select=contract,tokenId as token_id" + $"{(withMetadata ? ",metadata as token_metadata" : "")},holdersCount as holders_count,id," + $"lastTime as last_time&{sort}&limit={maxItems}");
public static string GetBalanceEndPoint(string walletAddress) => Path.Combine(_baseUrl, "accounts", walletAddress, "balance");
public static string GetRunViewEndPoint(string contract, string name) => Path.Combine(_baseUrl, $"helpers/view/{contract}/{name}");
public static string GetOperationStatusEndPoint(string operationHash) => Path.Combine(_baseUrl, $"operations/{operationHash}/status");

public static string GetTokenMetadataEndPoint(string tokenId)
{
var url = Path.Combine(_baseUrl, "tokens");
var queryParams = HttpUtility.ParseQueryString(string.Empty);
queryParams["tokenId"] = tokenId;
queryParams["limit"] = "1";
queryParams["select"] = "metadata";
return $"{url}?{queryParams}";
}

public static string GetTokensEndPoint(string address, int limit = 10)
{
var url = Path.Combine(_baseUrl, "tokens");
var queryParams = HttpUtility.ParseQueryString(string.Empty);
queryParams["sender"] = address;
queryParams["limit"] = limit.ToString();
return $"{url}?{queryParams}";
}
}
}
2 changes: 1 addition & 1 deletion Runtime/Scripts/Request/HttpClients/TezosClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class TezosClient

private int RequestTimeout { get; }

private T DeserializeJson<T>(string json) => JsonConvert.DeserializeObject<T>(json);
private T DeserializeJson<T>(string json) => JsonConvert.DeserializeObject<T>(json, new JsonSerializerSettings { MissingMemberHandling = MissingMemberHandling.Ignore });

public async UniTask<T> GetRequest<T>(string endpoint)
{
Expand Down
4 changes: 1 addition & 3 deletions Runtime/Scripts/Token/TokenBalance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

namespace Tezos.Token
{

public class TokenBalance
{
/// <summary>
Expand Down Expand Up @@ -37,12 +36,11 @@ public class TokenBalance
/// <summary>
/// Token metadata.
/// </summary>
public JsonElement TokenMetadata { get; set; }
public TokenMetadata TokenMetadata { get; set; }

/// <summary>
/// Timestamp of the block where the token balance was last changed.
/// </summary>
public DateTime LastTime { get; set; }
}

}
4 changes: 1 addition & 3 deletions Runtime/Scripts/Token/TokenData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

namespace Tezos.Token
{

public class TokenData
{
/// <summary>
Expand Down Expand Up @@ -91,7 +90,6 @@ public class TokenData
/// <summary>
/// Token metadata.
/// </summary>
public JsonElement TokenMetadata { get; set; }
public TokenMetadata TokenMetadata { get; set; }
}

}
Loading

0 comments on commit e27320c

Please sign in to comment.