Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
59 commits
Select commit Hold shift + click to select a range
a005071
Organizing calls
sneakzttv Nov 7, 2023
6a55525
Auto-duplicate Packages Samples
sneakzttv Nov 7, 2023
101fb55
Changes
sneakzttv Nov 7, 2023
edf7979
Merge branch 'sneakz/sample-organization' of https://github.com/Chain…
sneakzttv Nov 7, 2023
3b6c873
Auto-duplicate Packages Samples
sneakzttv Nov 7, 2023
1e2a509
Update Erc20.cs
sneakzttv Nov 7, 2023
7c5cb63
Merge branch 'sneakz/sample-organization' of https://github.com/Chain…
sneakzttv Nov 7, 2023
5a258f3
Erc721
sneakzttv Nov 7, 2023
630c988
Auto-duplicate Packages Samples
sneakzttv Nov 7, 2023
44bce4b
1155 calls
sneakzttv Nov 7, 2023
a963078
Merge branch 'sneakz/sample-organization' of https://github.com/Chain…
sneakzttv Nov 7, 2023
83077c5
Auto-duplicate Packages Samples
sneakzttv Nov 7, 2023
e928091
Test fixes
sneakzttv Nov 8, 2023
b86e17e
Evm & gelato scripts
sneakzttv Nov 8, 2023
e255903
Scene
sneakzttv Nov 8, 2023
f4f74f2
Fix tests
sneakzttv Nov 8, 2023
2c05f1c
Cleanup
sneakzttv Nov 8, 2023
bc73546
fix
sneakzttv Nov 8, 2023
42c8f87
ECDSA calls
sneakzttv Nov 8, 2023
d960e93
multicall
sneakzttv Nov 8, 2023
4d948db
multicall
sneakzttv Nov 8, 2023
a495056
Merge branch 'main' into sneakz/sample-organization
sneakzttv Nov 8, 2023
730885c
Auto-duplicate Packages Samples
sneakzttv Nov 8, 2023
0f48d9b
Update SampleMain.unity
sneakzttv Nov 8, 2023
255a8f4
Merge branch 'sneakz/sample-organization' of https://github.com/Chain…
sneakzttv Nov 8, 2023
960b938
Auto-duplicate Packages Samples
sneakzttv Nov 8, 2023
0b92982
Texture request
sneakzttv Nov 8, 2023
f4e72b1
Merge branch 'sneakz/sample-organization' of https://github.com/Chain…
sneakzttv Nov 8, 2023
bfd63e7
Auto-duplicate Packages Samples
sneakzttv Nov 8, 2023
dacfa9b
Update EvmCalls.cs
sneakzttv Nov 8, 2023
57e0b98
Merge branch 'sneakz/sample-organization' of https://github.com/Chain…
sneakzttv Nov 8, 2023
78c382f
Auto-duplicate Packages Samples
sneakzttv Nov 8, 2023
ac8959f
Cleanup
sneakzttv Nov 8, 2023
48f5aca
Merge branch 'sneakz/sample-organization' of https://github.com/Chain…
sneakzttv Nov 8, 2023
e5840a9
Auto-duplicate Packages Samples
sneakzttv Nov 8, 2023
ac98970
Fix tests
sneakzttv Nov 9, 2023
4889308
Auto-duplicate Packages Samples
sneakzttv Nov 9, 2023
370d38d
Fitting the typecast overload fix from Rob in
sneakzttv Nov 9, 2023
7936c5d
Merge branch 'sneakz/sample-organization' of https://github.com/Chain…
sneakzttv Nov 9, 2023
dedc977
Merge branch 'main' into sneakz/sample-organization
sneakzttv Nov 9, 2023
5c34b58
Auto-duplicate Packages Samples
sneakzttv Nov 9, 2023
a0677b9
static classes
sneakzttv Nov 9, 2023
8b5b16c
Auto-duplicate Packages Samples
sneakzttv Nov 9, 2023
e13578f
Fix image
sneakzttv Nov 9, 2023
1ec366b
Auto-duplicate Packages Samples
sneakzttv Nov 9, 2023
0afe7f1
Gelato disable fix
sneakzttv Nov 9, 2023
fc016fc
Auto-duplicate Packages Samples
sneakzttv Nov 9, 2023
154cab8
Update GelatoDisableContent.cs
sneakzttv Nov 9, 2023
ed1da37
Merge branch 'sneakz/sample-organization' of https://github.com/Chain…
sneakzttv Nov 9, 2023
7bd4a86
custom token fix
sneakzttv Nov 9, 2023
1c86967
Auto-duplicate Packages Samples
sneakzttv Nov 9, 2023
3a4574f
evm fix
sneakzttv Nov 9, 2023
98892f4
Merge branch 'sneakz/sample-organization' of https://github.com/Chain…
sneakzttv Nov 9, 2023
4c2e526
Auto-duplicate Packages Samples
sneakzttv Nov 9, 2023
fa499b2
Fix for multicall not working.
kantagara Nov 9, 2023
77eaf7d
Auto-duplicate Packages Samples
kantagara Nov 9, 2023
4805899
Fixes
sneakzttv Nov 9, 2023
13c3891
Merge branch 'sneakz/sample-organization' of https://github.com/Chain…
sneakzttv Nov 9, 2023
6d0f172
Auto-duplicate Packages Samples
sneakzttv Nov 9, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

