From 29c47a822696cb05e09b4d44b52dbf6d882365d4 Mon Sep 17 00:00:00 2001 From: Artem Date: Mon, 19 Dec 2022 17:56:00 +0000 Subject: [PATCH] Search for one country address by .1 domain name (#247) --- .env | 1 + .env.example | 2 ++ package.json | 3 ++- src/components/ui/Search.tsx | 29 ++++++++++++++++++++++++++++- src/config/index.ts | 4 +++- src/utils/oneCountry.ts | 12 ++++++++++++ 6 files changed, 48 insertions(+), 3 deletions(-) create mode 100644 src/utils/oneCountry.ts diff --git a/.env b/.env index 8714dd4..6f55d97 100644 --- a/.env +++ b/.env @@ -7,3 +7,4 @@ REACT_APP_EXPLORER_V1_API_URL=https://ctrver.t.hmny.io/ REACT_APP_INDEXER_IPFS_GATEWAY=https://ipfs.io/ipfs/ REACT_APP_PROD_ADDRESS=https://ws.explorer-v2-api.hmny.io REACT_APP_CONTRACT_SHARD=0 +REACT_APP_ONE_COUNTRY_CONTRACT_ADDRESS=0xaef596d26be185d1c25c0aadfab6ab054e7c011f diff --git a/.env.example b/.env.example index b9372f6..cb0f159 100644 --- a/.env.example +++ b/.env.example @@ -12,3 +12,5 @@ REACT_APP_INDEXER_IPFS_GATEWAY=https://ipfs.io/ipfs/ # backend websocket API url REACT_APP_PROD_ADDRESS=https://explorer-v2-api.hmny.io/ REACT_APP_CONTRACT_SHARD=0 +# 1.country contract address +REACT_APP_ONE_COUNTRY_CONTRACT_ADDRESS=0xaef596d26be185d1c25c0aadfab6ab054e7c011f diff --git a/package.json b/package.json index 27f346d..600f2b6 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ "grommet": "2.17.2", "grommet-icons": "^4.5.0", "json-bigint": "^1.0.0", + "one-country-sdk": "^0.2.0", "react": "^17.0.2", "react-chartjs-2": "^4.0.1", "react-dom": "^17.0.2", @@ -58,6 +59,7 @@ "@testing-library/user-event": "^12.1.10", "@types/big.js": "^6.0.2", "@types/jest": "^26.0.15", + "@types/json-bigint": "^1.0.1", "@types/node": "^12.0.0", "@types/react": "17.0.30", "@types/react-dom": "^17.0.0", @@ -66,7 +68,6 @@ "@types/react-virtualized-auto-sizer": "^1.0.0", "@types/react-window": "^1.8.3", "@types/styled-components": "^5.1.9", - "@types/json-bigint": "^1.0.1", "js-sha3": "^0.8.0", "node-fetch": "2", "prettier": "^2.2.1", diff --git a/src/components/ui/Search.tsx b/src/components/ui/Search.tsx index 7a23e23..af8163a 100644 --- a/src/components/ui/Search.tsx +++ b/src/components/ui/Search.tsx @@ -17,6 +17,8 @@ import { FixedSizeList as List } from "react-window"; import AutoSizer from "react-virtualized-auto-sizer"; import { Address } from "./Address"; import { config } from "../../config"; +import {getAddressByName} from "../../utils/oneCountry"; +import {toaster} from "../../App"; let timeoutID: any | null = null; @@ -94,6 +96,29 @@ export const SearchInput = () => { return; } + if(config.oneCountryContractAddress && v.endsWith('.1')) { + const [prefix] = v.split('.1') + if(prefix) { + try { + const address = await getAddressByName(prefix) + if(address) { + history.push(`/address/${address}`); + } else { + toaster.show({ + message: () => ( + + Address for "{v}" not found + + ), + time: 5000 + }) + } + } catch (e) { + console.log('Cannot get one country address', e) + } + } + } + if (v.length !== 66 && v.length !== 42) { return; } @@ -185,7 +210,9 @@ export const SearchInput = () => { } }; - exec(); + if(readySubmit) { + exec(); + } }, [readySubmit]); const Row = (options: { index: number; style: any }) => { diff --git a/src/config/index.ts b/src/config/index.ts index 74b3cf4..5e5deda 100644 --- a/src/config/index.ts +++ b/src/config/index.ts @@ -26,9 +26,11 @@ const shardUrls = availableShards .filter(url => url) const contractShardId = +(process.env.REACT_APP_CONTRACT_SHARD || '0') as ShardID +const oneCountryContractAddress = process.env.REACT_APP_ONE_COUNTRY_CONTRACT_ADDRESS || '' export const config = { availableShards, shardUrls, - contractShardId + contractShardId, + oneCountryContractAddress } diff --git a/src/utils/oneCountry.ts b/src/utils/oneCountry.ts new file mode 100644 index 0000000..745fbc3 --- /dev/null +++ b/src/utils/oneCountry.ts @@ -0,0 +1,12 @@ +import { OneCountry } from 'one-country-sdk' +import Web3 from "web3"; +import { config } from '../config' + +const { oneCountryContractAddress, shardUrls } = config +const provider = new Web3.providers.HttpProvider(shardUrls[0] || '') +const oneCountry = new OneCountry({ provider, contractAddress: oneCountryContractAddress }) + +export const getAddressByName = async (name: string) => { + const record = await oneCountry.getRecordByName(name) + return record ? record.renter : '' +}