Coder Social home page Coder Social logo

kennykwok1 / moralis-unity Goto Github PK

View Code? Open in Web Editor NEW

This project forked from 0xpr0f/moralis-unity

0.0 0.0 0.0 38.36 MB

The Moralis Unity SDK in full action, and code snippets need to easy the workload

Home Page: https://github.com/ethereum-boilerplate/ethereum-unity-boilerplate

C# 98.34% ShaderLab 1.27% HLSL 0.22% CSS 0.02% HTML 0.08% JavaScript 0.06%

moralis-unity's Introduction

Moralis-Unity

This is moralis unity query where we call different api methods.

if this project / readme helped you, give it a star

sending custom token with smart contract function without gas

    // sending custom erc20 with out specifying gas
    public async void sendCustomTokenwithoutcustomgas()
    {
        MoralisInterface.InsertContractInstance("LOL", ABI, "rinkeby", "0xfF75215204108992CFc2e902E560D461776BC906");
        Function f = MoralisInterface.EvmContractFunctionInstance("LOL", "rinkeby", "transfer");
        string playerAddress = "0xE1E891fE77ea200eaE62c9C9B3395443cc6ed7bE";
        string result = await f.SendTransactionAsync("0x37Ad540C876FceCf80090493F02068b115dDf8B6", playerAddress, 20);
        print(result);
    }

sending custom token with smart contract function with gas

 // sending custom erc20 specifying gas
 public async void sendCustomTokenwithcustomgas()
 {
     MoralisInterface.InsertContractInstance("LOL", ABI, "rinkeby", "0xfF75215204108992CFc2e902E560D461776BC906");
     // Set gas estimate
     HexBigInteger gas = new HexBigInteger(80000);
     string recieverAddress = "0xE1E891fE77ea200eaE62c9C9B3395443cc6ed7bE";
     string senderAddress = "0x37Ad540C876FceCf80090493F02068b115dDf8B6";
     object[] pars = { recieverAddress, 20};
     // Call the contract to claim the NFT reward.
     string result = await MoralisInterface.SendEvmTransactionAsync("LOL", "rinkeby", "transfer", senderAddress, gas, new HexBigInteger("0x0"), pars);

     print(result);
 }

web3Api methods

    public async void GetNFT()
    {
        OutPutAddress.text = "Fetching your NFTs...";
        NftOwnerCollection balance = await MoralisInterface.GetClient().Web3Api.Account.GetNFTs(AddressText.text.ToLower(), ChainList.mumbai);
        string NFTbalance = balance.ToJson();
        OutPutAddress.text = JToken.Parse(NFTbalance).ToString(Formatting.Indented);

    }

    public async void NFTInContractfromAdd()
    {
        OutPutAddress.text = "Fetching your NFTs from " + TokenText.text + "...";
        NftOwnerCollection balance = await MoralisInterface.GetClient().Web3Api.Account.GetNFTsForContract(AddressText.text.ToLower(), TokenText.text, ChainList.mumbai);
        string NFTbalance = balance.ToJson();
        OutPutAddress.text = JToken.Parse(NFTbalance).ToString(Formatting.Indented);
    }

    public async void TokenBalance()
    {
        OutPutAddress.text = "Fetching your ERC 20 Token balances...";
        List<Erc20TokenBalance> balance = await MoralisInterface.GetClient().Web3Api.Account.GetTokenBalances(AddressText.text.ToLower(), ChainList.eth);
        OutPutAddress.text = "";
        for (int i = 0; i < balance.Count; i++)
        {
            OutPutAddress.text += JToken.Parse(balance[i].ToJson()).ToString(Formatting.Indented);
        }

    }

    public async void TokenTransfers()
    {
        OutPutAddress.text = "Fetching your Token Trasactions..";
        Erc20TransactionCollection balance = await MoralisInterface.GetClient().Web3Api.Account.GetTokenTransfers(AddressText.text.ToLower(), ChainList.eth);
        string NFTbalance = balance.ToJson();
        OutPutAddress.text = JToken.Parse(NFTbalance).ToString(Formatting.Indented);
    }



    public async void AllNFTContract()
    {
        OutPutToken.text = "Fetching All metadata in " + TokenText.text;
        NftCollection nfts = await MoralisInterface.GetClient().Web3Api.Token.GetAllTokenIds(TokenText.text, ChainList.mumbai);
        string NFTbalance = nfts.ToJson();
        OutPutToken.text = JToken.Parse(NFTbalance).ToString(Formatting.Indented);
    }

    public async void NFTcontractTransfers()
    {
        OutPutToken.text = "Fetching All Contract transfers in " + TokenText.text;
        NftTransferCollection nftTransfers = await MoralisInterface.GetClient().Web3Api.Token.GetContractNFTTransfers(TokenText.text, ChainList.mumbai);
        string NFTbalance = nftTransfers.ToJson();
        OutPutToken.text = JToken.Parse(NFTbalance).ToString(Formatting.Indented);
    }
    public async void NFTmetadata()
    {
        OutPutToken.text = "Fetching All Contract metadata";
        NftContractMetadata metadata = await MoralisInterface.GetClient().Web3Api.Token.GetNFTMetadata(TokenText.text, ChainList.mumbai);
        string NFTbalance = metadata.ToJson();
        OutPutToken.text = JToken.Parse(NFTbalance).ToString(Formatting.Indented);
    }
    public async void Search()
    {
        OutPutToken.text = "Searching For NFT...";
        NftMetadataCollection metadata = await MoralisInterface.GetClient().Web3Api.Token.SearchNFTs(search.text, ChainList.eth, null, null, null, null, null, null, 0, 100);
        string NFTbalance = metadata.ToJson();
        OutPutToken.text = JToken.Parse(NFTbalance).ToString(Formatting.Indented);

    }

