我无法将我的前端与智能合约连接。我做了一个小的选择dapp,当我在remix ide上部署和测试它时,它工作正常,但从我的flutter应用程序中,当我尝试将它与智能合约连接时,它不工作
我试着再次部署它(相应地更改了abi和合同地址),它仍然不工作,只有在混音测试后才能工作,但我想将它连接到我的应用程序,现在我该怎么办?
错误:
call function has error ::::: RPCError: got code -32000 with msg "execution reverted".
智能合约:
//SPDX-License-Identifier:UNLICENSED
pragma solidity ^0.8.0;
contract Election{
struct Candidate{
string name;
uint numvotes;
}
struct Voter{
string name;
bool authorised;
uint whom;
bool voted;
}
modifier ownerOnly(){
require(msg.sender == owner);
_;
}
address public owner;
string public ElectionName;
mapping(address => Voter) public Voters;
Candidate[] public candidates;
uint public totalvotes=0;
function startElection(string memory _ElectionName)public{
owner = msg.sender;
ElectionName = _ElectionName;
}
function addCandidate(string memory _candidatename) ownerOnly public{
candidates.push(Candidate(_candidatename,0));
}
function authoriseVoter(address _voteradress)ownerOnly public{
require(!Voters[msg.sender].voted);
Voters[_voteradress].authorised = true;
}
function getNumcandidates()public view returns(uint){
return candidates.length;
}
function Vote(uint CandidateIndex)public {
require(!Voters[msg.sender].voted);
require(Voters[msg.sender].authorised = true);
Voters[msg.sender].whom = CandidateIndex;
Voters[msg.sender].voted = true;
candidates[CandidateIndex].numvotes++;
totalvotes++;
}
function candidateInfo(uint index) public view returns(Candidate memory){
return candidates[index];
}
function getTotalVotes()public view returns(uint) {
return totalvotes;
}
}
前端功能:
Future<DeployedContract> loadContract() async {
try{
String abi = await rootBundle.loadString('assets/abi.json');
String contractAddress = contractAdressConst;
final contract = DeployedContract(ContractAbi.fromJson(abi, 'Election'),
EthereumAddress.fromHex(contractAddress));
return contract;
}catch(e){
print('load contract failed ::::: $e');
print('{{{{{{{{{{{{{{{[[{{{{{');
String abi = await rootBundle.loadString('assets/abi.json');
String contractAddress = contractAdressConst;
final contract = DeployedContract(ContractAbi.fromJson(abi, 'Election'),
EthereumAddress.fromHex(contractAddress));
return contract;
}
}
Future<String> callFunction(String funcname, List<dynamic> args,
Web3Client ethClient, String privateKey) async {
try{
EthPrivateKey credentials = EthPrivateKey.fromHex(privateKey);
DeployedContract contract = await loadContract();
final ethFunction = contract.function(funcname);
final result = await ethClient.sendTransaction(
credentials,
Transaction.callContract(
contract: contract,
function: ethFunction,
parameters: args,
),
chainId: null,
fetchChainIdFromNetworkId: true);
return result;
}catch(e){
print('call function has error ::::: $e');
return e.toString();
}
}
Future<String> startElection(String name, Web3Client ethClient) async {
try{
var response =
await callFunction('startElection', [name], ethClient, owner_private_key);
print('Election started successfully');
return response;
}catch(e){
print("election not started : : : $e");
return e.toString();
}
}
Future<String> addCandidate(String name, Web3Client ethClient) async {
try{
var response =
await callFunction('addCandidate', [name], ethClient, owner_private_key);
print('Candidate added successfully');
return response;
}catch(e){
print(" candidate not added : : :$e");
return e.toString();
}
}
家。 dart :
import 'package:election/utils/Constants.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart';
import 'package:web3dart/web3dart.dart';
import '../services/functions.dart';
import 'Electioninfo.dart';
class Home extends StatefulWidget {
const Home({Key? key}) : super(key: key);
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
Client? httpClient;
Web3Client? ethClient;
TextEditingController controller = TextEditingController();
@override
void initState() {
httpClient = Client();
ethClient = Web3Client(infura_url, httpClient!);
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Start Election'),
),
body: Container(
padding: EdgeInsets.all(14),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
TextField(
controller: controller,
decoration: InputDecoration(
filled: true, hintText: 'Enter election name'),
),
SizedBox(
height: 10,
),
Container(
width: double.infinity,
height: 45,
child: ElevatedButton(
onPressed: () async {
if (controller.text.length > 0) {
await startElection(controller.text, ethClient!);
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => ElectionInfo(
ethClient: ethClient!,
electionName: controller.text)));
}
},
child: Text('Start Election')))
],
),
),
);
}
}
1条答案
按热度按时间e0bqpujr1#
这是一个常见的错误,你可能会遇到任何时候,当你开发区块链相关的产品有一些可能性,为什么这个错误发生检查它我做了: