Comments (5)
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.
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
- How do i read a method from a smart contract
2.How do i write to a method from a smart contract
from web3dart.
As for getting ether balance on the address I can it works perfectly well..
from web3dart.
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);
});
}
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.
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 havepure
orview
set in theirstateMutability
field in the contract ABI. - Basically, use
send
for all others. Calling methods withsend
will create a transaction that can write state and will show up on etherscan as they should.
from web3dart.
Related Issues (20)
- NFT dapp HOT 1
- How to set a timeout for Web3Client? HOT 1
- intToBytes mistake
- How to decodeParameters
- web3dart
- Ignore non_constant_identifier_names, camel_case_types in generated (.g.dart) files HOT 5
- Test Net HOT 2
- Support ethers.utils functionality HOT 1
- Bug: Zero address returned from contract call causes exception HOT 20
- web3dart: ^2.3.3
- private Api for centralized blockchain HOT 3
- How can i call smart contract functions through web3dart without private key in flutter HOT 2
- getBlockInformation() - Throws Error HOT 1
- Transaction Lists HOT 2
- How a new user can create an account without private key?
- Support for BEP20 Transfer?
- Value not in Range
- On Ganache V 7.0.0 I am receiving an rpc error "Unhandled Exception: RPCError: got code -32700 with msg "Invalid signature v value"." HOT 1
- Getting transactions with eth_getBlockByNumber
- Doesn't work after building project but works fine in debug
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from web3dart.