Skip to content

Commit

Permalink
refactor(web3Provider): add fallbackProvider && ens cache
Browse files Browse the repository at this point in the history
  • Loading branch information
kenshyx committed Oct 19, 2022
1 parent 6ec751b commit e679cc0
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 67 deletions.
2 changes: 1 addition & 1 deletion sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
"@openzeppelin/hardhat-upgrades": "1.21.0",
"chai": "4.3.6",
"dotenv-webpack": "8.0.1",
"hardhat": "2.11.2",
"hardhat": "2.12.0",
"tslib": "2.4.0"
},
"scripts": {
Expand Down
15 changes: 12 additions & 3 deletions sdk/src/common/web3.connector.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { inject, injectable } from 'inversify';
import { ethers } from 'ethers';
import {
TYPES,
EthProviders,
ILogger,
INJECTED_PROVIDERS,
IWeb3Connector,
PROVIDER_ERROR_CODES,
TYPES,
WEB3_EVENTS,
EthProviders,
IWeb3Connector,
} from '@akashaorg/typings/sdk';
import detectEthereumProvider from '@metamask/detect-provider';
import WalletConnectProvider from '@walletconnect/web3-provider';
Expand Down Expand Up @@ -89,6 +89,15 @@ class Web3Connector
get provider() {
if (this.#web3Instance) {
return this.#web3Instance;
} else {
this.#web3Instance = new ethers.providers.InfuraProvider(
{
name: this.network,
chainId: this.networkId[this.network],
},
process.env.INFURA_ID,
);
return this.#web3Instance;
}
throw new Error('Must connect first to a provider!');
}
Expand Down
38 changes: 34 additions & 4 deletions sdk/src/registry/ens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import EventBus from '../common/event-bus';
import { concatAll, map, tap } from 'rxjs/operators';
import IpfsConnector from '../common/ipfs.connector';
import { IsUserNameAvailable } from '../profiles/profile.graphql';
import Stash from '../stash/index';

