Skip to content
This repository has been archived by the owner on Sep 17, 2024. It is now read-only.

Commit

Permalink
Merge pull request #162 from MXCzkEVM/dapp_hooks
Browse files Browse the repository at this point in the history
Miner auto claim transaction
  • Loading branch information
reasje authored Feb 27, 2024
2 parents d2352d3 + 13a6111 commit fe6bbc5
Show file tree
Hide file tree
Showing 6 changed files with 432 additions and 172 deletions.
280 changes: 175 additions & 105 deletions lib/features/dapps/subfeatures/open_dapp/open_dapp_presenter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:datadashwallet/features/dapps/subfeatures/open_dapp/domain/dapps
import 'package:datadashwallet/features/dapps/subfeatures/open_dapp/widgets/add_asset_dialog.dart';
import 'package:datadashwallet/features/dapps/subfeatures/open_dapp/widgets/swtich_network_dialog.dart';
import 'package:datadashwallet/features/dapps/subfeatures/open_dapp/widgets/typed_message_dialog.dart';
import 'package:datadashwallet/features/settings/subfeatures/dapp_hooks/dapp_hooks_page.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:mxc_logic/mxc_logic.dart';
Expand Down Expand Up @@ -35,6 +36,7 @@ class OpenDAppPresenter extends CompletePresenter<OpenDAppState> {
late final _customTokensUseCase = ref.read(customTokensUseCaseProvider);
late final _errorUseCase = ref.read(errorUseCaseProvider);
late final _launcherUseCase = ref.read(launcherUseCaseProvider);
late final _dAppHooksUseCase = ref.read(dAppHooksUseCaseProvider);

@override
void initState() {
Expand All @@ -52,6 +54,10 @@ class OpenDAppPresenter extends CompletePresenter<OpenDAppState> {
notify(() => state.network = value);
}
});

listen(_dAppHooksUseCase.dappHooksData, (value) {
notify(() => state.dappHooksData = value);
});
}

