Skip to content

Commit

Permalink
cleanup and refactor Provider store
Browse files Browse the repository at this point in the history
  • Loading branch information
karczuRF committed Nov 28, 2024
1 parent 15b34a9 commit e00836f
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 57 deletions.
12 changes: 8 additions & 4 deletions src/components/Wallet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export const Wallet: React.FC = () => {
}, [provider, currentAccount])

const refreshAccount = useCallback(async () => {
if (!provider) return
try {
const { accounts } = await provider.getAccountsList()
setAccountsList(accounts)
Expand All @@ -36,6 +37,7 @@ export const Wallet: React.FC = () => {
}, [provider])

async function handleCreateAccount(accountName: string) {
if (!provider) return
try {
await provider.createFreeTestCoins(accountName)
dispatch(
Expand All @@ -51,7 +53,8 @@ export const Wallet: React.FC = () => {
}
}

async function get_free_coins() {
async function getFreeTestCoins() {
if (!provider) return
try {
const currentAccountName = currentAccount?.account.name ?? undefined
await provider.createFreeTestCoins(currentAccountName)
Expand All @@ -63,7 +66,8 @@ export const Wallet: React.FC = () => {
}
}

async function get_balances() {
async function getBalances() {
if (!provider) return
try {
const accountAddress = substateIdToString(currentAccount?.account.address ?? null)
const resp = await provider.getAccountBalances(accountAddress)
Expand Down Expand Up @@ -95,10 +99,10 @@ export const Wallet: React.FC = () => {
})}: ${substateIdToString(currentAccount?.account.address ?? null)}`}</Typography>
</Stack>
</Paper>
<Button onClick={get_free_coins} variant="contained" sx={{ width: 200 }}>
<Button onClick={getFreeTestCoins} variant="contained" sx={{ width: 200 }}>
{t("get-free-coins", { ns: "components" })}
</Button>
<Button onClick={get_balances} variant="contained" sx={{ width: 200 }}>
<Button onClick={getBalances} variant="contained" sx={{ width: 200 }}>
{t("get-balances", { ns: "components" })}
</Button>
</Box>
Expand Down
5 changes: 2 additions & 3 deletions src/store/devTapplets/devTapplets.action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,8 @@ export const addDevTappletAction = () => ({
) => {
const endpoint = action.payload.endpoint
try {
console.log(" ~~~ add dev tapplet", endpoint)
const tst = await invoke("add_dev_tapplet", { endpoint })
console.log(" ~~~ add dev tapplet ok", tst)
await invoke("add_dev_tapplet", { endpoint })

listenerApi.dispatch(devTappletsActions.addDevTappletSuccess({}))
listenerApi.dispatch(devTappletsActions.initializeRequest({}))
} catch (error) {
Expand Down
32 changes: 26 additions & 6 deletions src/store/provider/provider.action.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
import { ListenerEffectAPI, PayloadAction, ThunkDispatch, UnknownAction } from "@reduxjs/toolkit"
import { InitProviderRequestPayload, UpdatePermissionsRequestPayload } from "./provider.types"
import { providerActions } from "./provider.slice"
import { RootState } from "../store"
import { providerSelector } from "./provider.selector"
import { TUInternalProvider } from "@provider/TUInternalProvider"
import { WalletDaemonParameters } from "@tari-project/tarijs"
import {
TariPermissionAccountInfo,
TariPermissionAccountList,
TariPermissionKeyList,
TariPermissions,
TariPermissionSubstatesRead,
TariPermissionTransactionSend,
} from "@tari-project/tarijs/dist/providers/tari_universe"

export const initializeAction = () => ({
actionCreator: providerActions.initializeRequest,
Expand All @@ -11,11 +19,23 @@ export const initializeAction = () => ({
listenerApi: ListenerEffectAPI<unknown, ThunkDispatch<unknown, unknown, UnknownAction>, unknown>
) => {
try {
const state = listenerApi.getState() as RootState
const provider = providerSelector.selectProvider(state) //TODO why we need this?
console.log("[store] INIT PROVIDER", provider.providerName)
console.log("[store provider] INIT")
//TODO set default permissions
let permissions = new TariPermissions()
let optionalPermissions = new TariPermissions()
permissions.addPermission(new TariPermissionAccountInfo())
permissions.addPermission(new TariPermissionAccountList())
permissions.addPermission(new TariPermissionKeyList())
permissions.addPermission(new TariPermissionSubstatesRead())
permissions.addPermission(new TariPermissionTransactionSend())

listenerApi.dispatch(providerActions.initializeSuccess({ provider }))
const params: WalletDaemonParameters = {
permissions,
optionalPermissions,
}
const internalProvider = TUInternalProvider.build(params)

listenerApi.dispatch(providerActions.initializeSuccess({ provider: internalProvider }))
} catch (error) {
listenerApi.dispatch(providerActions.initializeFailure({ errorMsg: error as string }))
}
Expand Down
18 changes: 1 addition & 17 deletions src/store/provider/provider.selector.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,11 @@
import { TariPermissions, WalletDaemonParameters } from "@tari-project/tarijs"
import { createSelector } from "@reduxjs/toolkit"
import { RootState } from "../store"
import { TUInternalProvider } from "@provider/TUInternalProvider"
import { toPermission } from "@type/tariPermissions"

const providerStateSelector = (state: RootState) => state.provider

const isInitialized = createSelector([providerStateSelector], (state) => state.isInitialized)

const selectProvider = createSelector([providerStateSelector], (state) => {
let permissions = new TariPermissions()
let optionalPermissions = new TariPermissions()
if (state.permissions) {
state.permissions.requiredPermissions.map((p) => permissions.addPermission(toPermission(p)))
state.permissions.optionalPermissions.map((p) => optionalPermissions.addPermission(toPermission(p)))
}

const params: WalletDaemonParameters = {
permissions,
optionalPermissions,
}
return TUInternalProvider.build(params)
})
const selectProvider = createSelector([providerStateSelector], (state) => state.provider)

export const providerSelector = {
isInitialized,
Expand Down
16 changes: 1 addition & 15 deletions src/store/provider/provider.slice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,12 @@ import {
UpdatePermissionsRequestPayload,
UpdatePermissionsSuccessPayload,
} from "./provider.types"
import {
TariPermissionAccountInfo,
TariPermissionAccountList,
TariPermissionKeyList,
TariPermissionSubstatesRead,
TariPermissionTransactionSend,
} from "@tari-project/tarijs/dist/providers/tari_universe"

const initialState: ProviderStoreState = {
isInitialized: false,
provider: null,
// TODO default permissions for built-in provider
permissions: {
requiredPermissions: [
new TariPermissionAccountInfo(),
new TariPermissionAccountList(),
new TariPermissionKeyList(),
new TariPermissionSubstatesRead(),
new TariPermissionTransactionSend(),
],
requiredPermissions: [],
optionalPermissions: [],
},
}
Expand Down
10 changes: 5 additions & 5 deletions src/store/tappletProviders/tappletProviders.action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ export const addTappProviderAction = () => ({
listenerApi: ListenerEffectAPI<unknown, ThunkDispatch<unknown, unknown, UnknownAction>, unknown>
) => {
try {
console.log(" ^^^^ ADD PROVIDER ACTION")
console.log("[store tapp provider] ADD PROVIDER ACTION")
const launchedTappParams = action.payload.launchedTappParams
console.log(" ^^^^ ADD PROVIDER LAUNCHED", launchedTappParams)
console.log("[store tapp provider] ADD PROVIDER LAUNCHED", launchedTappParams)

let requiredPermissions = new TariPermissions()
let optionalPermissions = new TariPermissions()
Expand All @@ -51,10 +51,10 @@ export const addTappProviderAction = () => ({
optionalPermissions,
}
const provider: TappletProvider = {
id: action.payload.installedTappletId,
id: action.payload.installedTappletId, //TODO find a way to distinguish installedTappledId and devTappletId (can the same)
provider: TUInternalProvider.build(params),
}
console.log(" ^^^^ ADD PROVIDER PROVIDER", provider)
console.log("[store tapp provider] ADD TAPP PROVIDER", provider)
listenerApi.dispatch(tappletProvidersActions.addTappProviderSuccess({ tappletProvider: provider }))
} catch (error) {
listenerApi.dispatch(tappletProvidersActions.addTappProviderFailure({ errorMsg: error as string }))
Expand All @@ -69,7 +69,7 @@ export const deleteTappletProviderAction = () => ({
listenerApi: ListenerEffectAPI<unknown, ThunkDispatch<unknown, unknown, UnknownAction>, unknown>
) => {
const tappletProviderId = action.payload.tappletId
console.log("******** DELETE ME ", tappletProviderId)
console.log("[store tapp provider] DELETE tapp ID ", tappletProviderId)
try {
listenerApi.dispatch(tappletProvidersActions.deleteTappProviderSuccess({ tappletProviderId }))
} catch (error) {
Expand Down
8 changes: 1 addition & 7 deletions src/store/transaction/transaction.action.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import { ListenerEffectAPI, PayloadAction, ThunkDispatch, UnknownAction } from "@reduxjs/toolkit"
import { transactionActions } from "./transaction.slice"
import {
InitTransactionRequestPayload,
Transaction,
TransactionRequestPayload,
TUProviderMethod,
} from "./transaction.types"
import { InitTransactionRequestPayload, Transaction, TransactionRequestPayload } from "./transaction.types"
import { errorActions } from "../error/error.slice"
import { RootState } from "../store"
import { simulationActions } from "../simulation/simulation.slice"
Expand All @@ -14,7 +9,6 @@ import { SubmitTransactionRequest } from "@tari-project/tarijs"
import { invoke } from "@tauri-apps/api/core"
import { FinalizeResult, AccountsGetBalancesResponse } from "@tari-project/typescript-bindings"
import { BalanceUpdate } from "../simulation/simulation.types"
import { providerActions } from "../provider/provider.slice"
import { txCheck } from "@type/transaction"

export const addTransactionAction = () => ({
Expand Down

0 comments on commit e00836f

Please sign in to comment.