122 changes: 114 additions & 8 deletions Packages/io.chainsafe.web3-unity/Runtime/Scripts/EVM/Token/Erc1155.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,37 @@
using System;
using System.Collections.Generic;
using System.Numerics;
using System.Threading.Tasks;
using ChainSafe.Gaming.Web3;
using Scripts.EVM.Remote;
using UnityEngine;
using UnityEngine.Networking;
using EthMethod = ChainSafe.Gaming.UnityPackage.EthMethod;

namespace Scripts.EVM.Token
{
// todo convert this into a service
public class Erc1155
public static class Erc1155
{
private static readonly string Abi = ABI.Erc1155;

/// <summary>
/// Fetches all 1155 Nfts from an account
/// </summary>
/// <param name="web3"></param>
/// <param name="chain"></param>
/// <param name="network"></param>
/// <param name="account"></param>
/// <param name="contract"></param>
/// <param name="take"></param>
/// <param name="skip"></param>
/// <returns></returns>
public static async Task<TokenResponse[]> AllErc1155(Web3 web3, string chain, string network, string account, string contract, int take, int skip)
{
return await CSServer.AllErc1155(web3, chain, network, account, contract, take, skip);
}

/// <summary>
/// Balance of ERC1155 Token
/// Balance of ERC1155 Token (string parameter)
/// </summary>
/// <param name="web3"></param>
/// <param name="contractAddress"></param>
Expand All @@ -26,9 +46,9 @@ public static async Task<BigInteger> BalanceOf(Web3 web3, string contractAddress
tokenId
});
}

/// <summary>
/// Balance of ERC1155 Token
/// Balance of ERC1155 Token (biginteger parameter)
/// </summary>
/// <param name="web3"></param>
/// <param name="contractAddress"></param>
Expand All @@ -43,7 +63,6 @@ public static async Task<BigInteger> BalanceOf(Web3 web3, string contractAddress
tokenId
});
}

