/
use-register-soulname.ts
141 lines (120 loc) · 3.52 KB
/
use-register-soulname.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import { useAsyncFn } from 'react-use';
import { CreateSoulNameResult } from '@masa-finance/masa-sdk';
import { useSoulNamesPurchase } from '../../../../masa/use-soulnames-purchase';
import { useIdentityPurchase } from '../../../../masa/use-identity-purchase';
import { useConfig } from '../../../../base-provider';
import { useIdentity } from '../../../../masa/use-identity';
import { useCreateSoulnameModal } from './CreateSoulnameProvider';
// const errorMessages = {
// UNPREDICTABLE_GAS_LIMIT:
// 'You do not have sufficient funds in you wallet. Please add funds to your wallet and try again',
// ACTION_REJECTED: 'Transaction rejected by the user.',
// };
export const useRegisterSoulname = ({
onMintSuccess,
onMintError,
onRegisterFinish,
}: Partial<{
onMintSuccess?: (result: CreateSoulNameResult) => void;
onMintError?: () => void;
onRegisterFinish?: () => void;
}>) => {
const { soulNameStyle } = useConfig();
const { identity } = useIdentity();
const { purchaseSoulName } = useSoulNamesPurchase();
const { purchaseIdentityWithSoulName } = useIdentityPurchase();
const {
soulname,
registrationPeriod,
paymentMethod,
// soulNameError,
// registrationPrice,
} = useCreateSoulnameModal();
const [
{
value: hasRegisteredSoulname,
loading: isRegisteringSoulname,
error: errorRegisterSoulname,
},
onRegisterSoulname,
] = useAsyncFn(async () => {
try {
if (identity && identity.identityId) {
const result = await purchaseSoulName(
soulname,
registrationPeriod,
paymentMethod,
soulNameStyle
);
if (result instanceof Error || !result || !result.success) {
onMintError?.();
if (result instanceof Error) throw result;
throw new Error('Unexpected error');
}
if (result && result.success) {
console.log({ result });
onMintSuccess?.(result);
}
onRegisterFinish?.();
return result;
}
} catch (error_: unknown) {
console.log('ERROR registerSoulname', error_);
onMintError?.();
throw error_ as Error;
}
try {
const result = await purchaseIdentityWithSoulName(
paymentMethod,
soulname,
registrationPeriod,
soulNameStyle
);
if (result instanceof Error || !result || !result.success) {
onMintError?.();
if (result instanceof Error) throw result;
throw new Error('Unexpected error');
}
if (result && result.success) {
onMintSuccess?.(result);
}
onRegisterFinish?.();
return result;
} catch (error: unknown) {
// const errorObject = error as {
// code: string;
// message: string;
// };
// const subtitle =
// (errorMessages?.[errorObject.code] as string | undefined) ??
// ('Unknown error' as string);
// console.error(`Minting failed! ${errorObject.message}`);
console.log('ERROR registerSoulname catch2', error);
onMintError?.();
throw error as Error;
// return {
// title: '',
// subtitle,
// };
}
}, [
onRegisterFinish,
soulname,
identity,
paymentMethod,
registrationPeriod,
// registrationPrice,
// soulNameError,
purchaseIdentityWithSoulName,
purchaseSoulName,
soulNameStyle,
onMintSuccess,
onMintError,
]);
return {
onRegisterSoulname,
hasRegisteredSoulname,
isRegisteringSoulname,
errorRegisterSoulname,
};
};