Skip to content

Commit

Permalink
✨ Add dex example using Solana.Unity.Dex (magicblock-labs#42)
Browse files Browse the repository at this point in the history
* ✨ Add dex example using Solana.Unity.Dex

* 📦 Update Solana.Unity.Wallet

* 🐛 Fix WebGL execution
  • Loading branch information
GabrielePicco authored Jan 22, 2023
1 parent 4b66378 commit 2fbf86b
Show file tree
Hide file tree
Showing 358 changed files with 85,131 additions and 9,438 deletions.
Binary file added Packages/Solana.Unity.Dex.dll
Binary file not shown.
33 changes: 33 additions & 0 deletions Packages/Solana.Unity.Dex.dll.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added Packages/Solana.Unity.Extensions.dll
Binary file not shown.
33 changes: 33 additions & 0 deletions Packages/Solana.Unity.Extensions.dll.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified Packages/Solana.Unity.KeyStore.dll
Binary file not shown.
Binary file modified Packages/Solana.Unity.Programs.dll
Binary file not shown.
Binary file modified Packages/Solana.Unity.Rpc.dll
Binary file not shown.
Binary file modified Packages/Solana.Unity.Wallet.dll
Binary file not shown.
3 changes: 2 additions & 1 deletion Runtime/codebase/IWalletBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,9 @@ public interface IWalletBase
/// <summary>
/// Returns tokens held by the given publicKey
/// </summary>
/// <param name="commitment"></param>
/// <returns></returns>
Task<TokenAccount[]> GetTokenAccounts();
Task<TokenAccount[]> GetTokenAccounts(Commitment commitment = Commitment.Finalized);

/// <summary>
/// Sign a transaction
Expand Down
12 changes: 7 additions & 5 deletions Runtime/codebase/WalletBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,13 +179,15 @@ public async Task<TokenAccount[]> GetTokenAccounts(PublicKey tokenMint, PublicKe
}

/// <inheritdoc />
public async Task<TokenAccount[]> GetTokenAccounts()
public async Task<TokenAccount[]> GetTokenAccounts(Commitment commitment = Commitment.Finalized)
{
var rpc = ActiveRpcClient;
var result = await rpc.GetTokenAccountsByOwnerAsync(
Account.PublicKey,
null,
TokenProgram.ProgramIdKey);
var result = await
rpc.GetTokenAccountsByOwnerAsync(
Account.PublicKey,
null,
TokenProgram.ProgramIdKey,
commitment);
return result.Result?.Value?.ToArray();
}

Expand Down
23 changes: 15 additions & 8 deletions Runtime/codebase/data/MetaplexData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,17 +105,24 @@ public Metaplex ParseData(string base64Data)
index += 4;
List<CreatorData> creators = new List<CreatorData>();

for (int i = 0; i < creatorsLenght; i++)
try
{
CreatorData creatorData = new CreatorData();
ObjectToByte.DecodeUTF8StringFromByte(data, index, (int)nameLength, out string creator);
creatorData.address = creator;
index += 32;
creatorData.verified = BitConverter.ToBoolean(data, index++);
creatorData.share = data[index++];
for (int i = 0; i < creatorsLenght; i++)
{
CreatorData creatorData = new CreatorData();
ObjectToByte.DecodeUTF8StringFromByte(data, index, (int)nameLength, out string creator);
creatorData.address = creator;
index += 32;
creatorData.verified = BitConverter.ToBoolean(data, index++);
creatorData.share = data[index++];
}
metaplexData.data.creators = creators.ToArray();
}
catch (Exception)
{
metaplexData.data.creators = null;
}

metaplexData.data.creators = creators.ToArray();

return metaplexData;
}
Expand Down
45 changes: 4 additions & 41 deletions Runtime/codebase/nft/Nft.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
using Solana.Unity.Wallet;
using Solana.Unity.Wallet.Utilities;

// ReSharper disable once CheckNamespace

namespace Solana.Unity.SDK.Nft
{
[Serializable]
Expand All @@ -23,13 +25,6 @@ public class NftImage : iNftFile<Texture2D>
public Texture2D file { get; set; }

public int heightAndWidth = 75;

//~NftImage() {
// if (file != null)
// {
// GameObject.Destroy(file);
// }
//}
}

[Serializable]
Expand All @@ -44,21 +39,6 @@ public Nft(Metaplex metaplexData)
this.metaplexData = metaplexData;
}

public static async Task<NFTProData> TryGetNftPro(string mint, IRpcClient connection) {

var data = await GetAccountData(mint, connection);

Debug.Log(Newtonsoft.Json.JsonConvert.SerializeObject(data));

var accountLayout = AccountLayout.DeserializeAccountLayout(data.Data[0]);
if (data is {Data: {Count: > 0}})
{
Debug.Log(Newtonsoft.Json.JsonConvert.SerializeObject(accountLayout));
}

return null;
}