void onWebViewCreated(InAppWebViewController controller) async {
Expand Down Expand Up @@ -152,9 +158,6 @@ class OpenDAppPresenter extends CompletePresenter<OpenDAppState> {
}

void recordTransaction(TransactionModel tx) {
// final timeStamp = DateTime.now();
// const txStatus = TransactionStatus.pending;
// const txType = TransactionType.contractCall;
final currentNetwork = state.network!;
final chainId = currentNetwork.chainId;
final token = Token(
Expand All @@ -166,15 +169,6 @@ class OpenDAppPresenter extends CompletePresenter<OpenDAppState> {
);

tx = tx.copyWith(token: token);
// final tx = TransactionModel(
// hash: hash,
// timeStamp: timeStamp,
// status: txStatus,
// type: txType,
// value: null,
// token: token,
// action: null,
// );

_transactionHistoryUseCase.spyOnTransaction(
tx,
Expand Down Expand Up @@ -434,20 +428,13 @@ class OpenDAppPresenter extends CompletePresenter<OpenDAppState> {
}

void setChain(int? id) {
state.webviewController?.setChain(getConfig(), state.network!.chainId, id);
state.webviewController
?.setChain(getProviderConfig(), state.network!.chainId, id);
}

String getConfig() {
return """{
ethereum: {
chainId: ${state.network!.chainId},
rpcUrl: "${state.network!.web3RpcHttpUrl}",
address: "${state.account!.address}",
isDebug: true,
networkVersion: "${state.network!.chainId}",
isMetaMask: true
}
}""";
String getProviderConfig() {
return JSChannelScripts.walletProviderInfoScript(state.network!.chainId,
state.network!.web3RpcHttpUrl, state.account!.address);
}

void copy(List<dynamic> params) {
Expand All @@ -459,11 +446,10 @@ class OpenDAppPresenter extends CompletePresenter<OpenDAppState> {
}

void injectCopyHandling() {
state.webviewController!.evaluateJavascript(
source:
'javascript:navigator.clipboard.writeText = (msg) => { return window.flutter_inappwebview?.callHandler("axs-wallet-copy-clipboard", msg); }');
state.webviewController!
.evaluateJavascript(source: JSChannelScripts.clipboardHandlerScript);
state.webviewController!.addJavaScriptHandler(
handlerName: 'axs-wallet-copy-clipboard',
handlerName: JSChannelEvents.axsWalletCopyClipboard,
callback: (args) {
copy(args);
},
Expand Down Expand Up @@ -537,85 +523,11 @@ class OpenDAppPresenter extends CompletePresenter<OpenDAppState> {
final settleDuration = const Duration(milliseconds: 400);

injectScrollDetector() {
String jsCode = """
var pStart = { x: 0, y: 0 };
var pStop = { x: 0, y: 0 };
function swipeStart(e) {
if (typeof e["targetTouches"] !== "undefined") {
var touch = e.targetTouches[0];
pStart.x = touch.screenX;
pStart.y = touch.screenY;
} else {
pStart.x = e.screenX;
pStart.y = e.screenY;
}
}
function swipeEnd(e) {
if (typeof e["changedTouches"] !== "undefined") {
var touch = e.changedTouches[0];
pStop.x = touch.screenX;
pStop.y = touch.screenY;
} else {
pStop.x = e.screenX;
pStop.y = e.screenY;
}
swipeCheck();
}
function swipeCheck() {
var changeY = pStart.y - pStop.y;
var changeX = pStart.x - pStop.x;
if (isPullDown(changeY, changeX)) {
window.flutter_inappwebview?.callHandler("axs-scroll-detector", true);
} else if (isPullUp(changeY, changeX)) {
window.flutter_inappwebview?.callHandler("axs-scroll-detector", false);
}
}
function isPullDown(dY, dX) {
// methods of checking slope, length, direction of line created by swipe action
console.log(dY);
console.log(dX );
return (
dY < 0 &&
((Math.abs(dX) <= 100 && Math.abs(dY) >= 100 ) ||
(Math.abs(dX) / Math.abs(dY) <= 0.1 && dY >= 60))
);
}
function isPullUp(dY, dX) {
// Check if the gesture is a pull-up
console.log(dY);
console.log(dX);
return (
dY > 0 &&
((Math.abs(dX) <= 100 && Math.abs(dY) >= 100) ||
(Math.abs(dX) / Math.abs(dY) <= 0.1 && dY >= 60))
);
}
document.addEventListener(
"touchstart",
function (e) {
swipeStart(e);
},
false
);
document.addEventListener(
"touchend",
function (e) {
swipeEnd(e);
},
false
);
""";
state.webviewController!.evaluateJavascript(source: jsCode);
state.webviewController!
.evaluateJavascript(source: JSChannelScripts.overScrollScript);

state.webviewController!.addJavaScriptHandler(
handlerName: 'axs-scroll-detector',
handlerName: JSChannelEvents.axsWalletScrollDetector,
callback: (args) {
if (args[0] is bool) {
args[0] == true ? showPanel() : hidePanel();
Expand Down Expand Up @@ -681,4 +593,162 @@ class OpenDAppPresenter extends CompletePresenter<OpenDAppState> {
resetDoubleTapTime();
}
}

// call this on webview created
void injectMinerDappListeners() {
// Making It easy for accessing axs wallet
// use this way window.axs.callHandler
state.webviewController!.evaluateJavascript(
source: JSChannelScripts.axsWalletObjectInjectScript(
JSChannelConfig.axsWalletJSObjectName));

state.webviewController!.evaluateJavascript(
source: JSChannelScripts.axsWalletJSHandler(
JSChannelConfig.axsWalletJSObjectName,
JSChannelEvents.axsReadyEvent, {}));

state.webviewController!.addJavaScriptHandler(
handlerName: JSChannelEvents.changeCronTransitionEvent,
callback: (args) =>
jsChannelErrorHandler(args, handleChangeCronTransition));
state.webviewController!.addJavaScriptHandler(
handlerName: JSChannelEvents.changeCronTransitionStatusEvent,
callback: (args) =>
jsChannelErrorHandler(args, handleChangeCronTransitionStatusEvent));
state.webviewController!.addJavaScriptHandler(
handlerName: JSChannelEvents.getSystemInfoEvent,
callback: (args) => jsChannelErrorHandler(args, handleGetSystemInfo));
state.webviewController!.addJavaScriptHandler(
handlerName: JSChannelEvents.goToAdvancedSettingsEvent,
callback: (args) =>
jsChannelErrorHandler(args, handleGoToAdvancedSettings));
}

AXSJSChannelResponseModel jsChannelErrorHandler(List<dynamic> args,
Function(Map<String, dynamic>, AXSCronServices) callback) {
try {
final channelData = args[0];
final channelDataMap = json.decode(channelData) as Map<String, dynamic>;
final axsCronService =
AXSCronServicesExtension.getCronServiceFromJson(channelDataMap);
return callback(channelDataMap, axsCronService);
} catch (e) {
final response = AXSJSChannelResponseModel<MiningCronServiceDataModel>(
status: AXSJSChannelResponseStatus.failed,
data: null,
message: e.toString());
return response;
}
}

// Update via functions & get data via steam & send the data via event eaach time
// ready => updateSystemInfo (service statues, mining service status, time, selected miners, camera permission location permission)

AXSJSChannelResponseModel handleChangeCronTransition(
Map<String, dynamic> channelData, AXSCronServices axsCronService) {
final axsCronService =
AXSCronServicesExtension.getCronServiceFromJson(channelData);
if (axsCronService == AXSCronServices.miningAutoClaimCron) {
ChangeCronTransitionRequestModel;
final changeCronTransitionRequestModel =
ChangeCronTransitionRequestModel<MiningCronServiceDataModel>.fromMap(
channelData, MiningCronServiceDataModel.fromMap);

// Here i change the data thaqt won't effect the
final currentDappHooksData = state.dappHooksData;
final minersList = changeCronTransitionRequestModel.data?.minersList ??
currentDappHooksData.minerHooks.selectedMiners;
final time = changeCronTransitionRequestModel.data?.time ??
currentDappHooksData.minerHooks.time;
final updatedDappHooksData = currentDappHooksData.copyWith(
minerHooks: currentDappHooksData.minerHooks
.copyWith(selectedMiners: minersList, time: time));
_dAppHooksUseCase.updateItem(updatedDappHooksData);

final responseData = CronServiceDataModel.fromDAppHooksData(
axsCronService,
updatedDappHooksData,
MiningCronServiceDataModel.fromDAppHooksData(updatedDappHooksData));

final response = AXSJSChannelResponseModel<MiningCronServiceDataModel>(
status: AXSJSChannelResponseStatus.success,
data: responseData,
message: null);
return response;
} else {
throw 'Unknown service';
}
}

AXSJSChannelResponseModel handleChangeCronTransitionStatusEvent(
Map<String, dynamic> channelData, AXSCronServices axsCronService) {
if (axsCronService == AXSCronServices.miningAutoClaimCron) {
final status = channelData['status'];
final dappHooksData = state.dappHooksData;
final delay = dappHooksData.duration;

DAppHooksModel updatedDappHooksData;
if (status) {
updatedDappHooksData = dappHooksData.copyWith(enabled: status);
_dAppHooksUseCase.startDAppHooksService(delay);
}

updatedDappHooksData = dappHooksData.copyWith(
minerHooks: dappHooksData.minerHooks.copyWith(enabled: status));
_dAppHooksUseCase.updateItem(updatedDappHooksData);

final responseData = CronServiceDataModel.fromDAppHooksData(
axsCronService,
updatedDappHooksData,
MiningCronServiceDataModel.fromDAppHooksData(updatedDappHooksData));
final response = AXSJSChannelResponseModel<MiningCronServiceDataModel>(
status: AXSJSChannelResponseStatus.success,
message: null,
data: responseData);
return response;
} else {
throw 'Unknown cron service';
}
}

void addMiningAutoClaim() {
// if cron_claim
}
void removeMiningAutoClaim() {
// if cron_claim
}

void handleGoToAdvancedSettingsEvent() {}
AXSJSChannelResponseModel handleGetSystemInfo(
Map<String, dynamic> channelData, AXSCronServices axsCronService) {
if (axsCronService == AXSCronServices.miningAutoClaimCron) {
final dappHooksData = state.dappHooksData;

final responseData = CronServiceDataModel.fromDAppHooksData(
axsCronService,
dappHooksData,
MiningCronServiceDataModel.fromDAppHooksData(dappHooksData));
final response = AXSJSChannelResponseModel<MiningCronServiceDataModel>(
status: AXSJSChannelResponseStatus.success,
message: null,
data: responseData);
return response;
} else {
throw 'Unknown cron service';
}
}

AXSJSChannelResponseModel handleGoToAdvancedSettings(
Map<String, dynamic> channelData, AXSCronServices axsCronService) {
goToAdvancedSettings();
final response = AXSJSChannelResponseModel<MiningCronServiceDataModel>(
status: AXSJSChannelResponseStatus.success, message: null, data: null);
return response;
}

void goToAdvancedSettings() {
navigator!.push(route(
const DAppHooksPage(),
));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class OpenDAppState with EquatableMixin {
int panelHideTimer = 5;
Uri? currentUrl;
bool isSecure = false;
late DAppHooksModel dappHooksData;

@override
List<Object?> get props => [
Expand All @@ -21,6 +22,7 @@ class OpenDAppState with EquatableMixin {
network,
animationController,
currentUrl,
isSecure
isSecure,
dappHooksData
];
}
Loading

0 comments on commit fe6bbc5

Please sign in to comment.