Skip to content

Commit 51d7108

Browse files
committed
save in LS and load in dropdown
1 parent 51a7fa1 commit 51d7108

File tree

3 files changed

+55
-4
lines changed

3 files changed

+55
-4
lines changed

libs/remix-ui/run-tab/src/lib/actions/account.ts

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { RunTab } from "../types/run-tab"
33
import { clearInstances, setAccount, setExecEnv } from "./actions"
44
import { displayNotification, fetchAccountsListFailed, fetchAccountsListRequest, fetchAccountsListSuccess, setMatchPassphrase, setPassphrase } from "./payload"
55
import { toChecksumAddress } from '@ethereumjs/util'
6+
import { SmartAccount } from "../types"
67
import "viem/window"
78
import { custom, createWalletClient } from "viem"
89
import { sepolia } from "viem/chains"
@@ -26,6 +27,14 @@ export const fillAccountsList = async (plugin: RunTab, dispatch: React.Dispatch<
2627
dispatch(fetchAccountsListRequest())
2728
try {
2829
let accounts = await plugin.blockchain.getAccounts()
30+
const provider = plugin.blockchain.getProvider()
31+
if (provider && provider.startsWith('injected') && accounts?.length) {
32+
await loadSmartAccounts(plugin)
33+
if (plugin.REACT_API.smartAccounts) {
34+
const safeAddresses = Object.keys(plugin.REACT_API.smartAccounts)
35+
accounts.push(...safeAddresses)
36+
}
37+
}
2938
if (!accounts) accounts = []
3039

3140
const loadedAccounts = {}
@@ -34,7 +43,6 @@ export const fillAccountsList = async (plugin: RunTab, dispatch: React.Dispatch<
3443
const balance = await plugin.blockchain.getBalanceInEther(account)
3544
loadedAccounts[account] = shortenAddress(account, balance)
3645
}
37-
const provider = plugin.blockchain.getProvider()
3846

3947
if (provider && provider.startsWith('injected')) {
4048
const selectedAddress = plugin.blockchain.getInjectedWeb3Address()
@@ -93,7 +101,7 @@ export const createNewBlockchainAccount = async (plugin: RunTab, dispatch: React
93101
}
94102

95103
export const createSmartAccount = async (plugin: RunTab, dispatch: React.Dispatch<any>) => {
96-
console.log('createSmartAccount action')
104+
const localStorageKey = 'smartAccounts'
97105

98106
// @ts-ignore
99107
const [account] = await window.ethereum!.request({ method: 'eth_requestAccounts' })
@@ -105,21 +113,55 @@ export const createSmartAccount = async (plugin: RunTab, dispatch: React.Dispatc
105113
transport: custom(window.ethereum!),
106114
})
107115

116+
const salt = 0
108117
const safeAccount = await toSafeSmartAccount({
109118
client: walletClient,
110119
entryPoint: {
111120
address: entryPoint07Address,
112121
version: "0.7",
113122
},
114123
owners: [toAccount(account)],
115-
// saltNonce: 0n, // optional
124+
saltNonce: salt,
116125
version: "1.4.1"
117126
})
127+
const safeAddress = safeAccount.address
128+
129+
const sAccount: SmartAccount = {
130+
address : safeAccount.address,
131+
salt,
132+
ownerEOA: account,
133+
timestamp: Date.now()
134+
}
135+
plugin.REACT_API.smartAccounts[safeAddress] = sAccount
136+
// Save smart accounts in local storage
137+
const smartAccountsStr = localStorage.getItem(localStorageKey)
138+
const smartAccountsObj = JSON.parse(smartAccountsStr)
139+
smartAccountsObj[plugin.REACT_API.chainId] = plugin.REACT_API.smartAccounts
140+
localStorage.setItem(localStorageKey, JSON.stringify(smartAccountsObj))
118141

119-
console.log('safeAccount----->', safeAccount.address)
120142
return plugin.call('notification', 'toast', `Safe account ${safeAccount.address} created for owner ${account}`)
121143
}
122144

145+
export const loadSmartAccounts = async (plugin) => {
146+
const { chainId } = plugin.REACT_API
147+
const localStorageKey = 'smartAccounts'
148+
149+
const smartAccountsStr = localStorage.getItem(localStorageKey)
150+
if (smartAccountsStr) {
151+
const smartAccountsObj = JSON.parse(smartAccountsStr)
152+
if (smartAccountsObj[chainId]) {
153+
plugin.REACT_API.smartAccounts = smartAccountsObj[chainId]
154+
} else {
155+
smartAccountsObj[chainId] = {}
156+
localStorage.setItem(localStorageKey, JSON.stringify(smartAccountsObj))
157+
}
158+
} else {
159+
const objToStore = {}
160+
objToStore[chainId] = {}
161+
localStorage.setItem(localStorageKey, JSON.stringify(objToStore))
162+
}
163+
}
164+
123165
export const signMessageWithAddress = (plugin: RunTab, dispatch: React.Dispatch<any>, account: string, message: string, modalContent: (hash: string, data: string) => JSX.Element, passphrase?: string) => {
124166
plugin.blockchain.signMessage(message, account, passphrase, (err, msgHash, signedData) => {
125167
if (err) {

libs/remix-ui/run-tab/src/lib/reducers/runTab.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export const runTabInitialState: RunTabState = {
1616
error: null,
1717
selectedAccount: ''
1818
},
19+
smartAccounts: {},
1920
sendValue: '0',
2021
sendUnit: 'wei',
2122
gasLimit: 0,

libs/remix-ui/run-tab/src/lib/types/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ export type Provider = {
3838
position: number
3939
}
4040

41+
export type SmartAccount = {
42+
address: string
43+
salt: number
44+
ownerEOA: string
45+
timestamp: number
46+
}
47+
4148
export interface RunTabState {
4249
accounts: {
4350
loadedAccounts: Record<string, string>,
@@ -46,6 +53,7 @@ export interface RunTabState {
4653
error: string,
4754
selectedAccount: string
4855
},
56+
smartAccounts: Record<string, SmartAccount>
4957
sendValue: string,
5058
sendUnit: 'ether' | 'finney' | 'gwei' | 'wei',
5159
gasLimit: number,

0 commit comments

Comments
 (0)