-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathusePerpsPaymentToken.ts
More file actions
36 lines (31 loc) · 1.16 KB
/
usePerpsPaymentToken.ts
File metadata and controls
36 lines (31 loc) · 1.16 KB
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
import { Hex } from '@metamask/utils';
import { useCallback } from 'react';
import { AssetType } from '../../../Views/confirmations/types/token';
import { useTransactionPayToken } from '../../../Views/confirmations/hooks/pay/useTransactionPayToken';
import Engine from '../../../../core/Engine';
import { parsePayWithToken } from '../utils/parsePayWithToken';
export type PerpsPaymentTokenInput =
| AssetType
| { address: string; chainId: string }
| null;
export interface UsePerpsPaymentTokenResult {
onPaymentTokenChange: (token: PerpsPaymentTokenInput) => void;
}
export function usePerpsPaymentToken(): UsePerpsPaymentTokenResult {
const { setPayToken } = useTransactionPayToken();
const onPaymentTokenChange = useCallback(
(token: PerpsPaymentTokenInput) => {
const parsed =
token === null || token === undefined ? null : parsePayWithToken(token);
Engine.context.PerpsController?.setSelectedPaymentToken?.(parsed);
if (parsed !== null) {
setPayToken({
address: parsed.address as Hex,
chainId: parsed.chainId as Hex,
});
}
},
[setPayToken],
);
return { onPaymentTokenChange };
}