Skip to content

Commit

Permalink
[substrate_api/encointer_api] getNextMeetupTime uses now dart only
Browse files Browse the repository at this point in the history
  • Loading branch information
clangenb committed Nov 23, 2023
1 parent 8ee1f4c commit e815f75
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 34 deletions.
17 changes: 0 additions & 17 deletions app/js_service_encointer/src/service/encointer.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { unsubscribe } from '../utils/unsubscribe.js';
import { communityIdentifierToString } from '@encointer/util';
import {
getMeetupIndex as _getMeetupIndex,
getNextMeetupTime as _getNextMeetupTime,
getDemurrage as _getDemurrage, submitAndWatchTx,
} from '@encointer/node-api';
import { getFinalizedHeader } from './chain.js';
Expand Down Expand Up @@ -151,21 +150,6 @@ export async function getMeetupIndex (cid, cIndex, address) {
return _getMeetupIndex(api, cidT, cIndexT, address);
}

/**
* Gets the meetup time for a location.
*
* @param location Meetup location with fields as numbers, e.g. 35.153215322
* @returns {Promise<Moment>}
*/
export async function getNextMeetupTime (location) {
const locT = api.createType('Location', {
lat: stringToDegree(location.lat),
lon: stringToDegree(location.lon),
});

return _getNextMeetupTime(api, locT);
}

/**
* Checks if the ceremony rewards has been issued.
*
Expand Down Expand Up @@ -313,7 +297,6 @@ export default {
subscribeCommunityIdentifiers,
subscribeBusinessRegistry,
getProofOfAttendance,
getNextMeetupTime,
getMeetupIndex,
hasPendingIssuance,
getBalance,
Expand Down
36 changes: 23 additions & 13 deletions app/lib/service/substrate_api/encointer/encointer_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import 'package:encointer_wallet/service/substrate_api/encointer/encointer_dart_
import 'package:encointer_wallet/service/substrate_api/encointer/no_tee_api.dart';
import 'package:encointer_wallet/service/substrate_api/encointer/tee_proxy_api.dart';
import 'package:encointer_wallet/store/app.dart';
import 'package:ew_encointer_utils/ew_encointer_utils.dart' as ew_utils;
import 'package:ew_http/ew_http.dart';
import 'package:ew_keyring/ew_keyring.dart';
import 'package:ew_polkadart/ew_polkadart.dart';
Expand Down Expand Up @@ -255,32 +256,41 @@ class EncointerApi {
store.encointer.setCommunities(cidNames);
}

/// Queries the Scheduler pallet: encointerScheduler./-currentPhase(), -phaseDurations(phase), -nextPhaseTimestamp().
///
/// Fixme: Sometimes the PhaseAwareBox takes ages to update. This might be due to multiple network requests on JS side.
/// We could fetch the phaseDurations at application startup, cache them and supply them in the call here.
/// Queries the Scheduler pallet
Future<DateTime?> getMeetupTime() async {
Log.d('api: getMeetupTime', 'EncointerApi');

// I we are not assigned to a meetup, we just get any location to get an estimate of the chosen community's meetup
// times.
// I we are not assigned to a meetup, we just get any location to get
// an estimate of the chosen community's meetup times.
final locationIndex = store.encointer.communityAccount?.meetup?.locationIndex;

final mLocation = locationIndex != null && store.encointer.community?.meetupLocations != null
? store.encointer.community?.meetupLocations![locationIndex]
: store.encointer.community?.meetupLocations?.first;

final attestingStart = store.encointer.attestingPhaseStart;

if (mLocation == null) {
Log.d("No meetup locations found, can't get meetup time.", 'EncointerApi');
Log.p("No meetup locations found, can't get meetup time.", 'EncointerApi');
return Future.value();
}

final time =
await jsApi.evalJavascript<String>('encointer.getNextMeetupTime(${jsonEncode(mLocation)})').then(int.parse);
if (attestingStart == null) {
Log.p("attestingStart == 0, can't get meetup time.", 'EncointerApi');
return Future.value();
}

final offset = await encointerKusama.query.encointerCeremonies.meetupTimeOffset();

final meetupTime = ew_utils.meetupTime(
double.parse(mLocation.lon),
attestingStart,
offset,
ew_utils.momentsPerDay,
);

Log.d('api: Next Meetup Time: $time', 'EncointerApi');
store.encointer.community!.setMeetupTime(time);
return DateTime.fromMillisecondsSinceEpoch(time);
Log.d('api: Next Meetup Time: $meetupTime', 'EncointerApi');
store.encointer.community!.setMeetupTime(meetupTime);
return DateTime.fromMillisecondsSinceEpoch(meetupTime);
}

Future<void> getMeetupTimeOverride({bool devMode = false}) async {
Expand Down
7 changes: 7 additions & 0 deletions app/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,13 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.0.5"
ew_encointer_utils:
dependency: "direct main"
description:
path: "../packages/ew_encointer_utils"
relative: true
source: path
version: "0.1.0+1"
ew_http:
dependency: "direct main"
description:
Expand Down
2 changes: 2 additions & 0 deletions app/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ dependencies:
printing: ^5.11.0
pdf: ^3.10.4
connectivity_plus: ^5.0.1
ew_encointer_utils:
path: ../packages/ew_encointer_utils/
ew_storage:
path: ../packages/ew_storage/
ew_http:
Expand Down
4 changes: 3 additions & 1 deletion app/pubspec_overrides.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# melos_managed_dependency_overrides: ew_http,ew_storage,ew_polkadart,ew_substrate_fixed,ew_keyring,polkadart,polkadart_scale_codec,substrate_metadata
# melos_managed_dependency_overrides: ew_http,ew_storage,ew_polkadart,ew_substrate_fixed,ew_keyring,polkadart,polkadart_scale_codec,substrate_metadata,ew_encointer_utils
dependency_overrides:
ew_encointer_utils:
path: ../packages/ew_encointer_utils
ew_http:
path: ../packages/ew_http
ew_keyring:
Expand Down
4 changes: 2 additions & 2 deletions packages/ew_encointer_utils/lib/src/assigment.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ const momentsPerDay = 86400000;

const momentsPerDegree = momentsPerDay / 360;

int meetupTime(double longitude, double attestingStart, double meetupTimeOffset, double oneDay) {
final timePerDegree = oneDay / 360;
int meetupTime(double longitude, int attestingStart, int meetupTimeOffset, int oneDay) {
final timePerDegree = oneDay.toDouble() / 360;
// The meetups start at high sun at 180 degrees and during one day the meetup locations travel
// along the globe until the very last meetup happens at high sun at -180 degrees.
// So we scale the range 180...-180 to 0...360.
Expand Down
2 changes: 1 addition & 1 deletion packages/ew_encointer_utils/test/assignment_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class MeetupTimeTestCase {
final double longitude;

/// Global `MeetupTimeOffset` of the encointer chain.
final double offset;
final int offset;

/// Expected result in ms units.
final int expected;
Expand Down

0 comments on commit e815f75

Please sign in to comment.