private static async Task<BigInteger> BalanceOf(Web3 web3, string contractAddress, string account, object[] parameters)
{
var contract = web3.ContractBuilder.Build(Abi, contractAddress);
Expand Down Expand Up @@ -77,7 +96,7 @@ public static async Task<List<BigInteger>> BalanceOfBatch(Web3 web3, string cont
/// <param name="contractAddress"></param>
/// <param name="tokenId"></param>
/// <returns></returns>
public static async Task<string> URI(Web3 web3, string contractAddress, string tokenId)
public static async Task<string> Uri(Web3 web3, string contractAddress, string tokenId)
{
const string ipfsPath = "https://ipfs.io/ipfs/";
var contract = web3.ContractBuilder.Build(Abi, contractAddress);
Expand All @@ -92,6 +111,93 @@ public static async Task<string> URI(Web3 web3, string contractAddress, string t
});
return contractData[0].ToString();
}


/// <summary>
/// Mints ERC721 token
/// </summary>
/// <param name="web3"></param>
/// <param name="abi"></param>
/// <param name="contractAddress"></param>
/// <param name="id"></param>
/// <param name="amount"></param>
/// <returns></returns>
public static async Task<object[]> MintErc1155(Web3 web3, string abi, string contractAddress, BigInteger id, BigInteger amount)
{
byte[] dataObject = { };
const string method = "mint";
var destination = await web3.Signer.GetAddress();
var contract = web3.ContractBuilder.Build(abi, contractAddress);
return await contract.Send(method, new object[] { destination, id, amount, dataObject });
}

/// <summary>
/// Transfers ERC721 token
/// </summary>
/// <param name="web3"></param>
/// <param name="contractAddress"></param>
/// <param name="tokenId"></param>
/// <param name="amount"></param>
/// <param name="toAccount"></param>
/// <returns></returns>
public static async Task<object[]> TransferErc1155(Web3 web3, string contractAddress, BigInteger tokenId, BigInteger amount, string toAccount)
{
var account = await web3.Signer.GetAddress();
var abi = ABI.Erc1155;
var method = EthMethod.SafeTransferFrom;
byte[] dataObject = { };
var contract = web3.ContractBuilder.Build(abi, contractAddress);
var response = await contract.Send(method, new object[]
{
account,
toAccount,
tokenId,
amount,
dataObject
});
return response;
}

/// <summary>
/// Imports an NFT texture via Uri data
/// </summary>
/// <param name="web3"></param>
/// <param name="contract"></param>
/// <param name="tokenId"></param>
/// <returns></returns>
/// <exception cref="Exception"></exception>
public static async Task<Texture2D> ImportNftTexture1155(Web3 web3, string contract, string tokenId)
{
// fetch uri from chain
string uri = await Uri(web3, contract, tokenId);
// fetch json from uri
UnityWebRequest webRequest = UnityWebRequest.Get(uri);
await webRequest.SendWebRequest();
if (webRequest.result != UnityWebRequest.Result.Success)
{
throw new System.Exception(webRequest.error);
}
// Deserialize the data into the response class
Response data =
JsonUtility.FromJson<Response>(System.Text.Encoding.UTF8.GetString(webRequest.downloadHandler.data));
// parse json to get image uri
string imageUri = data.image;
Debug.Log("imageUri: " + imageUri);
if (imageUri.StartsWith("ipfs://"))
{
imageUri = imageUri.Replace("ipfs://", "https://ipfs.io/ipfs/");
}
Debug.Log("Revised URI: " + imageUri);
// fetch image and display in game
UnityWebRequest textureRequest = UnityWebRequestTexture.GetTexture(imageUri);
await textureRequest.SendWebRequest();
var response = ((DownloadHandlerTexture)textureRequest.downloadHandler).texture;
return response;
}

// Response class for the texture call above
public class Response
{
public string image;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
using System.Numerics;
using System.Threading.Tasks;
using ChainSafe.Gaming.Evm.Providers;
using ChainSafe.Gaming.UnityPackage;
using ChainSafe.Gaming.Web3;

namespace Scripts.EVM.Token
{
// todo convert this into a service
public class Erc20
public static class Erc20
{

private static readonly string Abi = ABI.Erc20;

/// <summary>
/// Balance Of ERC20 Address
/// </summary>
Expand All @@ -25,6 +26,32 @@ public static async Task<BigInteger> BalanceOf(Web3 web3, string contractAddress
});
return BigInteger.Parse(contractData[0].ToString());
}

/// <summary>
/// Custom ERC20 token balance of an address
/// </summary>
/// <param name="web3"></param>
/// <param name="contractAbi"></param>
/// <param name="contractAddress"></param>
/// <returns></returns>
public static async Task<BigInteger> CustomTokenBalance(Web3 web3, string contractAbi, string contractAddress)
{
var contract = web3.ContractBuilder.Build(contractAbi, contractAddress);
string address = await web3.Signer.GetAddress();
var contractData = await contract.Call(CommonMethod.BalanceOf, new object[] { address });
return BigInteger.Parse(contractData[0].ToString());
}

/// <summary>
/// Native ERC20 balance of an Address
/// </summary>
/// <param name="web3"></param>
/// <param name="account"></param>
/// <returns></returns>
public static async Task<BigInteger> NativeBalanceOf(Web3 web3, string account)
{
return await web3.RpcProvider.GetBalance(account);
}

/// <summary>
/// Name of ERC20 Token
Expand Down Expand Up @@ -77,5 +104,41 @@ public static async Task<BigInteger> TotalSupply(Web3 web3, string contractAddre
var totalSupply = await contract.Call(CommonMethod.TotalSupply);
return BigInteger.Parse(totalSupply[0].ToString());
}

/// <summary>
/// Mints ERC20 Tokens
/// </summary>
/// <param name="web3"></param>
/// <param name="toAddress"></param>
/// <param name="amount"></param>
/// <returns></returns>
public static async Task<object[]> MintErc20(Web3 web3, string contractAddress, string toAccount, BigInteger amount)
{
const string method = "mint";
var destination = await web3.Signer.GetAddress();
var contract = web3.ContractBuilder.Build(Abi, contractAddress);
return await contract.Send(method, new object[] { toAccount, amount });
}

/// <summary>
/// Transfers ERC20 Tokens
/// </summary>
/// <param name="web3"></param>
/// <param name="contractAddress"></param>
/// <param name="toAccount"></param>
/// <param name="amount"></param>
/// <returns></returns>
public static async Task<object[]> TransferErc20(Web3 web3, string contractAddress, string toAccount, BigInteger amount)
{
var abi = ABI.Erc20;
var method = EthMethod.Transfer;
var contract = web3.ContractBuilder.Build(abi, contractAddress);
var response = await contract.Send(method, new object[]
{
toAccount,
amount
});
return response;
}
}
}
}
Loading