forked from DA0-DA0/dao-dao-ui
-
Notifications
You must be signed in to change notification settings - Fork 1
/
useTokenSwapStatusInfoForContract.ts
107 lines (98 loc) · 3.16 KB
/
useTokenSwapStatusInfoForContract.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import { useRecoilValue } from 'recoil'
import {
CwTokenSwapSelectors,
genericTokenSelector,
} from '@dao-dao/state/recoil'
import { TokenSwapStatusProps, TokenType } from '@dao-dao/types'
import { convertMicroDenomToDenomWithDecimals } from '@dao-dao/utils'
import { EntityDisplay } from '../components'
export interface UseTokenSwapStatusInfoForContractOptions {
contractAddress: string
chainId?: string
selfPartyAddress: string
}
// Returns info for a given token swap, with the parties identified between self
// and counter. Also collects the metadata into props for the TokenSwapStatus
// stateless component. This hook is used in the FundTokenSwap and
// WithdrawTokenSwap stateful action components.
export const useTokenSwapStatusInfoForContract = ({
contractAddress,
chainId,
selfPartyAddress,
}: UseTokenSwapStatusInfoForContractOptions) => {
const tokenSwapStatus = useRecoilValue(
CwTokenSwapSelectors.statusSelector({
contractAddress,
chainId,
params: [],
})
)
const selfParty =
tokenSwapStatus.counterparty_one.address === selfPartyAddress
? tokenSwapStatus.counterparty_one
: tokenSwapStatus.counterparty_two
const counterparty =
tokenSwapStatus.counterparty_one.address === selfPartyAddress
? tokenSwapStatus.counterparty_two
: tokenSwapStatus.counterparty_one
const selfPartyTokenInfo = useRecoilValue(
genericTokenSelector({
chainId,
type: 'cw20' in selfParty.promise ? TokenType.Cw20 : TokenType.Native,
denomOrAddress:
'cw20' in selfParty.promise
? selfParty.promise.cw20.contract_addr
: selfParty.promise.native.denom,
})
)
const selfPartyAmount = convertMicroDenomToDenomWithDecimals(
'cw20' in selfParty.promise
? selfParty.promise.cw20.amount
: selfParty.promise.native.amount,
selfPartyTokenInfo.decimals
)
const counterpartyTokenInfo = useRecoilValue(
genericTokenSelector({
chainId,
type: 'cw20' in counterparty.promise ? TokenType.Cw20 : TokenType.Native,
denomOrAddress:
'cw20' in counterparty.promise
? counterparty.promise.cw20.contract_addr
: counterparty.promise.native.denom,
})
)
const counterpartyAmount = convertMicroDenomToDenomWithDecimals(
'cw20' in counterparty.promise
? counterparty.promise.cw20.amount
: counterparty.promise.native.amount,
counterpartyTokenInfo.decimals
)
const props: TokenSwapStatusProps = {
selfParty: {
address: selfParty.address,
amount: selfPartyAmount,
decimals: selfPartyTokenInfo.decimals,
symbol: selfPartyTokenInfo.symbol,
tokenLogoUrl: selfPartyTokenInfo.imageUrl,
provided: selfParty.provided,
},
counterparty: {
address: counterparty.address,
amount: counterpartyAmount,
decimals: counterpartyTokenInfo.decimals,
symbol: counterpartyTokenInfo.symbol,
tokenLogoUrl: counterpartyTokenInfo.imageUrl,
provided: counterparty.provided,
},
EntityDisplay,
}
return {
selfParty,
selfPartyTokenInfo,
selfPartyAmount,
counterparty,
counterpartyTokenInfo,
counterpartyAmount,
props,
}
}