Skip to content

Commit

Permalink
Merge pull request #74 from 3Dpass/dev
Browse files Browse the repository at this point in the history
hotfix: Liquidity pools. Version 2.16.1
  • Loading branch information
L3odr0id authored Aug 15, 2024
2 parents c21f12c + 424a345 commit c663f2c
Show file tree
Hide file tree
Showing 44 changed files with 308 additions and 244 deletions.
4 changes: 3 additions & 1 deletion android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@ android {

splits {
abi {
enable true
reset()
include "arm64-v8a"
include ("arm64-v8a", "x86", "x86_64", "armeabi-v7a")
it.universalApk = true
}
}

Expand Down
6 changes: 3 additions & 3 deletions assets/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@
"set_metadata_label_name": "Name",
"set_metadata_label_symbols": "Symbols",
"set_metadata_label_decimals": "Decimals",
"error_u8": "Expected 0 < value < 255",
"error_u8": "Expected 0 <= value < 255",
"set_metadata_notification_asset_id": "Asset id:",
"show_all_assets_label": "Show all assets",

Expand All @@ -486,8 +486,8 @@
"chain_status_recognized": "Recognized",

"@2.15.2": {},
"open_pools_label": "Pools",
"pools_page_appbar": "Pools",
"open_pools_label": "Liquidity pools",
"pools_page_appbar": "Liquidity pools",
"pools_text": "Earn fees by providing liquidity.",
"withdraw_button_label": "Withdraw",
"deposit_button_label": "Deposit",
Expand Down
2 changes: 1 addition & 1 deletion lib/core/polkawallet/bloc/app_service_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import 'package:threedpass/core/polkawallet/utils/balance_utils.dart';
import 'package:threedpass/core/polkawallet/utils/network_state_data_extension.dart';
import 'package:threedpass/core/polkawallet/utils/tx_update_event_logs_handler.dart';
import 'package:threedpass/features/accounts/domain/account_info.dart';
import 'package:threedpass/features/asset_conversion/ui/pools_page/bloc/pools_cubit.dart';
import 'package:threedpass/features/asset_conversion/ui/pools_list/bloc/pools_cubit.dart';
import 'package:threedpass/features/poscan_assets/bloc/poscan_assets_cubit.dart';
import 'package:threedpass/features/poscan_objects_query/bloc/poscan_objects_cubit.dart';
import 'package:threedpass/features/poscan_objects_query/data/poscan_local_repository.dart';
Expand Down
2 changes: 1 addition & 1 deletion lib/core/utils/validators.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Validators {
static String? onlyU8(final String? p0) {
final res = int.tryParse(p0 ?? '');

return res != null && res > 0 && res <= 255 ? null : 'error_u8'.tr();
return res != null && res >= 0 && res <= 255 ? null : 'error_u8'.tr();
}

static String? onlyBigInt(final String? p0) {
Expand Down
2 changes: 1 addition & 1 deletion lib/features/app/presentation/global_states_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_platform_widgets/flutter_platform_widgets.dart';
import 'package:threedpass/core/polkawallet/bloc/app_service_cubit.dart';
import 'package:threedpass/features/asset_conversion/ui/pools_page/bloc/pools_cubit.dart';
import 'package:threedpass/features/asset_conversion/ui/pools_list/bloc/pools_cubit.dart';
import 'package:threedpass/features/hashes_list/bloc/hashes_list_bloc.dart';
import 'package:threedpass/features/poscan_assets/bloc/poscan_assets_cubit.dart';
import 'package:threedpass/features/poscan_objects_query/bloc/poscan_objects_cubit.dart';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,14 @@ let res = await pools.getPoolReserves(api, m1, m2);
return res;
""";

logger.v('CODE ' + getPoolReserversFunc);
// logger.v('CODE ' + getPoolReserversFunc);

final dynamic res = await basicJSCall(
getPoolReserversFunc,
polkawalletSdk.webView!.webInstance!.webViewController,
);

logger.v('call res: $res');
logger.v('poolReserve res: $res');
// [10,530,015,434, 9,509,085,784]

if (res == null) {
Expand All @@ -173,7 +173,7 @@ return res;
polkawalletSdk.webView!.webInstance!.webViewController,
);
// [[[Native, {Asset: 4}], {lpToken: 0}]]
print(res);
logger.v('PoolsBasic res: $res');

res as List<dynamic>;
final resT = res
Expand Down Expand Up @@ -349,6 +349,9 @@ return res;
polkawalletSdk.webView!.webInstance!.webViewController,
);
// [[[Native, {Asset: 4}], {lpToken: 0}]]
logger.v(
'callTokenServiceRoutine. tokenServiceCall: $tokenServiceCall res: $res',
);
if (res is String) {
return Either.right(res);
} else {
Expand Down
4 changes: 2 additions & 2 deletions lib/features/asset_conversion/di_asset_conversion.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ import 'package:threedpass/features/asset_conversion/domain/use_cases/remove_liq
import 'package:threedpass/features/asset_conversion/domain/use_cases/swap_assets.dart';
import 'package:threedpass/features/asset_conversion/ui/add_liquidity/bloc/add_liquidity_cubit.dart';
import 'package:threedpass/features/asset_conversion/ui/create_pool/bloc/create_pool_cubit.dart';
import 'package:threedpass/features/asset_conversion/ui/pools_page/bloc/pools_cubit.dart';
import 'package:threedpass/features/asset_conversion/ui/pools_list/bloc/pools_cubit.dart';
import 'package:threedpass/features/asset_conversion/ui/remove_liquidity/bloc/remove_liquidity_cubit.dart';
import 'package:threedpass/features/asset_conversion/ui/swap_page/bloc/swap_cubit.dart';
import 'package:threedpass/features/asset_conversion/ui/swap/bloc/swap_cubit.dart';
import 'package:threedpass/features/poscan_assets/bloc/poscan_assets_cubit.dart';
import 'package:threedpass/features/poscan_assets/data/poscan_assets_repository.dart';
import 'package:threedpass/features/wallet_screen/notifications_page/bloc/notifications_bloc.dart';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'package:threedpass/core/polkawallet/bloc/app_service_cubit.dart';
import 'package:threedpass/core/polkawallet/utils/extrinsic_status.dart';
import 'package:threedpass/features/asset_conversion/ui/pools_page/bloc/pools_cubit.dart';
import 'package:threedpass/features/asset_conversion/ui/pools_list/bloc/pools_cubit.dart';
import 'package:threedpass/features/wallet_screen/notifications_page/bloc/notifications_bloc.dart';
import 'package:threedpass/features/wallet_screen/notifications_page/utils/primitive_event_logs_handler.dart';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// ignore_for_file: avoid_dynamic_calls

import 'package:equatable/equatable.dart';

class BasicPoolEntity {
final PoolAssetField firstAsset;
final PoolAssetField secondAsset;
Expand All @@ -16,7 +18,7 @@ class BasicPoolEntity {
? const PoolAssetField(isNative: true, assetId: null)
: PoolAssetField(
isNative: false,
assetId: int.parse(json[0]['Asset'].toString()),
assetId: int.parse(json[0][0]['Asset'].toString()),
),
secondAsset = json[0][1] == 'Native'
? const PoolAssetField(isNative: true, assetId: null)
Expand All @@ -27,7 +29,7 @@ class BasicPoolEntity {
lpTokenId = int.parse(json[1]['lpToken'].toString());
}

class PoolAssetField {
class PoolAssetField extends Equatable {
final bool isNative;
final int? assetId;

Expand All @@ -39,4 +41,7 @@ class PoolAssetField {
const PoolAssetField.native()
: isNative = true,
assetId = null;

@override
List<Object?> get props => [isNative, assetId];
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'package:threedpass/core/polkawallet/bloc/app_service_cubit.dart';
import 'package:threedpass/core/polkawallet/utils/extrinsic_status.dart';
import 'package:threedpass/features/asset_conversion/ui/pools_page/bloc/pools_cubit.dart';
import 'package:threedpass/features/asset_conversion/ui/pools_list/bloc/pools_cubit.dart';
import 'package:threedpass/features/wallet_screen/notifications_page/bloc/notifications_bloc.dart';
import 'package:threedpass/features/wallet_screen/notifications_page/utils/primitive_event_logs_handler.dart';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'package:threedpass/core/polkawallet/bloc/app_service_cubit.dart';
import 'package:threedpass/core/polkawallet/utils/extrinsic_status.dart';
import 'package:threedpass/features/asset_conversion/ui/pools_page/bloc/pools_cubit.dart';
import 'package:threedpass/features/asset_conversion/ui/pools_list/bloc/pools_cubit.dart';
import 'package:threedpass/features/wallet_screen/notifications_page/bloc/notifications_bloc.dart';
import 'package:threedpass/features/wallet_screen/notifications_page/utils/primitive_event_logs_handler.dart';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'package:threedpass/core/polkawallet/bloc/app_service_cubit.dart';
import 'package:threedpass/core/polkawallet/utils/extrinsic_status.dart';
import 'package:threedpass/features/asset_conversion/ui/pools_page/bloc/pools_cubit.dart';
import 'package:threedpass/features/asset_conversion/ui/pools_list/bloc/pools_cubit.dart';
import 'package:threedpass/features/poscan_assets/bloc/poscan_assets_cubit.dart';
import 'package:threedpass/features/wallet_screen/notifications_page/bloc/notifications_bloc.dart';
import 'package:threedpass/features/wallet_screen/notifications_page/utils/primitive_event_logs_handler.dart';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import 'package:threedpass/core/polkawallet/utils/extrinsic_status.dart';
import 'package:threedpass/features/asset_conversion/data/asset_conversion_repository.dart';
import 'package:threedpass/features/asset_conversion/domain/entities/add_liquidity_global_handler.dart';
import 'package:threedpass/features/asset_conversion/domain/entities/basic_pool_entity.dart';
import 'package:threedpass/features/asset_conversion/ui/pools_page/bloc/pools_cubit.dart';
import 'package:threedpass/features/asset_conversion/ui/pools_list/bloc/pools_cubit.dart';
import 'package:threedpass/features/wallet_screen/notifications_page/bloc/notifications_bloc.dart';

class AddLiquidity extends UseCase<void, AddLiquidityParams> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import 'package:threedpass/core/polkawallet/utils/extrinsic_status.dart';
import 'package:threedpass/features/asset_conversion/data/asset_conversion_repository.dart';
import 'package:threedpass/features/asset_conversion/domain/entities/basic_pool_entity.dart';
import 'package:threedpass/features/asset_conversion/domain/entities/create_pool_global_handler.dart';
import 'package:threedpass/features/asset_conversion/ui/pools_page/bloc/pools_cubit.dart';
import 'package:threedpass/features/asset_conversion/ui/pools_list/bloc/pools_cubit.dart';
import 'package:threedpass/features/wallet_screen/notifications_page/bloc/notifications_bloc.dart';

class CreatePool extends UseCase<void, CreatePoolParams> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import 'package:threedpass/core/polkawallet/utils/extrinsic_status.dart';
import 'package:threedpass/features/asset_conversion/data/asset_conversion_repository.dart';
import 'package:threedpass/features/asset_conversion/domain/entities/basic_pool_entity.dart';
import 'package:threedpass/features/asset_conversion/domain/entities/remove_liquidity_global_handler.dart';
import 'package:threedpass/features/asset_conversion/ui/pools_page/bloc/pools_cubit.dart';
import 'package:threedpass/features/asset_conversion/ui/pools_list/bloc/pools_cubit.dart';
import 'package:threedpass/features/wallet_screen/notifications_page/bloc/notifications_bloc.dart';

class RemoveLiquidity extends UseCase<void, RemoveLiquidityParams> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import 'package:threedpass/features/asset_conversion/data/asset_conversion_repos
import 'package:threedpass/features/asset_conversion/domain/entities/basic_pool_entity.dart';
import 'package:threedpass/features/asset_conversion/domain/entities/swap_asset_global_handler.dart';
import 'package:threedpass/features/asset_conversion/domain/entities/swap_method.dart';
import 'package:threedpass/features/asset_conversion/ui/pools_page/bloc/pools_cubit.dart';
import 'package:threedpass/features/asset_conversion/ui/pools_list/bloc/pools_cubit.dart';
import 'package:threedpass/features/poscan_assets/bloc/poscan_assets_cubit.dart';
import 'package:threedpass/features/wallet_screen/notifications_page/bloc/notifications_bloc.dart';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ class AddLiquidityCubit extends Cubit<AddLiquidityState>
// final amount1Min = TextEditingController();
// final amount2Min = TextEditingController();

bool get areNullReserves => poolFullInfo.rawPoolReserve == null;

Rational get slippageRate {
final slippage = int.parse(slippageController.text);
return Rational.fromInt(100 - slippage, 100);
Expand All @@ -89,32 +91,36 @@ class AddLiquidityCubit extends Cubit<AddLiquidityState>
? nativeDecimals
: poscanAssetsCubit.state.metadata[asset1.assetId]!.idecimals;

Decimal get amount1Desired => Decimal.parse(amount1DesiredController.text)
.setDecimalsForUserInput(asset1Decimals);
Decimal? get amount1Desired => Decimal.tryParse(amount1DesiredController.text)
?.setDecimalsForUserInput(asset1Decimals);

int get asset2Decimals => asset2.isNative
? nativeDecimals
: poscanAssetsCubit.state.metadata[asset2.assetId]!.idecimals;

Decimal get amount2Desired => Decimal.parse(amount2DesiredController.text)
.setDecimalsForUserInput(asset2Decimals);
Decimal? get amount2Desired => Decimal.tryParse(amount2DesiredController.text)
?.setDecimalsForUserInput(asset2Decimals);

Rational get amount2Optimal => quote(
amount1Desired,
amount1Desired!,
poolFullInfo.rawPoolReserve!.balance1Decimal,
poolFullInfo.rawPoolReserve!.balance2Decimal,
);

Rational get amount1Optimal => quote(
amount2Desired,
amount2Desired!,
poolFullInfo.rawPoolReserve!.balance2Decimal,
poolFullInfo.rawPoolReserve!.balance1Decimal,
);

Rational get amount1MinAmount => amount1Desired.toRational() * slippageRate;
Rational get amount2MinAmount => amount2Desired.toRational() * slippageRate;
Rational? get amount1MinAmount => amount1Desired != null
? amount1Desired!.toRational() * slippageRate
: null;
Rational? get amount2MinAmount => amount2Desired != null
? amount2Desired!.toRational() * slippageRate
: null;

Either<Object, _AddLiquidityInfo> calcInfo() {
Either<Object, _AddLiquidityInfo> calcInfoForNotNullPool() {
try {
final slippage = int.tryParse(slippageController.text);
if (slippage == null) {
Expand All @@ -130,7 +136,7 @@ class AddLiquidityCubit extends Cubit<AddLiquidityState>
}

logger.v(
'amount1optimal ${amount1Optimal.toDouble()} 1min: ${amount1MinAmount.toDouble()} amount2optimal ${amount2Optimal.toDouble()} 2min:${amount2MinAmount.toDouble()}',
'amount1optimal ${amount1Optimal.toDouble()} 1min: ${amount1MinAmount!.toDouble()} amount2optimal ${amount2Optimal.toDouble()} 2min:${amount2MinAmount!.toDouble()}',
);

String? error;
Expand All @@ -142,20 +148,20 @@ class AddLiquidityCubit extends Cubit<AddLiquidityState>
? 'P3D'
: poscanAssetsCubit.state.metadata[asset2.assetId]!.symbol;

if (amount2Optimal < amount2MinAmount) {
if (amount2Optimal < amount2MinAmount!) {
error = 'ERROR: Asset $asset2Symbols deposit did not meet minimum';
}

if (amount1Optimal < amount1MinAmount) {
if (amount1Optimal < amount1MinAmount!) {
error = 'ERROR: Asset $asset1Symbols deposit did not meet minimum';
}

return Either.right(
_AddLiquidityInfo(
asset1Desired: amount1Desired,
asset2Desired: amount2Desired,
asset1Min: amount1MinAmount,
asset2Min: amount2MinAmount,
asset1Desired: amount1Desired!,
asset2Desired: amount2Desired!,
asset1Min: amount1MinAmount!,
asset2Min: amount2MinAmount!,
error: error,
),
);
Expand All @@ -167,8 +173,10 @@ class AddLiquidityCubit extends Cubit<AddLiquidityState>

void onFirstDesiredChanged() {
try {
amount2DesiredController.text =
amount2Optimal.setDecimalsNToFixedString(asset2Decimals);
if (!areNullReserves) {
amount2DesiredController.text =
amount2Optimal.setDecimalsNToFixedString(asset2Decimals);
}
setSlippageTolerance();
} on Object catch (e) {
logger.v(e);
Expand All @@ -177,8 +185,10 @@ class AddLiquidityCubit extends Cubit<AddLiquidityState>

void onSecondDesiredChanged() {
try {
amount1DesiredController.text =
amount1Optimal.setDecimalsNToFixedString(asset1Decimals);
if (!areNullReserves) {
amount1DesiredController.text =
amount1Optimal.setDecimalsNToFixedString(asset1Decimals);
}
setSlippageTolerance();
} on Object catch (e) {
logger.v(e);
Expand All @@ -205,17 +215,35 @@ class AddLiquidityCubit extends Cubit<AddLiquidityState>

emit(
AddLiquidityState(
asset1Min: amount1MinAmount.setDecimalsNToFixedString(asset1Decimals),
asset2Min: amount2MinAmount.setDecimalsNToFixedString(asset2Decimals),
asset1Min: amount1MinAmount?.setDecimalsNToFixedString(asset1Decimals),
asset2Min: amount2MinAmount?.setDecimalsNToFixedString(asset2Decimals),
),
);
}

Either<Failure, _AddLiquidityInfo> calcInfoForNullPool() {
try {
return Either.right(
_AddLiquidityInfo(
asset1Desired: amount1Desired!,
asset2Desired: amount2Desired!,
asset1Min: amount1MinAmount!,
asset2Min: amount2MinAmount!,
error: null,
),
);
} on Object catch (e) {
logger.e(e);
return Either.left(BadDataFailure(e.toString()));
}
}

@override
Future<Either<Failure, void>> callExtrinsic(
final BuildContext context,
) async {
final info = calcInfo();
final info =
areNullReserves ? calcInfoForNullPool() : calcInfoForNotNullPool();

if (info.value is _AddLiquidityInfo) {
final infoT = info.value! as _AddLiquidityInfo;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import 'package:threedpass/core/widgets/layout/list_view_separated.dart';
import 'package:threedpass/core/widgets/other/padding_16.dart';
import 'package:threedpass/core/widgets/paddings.dart';
import 'package:threedpass/core/widgets/text/d3p_body_medium_text.dart';
import 'package:threedpass/features/asset_conversion/ui/pools_page/bloc/pools_cubit.dart';
import 'package:threedpass/features/asset_conversion/ui/pools_page/presentation/widgets/pool_card.dart';
import 'package:threedpass/features/asset_conversion/ui/pools_list/bloc/pools_cubit.dart';
import 'package:threedpass/features/asset_conversion/ui/pools_list/presentation/widgets/pool_card.dart';
import 'package:threedpass/router/router.gr.dart';

@RoutePage()
Expand Down Expand Up @@ -60,7 +60,7 @@ class PoolsPage extends StatelessWidget {
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
D3pBodyMediumText('pools_text'),
const D3pBodyMediumText('pools_text'),
const H16(),
ListViewSeparated(
separator: const H16(),
Expand Down
Loading

0 comments on commit c663f2c

Please sign in to comment.