pancake_swap_sdk 1.0.0
pancake_swap_sdk: ^1.0.0 copied to clipboard
A package is a project forked from two prominent web libraries, pancake swap sdk and uniswap swap sdk.
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:http/http.dart';
import 'package:pancake_swap_sdk/core/entities/index.dart';
import 'package:pancake_swap_sdk/core/entities/token/token_amount.dart';
import 'package:pancake_swap_sdk/v2/constants.dart';
import 'package:pancake_swap_sdk/v2/pair.dart';
import 'package:pancake_swap_sdk/v2/trade.dart';
import 'package:web3dart/credentials.dart';
import 'package:web3dart/web3dart.dart';
import 'json/LP_Contract.g.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: const MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key});
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
double priceBNB = 0;
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
const Text(
'Price(BNB/USDT):',
),
Text(
'${priceBNB.toStringAsFixed(5)} BUSD',
style: Theme.of(context).textTheme.headlineMedium,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _getPriceBNB,
tooltip: 'Get Price BNB',
child: const Icon(Icons.swap_calls),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
Future<void> _getPriceBNB() async {
final web3Client = Web3Client(
'https://bsc-dataseed.binance.org/',
Client(),
);
Token tokenFrom = Token.WETH[ChainId.MAINNET.value]!;
Token tokenTo = Token(
56,
EthereumAddress.fromHex('0xe9e7CEA3DedcA5984780Bafc599bD69ADd087D56'),
18);
var pairAddress = Pair.getAddress(tokenFrom, tokenTo);
final balanceFunc =
FarmContract(address: pairAddress!, client: web3Client, chainId: 56);
final data = await balanceFunc.getReserves();
final pair = Pair(
TokenAmount(
tokenFrom,
data.reserve0,
),
TokenAmount(
tokenTo,
data.reserve1,
),
);
final amountOne = BigInt.from(1 * pow(10, 18));
var bestTrade = Trade.bestTradeExactIn(
[pair],
TokenAmount(tokenFrom, amountOne),
tokenTo,
);
setState(() {
priceBNB = getEstimateTokenSwapOut(bestTrade[0].outputAmount.raw);
});
}
}
double getEstimateTokenSwapOut(BigInt outPut) {
return (outPut / BigInt.from(pow(10, 18))).toDouble();
}