We are college students developing simple blockchain games for our school project.
This is our first time building a chainsafe web3 game and we are having problems with connecting smart contracts to unity games with chainsafe SDK.
We wrote some test codes to check our connection and got errors on the web console.
We’re trying to fix this since march but still can’t figure it out. Can you guys give us some ideas?
- We don’t have polygon mainnet on neither our codes and unity settings. Where did that come from?
enter image description here
enter image description here
We are using Ethereum Sepolia testnet (11155111) and we got our RPC node via Chainlist.
//This is our C# code on unity
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using System.Numerics;
using System;
using Newtonsoft.Json;
using Web3Unity.Scripts.Library.Ethers.Contracts;
using Web3Unity.Scripts.Library.Ethers.Providers;
using Web3Unity.Scripts.Library.ETHEREUEM.EIP;
public class GameController : MonoBehaviour
{
public static string TokenContract = "contract address"; // token contract
public static string TokenABI = "abi"; // contract ABI
public static string RPC = "RPC"; // RPC node
// System UI
public TextMeshProUGUI ConnectedWallet;
public TextMeshProUGUI TokenBalance;
public TextMeshProUGUI TxStatus;
// UI for buying NFTs
public TextMeshProUGUI SellerAccount;
public TextMeshProUGUI Price;
private void Start()
{
// shows account of the connected wallet
ConnectedWallet.text = PlayerPrefs.GetString("Account");
// Transfer of the ERC20 token
//TransferToken();
}
// code to test our connection with smart contracts
public async void FetchBalance()
{
// using method
string method = "balanceOf";
// parameter for the method
string _owner = PlayerPrefs.GetString("Account");
// RPC endpoint
var provider = new JsonRpcProvider(RPC);
// data to interact with smart contracts
var contract = new Contract(TokenABI, TokenContract, provider);
var data = await contract.Call(method, new object[]
{
// method's parameter
_owner,
});
// testing sanity
print(data[0].ToString());
}
}
//This is our Solidity code
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
// ERC20 Token
contract GameToken is ERC20 {
constructor() ERC20("GameToken", "GT") {
_mint(msg.sender, 1000000 * (10 ** uint256(decimals())));
}
// Function to check the token balance of a specific address
function balanceOf(address account) public view override returns (uint256) {
return super.balanceOf(account);
}
}
We use chainsafe sdk and chainstack. We tried to change blockchain network and rpc node, but we could not solve these. Could you please provide us with solutions related to these?
Grace is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.