Skip to content

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.

Requirements

Before using ink_cli, ensure you have:

  • Dart >=3.6.0
  • polkadart & polkadart_keyring for blockchain interaction
  • ink_abi for 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:

Terminal window
dart pub get

or

Terminal window
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:

import 'dart:io';
import 'package:ink_cli/ink_cli.dart';
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:

import 'dart:io';
import 'dart:typed_data' show Uint8List;
import 'dart:typed_data';
import 'package:ink_cli/ink_cli.dart';
import 'package:polkadart/polkadart.dart';
import 'package:polkadart_keyring/polkadart_keyring.dart';
import 'generated_flipper.dart';
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:

import 'dart:typed_data';
import 'package:polkadart/polkadart.dart';
import 'generated_flipper.dart';
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 polkadart for blockchain communication.
  • Scalable & Extendable: Supports multiple versions of ink! metadata.