LoginSignup
5
3

More than 1 year has passed since last update.

Unityでブロックチェーンに触れてみよう2(Symbol)

Posted at

前回記事の状態だとデータは読めたけど、見づらくて良く分からんと思うので、少し扱いやすくしてみます。

1.扱いやすくするためのスクリプトを作成

読み込んだデータを扱いやすくするためのスクリプトを作成します。

①前回記事でやったように、メニューバーのAssets->Create->C# Scriptを選択し、空のスクリプトを作成します。
ファイル名は何でも良いですが『ApiAccount』としておきます。

②以下のコードをコピペして保存します。

ApiAccount.cs
using System;
using System.Collections.Generic;
using UnityEngine;

namespace SymbolData
{
    [Serializable]
    public class ApiAccount : MonoBehaviour
    {
        [Serializable]
        public class Linked
        {
            public string publicKey;
        }

        [Serializable]
        public class Node
        {
            public string publicKey;
        }

        [Serializable]
        public class Vrf
        {
            public string publicKey;
        }
        
        [Serializable]
        public class Voting
        {
            public List<string> keys;
        }

        [Serializable]
        public class SupplementalPublicKeys
        {
            public Linked linked;
            public Node node;
            public Vrf vrf;
            public List<Voting> voting;
        }

        [Serializable]
        public class ActivityBucket
        {
            public string startHeight;
            public string totalFeesPaid;
            public int beneficiaryCount;
            public string rawScore;
        }

        [Serializable]
        public class Mosaic
        {
            public string id;
            public string amount;
        }

        [Serializable]
        public class Account
        {
            public int version;
            public string address;
            public string addressHeight;
            public string publicKey;
            public string publicKeyHeight;
            public int accountType;
            public SupplementalPublicKeys supplementalPublicKeys;
            public List<ActivityBucket> activityBuckets;
            public List<Mosaic> mosaics;
            public string importance;
            public string importanceHeight;
        }

        [Serializable]
        public class AccountDatum
        {
            public Account account;
            public string id;
        }

        [Serializable]
        public class AccountPagination
        {
            public int pageNumber;
            public int pageSize;
        }

        [Serializable]
        public class AccountRoot
        {
            public List<AccountDatum> data;
            public AccountPagination pagination;
        }
    }
}

※上記スクリプトは、toshiさんが以前作られたSymnityから拝借しています

2.AccountCheckスクリプトを更新

前回記事で作成したAccountCheckスクリプトを更新して、データを見やすく表示します。

①AccountCheckスクリプトを開いて、以下のコードをコピペして保存します。

AccountCheck.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using Cysharp.Threading.Tasks;

public class AccountCheck : MonoBehaviour
{
	async void Start()
	{
		string node = "http://wolf.importance.jp:3000";
		string checkAddress = "NA6DKVBRZCLZ47KBLFJGZCODAZLYMBDLAVAYH2I";
		UnityWebRequest www = UnityWebRequest.Get( node + "/accounts/" + checkAddress );
		www.SetRequestHeader( "Content-Type", "application/json" );
		await www.SendWebRequest();
		var response = www.downloadHandler.text;

		SymbolData.ApiAccount.AccountDatum accountRoot = JsonUtility.FromJson<SymbolData.ApiAccount.AccountDatum>( www.downloadHandler.text );
		var responseJson = JsonUtility.ToJson( accountRoot, true );
		Debug.Log( responseJson );
	}
}

3.プロジェクトを実行して、結果を確認

プロジェクトを実行して、ブロックチェーン上からデータを読み出し、ログに表示します。

①再生ボタンをクリックし実行
7.png
②Consoleウィンドウに、ブロックチェーン上から読み出したデータの情報が表示されていることを確認します。
1.JPG

4.おわり

3のログが前回記事より少し見やすくなったのでは無いでしょうか?(前回のは1行に情報が詰め込まれていましたが、今回は複数行に分かれていて見やすくなっていると思います)

ここで、少し今回行ったことを補足しておきます。

1で作ったApiAccountというスクリプトは、ブロックチェーン上から読み出したアカウントデータを格納するためのクラスになります。
このクラスにデータを入れることで、内部の各種データにアクセスしやすくなります。
例えば、以下のようなスクリプトを書くと、所有しているトークン(モザイク)のIDをログに表示することができたりします。

SymbolData.ApiAccount.AccountDatum accountRoot = JsonUtility.FromJson<SymbolData.ApiAccount.AccountDatum>( www.downloadHandler.text );
var responseId = accountRoot.account.mosaics[ 0 ].id;
Debug.Log( responseId );

これを応用すれば、持っているトークン(モザイク)に応じて、イベントを起こすといったことも簡単に行えるようになると思います。

2で変更した内容は、1で作ったクラスにアカウントデータを格納し、JsonUtilityという機能で見やすい形に整えてログに表示する形にしている感じになります。

おまけ

トークン(モザイク)のIDってどんなの?という方もいらっしゃると思うので、少し補足をば。
以下のリンクは、SymbolのNFTを扱っているプラットフォームであるCOMSAで私が販売しているNFTになります。
https://comsa.io/ncft/07976575-a936-4711-ada7-d286ee8cd03c
このページの中ほどにあるMosaic IDがトークン(モザイク)のIDになり、このNFTを表すIDになります。
2.JPG
こちらのNFTは2個所有しているので、ほんとに2個もってるかどうかログを確認してみるのも良いかと思います。(2023/1/18時点ではですがw)

5
3
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
3