import 'package:smart_contract_client/smart_contract_client.dart';
import 'package:web3dart/web3dart.dart';
class SmartContractClient {
SmartContractClient({
required Web3Client web3client,
required String privateKey,
required String abi,
required String contractAddress,
}) : _web3Client = web3client,
_privateKey = privateKey,
_abi = abi,
_contractAddress = contractAddress;
SmartContract get _contract => SmartContract.fromData(
abiInfo: _abi,
privateKey: _privateKey,
contractAddress: _contractAddress,
);
final Web3Client _web3Client;
final String _privateKey;
final String _abi;
final String _contractAddress;
Future<String> getName() async {
try {
final name = await _web3Client.call(
contract: _contract,
function: _contract.getName(),
params: <dynamic>[],
);
return name[0] as String;
} catch (e) {
throw SmartContractClientException(e);
}
}
Future<String> setName({required String name}) async {
try {
final transactionHash = await _web3Client.sendTransaction(
_contract.credentials,
Transaction.callContract(
contract: _contract,
function: _contract.setName(),
parameters: <dynamic>[name],
),
);
return transactionHash;
} catch (e) {
throw SmartContractClientException(e);
}
}
}
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter