mars-v2-frontend/src/store/slices/common.ts
Linkie Link 21268e5536
UI style updates (#106)
* feat: updates on the button styles

* env: updated yarn.lock

* fix: added account actions

* fix: updated the orbs logic

* fix: fixed the blur presets

* feat: updated the button logic

* fix: wallet modal style adjustments

* fix: updated close icon

* fix: fixed the close button

* fix: fix types

* fix: fixed the build

* tidy: component cleanup

* feat:  added new AccountDetails component

* refactor: propper usage of tailwind

* refactor: imports

* feat: added pages for all scenarios

* fix: fix the loading component

* fix: remove loading from default trade

* fix: fixed the build

* fix: fixed losing the provider on hotplug

* tidy: remove unused code

* fix: added error messages

* add borrow page structure

* env: enhanced debugging by restructuring the ENV object

* fix: fixed the build

* fix: fixed the wording on missing env variables

* feat: added button hover (#112)

* feat: added button hover

* fix: added bg transition to primary buttons

* feat: pages refactored (#111)

* feat: pages refactored

* fix: added loader for AccountNavigation

* fix: fixed the wallet store management

* fix: get rid of the walletSlice and refactor

* fix: added gap to the borrow page

* fix: fixed some dependencies

* fix: added initClients back

* fix: fixed according to feedback

---------

Co-authored-by: bwvdhelm <34470358+bobthebuidlr@users.noreply.github.com>
2023-03-08 10:44:39 +01:00

99 lines
2.9 KiB
TypeScript

import { SigningCosmWasmClient } from '@cosmjs/cosmwasm-stargate'
import { Coin } from '@cosmjs/stargate'
import { WalletClient, WalletConnectionStatus } from '@marsprotocol/wallet-connector'
import BigNumber from 'bignumber.js'
import { GetState, SetState } from 'zustand'
import { ENV } from 'constants/env'
import { MarsAccountNftClient } from 'types/generated/mars-account-nft/MarsAccountNft.client'
import { MarsCreditManagerClient } from 'types/generated/mars-credit-manager/MarsCreditManager.client'
import { MarsSwapperBaseClient } from 'types/generated/mars-swapper-base/MarsSwapperBase.client'
import { getMarketAssets } from 'utils/assets'
import { formatValue } from 'utils/formatters'
export interface CommonSlice {
address?: string
borrowModal: boolean
client?: WalletClient
clients: {
accountNft?: MarsAccountNftClient
creditManager?: MarsCreditManagerClient
swapperBase?: MarsSwapperBaseClient
}
createAccountModal: boolean
creditAccounts: string[] | null
deleteAccountModal: boolean
enableAnimations: boolean
fundAccountModal: boolean
isOpen: boolean
prices: Coin[]
repayModal: boolean
selectedAccount: string | null
signingClient?: SigningCosmWasmClient
status: WalletConnectionStatus
withdrawModal: boolean
formatCurrency: (coin: Coin) => string
initClients: (address: string, signingClient: SigningCosmWasmClient) => void
}
export function createCommonSlice(set: SetState<CommonSlice>, get: GetState<CommonSlice>) {
return {
borrowModal: false,
createAccountModal: false,
clients: {},
creditAccounts: null,
deleteAccountModal: false,
enableAnimations: true,
fundAccountModal: false,
isOpen: true,
prices: [],
repayModal: false,
selectedAccount: null,
status: WalletConnectionStatus.Unconnected,
withdrawModal: false,
formatCurrency: (coin: Coin) => {
const price = get().prices.find((price) => price.denom === coin.denom)
const marketAsset = getMarketAssets().find((asset) => asset.denom === coin.denom)
if (!price || !marketAsset) return ''
return formatValue(
new BigNumber(coin.amount)
.times(price.amount)
.dividedBy(10 ** marketAsset.decimals)
.toNumber(),
{
minDecimals: 0,
prefix: '$',
},
)
},
initClients: (address: string, signingClient: SigningCosmWasmClient) => {
if (!signingClient) return
const accountNft = new MarsAccountNftClient(
signingClient,
address,
ENV.ADDRESS_ACCOUNT_NFT || '',
)
const creditManager = new MarsCreditManagerClient(
signingClient,
address,
ENV.ADDRESS_CREDIT_MANAGER || '',
)
const swapperBase = new MarsSwapperBaseClient(
signingClient,
address,
ENV.ADDRESS_SWAPPER || '',
)
set(() => ({
clients: {
accountNft,
creditManager,
swapperBase,
},
}))
},
}
}