Skip to content

Commit

Permalink
React to Tor/Ipfs overwritten events in my domains list
Browse files Browse the repository at this point in the history
  • Loading branch information
simonesestito committed Jan 20, 2024
1 parent b10771f commit d18eb59
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 20 deletions.
15 changes: 15 additions & 0 deletions lib/bloc/domains/domains_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class DomainsBloc extends Bloc<DomainsEvent, DomainsState> {
final SellingRepository sellingRepository;
final GlobalErrorsSink globalErrorsSink;
final GlobalEventsSink globalEventsSink;
String? currentUser;

DomainsBloc({
required this.domainsRepository,
Expand All @@ -41,8 +42,10 @@ class DomainsBloc extends Bloc<DomainsEvent, DomainsState> {
authStateChanges.listen((state) {
debugPrint('DomainsBloc: authStateChanges.listen: $state');
if (state is AuthLoggedIn) {
currentUser = state.account.address;
add(const LoadDomainsEvent());
} else {
currentUser = null;
add(const _UpdateDomainsEvent(domains: null));
}
});
Expand All @@ -61,6 +64,15 @@ class DomainsBloc extends Bloc<DomainsEvent, DomainsState> {
debugPrint('DomainsBloc: globalEventsSink.domainListings.listen: $event');
add(DomainListedForSaleEvent(event.domainName, event.price));
});

// Listen to the global events of domain edits
// and react to my domains only
globalEventsSink.domainEdits.map(_removeGethSuffix).listen((event) {
if (event.owner == currentUser) {
debugPrint('DomainsBloc: globalEventsSink.domainEdits.listen: $event');
add(const LoadDomainsEvent());
}
});
}

FutureOr<void> _onLoadDomainsEvent(
Expand Down Expand Up @@ -291,6 +303,9 @@ class DomainsBloc extends Bloc<DomainsEvent, DomainsState> {
Web3DomainTransfer _ => event.copyWith(
domainName: removeDomainName(event.domainName),
),
Web3DomainEdited _ => event.copyWith(
domainName: removeDomainName(event.domainName),
),
_ => throw UnimplementedError('[DomainsBloc] Unknown event type: $event'),
} as T;
}
Expand Down
6 changes: 6 additions & 0 deletions lib/bloc/global_errors/banner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,12 @@ class _GlobalErrorsBannerState extends State<GlobalErrorsBanner> {
return const SizedBox.shrink();
}

if (notice is Web3DomainEdited && notice.owner != authenticatedAccount) {
// Do not show banners for domain edited events,
// if the domain is not owned by the user
return const SizedBox.shrink();
}

return BannerCard(
color: color,
icon: icon,
Expand Down
3 changes: 3 additions & 0 deletions lib/bloc/global_errors/global_events.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ class GlobalEventsSink {
Stream<Web3DomainSold> get domainPurchases =>
_getWeb3Events<Web3DomainSold>();

Stream<Web3DomainEdited> get domainEdits =>
_getWeb3Events<Web3DomainEdited>();

void addWeb3Event(Web3Event event) {
debugPrint('[Web3Event] $event');
_web3ErrorsStreamController.add(event);
Expand Down
29 changes: 29 additions & 0 deletions lib/contracts/events.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ sealed class Web3Event extends Web3Notice {
'domainListingForSale' => Web3DomainListingForSale.fromJson(message),
'domainSold' => Web3DomainSold.fromJson(message),
'royaltiesPaid' => Web3RoyaltiesPaid.fromJson(message),
'domainPointerOverwritten' => Web3DomainEdited.fromJson(message),
_ => throw Exception('Unknown Web3Event tag: $tag'),
};
}
Expand Down Expand Up @@ -212,3 +213,31 @@ class Web3RoyaltiesPaid extends Web3DomainEvent {
);
}
}

class Web3DomainEdited extends Web3DomainEvent {
final String owner;

const Web3DomainEdited({
required this.owner,
required String domainName,
}) : super(domainName, 'Domain $domainName edited');

factory Web3DomainEdited.fromJson(String json) {
const domainEncoder = DomainEncoder(
domainSuffix: DomainInputValidator.domainSuffix,
);
final data = jsonDecode(json);
final domainBytes = receiveUint8ListFromHex(data['domainBytes']);

return Web3DomainEdited(
owner: data['owner'],
domainName:
domainEncoder.decode(domainBytes) + DomainInputValidator.domainSuffix,
);
}

Web3DomainEdited copyWith({String? domainName}) => Web3DomainEdited(
owner: owner,
domainName: domainName ?? this.domainName,
);
}
46 changes: 26 additions & 20 deletions web/js/geth_domains.js
Original file line number Diff line number Diff line change
Expand Up @@ -1279,27 +1279,22 @@ let domains_received_event_emitter = null;
let domains_listing_event_emitter = null;
let domains_selling_event_emitter = null;
let domains_royalties_event_emitter = null;
let domains_ipfs_overwritten_event_emitter = null;
let domains_tor_overwritten_event_emitter = null;
async function _initDomainsEvents() {
if (domains_sent_event_emitter !== null) {
domains_sent_event_emitter.removeAllListeners('data');
domains_sent_event_emitter.removeAllListeners('error');
}
if (domains_received_event_emitter !== null) {
domains_received_event_emitter.removeAllListeners('data');
domains_received_event_emitter.removeAllListeners('error');
}
if (domains_listing_event_emitter !== null) {
domains_listing_event_emitter.removeAllListeners('data');
domains_listing_event_emitter.removeAllListeners('error');
}
if (domains_selling_event_emitter !== null) {
domains_selling_event_emitter.removeAllListeners('data');
domains_selling_event_emitter.removeAllListeners('error');
}
if (domains_royalties_event_emitter !== null) {
domains_royalties_event_emitter.removeAllListeners('data');
domains_royalties_event_emitter.removeAllListeners('error');
}
// Remove all the previous listeners
[
domains_sent_event_emitter,
domains_received_event_emitter,
domains_listing_event_emitter,
domains_selling_event_emitter,
domains_royalties_event_emitter,
domains_ipfs_overwritten_event_emitter,
domains_tor_overwritten_event_emitter,
].filter((x) => x !== null).forEach((x) => {
x.removeAllListeners('data');
x.removeAllListeners('error');
});

const [contract, user] = await _initializeGethDomainsContract();
if (user === null) {
Expand Down Expand Up @@ -1358,5 +1353,16 @@ async function _initDomainsEvents() {
royaltiesAmount: event.returnValues.royaltiesAmount,
}));
}).on('error', _onError);

// Subscribe to IpfsOverwritten and TorOverwritten events
const _onOverwrittenEvent = function(event) {
console.log(event);
web3EventsSink('domainPointerOverwritten', JSON.stringify({
domainBytes: event.returnValues.domain,
owner: event.returnValues.owner,
}));
};
domains_ipfs_overwritten_event_emitter = contract.events.IpfsOverwritten().on('data', _onOverwrittenEvent).on('error', _onError);
domains_tor_overwritten_event_emitter = contract.events.TorOverwritten().on('data', _onOverwrittenEvent).on('error', _onError);
}
_initDomainsEvents().catch(console.error);

0 comments on commit d18eb59

Please sign in to comment.