Skip to content

Commit

Permalink
[WIP] Implement domains search
Browse files Browse the repository at this point in the history
  • Loading branch information
simonesestito committed Jan 11, 2024
1 parent 6298835 commit ac66cf6
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 49 deletions.
11 changes: 10 additions & 1 deletion lib/bloc/domain_search/domain_search_bloc.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'dart:async';

import 'package:flutter/foundation.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:gethdomains/bloc/global_errors/global_events.dart';
import 'package:gethdomains/model/domain.dart';
Expand All @@ -21,6 +22,11 @@ class DomainSearchBloc extends Bloc<DomainSearchEvent, DomainSearchState> {

// Listen for events in domains
globalEventsSink.domainTransfers.listen((event) {
debugPrint('[DomainSearchBloc] transfer event received: $event');
debugPrint(
'[DomainSearchBloc] last searched domain: ${_getLastSearchedDomain()}');
debugPrint('[DomainSearchBloc] event domain: ${event.domainName}');

if (_getLastSearchedDomain() == event.domainName) {
// Reload the domain
search(event.domainName);
Expand All @@ -46,7 +52,10 @@ class DomainSearchBloc extends Bloc<DomainSearchEvent, DomainSearchState> {
} else {
emit(DomainSearchStateNoResults(event.domainName));
}
} catch (e) {
} catch (e, stackTrace) {
debugPrint(e.toString());
debugPrintStack(stackTrace: stackTrace);

emit(DomainSearchStateError(
errorMessage: e.toString(),
domainName: event.domainName,
Expand Down
10 changes: 6 additions & 4 deletions lib/contracts/geth_domains.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,17 @@ class GethDomainsContract {
),
);

Future<Map<String, dynamic>?> searchDomain(String domain) async {
final jsonString = await metamaskPromise<String?>(_searchDomain(domain));
Future<Map<String, dynamic>?> searchDomain(Uint8List domain) async {
final jsonString = await metamaskPromise<String?>(_searchDomain(
sendUint8List(domain),
));
if (jsonString == null) {
return null;
}

final jsonData = jsonDecode(jsonString);
jsonData['dominioTorOrIpfs'] =
receiveUint8ListFromHex(jsonData['dominioTorOrIpfs']);
jsonData['pointedAddress'] =
receiveUint8ListFromHex(jsonData['pointedAddress']);
return jsonData;
}
}
4 changes: 2 additions & 2 deletions lib/model/domain.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Domain with _$Domain {
) {
final isTor = data['isTor'] == true;
final decodedDomain = (isTor ? torAddressEncoder : ipfsCidEncoder)
.decode(data['dominioTorOrIpfs']);
.decode(data['pointedAddress']);
return Domain(
domainName: originalDomain,
realAddress: decodedDomain,
Expand All @@ -35,7 +35,7 @@ class Domain with _$Domain {
);
}

bool get isForSale => price > 0;
bool get isForSale => price.compareTo(BigInt.zero) > 0;
}

enum DomainType {
Expand Down
53 changes: 28 additions & 25 deletions lib/repository/domain_repository.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:typed_data';

import 'package:data_encoder/data_encoder.dart';
import 'package:gethdomains/contracts/geth_domains.dart';
import 'package:gethdomains/input/validators/domain_input.dart';
Expand All @@ -17,7 +19,9 @@ class DomainRepository {
});

Future<Domain?> searchDomain(String domainName) async {
final jsonResult = await contract.searchDomain(domainName);
final jsonResult = await contract.searchDomain(
_encodeDomain(domainName),
);
if (jsonResult == null) {
return null;
}
Expand Down Expand Up @@ -46,47 +50,46 @@ class DomainRepository {
return List.empty();
}

Future<BigInt> predictDomainPurchaseFees(String domainName,
String pointedAddress,
DomainType domainType,) {
if (domainName.endsWith(DomainInputValidator.domainSuffix)) {
domainName = domainName.substring(
0,
domainName.length - DomainInputValidator.domainSuffix.length,
);
}

final encodedDomain = domainEncoder.encode(domainName);
Future<BigInt> predictDomainPurchaseFees(
String domainName,
String pointedAddress,
DomainType domainType,
) {
final encodedPointer = switch (domainType) {
DomainType.ipfs => ipfsCidEncoder.encode(pointedAddress),
DomainType.tor => torAddressEncoder.encode(pointedAddress),
};
return contract.purchaseNewDomainFees(
encodedDomain,
_encodeDomain(domainName),
encodedPointer,
domainType,
);
}

Future<String> purchaseNewDomain(String domainName,
String pointedAddress,
DomainType domainType,) async {
if (domainName.endsWith(DomainInputValidator.domainSuffix)) {
domainName = domainName.substring(
0,
domainName.length - DomainInputValidator.domainSuffix.length,
);
}

final encodedDomain = domainEncoder.encode(domainName);
Future<String> purchaseNewDomain(
String domainName,
String pointedAddress,
DomainType domainType,
) async {
final encodedPointer = switch (domainType) {
DomainType.ipfs => ipfsCidEncoder.encode(pointedAddress),
DomainType.tor => torAddressEncoder.encode(pointedAddress),
};
return contract.purchaseNewDomain(
encodedDomain,
_encodeDomain(domainName),
encodedPointer,
domainType,
);
}

Uint8List _encodeDomain(String domain) {
if (domain.endsWith(DomainInputValidator.domainSuffix)) {
domain = domain.substring(
0,
domain.length - DomainInputValidator.domainSuffix.length,
);
}

return domainEncoder.encode(domain);
}
}
53 changes: 36 additions & 17 deletions web/js/geth_domains.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const geth_domains_address = "0x0806Dfae0DcA325998a8B290955429fDb8491c78";
const geth_domains_address = "0xA4eC183d35DC830e9280796d536049597168DB0a";
const geth_domains_abi = [
{
"inputs": [
Expand Down Expand Up @@ -653,6 +653,25 @@ const geth_domains_abi = [
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "owner",
"type": "address"
}
],
"name": "balanceOf",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
Expand All @@ -661,7 +680,7 @@ const geth_domains_abi = [
"type": "bytes"
}
],
"name": "_domains",
"name": "domains",
"outputs": [
{
"internalType": "uint32",
Expand Down Expand Up @@ -690,17 +709,17 @@ const geth_domains_abi = [
{
"inputs": [
{
"internalType": "address",
"name": "owner",
"type": "address"
"internalType": "uint256",
"name": "tokenId",
"type": "uint256"
}
],
"name": "balanceOf",
"name": "getApproved",
"outputs": [
{
"internalType": "uint256",
"internalType": "address",
"name": "",
"type": "uint256"
"type": "address"
}
],
"stateMutability": "view",
Expand All @@ -709,20 +728,20 @@ const geth_domains_abi = [
{
"inputs": [
{
"internalType": "uint256",
"name": "tokenId",
"type": "uint256"
"internalType": "bytes",
"name": "domain",
"type": "bytes"
}
],
"name": "getApproved",
"name": "getId",
"outputs": [
{
"internalType": "address",
"name": "",
"type": "address"
"internalType": "uint256",
"name": "id",
"type": "uint256"
}
],
"stateMutability": "view",
"stateMutability": "pure",
"type": "function"
},
{
Expand Down Expand Up @@ -983,7 +1002,7 @@ async function domains_searchDomain(domain) {
domain = _receiveBytes(domain);
const [contract, user] = await _initializeGethDomainsContract();
const result = await contract.methods.domains(domain).call({from: user});
if (result.dominoTorOrIpfs == null) {
if (result.dominioTorOrIpfs == null) {
// Result not found.
return null;
}
Expand Down

0 comments on commit ac66cf6

Please sign in to comment.