scallop_deepbook_kit 5.0.0
scallop_deepbook_kit: ^5.0.0 copied to clipboard
A Dart/Flutter toolkit for interacting with DeepBook V3 Margin Pools on the Sui blockchain.
example/example.dart
// Canonical example for `scallop_deepbook_kit`.
//
// Part 1 reads DeepBook V3 margin-pool analytics over gRPC — no private key, no
// signing, no on-chain side effects, so it is safe to run anytime.
//
// Part 2 runs the signed supply/withdraw flow, and only executes when a
// PRIVATE_KEY is provided in the environment.
//
// dart run example/example.dart # read-only analytics
// PRIVATE_KEY=<suiprivkey…|hex|base64> dart run example/example.dart # full flow
//
// See also:
// example/margin_pool_demo.dart — batch pool analytics with a custom config
// example/toolkit_demo.dart — full private-key supply/withdraw walkthrough
// example/wallet_flow_demo.dart — build-then-sign with your own wallet/signer
import 'dart:io';
import 'package:scallop_deepbook_kit/scallop_deepbook_kit.dart';
Future<void> main() async {
// 1) Read pool parameters + APR. No key, no signing, no side effects.
final pool = DeepBookMarginPool(network: 'mainnet');
try {
final sui = await pool.getPoolParameters('SUI');
print('SUI margin pool (mainnet):');
print(
' supply APR: ${sui.supplyApr}',
); // normalized fraction (0.045 = 4.5%)
print(' borrow APR @hi: ${sui.borrowAprOnHighKink}');
print(' utilization: ${sui.utilizationRate}');
} finally {
await pool.dispose(); // release the gRPC channel
}
// 2) Signed supply/withdraw flow — runs only when PRIVATE_KEY is set.
// NEVER hard-code a key in app code; load it from secure storage.
final privateKey = Platform.environment['PRIVATE_KEY'];
if (privateKey == null || privateKey.isEmpty) {
print(
'\nSet PRIVATE_KEY to run the signed supply/withdraw flow on testnet.',
);
return;
}
final toolkit = DeepBookMarginToolkit(
ToolkitConfig(network: 'testnet', privateKey: privateKey),
);
try {
await toolkit.initialize(); // create the SupplierCap if one does not exist
print('\nWallet: ${toolkit.getAddress()}');
final before = await toolkit.getBalance('SUI');
print(
'Before: supply=${before.userSupplyAmount} SUI, '
'wallet=${before.walletBalance} SUI',
);
await toolkit.supplyToMarginPool('SUI', 0.1); // supply 0.1 SUI
final after = await toolkit.getBalance('SUI');
print(
'After: supply=${after.userSupplyAmount} SUI, '
'wallet=${after.walletBalance} SUI',
);
await toolkit.withdrawFromMarginPool('SUI'); // omit amount -> withdraw all
print('Withdrew all supplied SUI.');
} finally {
await toolkit.dispose();
}
}