export const isEncodedLabelHash = hash => {
return hash.startsWith('[') && hash.endsWith(']') && hash.length === 66;
Expand All @@ -40,6 +41,7 @@ class AWF_ENS implements AWF_IENS {
private _auth: AWF_Auth;
private _settings: Settings;
private _globalChannel: EventBus;
private _stash: Stash;
private _chainChecked = false;
//private _AkashaRegistrarInstance;
private _ReverseRegistrarInstance;
Expand All @@ -59,6 +61,7 @@ class AWF_ENS implements AWF_IENS {
@inject(TYPES.EventBus) globalChannel: EventBus,
@inject(TYPES.Web3) web3: Web3Connector,
@inject(TYPES.IPFS) ipfs: IpfsConnector,
@inject(TYPES.Stash) stash: Stash,
) {
this._log = log.create('AWF_ENS');
this._gql = gql;
Expand All @@ -67,6 +70,7 @@ class AWF_ENS implements AWF_IENS {
this._globalChannel = globalChannel;
this._web3 = web3;
this._ipfs = ipfs;
this._stash = stash;
}

registerName(name: string) {
Expand Down Expand Up @@ -164,17 +168,43 @@ class AWF_ENS implements AWF_IENS {
);
}

/**
* Returns ENS name associated with the ethereum address
*/
async resolveAddress(ethAddress: string) {
if (!this._chainChecked) {
await this.setupContracts();
}
const address = await this._web3.provider.lookupAddress(ethAddress);
return createFormattedValue(address);
const uiStash = this._stash.getUiStash();
const key = `ens:resolveAddress:${ethAddress}`;
let ensName;
if (uiStash.has(key)) {
ensName = uiStash.get(key);
} else {
ensName = await this._web3.provider.lookupAddress(ethAddress);
uiStash.set(key, ensName);
}

return createFormattedValue(ensName);
}

/**
* Returns eth address associated with the ens name
*/
async resolveName(name: string) {
const result = await this._web3.provider.resolveName(name);
return createFormattedValue(result);
if (!this._chainChecked) {
await this.setupContracts();
}
const uiStash = this._stash.getUiStash();
const key = `ens:resolveName:${name}`;
let ensAddress;
if (uiStash.has(key)) {
ensAddress = uiStash.get(key);
} else {
ensAddress = await this._web3.provider.resolveName(name);
uiStash.set(key, ensAddress);
}
return createFormattedValue(ensAddress);
}

public async setupContracts() {
Expand Down
118 changes: 59 additions & 59 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4447,71 +4447,71 @@
mcl-wasm "^0.7.1"
rustbn.js "~0.2.0"

"@nomicfoundation/solidity-analyzer-darwin-arm64@0.0.3":
version "0.0.3"
resolved "https://registry.yarnpkg.com/@nomicfoundation/solidity-analyzer-darwin-arm64/-/solidity-analyzer-darwin-arm64-0.0.3.tgz#1d49e4ac028831a3011a9f3dca60bd1963185342"
integrity sha512-W+bIiNiZmiy+MTYFZn3nwjyPUO6wfWJ0lnXx2zZrM8xExKObMrhCh50yy8pQING24mHfpPFCn89wEB/iG7vZDw==
"@nomicfoundation/solidity-analyzer-darwin-arm64@0.1.0":
version "0.1.0"
resolved "https://registry.yarnpkg.com/@nomicfoundation/solidity-analyzer-darwin-arm64/-/solidity-analyzer-darwin-arm64-0.1.0.tgz#83a7367342bd053a76d04bbcf4f373fef07cf760"
integrity sha512-vEF3yKuuzfMHsZecHQcnkUrqm8mnTWfJeEVFHpg+cO+le96xQA4lAJYdUan8pXZohQxv1fSReQsn4QGNuBNuCw==

"@nomicfoundation/solidity-analyzer-darwin-x64@0.0.3":
version "0.0.3"
resolved "https://registry.yarnpkg.com/@nomicfoundation/solidity-analyzer-darwin-x64/-/solidity-analyzer-darwin-x64-0.0.3.tgz#c0fccecc5506ff5466225e41e65691abafef3dbe"
integrity sha512-HuJd1K+2MgmFIYEpx46uzwEFjvzKAI765mmoMxy4K+Aqq1p+q7hHRlsFU2kx3NB8InwotkkIq3A5FLU1sI1WDw==
"@nomicfoundation/solidity-analyzer-darwin-x64@0.1.0":
version "0.1.0"
resolved "https://registry.yarnpkg.com/@nomicfoundation/solidity-analyzer-darwin-x64/-/solidity-analyzer-darwin-x64-0.1.0.tgz#1225f7da647ae1ad25a87125664704ecc0af6ccc"
integrity sha512-dlHeIg0pTL4dB1l9JDwbi/JG6dHQaU1xpDK+ugYO8eJ1kxx9Dh2isEUtA4d02cQAl22cjOHTvifAk96A+ItEHA==

"@nomicfoundation/solidity-analyzer-freebsd-x64@0.0.3":
version "0.0.3"
resolved "https://registry.yarnpkg.com/@nomicfoundation/solidity-analyzer-freebsd-x64/-/solidity-analyzer-freebsd-x64-0.0.3.tgz#8261d033f7172b347490cd005931ef8168ab4d73"
integrity sha512-2cR8JNy23jZaO/vZrsAnWCsO73asU7ylrHIe0fEsXbZYqBP9sMr+/+xP3CELDHJxUbzBY8zqGvQt1ULpyrG+Kw==
"@nomicfoundation/solidity-analyzer-freebsd-x64@0.1.0":
version "0.1.0"
resolved "https://registry.yarnpkg.com/@nomicfoundation/solidity-analyzer-freebsd-x64/-/solidity-analyzer-freebsd-x64-0.1.0.tgz#dbc052dcdfd50ae50fd5ae1788b69b4e0fa40040"
integrity sha512-WFCZYMv86WowDA4GiJKnebMQRt3kCcFqHeIomW6NMyqiKqhK1kIZCxSLDYsxqlx396kKLPN1713Q1S8tu68GKg==

"@nomicfoundation/solidity-analyzer-linux-arm64-gnu@0.0.3":
version "0.0.3"
resolved "https://registry.yarnpkg.com/@nomicfoundation/solidity-analyzer-linux-arm64-gnu/-/solidity-analyzer-linux-arm64-gnu-0.0.3.tgz#1ba64b1d76425f8953dedc6367bd7dd46f31dfc5"
integrity sha512-Eyv50EfYbFthoOb0I1568p+eqHGLwEUhYGOxcRNywtlTE9nj+c+MT1LA53HnxD9GsboH4YtOOmJOulrjG7KtbA==
"@nomicfoundation/solidity-analyzer-linux-arm64-gnu@0.1.0":
version "0.1.0"
resolved "https://registry.yarnpkg.com/@nomicfoundation/solidity-analyzer-linux-arm64-gnu/-/solidity-analyzer-linux-arm64-gnu-0.1.0.tgz#e6b2eea633995b557e74e881d2a43eab4760903d"
integrity sha512-DTw6MNQWWlCgc71Pq7CEhEqkb7fZnS7oly13pujs4cMH1sR0JzNk90Mp1zpSCsCs4oKan2ClhMlLKtNat/XRKQ==

"@nomicfoundation/solidity-analyzer-linux-arm64-musl@0.0.3":
version "0.0.3"
resolved "https://registry.yarnpkg.com/@nomicfoundation/solidity-analyzer-linux-arm64-musl/-/solidity-analyzer-linux-arm64-musl-0.0.3.tgz#8d864c49b55e683f7e3b5cce9d10b628797280ac"
integrity sha512-V8grDqI+ivNrgwEt2HFdlwqV2/EQbYAdj3hbOvjrA8Qv+nq4h9jhQUxFpegYMDtpU8URJmNNlXgtfucSrAQwtQ==
"@nomicfoundation/solidity-analyzer-linux-arm64-musl@0.1.0":
version "0.1.0"
resolved "https://registry.yarnpkg.com/@nomicfoundation/solidity-analyzer-linux-arm64-musl/-/solidity-analyzer-linux-arm64-musl-0.1.0.tgz#af81107f5afa794f19988a368647727806e18dc4"
integrity sha512-wUpUnR/3GV5Da88MhrxXh/lhb9kxh9V3Jya2NpBEhKDIRCDmtXMSqPMXHZmOR9DfCwCvG6vLFPr/+YrPCnUN0w==

"@nomicfoundation/solidity-analyzer-linux-x64-gnu@0.0.3":
version "0.0.3"
resolved "https://registry.yarnpkg.com/@nomicfoundation/solidity-analyzer-linux-x64-gnu/-/solidity-analyzer-linux-x64-gnu-0.0.3.tgz#16e769500cf1a8bb42ab9498cee3b93c30f78295"
integrity sha512-uRfVDlxtwT1vIy7MAExWAkRD4r9M79zMG7S09mCrWUn58DbLs7UFl+dZXBX0/8FTGYWHhOT/1Etw1ZpAf5DTrg==
"@nomicfoundation/solidity-analyzer-linux-x64-gnu@0.1.0":
version "0.1.0"
resolved "https://registry.yarnpkg.com/@nomicfoundation/solidity-analyzer-linux-x64-gnu/-/solidity-analyzer-linux-x64-gnu-0.1.0.tgz#6877e1da1a06a9f08446070ab6e0a5347109f868"
integrity sha512-lR0AxK1x/MeKQ/3Pt923kPvwigmGX3OxeU5qNtQ9pj9iucgk4PzhbS3ruUeSpYhUxG50jN4RkIGwUMoev5lguw==

"@nomicfoundation/solidity-analyzer-linux-x64-musl@0.0.3":
version "0.0.3"
resolved "https://registry.yarnpkg.com/@nomicfoundation/solidity-analyzer-linux-x64-musl/-/solidity-analyzer-linux-x64-musl-0.0.3.tgz#75f4e1a25526d54c506e4eba63b3d698b6255b8f"
integrity sha512-8HPwYdLbhcPpSwsE0yiU/aZkXV43vlXT2ycH+XlOjWOnLfH8C41z0njK8DHRtEFnp4OVN6E7E5lHBBKDZXCliA==
"@nomicfoundation/solidity-analyzer-linux-x64-musl@0.1.0":
version "0.1.0"
resolved "https://registry.yarnpkg.com/@nomicfoundation/solidity-analyzer-linux-x64-musl/-/solidity-analyzer-linux-x64-musl-0.1.0.tgz#bb6cd83a0c259eccef4183796b6329a66cf7ebd9"
integrity sha512-A1he/8gy/JeBD3FKvmI6WUJrGrI5uWJNr5Xb9WdV+DK0F8msuOqpEByLlnTdLkXMwW7nSl3awvLezOs9xBHJEg==

"@nomicfoundation/solidity-analyzer-win32-arm64-msvc@0.0.3":
version "0.0.3"
resolved "https://registry.yarnpkg.com/@nomicfoundation/solidity-analyzer-win32-arm64-msvc/-/solidity-analyzer-win32-arm64-msvc-0.0.3.tgz#ef6e20cfad5eedfdb145cc34a44501644cd7d015"
integrity sha512-5WWcT6ZNvfCuxjlpZOY7tdvOqT1kIQYlDF9Q42wMpZ5aTm4PvjdCmFDDmmTvyXEBJ4WTVmY5dWNWaxy8h/E28g==
"@nomicfoundation/solidity-analyzer-win32-arm64-msvc@0.1.0":
version "0.1.0"
resolved "https://registry.yarnpkg.com/@nomicfoundation/solidity-analyzer-win32-arm64-msvc/-/solidity-analyzer-win32-arm64-msvc-0.1.0.tgz#9d4bca1cc9a1333fde985675083b0b7d165f6076"
integrity sha512-7x5SXZ9R9H4SluJZZP8XPN+ju7Mx+XeUMWZw7ZAqkdhP5mK19I4vz3x0zIWygmfE8RT7uQ5xMap0/9NPsO+ykw==

"@nomicfoundation/solidity-analyzer-win32-ia32-msvc@0.0.3":
version "0.0.3"
resolved "https://registry.yarnpkg.com/@nomicfoundation/solidity-analyzer-win32-ia32-msvc/-/solidity-analyzer-win32-ia32-msvc-0.0.3.tgz#98c4e3af9cee68896220fa7e270aefdf7fc89c7b"
integrity sha512-P/LWGZwWkyjSwkzq6skvS2wRc3gabzAbk6Akqs1/Iiuggql2CqdLBkcYWL5Xfv3haynhL+2jlNkak+v2BTZI4A==
"@nomicfoundation/solidity-analyzer-win32-ia32-msvc@0.1.0":
version "0.1.0"
resolved "https://registry.yarnpkg.com/@nomicfoundation/solidity-analyzer-win32-ia32-msvc/-/solidity-analyzer-win32-ia32-msvc-0.1.0.tgz#0db5bfc6aa952bea4098d8d2c8947b4e5c4337ee"
integrity sha512-m7w3xf+hnE774YRXu+2mGV7RiF3QJtUoiYU61FascCkQhX3QMQavh7saH/vzb2jN5D24nT/jwvaHYX/MAM9zUw==

"@nomicfoundation/solidity-analyzer-win32-x64-msvc@0.0.3":
version "0.0.3"
resolved "https://registry.yarnpkg.com/@nomicfoundation/solidity-analyzer-win32-x64-msvc/-/solidity-analyzer-win32-x64-msvc-0.0.3.tgz#12da288e7ef17ec14848f19c1e8561fed20d231d"
integrity sha512-4AcTtLZG1s/S5mYAIr/sdzywdNwJpOcdStGF3QMBzEt+cGn3MchMaS9b1gyhb2KKM2c39SmPF5fUuWq1oBSQZQ==
"@nomicfoundation/solidity-analyzer-win32-x64-msvc@0.1.0":
version "0.1.0"
resolved "https://registry.yarnpkg.com/@nomicfoundation/solidity-analyzer-win32-x64-msvc/-/solidity-analyzer-win32-x64-msvc-0.1.0.tgz#2e0f39a2924dcd77db6b419828595e984fabcb33"
integrity sha512-xCuybjY0sLJQnJhupiFAXaek2EqF0AP0eBjgzaalPXSNvCEN6ZYHvUzdA50ENDVeSYFXcUsYf3+FsD3XKaeptA==

"@nomicfoundation/solidity-analyzer@^0.0.3":
version "0.0.3"
resolved "https://registry.yarnpkg.com/@nomicfoundation/solidity-analyzer/-/solidity-analyzer-0.0.3.tgz#d1029f872e66cb1082503b02cc8b0be12f8dd95e"
integrity sha512-VFMiOQvsw7nx5bFmrmVp2Q9rhIjw2AFST4DYvWVVO9PMHPE23BY2+kyfrQ4J3xCMFC8fcBbGLt7l4q7m1SlTqg==
"@nomicfoundation/solidity-analyzer@^0.1.0":
version "0.1.0"
resolved "https://registry.yarnpkg.com/@nomicfoundation/solidity-analyzer/-/solidity-analyzer-0.1.0.tgz#e5ddc43ad5c0aab96e5054520d8e16212e125f50"
integrity sha512-xGWAiVCGOycvGiP/qrlf9f9eOn7fpNbyJygcB0P21a1MDuVPlKt0Srp7rvtBEutYQ48ouYnRXm33zlRnlTOPHg==
optionalDependencies:
"@nomicfoundation/solidity-analyzer-darwin-arm64" "0.0.3"
"@nomicfoundation/solidity-analyzer-darwin-x64" "0.0.3"
"@nomicfoundation/solidity-analyzer-freebsd-x64" "0.0.3"
"@nomicfoundation/solidity-analyzer-linux-arm64-gnu" "0.0.3"
"@nomicfoundation/solidity-analyzer-linux-arm64-musl" "0.0.3"
"@nomicfoundation/solidity-analyzer-linux-x64-gnu" "0.0.3"
"@nomicfoundation/solidity-analyzer-linux-x64-musl" "0.0.3"
"@nomicfoundation/solidity-analyzer-win32-arm64-msvc" "0.0.3"
"@nomicfoundation/solidity-analyzer-win32-ia32-msvc" "0.0.3"
"@nomicfoundation/solidity-analyzer-win32-x64-msvc" "0.0.3"
"@nomicfoundation/solidity-analyzer-darwin-arm64" "0.1.0"
"@nomicfoundation/solidity-analyzer-darwin-x64" "0.1.0"
"@nomicfoundation/solidity-analyzer-freebsd-x64" "0.1.0"
"@nomicfoundation/solidity-analyzer-linux-arm64-gnu" "0.1.0"
"@nomicfoundation/solidity-analyzer-linux-arm64-musl" "0.1.0"
"@nomicfoundation/solidity-analyzer-linux-x64-gnu" "0.1.0"
"@nomicfoundation/solidity-analyzer-linux-x64-musl" "0.1.0"
"@nomicfoundation/solidity-analyzer-win32-arm64-msvc" "0.1.0"
"@nomicfoundation/solidity-analyzer-win32-ia32-msvc" "0.1.0"
"@nomicfoundation/solidity-analyzer-win32-x64-msvc" "0.1.0"

"@nomiclabs/[email protected]":
version "2.1.1"
Expand Down Expand Up @@ -15459,10 +15459,10 @@ hard-rejection@^2.1.0:
resolved "https://registry.yarnpkg.com/hard-rejection/-/hard-rejection-2.1.0.tgz#1c6eda5c1685c63942766d79bb40ae773cecd883"
integrity sha512-VIZB+ibDhx7ObhAe7OVtoEbuP4h/MuOTHJ+J8h/eBXotJYl0fBgR72xDFCKgIh22OJZIOVNxBMWuhAr10r8HdA==

hardhat@2.11.2:
version "2.11.2"
resolved "https://registry.yarnpkg.com/hardhat/-/hardhat-2.11.2.tgz#c81388630255823bb1717ec07c4ee651b1fbe97f"
integrity sha512-BdsXC1CFJQDJKmAgCwpmGhFuVU6dcqlgMgT0Kg/xmFAFVugkpYu6NRmh4AaJ3Fah0/BR9DOR4XgQGIbg4eon/Q==
hardhat@2.12.0:
version "2.12.0"
resolved "https://registry.yarnpkg.com/hardhat/-/hardhat-2.12.0.tgz#51e59f1ff4991bcb66d1a380ea807e6c15fcac34"
integrity sha512-mNJFbVG479HwOzxiaLxobyvED2M1aEAuPPYhEo1+88yicMDSTrU2JIS7vV+V0GSNQKaDoiHCmV6bcKjiljT/dQ==
dependencies:
"@ethersproject/abi" "^5.1.2"
"@metamask/eth-sig-util" "^4.0.0"
Expand All @@ -15476,7 +15476,7 @@ [email protected]:
"@nomicfoundation/ethereumjs-tx" "^4.0.0"
"@nomicfoundation/ethereumjs-util" "^8.0.0"
"@nomicfoundation/ethereumjs-vm" "^6.0.0"
"@nomicfoundation/solidity-analyzer" "^0.0.3"
"@nomicfoundation/solidity-analyzer" "^0.1.0"
"@sentry/node" "^5.18.1"
"@types/bn.js" "^5.1.0"
"@types/lru-cache" "^5.1.0"
Expand Down

0 comments on commit e679cc0

Please sign in to comment.