-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathaccount.ts
102 lines (89 loc) · 4.22 KB
/
account.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import { shortenAddress } from "@remix-ui/helper"
import { RunTab } from "../types/run-tab"
import { clearInstances, setAccount, setExecEnv } from "./actions"
import { displayNotification, fetchAccountsListFailed, fetchAccountsListRequest, fetchAccountsListSuccess, setMatchPassphrase, setPassphrase } from "./payload"
import { toChecksumAddress } from '@ethereumjs/util'
export const updateAccountBalances = async (plugin: RunTab, dispatch: React.Dispatch<any>) => {
const accounts = plugin.REACT_API.accounts.loadedAccounts
for (const account of Object.keys(accounts)) {
const balance = await plugin.blockchain.getBalanceInEther(account)
const updated = shortenAddress(account, balance)
accounts[account] = updated
}
dispatch(fetchAccountsListSuccess(accounts))
}
export const fillAccountsList = async (plugin: RunTab, dispatch: React.Dispatch<any>) => {
try {
dispatch(fetchAccountsListRequest())
try {
let accounts = await plugin.blockchain.getAccounts()
if (!accounts) accounts = []
const loadedAccounts = {}
for (const account of accounts) {
const balance = await plugin.blockchain.getBalanceInEther(account)
loadedAccounts[account] = shortenAddress(account, balance)
}
const provider = plugin.blockchain.getProvider()
if (provider && provider.startsWith('injected')) {
const selectedAddress = plugin.blockchain.getInjectedWeb3Address() || Object.keys(loadedAccounts)[0]; //Setting default account to first account
if (!(Object.keys(loadedAccounts).includes(toChecksumAddress(selectedAddress)))) setAccount(dispatch, null)
}
dispatch(fetchAccountsListSuccess(loadedAccounts))
} catch (e) {
dispatch(fetchAccountsListFailed(e.message))
}
} catch (e) {
plugin.call('notification', 'toast', `Cannot get account list: ${e}`)
}
}
export const setFinalContext = (plugin: RunTab, dispatch: React.Dispatch<any>) => {
dispatch(fetchAccountsListRequest())
// set the final context. Cause it is possible that this is not the one we've originally selected
const value = _getProviderDropdownValue(plugin)
setExecEnv(dispatch, value)
clearInstances(dispatch)
}
const _getProviderDropdownValue = (plugin: RunTab): string => {
return plugin.blockchain.getProvider()
}
export const setExecutionContext = (plugin: RunTab, dispatch: React.Dispatch<any>, executionContext: { context: string, fork: string }) => {
plugin.blockchain.changeExecutionContext(executionContext, null, (alertMsg) => {
plugin.call('notification', 'toast', alertMsg)
}, () => { setFinalContext(plugin, dispatch) })
}
export const createNewBlockchainAccount = async (plugin: RunTab, dispatch: React.Dispatch<any>, cbMessage: JSX.Element) => {
plugin.blockchain.newAccount(
'',
(cb) => {
dispatch(displayNotification('Enter Passphrase', cbMessage, 'OK', 'Cancel', async () => {
if (plugin.REACT_API.passphrase === plugin.REACT_API.matchPassphrase) {
cb(plugin.REACT_API.passphrase)
} else {
dispatch(displayNotification('Error', 'Passphrase does not match', 'OK', null))
}
setPassphrase('')
setMatchPassphrase('')
}, () => {}))
},
async (error, address) => {
if (error) {
return plugin.call('notification', 'toast', 'Cannot create an account: ' + error)
}
plugin.call('notification', 'toast', `account ${address} created`)
await fillAccountsList(plugin, dispatch)
}
)
}
export const signMessageWithAddress = (plugin: RunTab, dispatch: React.Dispatch<any>, account: string, message: string, modalContent: (hash: string, data: string) => JSX.Element, passphrase?: string) => {
plugin.blockchain.signMessage(message, account, passphrase, (err, msgHash, signedData) => {
if (err) {
console.error(err)
return plugin.call('notification', 'toast', typeof err === 'string' ? err : err.message)
}
dispatch(displayNotification('Signed Message', modalContent(msgHash, signedData), 'OK', null, () => {}, null))
})
}
export const addFileInternal = async (plugin: RunTab, path: string, content: string) => {
const file = await plugin.call('fileManager', 'writeFileNoRewrite', path, content)
await plugin.call('fileManager', 'open', file.newPath)
}