Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix config token #363

Merged
merged 1 commit into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
307 changes: 154 additions & 153 deletions src/logic/tokens/store/actions/fetchSafeTokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,181 +51,182 @@ const extractDataFromResult = (

export const fetchSafeTokens =
(safeAddress: string, currency?: string) =>
async (dispatch: Dispatch, getState: () => AppReduxState): Promise<void> => {
const state = getState()
const safe = currentSafe(state)
async (dispatch: Dispatch, getState: () => AppReduxState): Promise<void> => {
const state = getState()
const safe = currentSafe(state)

if (!safe) {
return
}
const selectedCurrency = currency ?? currentCurrencySelector(state)

let tokenCurrenciesBalances: SafeBalanceResponse
try {
tokenCurrenciesBalances = await fetchTokenCurrenciesBalances({
safeAddress,
selectedCurrency,
})
} catch (e) {
logError(Errors._601, e.message)
return
}
if (!safe) {
return
}
const selectedCurrency = currency ?? currentCurrencySelector(state)

const { balances, nativeBalance, tokens } = tokenCurrenciesBalances.items.reduce<ExtractedData>(
extractDataFromResult,
{
balances: [],
nativeBalance: '0',
tokens: [],
},
)

dispatch(
updateSafe({
address: safeAddress,
balances,
nativeBalance: '0',
totalFiatBalance: new BigNumber(tokenCurrenciesBalances.fiatTotal).toFixed(6),
}),
)
dispatch(addTokens(tokens))
}
let tokenCurrenciesBalances: SafeBalanceResponse
try {
tokenCurrenciesBalances = await fetchTokenCurrenciesBalances({
safeAddress,
selectedCurrency,
})
} catch (e) {
logError(Errors._601, e.message)
return
}

const { balances, nativeBalance, tokens } = tokenCurrenciesBalances.items.reduce<ExtractedData>(
extractDataFromResult,
{
balances: [],
nativeBalance: '0',
tokens: [],
},
)

dispatch(
updateSafe({
address: safeAddress,
balances,
nativeBalance: '0',
totalFiatBalance: new BigNumber(tokenCurrenciesBalances.fiatTotal).toFixed(6),
}),
)
dispatch(addTokens(tokens))
}

export const fetchMSafeTokens =
(safeInfo: IMSafeInfo) =>
async (dispatch: Dispatch, getState: () => AppReduxState): Promise<void> => {
let listTokens: any[] = []
const cw20Tokens = safeInfo.assets.CW20.asset.map((asset) => ({
name: asset.asset_info.data.name,
decimals: asset.asset_info.data.decimals,
symbol: asset.asset_info.data.symbol,
address: asset.contract_address,
_id: asset['_id'],
}))
const listSafeTokens = [...(safeInfo?.balance || []), ...cw20Tokens]
const state = getState()
const safe = safeByAddressSelector(state, safeInfo.address)
if (safeInfo?.balance) {
const listChain = getChains()
const tokenDetailsListData = await getTokenDetail()
const tokenDetailsList = await tokenDetailsListData.json()
listTokens = [...tokenDetailsList['ibc'], ...tokenDetailsList['cw20']]
const importedConfig =
safe?.coinConfig?.filter((c) => {
if (c.isAddedToken) {
return !listTokens.some((t) => t.address === c.address)
async (dispatch: Dispatch, getState: () => AppReduxState): Promise<void> => {
let listTokens: any[] = []
const cw20Tokens = safeInfo.assets.CW20.asset.map((asset) => ({
name: asset.asset_info.data.name,
decimals: asset.asset_info.data.decimals,
symbol: asset.asset_info.data.symbol,
address: asset.contract_address,
_id: asset['_id'],
}))
const listSafeTokens = [...(safeInfo?.balance || []), ...cw20Tokens]
const state = getState()
const safe = safeByAddressSelector(state, safeInfo.address)
if (safeInfo?.balance) {
const listChain = getChains()
const tokenDetailsListData = await getTokenDetail()
const tokenDetailsList = await tokenDetailsListData.json()
listTokens = [...tokenDetailsList['ibc'], ...tokenDetailsList['cw20']]
const importedConfig =
safe?.coinConfig?.filter((c) => {
if (c.isAddedToken) {
return !listTokens.some((t) => t.address === c.address)
}
return false
}) || []
listTokens = [...listTokens, ...importedConfig]
const filteredListTokens = listTokens.map((token) => {
const isExist = listSafeTokens.some((t) => t.denom === token.minCoinDenom || t.address === token.address)
return { ...token, enable: isExist }
})
const chainInfo: any = listChain.find((x: any) => x.internalChainId === safeInfo?.internalChainId)
const nativeTokenData = safeInfo.balance?.find((balance) => balance.denom == chainInfo.denom)
const balances: any[] = []
if (nativeTokenData) {
const nativeToken = {
tokenBalance: `${humanReadableValue(
+nativeTokenData?.amount > 0 ? nativeTokenData?.amount : 0,
chainInfo.nativeCurrency.decimals,
)}`,
tokenAddress: '0000000000000000000000000000000000000000',
decimals: chainInfo.nativeCurrency.decimals,
logoUri: chainInfo.nativeCurrency.logoUri,
name: chainInfo.nativeCurrency.name,
symbol: chainInfo.nativeCurrency.symbol,
denom: chainInfo.denom,
type: 'native',
enable: true,
}
return false
}) || []
listTokens = [...listTokens, ...importedConfig]
const filteredListTokens = listTokens.map((token) => {
const isExist = listSafeTokens.some((t) => t.denom === token.minCoinDenom || t.address === token.address)
return { ...token, enable: isExist }
})
const chainInfo: any = listChain.find((x: any) => x.internalChainId === safeInfo?.internalChainId)
const nativeTokenData = safeInfo.balance?.find((balance) => balance.denom == chainInfo.denom)
const balances: any[] = []
if (nativeTokenData) {
const nativeToken = {
tokenBalance: `${humanReadableValue(
+nativeTokenData?.amount > 0 ? nativeTokenData?.amount : 0,
chainInfo.nativeCurrency.decimals,
)}`,
tokenAddress: '0000000000000000000000000000000000000000',
decimals: chainInfo.nativeCurrency.decimals,
logoUri: chainInfo.nativeCurrency.logoUri,
name: chainInfo.nativeCurrency.name,
symbol: chainInfo.nativeCurrency.symbol,
denom: chainInfo.denom,
type: 'native',
enable: true,
balances.push(nativeToken)
const { tokenBalance, ...modifiedNativeToken } = nativeToken;
filteredListTokens.unshift(modifiedNativeToken)
}
balances.push(nativeToken)
filteredListTokens.unshift(nativeToken)
}

const coinConfig = safe?.coinConfig?.length
? filteredListTokens
const coinConfig = safe?.coinConfig?.length
? filteredListTokens
.filter(
(item) =>
!safe?.coinConfig?.some((token) => token.denom === item.denom || token.address === item.address),
)
.concat(safe?.coinConfig)
: filteredListTokens

safeInfo.balance
?.filter((balance) => balance.denom != chainInfo.denom)
.forEach((data: any) => {
const tokenDetail = listTokens.find((token) => token.cosmosDenom == data.minimal_denom)
balances.push({
tokenBalance: `${humanReadableValue(+data?.amount > 0 ? data?.amount : 0, tokenDetail?.decimals || 6)}`,
tokenAddress: tokenDetail?.address,
decimals: tokenDetail?.decimals || 6,
logoUri:
tokenDetail?.icon ||
tokenDetail?.logoUri ||
'https://aura-nw.github.io/token-registry/images/undefined.png',
name: tokenDetail?.name,
symbol: tokenDetail?.coinDenom,
denom: tokenDetail?.minCoinDenom,
cosmosDenom: tokenDetail?.cosmosDenom,
type: 'ibc',
})
})
: filteredListTokens

if (safeInfo.assets.CW20.asset?.length > 0) {
safeInfo.assets.CW20.asset.forEach((data) => {
const tokenDetail = listTokens.find((token) => token.address == data.contract_address)
if (tokenDetail) {
safeInfo.balance
?.filter((balance) => balance.denom != chainInfo.denom)
.forEach((data: any) => {
const tokenDetail = listTokens.find((token) => token.cosmosDenom == data.minimal_denom)
balances.push({
tokenBalance: `${humanReadableValue(+data?.balance > 0 ? data?.balance : 0, tokenDetail?.decimals || 6)}`,
tokenBalance: `${humanReadableValue(+data?.amount > 0 ? data?.amount : 0, tokenDetail?.decimals || 6)}`,
tokenAddress: tokenDetail?.address,
decimals: tokenDetail?.decimals || 6,
name: tokenDetail?.name,
logoUri:
tokenDetail?.icon ||
tokenDetail?.logoUri ||
'https://aura-nw.github.io/token-registry/images/undefined.png',
symbol: tokenDetail?.symbol,
denom: tokenDetail?.symbol,
type: 'CW20',
name: tokenDetail?.name,
symbol: tokenDetail?.coinDenom,
denom: tokenDetail?.minCoinDenom,
cosmosDenom: tokenDetail?.cosmosDenom,
type: 'ibc',
})
} else {
listTokens.forEach((token) => {
if (token.tokenType !== 'ibc' && token.tokenType !== 'native') {
const isTokenInAsset = safeInfo.assets.CW20.asset.some(
(data) => data.contract_address === token.address,
)
if (!isTokenInAsset) {
balances.push({
tokenBalance: `${humanReadableValue(0, tokenDetail?.decimals || 6)}`,
tokenAddress: token?.address,
decimals: token?.decimals || 6,
name: token?.name,
logoUri:
token?.icon || token?.logoUri || 'https://aura-nw.github.io/token-registry/images/undefined.png',
symbol: token?.symbol,
denom: token?.symbol,
type: 'CW20',
})
})

if (safeInfo.assets.CW20.asset?.length > 0) {
safeInfo.assets.CW20.asset.forEach((data) => {
const tokenDetail = listTokens.find((token) => token.address == data.contract_address)
if (tokenDetail) {
balances.push({
tokenBalance: `${humanReadableValue(+data?.balance > 0 ? data?.balance : 0, tokenDetail?.decimals || 6)}`,
tokenAddress: tokenDetail?.address,
decimals: tokenDetail?.decimals || 6,
name: tokenDetail?.name,
logoUri:
tokenDetail?.icon ||
tokenDetail?.logoUri ||
'https://aura-nw.github.io/token-registry/images/undefined.png',
symbol: tokenDetail?.symbol,
denom: tokenDetail?.symbol,
type: 'CW20',
})
} else {
listTokens.forEach((token) => {
if (token.tokenType !== 'ibc' && token.tokenType !== 'native') {
const isTokenInAsset = safeInfo.assets.CW20.asset.some(
(data) => data.contract_address === token.address,
)
if (!isTokenInAsset) {
balances.push({
tokenBalance: `${humanReadableValue(0, tokenDetail?.decimals || 6)}`,
tokenAddress: token?.address,
decimals: token?.decimals || 6,
name: token?.name,
logoUri:
token?.icon || token?.logoUri || 'https://aura-nw.github.io/token-registry/images/undefined.png',
symbol: token?.symbol,
denom: token?.symbol,
type: 'CW20',
})
}
}
}
})
}
})
}
})
}
})
}

const nativeBalance = humanReadableValue(
nativeTokenData?.amount ? nativeTokenData?.amount : '0',
chainInfo.nativeCurrency.decimals,
)
dispatch(
updateSafe({
address: safeInfo.address,
balances,
nativeBalance,
coinConfig,
}),
)
const nativeBalance = humanReadableValue(
nativeTokenData?.amount ? nativeTokenData?.amount : '0',
chainInfo.nativeCurrency.decimals,
)
dispatch(
updateSafe({
address: safeInfo.address,
balances,
nativeBalance,
coinConfig,
}),
)
}
}
}
4 changes: 4 additions & 0 deletions src/pages/Assets/Tokens/ImportTokenPopup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ import { updateSafe } from 'src/logic/safe/store/actions/updateSafe'
import { currentSafeWithNames } from 'src/logic/safe/store/selectors'
import { extractSafeAddress, extractSafeId } from 'src/routes/routes'
import { getDetailToken } from 'src/services'
import { LS_TOKEN_CONFIG } from 'src/utils/constants'
import { isValidAddress } from 'src/utils/isValidAddress'
import { saveToLocalStorage } from 'src/utils/storage/local'
import styled from 'styled-components'

const Wrap = styled.div`
Expand Down Expand Up @@ -63,6 +65,7 @@ type IImportTokenPopup = {
onImport?: () => void
addressContract?: any
}

const ImportTokenPopup = ({ open, onBack, onClose, addressContract, onImport }: IImportTokenPopup) => {
const dispatch = useDispatch()
const [token, setToken] = useState<IToken>(defaultToken)
Expand Down Expand Up @@ -117,6 +120,7 @@ const ImportTokenPopup = ({ open, onBack, onClose, addressContract, onImport }:
newCoinConfig.push(token)
}
}
saveToLocalStorage(LS_TOKEN_CONFIG, newCoinConfig ?? coinConfig)
dispatch(
updateSafe({
address,
Expand Down
Loading