Coder Social home page Coder Social logo

Comments (1)

gsmachado avatar gsmachado commented on June 16, 2024

The contract invocation feature is implemented since neow3j version 2.0.0. Check it out for the latest release here.

Anyway, I'm attaching here an example based on neow3j version 2.0.1:

import io.neow3j.contract.ContractInvocation;
import io.neow3j.contract.ContractParameter;
import io.neow3j.protocol.Neow3j;
import io.neow3j.protocol.core.methods.response.InvocationResult;
import io.neow3j.protocol.exceptions.ErrorResponseException;
import io.neow3j.protocol.http.HttpService;
import io.neow3j.transaction.InvocationTransaction;
import io.neow3j.wallet.Account;

import java.io.IOException;
import java.math.BigDecimal;

public class ContractInvocationExample {

    public static void main(String[] args) throws IOException, ErrorResponseException {

        // Crate a connection to an RPC node.
        Neow3j neow3j = Neow3j.build(new HttpService("https://node2.neocompiler.io"));

        // The script hash of the deployed contract. In this case it is the name service smart
        // contract found at this link
        // https://github.com/CityOfZion/python-smart-contract-workshop/blob/master/4-domain.py
        String contractScriptHash = "1a70eac53f5882e40dd90f55463cce31a9f72cd4";

        // Instantiate your account. It will be the originator of the contract invocation.
        Account acct = Account.fromWIF("KxDgvEKzgSBPPfuVfw67oPQBSjidEiqTHURKSDL1R7yGaGYAeYnr").build();

        // Fetch the account's balances. This is needed to pay for potential fees in the invocation.
        acct.updateAssetBalances(neow3j);

        // Build the ContractInvocation according to your needs.
        ContractInvocation invoc = new ContractInvocation.Builder(neow3j)
                .contractScriptHash(contractScriptHash)
                .account(acct)
                // Add parameters in the correct order as they are expected by the contract.
                .parameter(ContractParameter.string("register"))
                .parameter(ContractParameter.array(
                        ContractParameter.string("neow3j.com"),
                        ContractParameter.byteArrayFromAddress(acct.getAddress())))
                // You can add a network fee for priority. Your account needs to own the GAS for it.
                .networkFee(new BigDecimal("0.1"))
                .build();

        // You can get the invocation transaction object if you for example need to create a custom
        // signature from it.
        InvocationTransaction tx = invoc.getTransaction();

        // If you don't need a special signature, simply let the transaction be signed with the
        // account given above.
        invoc.sign();

        // And finally perform the invocation. The RPC node doesn't inform us about the result of
        // the invocation. Therefore this call does not return any information about the call results.
        invoc.invoke();

        // You can also run a test invocation which gives you the GAS consumption of your invocation
        // and the return value that the contract produces when invoked. BUT be careful! If the
        // invocation runs trough a CheckWitness() method call in the smart contract it will fail
        // and return VM state FAULT. This does not mean that your invocation with invoc.incoke()
        // fails. It is just because in this test invoke it is not possible to provide the
        // information for completing the witness check.
        InvocationResult result = invoc.testInvoke();
    }
}

from neow3j.

Related Issues (20)

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.