Giter Site home page Giter Site logo

Comments (5)

simolus3 avatar simolus3 commented on July 29, 2024

You're trying to call setInstructor on the smart contract, right? Here, "modifies state" means that calling the function will write data onto the Blockchain: The function has a mutability of nonPayable, which means that the contract can still change its internal state.
Call-ing a transaction means that it will be executed as read-only. This means that the transaction is not actually sent to the network. Instead, the node you're connected with will execute the called function locally, which is free to use. Of course, this can't work when you're writing data, as that needs to be properly synchronized around the network. What you'll want to use instead is await finalizedTransaction.send(ethClient). Notice that writes won't work with a maximum gas of 0, you'll have to spend some Ether on that operation.
Please let me know if that doesn't answer your question, thanks.

from web3dart.

mtellect avatar mtellect commented on July 29, 2024

Thanks for your swift reply

I get this error

Performing hot restart...
Syncing files to device iPhone X...
Restarted application in 5,929ms.
flutter: getInstructors
[VERBOSE-2:ui_dart_state.cc(148)] Unhandled Exception: RPCError: got code -32000 with msg "invalid sender".
#0      JsonRPC.call (package:web3dart/src/io/jsonrpc.dart:47:4)
<asynchronous suspension>
#1      Web3Client._makeRPCCall (package:web3dart/src/web3client.dart:29:30)
<asynchronous suspension>
#2      Web3Client.sendRawTransaction (package:web3dart/src/web3client.dart:184:10)
#3      FinalizedTransaction.send.<anonymous closure> (package:web3dart/src/transaction.dart:115:18)
#4      _rootRunUnary (dart:async/zone.dart:1132:38)
#5      _CustomZone.runUnary (dart:async/zone.dart:1029:19)
#6      _FutureListener.handleValue (dart:async/future_impl.dart:126:18)
#7      Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:639:45)
#8      Future._propagateToListeners (dart:async/future_impl.dart:668:32)
#9      Future._complete (dart:async/future_impl.dart:473:7)
#10     _SyncCompleter.complete (dart:async/future_impl.dart:51:12)
#11     _AsyncAwaitCompleter.complete (dart:a<…>

with re writing the code this way

void _getSmartCoursesContract() async {
    String courseContractABI = json.encode(coursesABI);
    String tokenAddress = "0x8b373636Ff3147FCc01B40b57114eC282e77E2df";
    String privateKeyHex =
        "79FD6A46633A4AF7B1C3C1CA1D5EDFEC35245D42A5A3661B29AFFD8263E32853";

    var httpClient = new Client();
    var ethClient = new Web3Client(apiUrl, httpClient);
    var credentials = Credentials.fromPrivateKeyHex(privateKeyHex);

    var contractHelloABI =
        ContractABI.parseFromJSON(courseContractABI, "Courses");
    var coursesContract = new DeployedContract(contractHelloABI,
        new EthereumAddress(tokenAddress), ethClient, credentials);

    var getCoursesFn =
        coursesContract.findFunctionsByName("countInstructors").first;
    var listTokenFn = coursesContract.functions;

    print(listTokenFn[1].name);

//    for (FunctionParameter fp in getCoursesFn.parameters) {
//      print("Function ${getCoursesFn.name} ---- Parameters ${fp.name}");
//    }
//
//    for (int a = 0; a < listTokenFn.length; a++) {
//      print(listTokenFn[a].name);
//    }

//    for (ContractFunction fn in listTokenFn) {
//      for (FunctionParameter fp in fn.parameters) {
//        print("Function ${fn.name} ---- Parameters ${fp.name}");
//      }
//    }

    var age = new BigInt.from(24);
    var coursesResponse =
        new Transaction(keys: credentials, maximumGas: 127489);
    FinalizedTransaction finalizedTransaction = coursesResponse.prepareForCall(
      coursesContract,
      listTokenFn[2],
      [credentials.address.number, "Maugost", "Okore", age],
    );

    await finalizedTransaction.send(ethClient).then((result) {
      print(result);
    });
  }

Please how to i resolve this and also

  1. How do i read a method from a smart contract
    2.How do i write to a method from a smart contract

from web3dart.

mtellect avatar mtellect commented on July 29, 2024

As for getting ether balance on the address I can it works perfectly well..

from web3dart.

mtellect avatar mtellect commented on July 29, 2024

it works fine when i specify a chaidID

void _getSmartCoursesContract() async {
    String courseContractABI = json.encode(coursesABI);
    String privateKeyHex =
        "79FD6A46633A4AF7B1C3C1CA1D5EDFEC35245D42A5A3661B29AFFD8263E32853";

    var httpClient = new Client();
    var ethClient = new Web3Client(apiUrl, httpClient);
    var credentials = Credentials.fromPrivateKeyHex(privateKeyHex);

    var contractABI = ContractABI.parseFromJSON(courseContractABI, "Courses");
    var coursesContract = new DeployedContract(
        contractABI,
        new EthereumAddress(credentials.address.toString()),
        ethClient,
        credentials);

    var listTokenFn = coursesContract.functions;

    ContractFunction getInstructor = listTokenFn[0];
    ContractFunction getInstructors = listTokenFn[1];
    ContractFunction setInstructor = listTokenFn[2];
    ContractFunction countInstructor = listTokenFn[3];
    ContractFunction instructorAccounts = listTokenFn[4];

    //setting  instructors

    var age = new BigInt.from(24);
    var setInstructorResponse =
        new Transaction(keys: credentials, maximumGas: 127489);
    FinalizedTransaction setInstructorTransaction =
        setInstructorResponse.prepareForCall(
      coursesContract,
      setInstructor,
      [credentials.address.number, "Maugost", "Okore", age],
    );
    await setInstructorTransaction.send(ethClient, chainId: 4).then((result) {
      print(result);
    });

    //getting instructor

    var getInstructorResponse =
        new Transaction(keys: credentials, maximumGas: 127489);
    FinalizedTransaction getInstructorTransaction =
        getInstructorResponse.prepareForCall(
      coursesContract,
      getInstructor,
      [credentials.address.number],
    );
    await getInstructorTransaction.send(ethClient, chainId: 4).then((result) {
      print(result);
    });
  }

image

Transactions are being recorded on ether scan but now i get a little confuse sir..please what's the difference between the call(), and the send() in finalized transactions?
Please help??

from web3dart.

simolus3 avatar simolus3 commented on July 29, 2024

Hm that's weird, perhaps the first issue (transactions only work when specifying a chain id) is related to #28, I'll have to look into that some more.
To answer your questions on whether to use call or send:

  • Use call for functions that don't write data to the Blockchain. These functions either have pure or view set in their stateMutability field in the contract ABI.
  • Basically, use send for all others. Calling methods with send will create a transaction that can write state and will show up on etherscan as they should.

from web3dart.

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.