-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
107 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
apps/frontend/src/app/5_pages/BobGateway/hooks/useSendGatewayTransaction.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import { GatewayQuoteParams, GatewaySDK } from '@gobob/bob-sdk'; | ||
import { useAccount } from '@gobob/sats-wagmi'; | ||
import { | ||
Optional, | ||
useMutation, | ||
UseMutationOptions, | ||
} from '@tanstack/react-query'; | ||
|
||
type SendGatewayTransactionParams = { | ||
toToken: string; | ||
evmAddress: string; | ||
value: bigint; | ||
strategyAddress?: string; | ||
}; | ||
|
||
type UseSendGatewayTransactionProps = Omit< | ||
{ gatewaySDK?: GatewaySDK } & Omit< | ||
Optional< | ||
GatewayQuoteParams, | ||
| 'fromUserAddress' | ||
| 'toUserAddress' | ||
| 'amount' | ||
| 'toToken' | ||
| 'fromChain' | ||
| 'fromToken' | ||
>, | ||
'toChain' | ||
> & { toChain: 'bob' | 'bob-sepolia' } & UseMutationOptions< | ||
string | undefined, | ||
Error, | ||
SendGatewayTransactionParams, | ||
unknown | ||
>, | ||
'mutationKey' | 'mutationFn' | ||
>; | ||
|
||
const useSendGatewayTransaction = ({ | ||
gatewaySDK, | ||
toChain = 'bob', | ||
...props | ||
}: UseSendGatewayTransactionProps) => { | ||
const { | ||
address: btcAddress, | ||
publicKey: btcPublicKey, | ||
connector, | ||
} = useAccount(); | ||
|
||
const { mutate, mutateAsync, ...result } = useMutation({ | ||
mutationKey: ['sats-send-gateway-transaction', btcAddress], | ||
mutationFn: async ({ | ||
toToken, | ||
evmAddress, | ||
value, | ||
strategyAddress, | ||
}: SendGatewayTransactionParams) => { | ||
if (!connector) return undefined; | ||
if (!btcAddress) return undefined; | ||
|
||
const gatewayClient = gatewaySDK || new GatewaySDK(toChain); | ||
|
||
const params = { | ||
...props, | ||
fromChain: props.fromChain || 'bitcoin', | ||
fromToken: props.fromToken || 'BTC', | ||
toChain, | ||
toToken, | ||
gasRefill: 0, | ||
fromUserAddress: btcAddress, | ||
fromUserPublicKey: btcPublicKey, | ||
toUserAddress: evmAddress, | ||
amount: Number(value), | ||
}; | ||
const quote = await gatewayClient.getQuote(params); | ||
|
||
if (strategyAddress) { | ||
quote.strategyAddress = strategyAddress; | ||
} | ||
console.log({ | ||
quote, | ||
}); | ||
|
||
const { uuid, psbtBase64 } = await gatewayClient.startOrder( | ||
quote, | ||
params, | ||
); | ||
|
||
if (!psbtBase64) throw new Error('No psbt'); | ||
|
||
const bitcoinTxHex = await connector.signAllInputs(psbtBase64); | ||
|
||
return await gatewayClient.finalizeOrder(uuid, bitcoinTxHex); | ||
}, | ||
...props, | ||
}); | ||
|
||
return { | ||
...result, | ||
sendGatewayTransaction: mutate, | ||
sendGatewayTransactionAsync: mutateAsync, | ||
}; | ||
}; | ||
|
||
export { useSendGatewayTransaction }; |