Skip to content

Commit 0452476

Browse files
committed
chore: updated README.md
1 parent 8009b45 commit 0452476

File tree

1 file changed

+40
-23
lines changed

1 file changed

+40
-23
lines changed

README.md

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -91,37 +91,30 @@ const Ethereum = ExtensionTools.Ethereum
9191
//or
9292
import {Ethereum} from '@unique-nft/utils/extension'
9393

94-
const getAccountsResult = await Ethereum.getAccounts()
95-
/*
96-
{
97-
accounts: ['0xf8cC75F76d46c3b1c5F270Fe06c8FFdeAB8E5eaB'],
98-
info: {extensionFound: true, chainId: '0x22b2', chainIdNumber: 8882},
99-
selectedAddress: 0xf8cC75F76d46c3b1c5F270Fe06c8FFdeAB8E5eaB
100-
}
101-
// or, when there is no granted account:
102-
{accounts: [], selectedAddress: null, info: {extensionFound: true, chainId: '0x22b2', chainIdNumber: 8882}}
103-
// or, when there is no extension:
104-
{accounts: [], selectedAddress: null, info: {extensionFound: false}}
105-
// in Node.js `info.extensionFound is` always false.
106-
*/
94+
const {error, address, chainId} = await Ethereum.getAccounts()
95+
// or
96+
const {error, address, chainId} = await Ethereum.requestAccounts()
97+
// or
98+
const {error, address, chainId} = await Ethereum.getOrRequestAccounts(boolean) // true - resuest, false - get
99+
100+
//subscribe on chainId or SelectedAddress changes:
101+
const unsubscribe = Ethereum.subscribeOnChanges(({reason, address, chainId}) => {/*...*/})
107102
```
108103

109-
Simple example which just checks extension and tries to get an address without prompting user:
104+
Also, Ethereum extension tool provide some helpers to work with Unique chains.
105+
All this helpers have all 4 chains, so every helper can be used for `unique`, `quartz`, `opal` and `sapphire`.
110106

111107
```ts
112-
import {Ethereum} from '@unique-nft/utils/extension'
113-
114-
let result = await Ethereum.getAccounts()
108+
Ethereum.currentChainIs.opal() // true - false
109+
Ethereum.addChain.quartz()
110+
Ethereum.switchChainTo.unique()
115111

116-
if (result.info.extensionFound && result.selectedAddress) {
117-
//woohoo, let's create a Web3 Provider like that:
118-
const provider = new ethers.providers.Web3Provider(window.ethereum)
119-
console.log(ethers.utils.formatEther(await provider.getBalance(result.selectedAddress)))
120-
}
112+
Ethereum.chainNameToChainId.unique // 8880
113+
Ethereum.chainIdToChainName[8881] // quartz
121114
```
122115

123116
More complex example, when we want to request user to grant access.
124-
_Note: If user has already granted access, it will work silently, just like_ `getAccounts`.
117+
If user has already granted access, it will work silently, just like `getAccounts`.
125118

126119
```ts
127120
import {Ethereum} from '@unique-nft/utils/extension'
@@ -141,6 +134,30 @@ if (result.selectedAddress) {
141134
}
142135
```
143136

137+
```ts
138+
import {Ethereum} from '@unique-nft/utils/extension'
139+
140+
let {error, address, chainId} = await Ethereum.requestAccounts()
141+
142+
if (address) {
143+
//woohoo, let's create a Web3 Provider like that:
144+
let provider = new ethers.providers.Web3Provider(window.ethereum)
145+
console.log(ethers.utils.formatEther(await provider.getBalance(result.selectedAddress)))
146+
} else {
147+
if (error) {
148+
if (error.extensionNotFound) {
149+
alert(`Please install some ethereum browser extension`)
150+
} else if (error.userRejected) {
151+
alert(`But whyyyyyyy?`)
152+
} else {
153+
alert(`Connection to ethereum extension failed: ${error.message}`)
154+
}
155+
} else {
156+
alert('Please, create some account or grant permissions for an account')
157+
}
158+
}
159+
```
160+
144161
### Polkadot
145162

146163
A tiny (1.5 Kb) and zero-dependency (no WASM, no anything) module to work with Polkadot extensions family -

0 commit comments

Comments
 (0)