Token Config

2. Multi-Chain Token Configuration

Overview

The NAREON framework supports multiple blockchain networks, including Ethereum (ERC20, ERC721) and Solana (SPL Tokens, Metaplex NFTs). This documentation covers the configuration and implementation details for managing these assets in your Unity project.

Configuration Setup

In your NAREONSettings.asset, configure the tokens section with the following structure based on your chosen blockchain:

Network Configuration

{
    "networks": {
        "ethereum": {
            "rpc": "https://mainnet.infura.io/v3/YOUR-PROJECT-ID",
            "chainId": 1,
            "defaultGasLimit": 200000
        },
        "solana": {
            "rpc": "https://api.mainnet-beta.solana.com",
            "commitment": "confirmed",
            "defaultTimeout": 30000
        }
    }
}

Token Configurations

Ethereum Tokens (ERC20)

{
    "tokenName": "GoldCoin",
    "network": "ethereum",
    "contractAddress": "0x123abc...",
    "tokenType": "ERC20",
    "decimals": 18,
    "defaultAmount": 100,
    "triggerName": "OnNPCDialogueEnd"
}

Ethereum NFTs (ERC721)

{
    "tokenName": "DragonNFT",
    "network": "ethereum",
    "contractAddress": "0xabc123...",
    "tokenType": "ERC721",
    "tokenId": 999,
    "triggerName": "OnQuestComplete"
}

Solana SPL Tokens

{
    "tokenName": "GoldCoin",
    "network": "solana",
    "mintAddress": "7xKXtg2CW87d97TXJSDpbD5jBkheTqA83TZRuJosgAsU",
    "tokenType": "SPL",
    "decimals": 9,
    "defaultAmount": 100,
    "triggerName": "OnNPCDialogueEnd"
}

Solana NFTs (Metaplex)

{
    "tokenName": "DragonNFT",
    "network": "solana",
    "collectionAddress": "AKxZWve8njQodwcUGFrm1Jz9ZWQXRXXCkqQ7PiXe2gdL",
    "tokenType": "MetaplexNFT",
    "metadataFormat": "standard",
    "triggerName": "OnQuestComplete",
    "updateAuthority": "your_authority_address"
}

Implementation Reference

Multi-Chain Token Manager

using UnityEngine;
using NAREON.Core;
using NAREON.Ethereum;
using NAREON.Solana;

public class MultiChainTokenManager : MonoBehaviour
{
    public async Task GiveReward(string recipientAddress, string tokenName, decimal amount)
    {
        var tokenConfig = NAREONSettings.Instance.GetTokenConfig(tokenName);
        
        switch (tokenConfig.network.ToLower())
        {
            case "ethereum":
                await GiveEthereumReward(recipientAddress, tokenConfig, amount);
                break;
            
            case "solana":
                await GiveSolanaReward(recipientAddress, tokenConfig, amount);
                break;
            
            default:
                throw new NotSupportedException($"Network {tokenConfig.network} not supported");
        }
    }

    private async Task GiveEthereumReward(string recipientAddress, TokenConfig config, decimal amount)
    {
        try
        {
            var result = await NAREONEthereum.TransferToken(
                recipientAddress,
                config.contractAddress,
                amount,
                config.tokenType == "ERC20"
            );
            
            Debug.Log($"Ethereum transfer complete. Hash: {result.TransactionHash}");
        }
        catch (EthereumException ex)
        {
            Debug.LogError($"Ethereum transfer failed: {ex.Message}");
        }
    }

    private async Task GiveSolanaReward(string recipientAddress, TokenConfig config, decimal amount)
    {
        try
        {
            await NAREONSolana.CreateAssociatedTokenAccountIfNeeded(
                recipientAddress, 
                config.mintAddress
            );
            
            var result = await NAREONSolana.TransferSPLTokens(
                recipientAddress,
                config.mintAddress,
                (ulong)amount,
                commitment: "confirmed"
            );
            
            Debug.Log($"Solana transfer complete. Signature: {result.Signature}");
        }
        catch (SolanaException ex)
        {
            Debug.LogError($"Solana transfer failed: {ex.Message}");
        }
    }
}

Multi-Chain NFT Minter

