-
Notifications
You must be signed in to change notification settings - Fork 306
/
useCreateCallback.tsx
191 lines (169 loc) · 6.29 KB
/
useCreateCallback.tsx
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import { useCallback } from 'react'
import { useAsync, useAsyncFn } from 'react-use'
import * as web3_utils from /* webpackDefer: true */ 'web3-utils'
import { omit } from 'lodash-es'
import type { NetworkPluginID } from '@masknet/shared-base'
import { useChainContext } from '@masknet/web3-hooks-base'
import type { HappyRedPacketV4 } from '@masknet/web3-contracts/types/HappyRedPacketV4.js'
import { type FungibleToken, isLessThan, toFixed } from '@masknet/web3-shared-base'
import {
type ChainId,
SchemaType,
useTokenConstants,
decodeEvents,
ContractTransaction,
type GasConfig,
} from '@masknet/web3-shared-evm'
import { EVMWeb3 } from '@masknet/web3-providers'
import { useRedPacketContract } from './useRedPacketContract.js'
export interface RedPacketSettings {
shares: number
duration: number
isRandom: boolean
total: string
name: string
message: string
token?: FungibleToken<ChainId, SchemaType.Native | SchemaType.ERC20>
}
export type ParamsObjType = {
publicKey: string
shares: number
isRandom: boolean
duration: number
seed: string
message: string
name: string
tokenType: number
tokenAddress: string
total: string
token?: FungibleToken<ChainId, SchemaType.Native | SchemaType.ERC20>
}
export function checkParams(paramsObj: ParamsObjType) {
if (isLessThan(paramsObj.total, paramsObj.shares)) {
throw new Error('At least [number of lucky drops] tokens to your lucky drop.')
}
if (paramsObj.shares <= 0) {
throw new Error('At least 1 person should be able to claim the lucky drop.')
}
return true
}
export type MethodParameters = Parameters<HappyRedPacketV4['methods']['create_red_packet']>
interface CreateParams {
gas: string | undefined
params: MethodParameters
paramsObj: ParamsObjType
gasError: Error | null
}
function useCreateParamsCallback(
expectedChainId: ChainId,
redPacketSettings: RedPacketSettings | undefined,
version: number,
publicKey: string,
) {
const { account, chainId } = useChainContext<NetworkPluginID.PLUGIN_EVM>({ chainId: expectedChainId })
const { NATIVE_TOKEN_ADDRESS } = useTokenConstants(chainId)
const redPacketContract = useRedPacketContract(chainId, version)
const getCreateParams = useCallback(async (): Promise<CreateParams | null> => {
if (!redPacketSettings || !redPacketContract) return null
const { duration, isRandom, message, name, shares, total, token } = redPacketSettings
const seed = Math.random().toString()
const tokenType = token!.schema === SchemaType.Native ? 0 : 1
const tokenAddress = token!.schema === SchemaType.Native ? NATIVE_TOKEN_ADDRESS : token!.address
if (!tokenAddress) {
if (process.env.NODE_ENV === 'development' && !NATIVE_TOKEN_ADDRESS) {
console.error(
'Not native token address for chain %s. Do you forget to configure it in token.json file?',
token!.chainId,
)
}
return null
}
const paramsObj: ParamsObjType = {
publicKey,
shares,
isRandom,
duration,
seed: web3_utils.sha3(seed)!,
message,
name,
tokenType,
tokenAddress,
total,
token,
}
try {
checkParams(paramsObj)
} catch {
return null
}
const params = Object.values(omit(paramsObj, ['token'])) as MethodParameters
let gasError: Error | null = null
const value = toFixed(paramsObj.token?.schema === SchemaType.Native ? total : 0)
const gas = await (redPacketContract as HappyRedPacketV4).methods
.create_red_packet(...params)
.estimateGas({ from: account, value })
.catch((error: Error) => {
gasError = error
})
return { gas: gas ? toFixed(gas) : undefined, params, paramsObj, gasError }
}, [redPacketSettings, account, redPacketContract])
return getCreateParams
}
export function useCreateParams(
expectedChainId: ChainId,
redPacketSettings: RedPacketSettings,
version: number,
publicKey: string,
) {
const getCreateParams = useCreateParamsCallback(expectedChainId, redPacketSettings, version, publicKey)
return useAsync(() => getCreateParams(), [JSON.stringify(redPacketSettings), version, publicKey])
}
export function useCreateCallback(
expectedChainId: ChainId,
redPacketSettings: RedPacketSettings,
version: number,
publicKey: string,
gasOption?: GasConfig,
) {
const { account, chainId } = useChainContext<NetworkPluginID.PLUGIN_EVM>({ chainId: expectedChainId })
const redPacketContract = useRedPacketContract(chainId, version)
const getCreateParams = useCreateParamsCallback(expectedChainId, redPacketSettings, version, publicKey)
return useAsyncFn(async () => {
const { token } = redPacketSettings
const createParams = await getCreateParams()
if (!token || !redPacketContract || !createParams) return
const { gas, params, paramsObj, gasError } = createParams
if (gasError) return
try {
checkParams(paramsObj)
} catch (error) {
return
}
// estimate gas and compose transaction
const tx = await new ContractTransaction(redPacketContract).fillAll(
redPacketContract.methods.create_red_packet(...params),
{
from: account,
value: toFixed(token.schema === SchemaType.Native ? paramsObj.total : 0),
gas,
chainId,
...gasOption,
},
)
const hash = await EVMWeb3.sendTransaction(tx, {
paymentToken: gasOption?.gasCurrency,
chainId,
gasOptionType: gasOption?.gasOptionType,
})
const receipt = await EVMWeb3.getTransactionReceipt(hash, { chainId })
if (receipt) {
const events = decodeEvents(redPacketContract.options.jsonInterface, receipt.logs)
return {
hash,
receipt,
events,
}
}
return { hash, receipt }
}, [account, redPacketContract, redPacketSettings, gasOption, chainId])
}