Custom Objects

public class Character : MoralisObject
{
    public int Strength { get; set; }
    public int Level { get; set; }
    public string Name { get; set; }
    public string Skill { get; set; }
    public List<string> Bag { get; set; }

    public Character() : base("Character")
    {
        Bag = new List<string>();
    }
}

Saving objects to the database

public async void SaveObjectToDB()
    {
        Character character = MoralisInterface.GetClient().Create<Character>();
        character.Name = "Morphis";
        character.Strength = 100;
        character.Level = 80;
        character.Skill = "Strength";
        character.Bag.Add("Axe of Doom");
        character.Bag.Add("moralisSDK");
        await character.SaveAsync();
    }

Retrieving from database

public async void RetrieveObjectFromDB()
    {
        MoralisQuery<Character> character = MoralisInterface.GetClient().Query<Character>().WhereEqualTo("Level", 80);
        IEnumerable<Character> result = await character.FindAsync();
        foreach(Character c in result)
        {
            print("The warlord is " + c.Name + " with" + c.Skill);
        }
    }

Sending raw ETH

public async void SendRawETH()
    {
        // Retrieve from address, the address used to athenticate the user.
        MoralisUser user = await MoralisInterface.GetUserAsync();
        int transferAmount = 10;
        string fromAddress = user.authData["moralisEth"]["id"].ToString();
        string toAddress = "0xE1E891fE77ea200eaE62c9C9B3395443cc6ed7bE";
        // Create transaction request.
        TransactionInput txnRequest = new TransactionInput()
        {
            Data = String.Empty,
            From = fromAddress,
            To = toAddress,
            Value = new HexBigInteger(transferAmount)
        };
        try
        {
            // Execute the transaction.
            string txnHash = await MoralisInterface.Web3Client.Eth.TransactionManager.SendTransactionAsync(txnRequest);

            Debug.Log($"Transfered {transferAmount} WEI from {fromAddress} to {toAddress}.  TxnHash: {txnHash}");
        }
        catch (Exception exp)
        {
            Debug.Log($"Transfer of {transferAmount} WEI from {fromAddress} to {toAddress} failed! with error {exp}");
        }
    }

Fixing ipfs link for images

    public string FixImageUri(string imageuri)
    {
        if (imageuri.StartsWith("ipfs://"))
        {
            return imageuri.Replace("ipfs://", "https://ipfs.moralis.io:2053/ipfs/");
        }
        return imageuri;

    }
    public void call()
    {
      string result =  FixImageUri("ipfs://QmW5qHWBfE7yH8LFkeCmDNjBRPGEWqYDqjHgaLiQBDsuQg/4731.png");
        print(result);
    }

moralis-unity's People

Contributors

0xpr0f avatar 10xdevcoder avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.