Skip to content

Commit

Permalink
fix: mf-4475 ito swap (#10397)
Browse files Browse the repository at this point in the history
* fix: ito swap

* chore: remove annotation

* Revert "chore: remove annotation"

This reverts commit aa82fb4.
  • Loading branch information
zhouhanseng committed Aug 15, 2023
1 parent a07d345 commit 1710f6d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 30 deletions.
13 changes: 8 additions & 5 deletions packages/plugins/ITO/src/SiteAdaptor/SwapDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { useQualificationVerify } from './hooks/useQualificationVerify.js'
import { useSwapCallback } from './hooks/useSwapCallback.js'
import { useI18N } from '../locales/index.js'
import { useSiteAdaptorContext } from '@masknet/plugin-infra/dom'
import { useAsyncFn } from 'react-use'

const useStyles = makeStyles()((theme) => ({
button: {},
Expand Down Expand Up @@ -159,16 +160,18 @@ export function SwapDialog(props: SwapDialogProps) {
payload.contract_address,
)

const [{ loading: isSwapping }, swapCallback] = useSwapCallback(
const swapCallback = useSwapCallback(
payload,
swapAmount.toFixed(),
swapToken ? swapToken : { address: NATIVE_TOKEN_ADDRESS },
qualificationInfo?.isQualificationHasLucky,
)
const onSwap = useCallback(async () => {
const receipt = await swapCallback()
if (typeof receipt?.transactionHash === 'string') {
const { to_value } = (receipt.events?.SwapSuccess?.returnValues ?? { to_value: 0 }) as {
const [{ loading: isSwapping }, onSwap] = useAsyncFn(async () => {
const result = await swapCallback()
const { receipt, events } = result ?? {}

if (typeof receipt?.transactionHash === 'string' && events) {
const { to_value } = (events?.SwapSuccess?.returnValues ?? { to_value: 0 }) as {
to_value: string
}

Expand Down
54 changes: 29 additions & 25 deletions packages/plugins/ITO/src/SiteAdaptor/hooks/useSwapCallback.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { useAsyncFn } from 'react-use'
import type { TransactionReceipt } from 'web3-core'
import Web3Utils from 'web3-utils'
import type { ITO } from '@masknet/web3-contracts/types/ITO.js'
import type { ITO2 } from '@masknet/web3-contracts/types/ITO2.js'
import type { Qualification } from '@masknet/web3-contracts/types/Qualification.js'
import type { Qualification2 } from '@masknet/web3-contracts/types/Qualification2.js'
import type { PayableTx } from '@masknet/web3-contracts/types/types.js'
import { type ChainId, SchemaType, TransactionEventType, useITOConstants } from '@masknet/web3-shared-evm'
import {
type ChainId,
SchemaType,
useITOConstants,
ContractTransaction,
decodeEvents,
type TransactionReceipt,
} from '@masknet/web3-shared-evm'
import {
isSameAddress,
isPositive,
Expand All @@ -21,13 +25,19 @@ import type { JSON_PayloadInMask } from '../../types.js'
import { useITO_Contract } from './useITO_Contract.js'
import { useQualificationContract } from './useQualificationContract.js'
import { checkAvailability } from '../utils/checkAvailability.js'
import { Web3 } from '@masknet/web3-providers'
import { useCallback } from 'react'
import type { EventLog } from 'web3-core'

export function useSwapCallback(
payload: JSON_PayloadInMask,
total: string,
token: Partial<FungibleToken<ChainId, SchemaType>>,
isQualificationHasLucky = false,
) {
): () => Promise<
| { receipt: TransactionReceipt | null; events: { [eventName: string]: EventLog | undefined } | undefined }
| undefined
> {
const { account, chainId } = useChainContext<NetworkPluginID.PLUGIN_EVM>()
const { ITO_CONTRACT_ADDRESS } = useITOConstants(chainId)
const { contract: ITO_Contract, version } = useITO_Contract(chainId, payload.contract_address)
Expand All @@ -37,7 +47,7 @@ export function useSwapCallback(
payload.contract_address,
)

return useAsyncFn(async () => {
return useCallback(async () => {
if (!ITO_Contract || !qualificationContract || !payload) return

const { pid, password } = payload
Expand Down Expand Up @@ -110,33 +120,27 @@ export function useSwapCallback(
const value = toFixed(token.schema === SchemaType.Native ? total : 0)
const config = {
from: account,
gas: isQualificationHasLucky
? 200000
gas: (isQualificationHasLucky
? '200000'
: await (version === 1
? (ITO_Contract as ITO).methods.swap(...swapParamsV1)
: (ITO_Contract as ITO2).methods.swap(...swapParamsV2)
).estimateGas({
from: account,
value,
}),
})) as string,
value,
}

// send transaction and wait for hash
return new Promise<TransactionReceipt>((resolve, reject) => {
const onSucceed = (_: number, receipt: TransactionReceipt) => {
resolve(receipt)
}
const onFailed = (error: Error) => {
reject(error)
}
;(version === 1
? (ITO_Contract as ITO).methods.swap(...swapParamsV1)
: (ITO_Contract as ITO2).methods.swap(...swapParamsV2)
)
.send(config as PayableTx)
.on(TransactionEventType.CONFIRMATION, onSucceed)
.on(TransactionEventType.ERROR, onFailed)
})
const tx = await new ContractTransaction(ITO_Contract).fillAll(
ITO_Contract.methods.swap(...swapParamsV2),
config,
)

const hash = await Web3.sendTransaction(tx, { chainId })
const receipt = await Web3.getTransactionReceipt(hash, { chainId })
const events = receipt ? decodeEvents(ITO_Contract.options.jsonInterface, receipt) : undefined

return { receipt, events }
}, [ITO_Contract, chainId, qualificationContract, account, payload, total, token.address, isQualificationHasLucky])
}

0 comments on commit 1710f6d

Please sign in to comment.