Generated by the Very Good CLI 🤖
This project uses:
- Visual Studio Code Insider - Version: 1.64.0-insider (Universal)
- Remix IDE - Version 1.3.3 (1.3.3.680)
- Ganache - Version 2.5.4 (2.5.4.1367)
- Dart - Dart SDK version: 2.14.4 (stable)
- Flutter - Flutter Channel beta, 2.8.0-3.3.pre, on macOS 12.1 21C5021h darwin-x64
This project contains 3 parts:
- contracts
- flutter app
This smart contract handles simple CRUD functions in Solidity
Solidity:
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
contract SimpleListContract {
address[] public people;
address[] private auxArray;
function addPerson(address _address) public {
people.push(_address);
}
function getPeople() public view returns(address[] memory){
return people;
}
function removePerson(address _value) public{
address[] memory emptyAddress;
for (uint i = 0; i < people.length; i++){
if (people[i] != _value){
auxArray.push(people[i]);
}
}
people = auxArray;
auxArray = emptyAddress;
}
}
Dart:
import 'dart:math';
import 'package:http/http.dart';
import 'package:wallet_core/wallet_core.dart';
import 'package:web3dart/web3dart.dart';
import 'lib/contracts/abi/simple_list.dart';
void test() async {
var apiUrl = 'http://localhost:7545'; //Replace with your API
final client = Web3Client(apiUrl, Client());
final credentials = EthPrivateKey.fromHex(
'12889a81fab17152a1b3374e973edfd288a226cbc7745b63543b7c0e02e47355',
);
final ownAddress = await credentials.extractAddress();
final contractAddr = EthereumAddress.fromHex(
'0x7547587bC874e2d35197206FEf34C84FEEfF1202',
);
// read the contract abi and tell web3dart where it's deployed (contractAddr)
final contract = DeployedContract(
ContractAbi.fromJson(simpleListAbi, 'SimpleListContract'),
contractAddr,
);
// Extracting some functions that we'll need later
final getPeople = contract.function('getPeople');
final addPerson = contract.function('addPerson');
final removePerson = contract.function('removePerson');
await client.sendTransaction(
credentials,
Transaction.callContract(
contract: contract,
function: addPerson,
parameters: [ownAddress],
),
);
for (var i = 0; i < 10; i++) {
final addPersonResult = await client.sendTransaction(
credentials,
Transaction.callContract(
contract: contract,
function: addPerson,
parameters: [EthPrivateKey.createRandom(Random()).address],
),
);
/// Transaction successful, id: 0x0bf35baf5609fffb740555521a06c2abc7d1324dffe2c02429b1770044759a5e
print('Transaction successful, id: $addPersonResult');
}
final getPeopleResult = await client.call(
contract: contract,
function: getPeople,
params: [],
);
final removePersonResult = await client.sendTransaction(
credentials,
Transaction.callContract(
contract: contract,
function: removePerson,
parameters: [ownAddress],
),
);
print('List of all people on the blockchain: ${getPeopleResult.first}');
/// List of all people on the blockchain: [
/// 0x2ffbdfb832d5295efc087db2e0fd1adeee3439de,
/// 0x2ffbdfb832d5295efc087db2e0fd1adeee3439de,
/// 0x2ffbdfb832d5295efc087db2e0fd1adeee3439de
/// ]
}