/// <summary>
/// Returns all data for listed nft
/// </summary>
Expand Down Expand Up @@ -89,7 +69,7 @@ public static async Task<Nft> TryGetNftData(string mint, IRpcClient connection,
{
met.data.json = jsonData;
var texture = await FileLoader.LoadFile<Texture2D>(met.data.json.image);
var compressedTexture = Resize(texture, 75, 75);
var compressedTexture = FileLoader.Resize(texture, 75, 75);
FileLoader.SaveToPersistentDataPath(Path.Combine(Application.persistentDataPath, $"{mint}.png"), compressedTexture);
if (compressedTexture)
{
Expand Down Expand Up @@ -212,7 +192,6 @@ public static async Task<T> GetMetaplexJsonData<T>(string jsonUrl)
try
{
string responseBody = await response.Content.ReadAsStringAsync();
Debug.Log(responseBody);
T data = Newtonsoft.Json.JsonConvert.DeserializeObject<T>(responseBody);
client.Dispose();
return data;
Expand All @@ -223,23 +202,7 @@ public static async Task<T> GetMetaplexJsonData<T>(string jsonUrl)
return default;
}
}
/// <summary>
/// Resize great textures to small, because of performance
/// </summary>
/// <param name="texture2D"> Texture to resize</param>
/// <param name="targetX"> Target width</param>
/// <param name="targetY"> Target height</param>
/// <returns></returns>
private static Texture2D Resize(Texture texture2D, int targetX, int targetY)
{
RenderTexture rt = new RenderTexture(targetX, targetY, 24);
RenderTexture.active = rt;
Graphics.Blit(texture2D, rt);
Texture2D result = new Texture2D(targetX, targetY);
result.ReadPixels(new Rect(0, 0, targetX, targetY), 0, 0);
result.Apply();
return result;
}


/// <summary>
/// Get AccountData
Expand Down
25 changes: 21 additions & 4 deletions Runtime/codebase/utility/FileDownloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,11 @@ private static async Task<T> LoadJsonWebRequest<T>(string path)
private static async Task<T> LoadJson<T>(string path)
{
var client = new HttpClient();

var response = await client.GetAsync(path);
response.EnsureSuccessStatusCode();


try
{
var response = await client.GetAsync(path);
response.EnsureSuccessStatusCode();
var responseBody = await response.Content.ReadAsStringAsync();
var data = Newtonsoft.Json.JsonConvert.DeserializeObject<T>(responseBody);
client.Dispose();
Expand Down Expand Up @@ -137,6 +136,24 @@ public static void SaveToPersistentDataPath<T>(string path, T data)
File.WriteAllText(path, dataString);
}
}

/// <summary>
/// Resize great textures to small, because of performance
/// </summary>
/// <param name="texture2D"> Texture to resize</param>
/// <param name="targetX"> Target width</param>
/// <param name="targetY"> Target height</param>
/// <returns></returns>
public static Texture2D Resize(Texture texture2D, int targetX, int targetY)
{
RenderTexture rt = new RenderTexture(targetX, targetY, 24);
RenderTexture.active = rt;
Graphics.Blit(texture2D, rt);
Texture2D result = new Texture2D(targetX, targetY);
result.ReadPixels(new Rect(0, 0, targetX, targetY), 0, 0);
result.Apply();
return result;
}

}

Expand Down
2 changes: 1 addition & 1 deletion Runtime/codebase/utility/ObjectToByte.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Runtime.InteropServices;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using UnityEngine;

namespace Solana.Unity.SDK.Utility
{
Expand Down Expand Up @@ -68,7 +69,6 @@ public static void DecodeUTF8StringFromByte(byte[] data, int offset, int length,
decodedData = "";
var dataCopy = new byte[length];
Array.Copy(data, (long)offset, dataCopy, 0, length);

decodedData = Encoding.UTF8.GetString(dataCopy).Replace("\u0000", "");
}

Expand Down
8 changes: 8 additions & 0 deletions Samples~/Solana Wallet/Plugins/UniTask.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Samples~/Solana Wallet/Plugins/UniTask/Editor.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

62 changes: 62 additions & 0 deletions Samples~/Solana Wallet/Plugins/UniTask/Editor/SplitterGUILayout.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member

using System;
using System.Linq;
using System.Reflection;
using UnityEditor;
using UnityEngine;

namespace Cysharp.Threading.Tasks.Editor
{
// reflection call of UnityEditor.SplitterGUILayout
internal static class SplitterGUILayout
{
static BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static;

static Lazy<Type> splitterStateType = new Lazy<Type>(() =>
{
var type = typeof(EditorWindow).Assembly.GetTypes().First(x => x.FullName == "UnityEditor.SplitterState");
return type;
});

static Lazy<ConstructorInfo> splitterStateCtor = new Lazy<ConstructorInfo>(() =>
{
var type = splitterStateType.Value;
return type.GetConstructor(flags, null, new Type[] { typeof(float[]), typeof(int[]), typeof(int[]) }, null);
});

static Lazy<Type> splitterGUILayoutType = new Lazy<Type>(() =>
{
var type = typeof(EditorWindow).Assembly.GetTypes().First(x => x.FullName == "UnityEditor.SplitterGUILayout");
return type;
});

static Lazy<MethodInfo> beginVerticalSplit = new Lazy<MethodInfo>(() =>
{
var type = splitterGUILayoutType.Value;
return type.GetMethod("BeginVerticalSplit", flags, null, new Type[] { splitterStateType.Value, typeof(GUILayoutOption[]) }, null);
});

static Lazy<MethodInfo> endVerticalSplit = new Lazy<MethodInfo>(() =>
{
var type = splitterGUILayoutType.Value;
return type.GetMethod("EndVerticalSplit", flags, null, Type.EmptyTypes, null);
});

public static object CreateSplitterState(float[] relativeSizes, int[] minSizes, int[] maxSizes)
{
return splitterStateCtor.Value.Invoke(new object[] { relativeSizes, minSizes, maxSizes });
}

public static void BeginVerticalSplit(object splitterState, params GUILayoutOption[] options)
{
beginVerticalSplit.Value.Invoke(null, new object[] { splitterState, options });
}

public static void EndVerticalSplit()
{
endVerticalSplit.Value.Invoke(null, Type.EmptyTypes);
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "UniTask.Editor",
"references": [
"UniTask"
],
"includePlatforms": [
"Editor"
],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": false,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 2fbf86b

Please sign in to comment.