using UnityEngine;
using NAREON.Core;
using NAREON.Ethereum;
using NAREON.Solana;
using NAREON.Metaplex;

public class MultiChainNFTMinter : MonoBehaviour
{
    public async Task<string> MintNFT(
        string tokenName,
        string recipientAddress,
        string npcName,
        string imageUrl)
    {
        var nftConfig = NAREONSettings.Instance.GetTokenConfig(tokenName);
        
        switch (nftConfig.network.ToLower())
        {
            case "ethereum":
                return await MintEthereumNFT(nftConfig, recipientAddress, npcName, imageUrl);
            
            case "solana":
                return await MintSolanaNFT(nftConfig, npcName, imageUrl);
            
            default:
                throw new NotSupportedException($"Network {nftConfig.network} not supported");
        }
    }

    private async Task<string> MintEthereumNFT(
        TokenConfig config,
        string recipientAddress,
        string npcName,
        string imageUrl)
    {
        var metadata = new ERC721Metadata
        {
            name = npcName,
            image = imageUrl,
            description = "NAREON Game NFT"
        };

        try
        {
            var result = await NAREONEthereum.MintNFT(
                config.contractAddress,
                recipientAddress,
                metadata
            );
            
            Debug.Log($"Ethereum NFT minted. Hash: {result.TransactionHash}");
            return result.TokenId.ToString();
        }
        catch (EthereumException ex)
        {
            Debug.LogError($"Ethereum NFT minting failed: {ex.Message}");
            return null;
        }
    }

    private async Task<string> MintSolanaNFT(
        TokenConfig config,
        string npcName,
        string imageUrl)
    {
        var metadata = new MetaplexMetadata
        {
            name = npcName,
            symbol = "NPC",
            uri = imageUrl,
            sellerFeeBasisPoints = 500,
            creators = new[]
            {
                new Creator
                {
                    address = NAREONSolana.GetProgramAddress(),
                    verified = true,
                    share = 100
                }
            },
            collection = new Collection
            {
                verified = true,
                key = config.collectionAddress
            }
        };

        try
        {
            var result = await NAREONSolana.MintNFT(
                metadata,
                config.collectionAddress,
                useCompression: false,
                commitment: "finalized"
            );
            
            Debug.Log($"Solana NFT minted. Signature: {result.Signature}");
            return result.MintAddress;
        }
        catch (MetaplexException ex)
        {
            Debug.LogError($"Solana NFT minting failed: {ex.Message}");
            return null;
        }
    }
}

Chain-Specific Considerations

Ethereum

  1. Gas Management

    • Implement proper gas estimation

    • Handle gas price fluctuations

    • Consider implementing EIP-1559 support

  2. Transaction Confirmation

    • Wait for sufficient block confirmations

    • Handle chain reorganizations

    • Implement transaction receipt polling

Solana

  1. Account Management

    • Handle associated token accounts

    • Manage program derived addresses

    • Consider rent-exempt requirements

  2. Performance Optimization

    • Use compressed NFTs for large collections

    • Implement proper commitment levels

    • Handle rate limiting and RPC node management

Best Practices

Cross-Chain Development

  1. Error Handling

    • Implement chain-specific error handling

    • Use proper exception hierarchies

    • Maintain consistent error reporting

  2. Configuration Management

    • Use environment-based network selection

    • Implement proper secret management

    • Cache network configurations

  3. Testing

    • Test on respective test networks

    • Implement proper mocking for tests

    • Use chain-specific test helpers

Security

  1. Key Management

    • Secure storage of private keys

    • Implement proper signing procedures

    • Regular key rotation policies

  2. Transaction Safety

    • Implement transaction simulation

    • Proper parameter validation

    • Handle transaction timeouts

  3. Network Security

    • RPC endpoint security

    • Rate limiting implementation

    • Proper error handling

Troubleshooting Guide

Common Issues

  1. Transaction Failures

    • Ethereum: Check gas prices and limits

    • Solana: Verify account existence and balances

    • Both: Validate network status

  2. NFT Minting Issues

    • Ethereum: Contract permissions

    • Solana: Collection verification

    • Both: Metadata validation

  3. Network Connectivity

    • RPC node availability

    • Rate limiting issues

    • Network congestion handling

Last updated