Ink
Ink CLI
ink_cli is a Dart library designed to simplify the process of generating Dart classes and methods for interacting with ink! smart contracts. This documentation will show you how to use ink_cli with examples from a simple Flipper contract.
ink_cli is a Dart library designed to simplify the process of generating Dart classes and methods for interacting with ink! smart contracts. This documentation will show you how to use ink_cli with examples from a simple Flipper contract.
Requirements
Before using ink_cli, ensure you have:
- Dart
>=3.6.0 polkadart&polkadart_keyringfor blockchain interactionink_abifor contract ABI parsing
Installation
Add the following dependencies to your pubspec.yaml file:
dependencies:
ink_abi: any
ink_cli: any
polkadart: any
polkadart_keyring: any
Then, run:
dart pub get
or
flutter pub get
Generating Contract Classes
The core functionality of ink_cli is generating type-safe Dart classes from ink! contract metadata. Here's how to generate contract classes:
void generateTypes() {
final fileOutput = FileOutput('generated_flipper.dart');
final generator = TypeGenerator(
abiFilePath: 'flipper.json', fileOutput: fileOutput);
generator.generate();
fileOutput.write();
}
Deploying Contracts
Once classes are generated, you can deploy contracts using the generated code:
Future<InstantiateRequest> deployContract() async {
final polkadart = Provider.fromUri(Uri.parse('ws://127.0.0.1:9944'));
final alice = await KeyPair.sr25519.fromUri('//Alice');
final contract = Contract(
provider: polkadart,
address: Uint8List.fromList(alice.publicKey.bytes.toList()),
);
final contractFile = File('flipper.wasm');
final InstantiateRequest result = await contract.new_contract(
init_value: true,
code: await contractFile.readAsBytes(),
keyPair: alice,
);
print('Contract address: 0x${encodeHex(result.contractAddress)}');
return result;
}
Contract Interaction
Here's an example of interacting with a deployed contract:
void callContract(List<int> address) async {
final polkadart = Provider.fromUri(Uri.parse('ws://127.0.0.1:9944'));
final contract = Contract(
provider: polkadart,
address: Uint8List.fromList(address),
);
// Call the get method
print('Get value: ${await contract.get()}');
}
Features
ink_cli provides several key features:
- Automated Code Generation: No need to manually write contract interaction code.
- Seamless Integration: Works with
polkadartfor blockchain communication. - Scalable & Extendable: Supports multiple versions of ink! metadata.