Skip to content
This repository was archived by the owner on Mar 11, 2024. It is now read-only.

Commit

Permalink
Merge pull request #706 from alephium/extract-stuff-to-sdk
Browse files Browse the repository at this point in the history
Extract common code with explorer to SDK
  • Loading branch information
nop33 authored May 11, 2023
2 parents b74f7df + c437832 commit 148e22d
Show file tree
Hide file tree
Showing 19 changed files with 34 additions and 60 deletions.
16 changes: 9 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"electron-updater": "^5.3.0"
},
"devDependencies": {
"@alephium/sdk": "0.7.0-rc.1",
"@alephium/sdk": "0.7.0-rc.2",
"@alephium/token-list": "^0.0.7",
"@alephium/walletconnect-provider": "^0.11.0",
"@alephium/web3": "^0.11.0",
Expand Down
8 changes: 6 additions & 2 deletions src/api/addresses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ export const fetchAddressesData = async (addressHashes: AddressHash[]): Promise<
const results = []

for (const addressHash of addressHashes) {
const details = await client.explorer.addresses.getAddressesAddress(addressHash)
const balances = await client.explorer.addresses.getAddressesAddressBalance(addressHash)
const txNumber = await client.explorer.addresses.getAddressesAddressTotalTransactions(addressHash)
const transactions = await client.explorer.addresses.getAddressesAddressTransactions(addressHash, { page: 1 })
const mempoolTransactions = await client.explorer.addresses.getAddressesAddressMempoolTransactions(addressHash)
const tokenIds = await client.explorer.addresses.getAddressesAddressTokens(addressHash)
Expand All @@ -41,7 +42,10 @@ export const fetchAddressesData = async (addressHashes: AddressHash[]): Promise<

results.push({
hash: addressHash,
details,
details: {
...balances,
txNumber
},
transactions,
mempoolTransactions,
tokens
Expand Down
3 changes: 1 addition & 2 deletions src/components/Amount.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@ You should have received a copy of the GNU Lesser General Public License
along with the library. If not, see <http://www.gnu.org/licenses/>.
*/

import { formatAmountForDisplay, formatFiatAmountForDisplay } from '@alephium/sdk'
import { convertToPositive, formatAmountForDisplay, formatFiatAmountForDisplay } from '@alephium/sdk'
import styled from 'styled-components'

import { useAppSelector } from '@/hooks/redux'
import { convertToPositive } from '@/utils/misc'

interface AmountProps {
value?: bigint | number
Expand Down
2 changes: 1 addition & 1 deletion src/components/AssetBadge.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ You should have received a copy of the GNU Lesser General Public License
along with the library. If not, see <http://www.gnu.org/licenses/>.
*/

import { Asset } from '@alephium/sdk'
import styled from 'styled-components'

import AssetLogo from '@/components/AssetLogo'
import { useAppSelector } from '@/hooks/redux'
import { selectAssetInfoById } from '@/storage/assets/assetsSelectors'
import { Asset } from '@/types/assets'

interface AssetBadgeProps {
assetId: Asset['id']
Expand Down
2 changes: 1 addition & 1 deletion src/components/Inputs/SelectOptionAsset.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ You should have received a copy of the GNU Lesser General Public License
along with the library. If not, see <http://www.gnu.org/licenses/>.
*/

import { Asset } from '@alephium/sdk'
import styled from 'styled-components'

import Amount from '@/components/Amount'
import AssetLogo from '@/components/AssetLogo'
import HashEllipsed from '@/components/HashEllipsed'
import SelectOptionItemContent from '@/components/Inputs/SelectOptionItemContent'
import Truncate from '@/components/Truncate'
import { Asset } from '@/types/assets'

interface SelectOptionAssetProps {
asset: Asset
Expand Down
2 changes: 1 addition & 1 deletion src/components/TransactionList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ You should have received a copy of the GNU Lesser General Public License
along with the library. If not, see <http://www.gnu.org/licenses/>.
*/

import { Asset } from '@alephium/sdk'
import { ChevronRight } from 'lucide-react'
import { ReactNode, useCallback, useEffect, useMemo, useRef, useState } from 'react'
import { useTranslation } from 'react-i18next'
Expand Down Expand Up @@ -44,7 +45,6 @@ import {
makeSelectAddressesPendingTransactions
} from '@/storage/transactions/transactionsSelectors'
import { AddressHash } from '@/types/addresses'
import { Asset } from '@/types/assets'
import { AddressConfirmedTransaction, Direction } from '@/types/transactions'
import { getTransactionInfo } from '@/utils/transactions'

Expand Down
3 changes: 1 addition & 2 deletions src/contexts/walletconnect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ You should have received a copy of the GNU Lesser General Public License
along with the library. If not, see <http://www.gnu.org/licenses/>.
*/

import { getHumanReadableError } from '@alephium/sdk'
import { AssetAmount, getHumanReadableError } from '@alephium/sdk'
import { ALPH } from '@alephium/token-list'
import { ChainInfo, parseChain, PROVIDER_NAMESPACE, RelayMethod } from '@alephium/walletconnect-provider'
import {
Expand All @@ -40,7 +40,6 @@ import SendModalCallContract from '@/modals/SendModals/CallContract'
import SendModalDeployContract from '@/modals/SendModals/DeployContract'
import { selectAllAddresses } from '@/storage/addresses/addressesSelectors'
import { walletConnectPairingFailed } from '@/storage/dApps/dAppActions'
import { AssetAmount } from '@/types/assets'
import {
CallContractTxData,
DappTxData,
Expand Down
4 changes: 2 additions & 2 deletions src/modals/SendModals/AssetAmountsInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ You should have received a copy of the GNU Lesser General Public License
along with the library. If not, see <http://www.gnu.org/licenses/>.
*/

import { fromHumanReadableAmount, getNumberOfDecimals, toHumanReadableAmount } from '@alephium/sdk'
import { Asset, fromHumanReadableAmount, getNumberOfDecimals, toHumanReadableAmount } from '@alephium/sdk'
import { ALPH } from '@alephium/token-list'
import { MIN_UTXO_SET_AMOUNT } from '@alephium/web3'
import { MoreVertical, Plus } from 'lucide-react'
Expand All @@ -42,7 +42,7 @@ import ModalPortal from '@/modals/ModalPortal'
import InputsSection from '@/modals/SendModals/InputsSection'
import { makeSelectAddressesAssets } from '@/storage/addresses/addressesSelectors'
import { Address } from '@/types/addresses'
import { Asset, AssetAmountInputType } from '@/types/assets'
import { AssetAmountInputType } from '@/types/assets'
import { onEnterOrSpace } from '@/utils/misc'

interface AssetAmountsInputProps {
Expand Down
2 changes: 1 addition & 1 deletion src/modals/SendModals/CheckAmountsBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ You should have received a copy of the GNU Lesser General Public License
along with the library. If not, see <http://www.gnu.org/licenses/>.
*/

import { AssetAmount } from '@alephium/sdk'
import { ALPH } from '@alephium/token-list'
import { Fragment } from 'react'
import styled from 'styled-components'
Expand All @@ -25,7 +26,6 @@ import AssetLogo from '@/components/AssetLogo'
import Box from '@/components/Box'
import HorizontalDivider from '@/components/Dividers/HorizontalDivider'
import { useAppSelector } from '@/hooks/redux'
import { AssetAmount } from '@/types/assets'
import { getTransactionAssetAmounts } from '@/utils/transactions'

interface CheckAmountsBoxProps {
Expand Down
2 changes: 1 addition & 1 deletion src/pages/UnlockedWallet/TransfersPage/FiltersPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with the library. If not, see <http://www.gnu.org/licenses/>.
*/
import { Asset } from '@alephium/sdk'
import { colord } from 'colord'
import { useEffect, useMemo } from 'react'
import { useTranslation } from 'react-i18next'
Expand All @@ -30,7 +31,6 @@ import { makeSelectAddressesAssets, selectAllAddresses } from '@/storage/address
import { selectIsLoadingAssetsInfo } from '@/storage/assets/assetsSelectors'
import { appHeaderHeightPx } from '@/style/globalStyles'
import { Address } from '@/types/addresses'
import { Asset } from '@/types/assets'
import { directionOptions } from '@/utils/transactions'

interface FiltersPanelProps {
Expand Down
2 changes: 1 addition & 1 deletion src/pages/UnlockedWallet/TransfersPage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ You should have received a copy of the GNU Lesser General Public License
along with the library. If not, see <http://www.gnu.org/licenses/>.
*/

import { Asset } from '@alephium/sdk'
import { colord } from 'colord'
import { motion } from 'framer-motion'
import { map } from 'lodash'
Expand All @@ -36,7 +37,6 @@ import UnlockedWalletPage from '@/pages/UnlockedWallet/UnlockedWalletPage'
import { selectAllAddresses, selectDefaultAddress } from '@/storage/addresses/addressesSelectors'
import { transfersPageInfoMessageClosed } from '@/storage/global/globalActions'
import { walletSidebarWidthPx } from '@/style/globalStyles'
import { Asset } from '@/types/assets'
import { links } from '@/utils/links'
import { directionOptions } from '@/utils/transactions'

Expand Down
2 changes: 1 addition & 1 deletion src/storage/addresses/addressesSelectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ You should have received a copy of the GNU Lesser General Public License
along with the library. If not, see <http://www.gnu.org/licenses/>.
*/

import { Asset, TokenDisplayBalances } from '@alephium/sdk'
import { ALPH } from '@alephium/token-list'
import { createSelector } from '@reduxjs/toolkit'

import { addressesAdapter, contactsAdapter } from '@/storage/addresses/addressesAdapters'
import { selectAllAssetsInfo } from '@/storage/assets/assetsSelectors'
import { RootState } from '@/storage/store'
import { AddressHash } from '@/types/addresses'
import { Asset, TokenDisplayBalances } from '@/types/assets'
import { filterAddressesWithoutAssets } from '@/utils/addresses'

export const {
Expand Down
3 changes: 1 addition & 2 deletions src/types/addresses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@ You should have received a copy of the GNU Lesser General Public License
along with the library. If not, see <http://www.gnu.org/licenses/>.
*/

import { AddressKeyPair } from '@alephium/sdk'
import { AddressKeyPair, TokenBalances } from '@alephium/sdk'
import { explorer } from '@alephium/web3'
import { EntityState } from '@reduxjs/toolkit'

import { TokenBalances } from '@/types/assets'
import { TimeInMs } from '@/types/numbers'
import { PendingTransaction } from '@/types/transactions'

Expand Down
20 changes: 1 addition & 19 deletions src/types/assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,6 @@ You should have received a copy of the GNU Lesser General Public License
along with the library. If not, see <http://www.gnu.org/licenses/>.
*/

import { TokenInfo } from '@alephium/token-list'
import { Optional } from '@alephium/web3'
import { explorer } from '@alephium/web3'

// Used in Redux, amounts need to be in string format
export type TokenBalances = explorer.AddressBalance & { id: explorer.Token['id'] }

// Same as TokenBalances, but amounts are in BigInt, useful for display purposes
export type TokenDisplayBalances = Omit<TokenBalances, 'balance' | 'lockedBalance'> & {
balance: bigint
lockedBalance: bigint
}

export type Asset = TokenDisplayBalances & Optional<TokenInfo, 'symbol' | 'name'>

export type AssetAmount = { id: Asset['id']; amount?: bigint }
import { AssetAmount } from '@alephium/sdk'

export type AssetAmountInputType = AssetAmount & { amountInput?: string }

export type TransactionInfoAsset = Optional<Omit<Asset, 'balance' | 'lockedBalance'>, 'decimals'> &
Required<AssetAmount>
3 changes: 1 addition & 2 deletions src/types/transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@ You should have received a copy of the GNU Lesser General Public License
along with the library. If not, see <http://www.gnu.org/licenses/>.
*/

import { TransactionDirection, TransactionInfoType } from '@alephium/sdk'
import { AssetAmount, TransactionDirection, TransactionInfoAsset, TransactionInfoType } from '@alephium/sdk'
import { explorer, node } from '@alephium/web3'

import { Address, AddressHash } from '@/types/addresses'
import { AssetAmount, TransactionInfoAsset } from '@/types/assets'

export enum TxType {
TRANSFER,
Expand Down
2 changes: 1 addition & 1 deletion src/utils/addresses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ You should have received a copy of the GNU Lesser General Public License
along with the library. If not, see <http://www.gnu.org/licenses/>.
*/

import { AssetAmount } from '@alephium/sdk'
import { TokenInfo } from '@alephium/token-list'
import { ALPH } from '@alephium/token-list'
import { explorer } from '@alephium/web3'
import { Dictionary } from '@reduxjs/toolkit'

import { Address, AddressSettings } from '@/types/addresses'
import { AssetAmount } from '@/types/assets'
import { AddressTransaction, PendingTransaction } from '@/types/transactions'
import { getRandomLabelColor } from '@/utils/colors'

Expand Down
3 changes: 0 additions & 3 deletions src/utils/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,6 @@ export const onTabPress = (event: KeyboardEvent, callback: () => void) => {
callback()
}

export const convertToPositive = (num: bigint): bigint => (num < 0 ? num * BigInt(-1) : num)
export const convertToNegative = (num: bigint): bigint => (num > 0 ? num * BigInt(-1) : num)

export function removeItemFromArray<T>(array: T[], index: number) {
const newArray = [...array]
newArray.splice(index, 1)
Expand Down
13 changes: 3 additions & 10 deletions src/utils/transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@ along with the library. If not, see <http://www.gnu.org/licenses/>.
*/

import {
AssetAmount,
calcTxAmountsDeltaForAddress,
convertToNegative,
getDirection,
isConsolidationTx,
isSwap,
TransactionDirection,
TransactionInfoType
} from '@alephium/sdk'
Expand All @@ -32,7 +35,6 @@ import { SelectOption } from '@/components/Inputs/Select'
import i18n from '@/i18n'
import { store } from '@/storage/store'
import { Address } from '@/types/addresses'
import { AssetAmount } from '@/types/assets'
import { TranslationKey } from '@/types/i18next'
import {
AddressPendingTransaction,
Expand All @@ -41,7 +43,6 @@ import {
TransactionInfo,
TransactionTimePeriod
} from '@/types/transactions'
import { convertToNegative } from '@/utils/misc'

export const isAmountWithinRange = (amount: bigint, maxAmount: bigint): boolean =>
amount >= MIN_UTXO_SET_AMOUNT && amount <= maxAmount
Expand Down Expand Up @@ -193,11 +194,3 @@ export const getTransactionInfo = (tx: AddressTransaction, showInternalInflows?:
lockTime
}
}

const isSwap = (alphAmout: bigint, tokensAmount: Required<AssetAmount>[]) => {
const allAmounts = [alphAmout, ...tokensAmount.map((tokenAmount) => tokenAmount.amount)]
const allAmountsArePositive = allAmounts.every((amount) => amount >= 0)
const allAmountsAreNegative = allAmounts.every((amount) => amount <= 0)

return !allAmountsArePositive && !allAmountsAreNegative
}

0 comments on commit 148e22d

Please sign in to comment.