Skip to content

Commit

Permalink
fix: update send gatway transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
rick23p committed Dec 18, 2024
1 parent 72deefd commit 3ada817
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import { Token } from '@gobob/bob-sdk/dist/gateway/types';
import {
useAccount,
useBalance,
useSendGatewayTransaction,
} from '@gobob/sats-wagmi';
import { useAccount, useBalance } from '@gobob/sats-wagmi';

import React, { FC, useEffect, useState } from 'react';

Expand All @@ -16,6 +12,7 @@ import { BOB_CHAIN_ID } from '../../../../../config/chains';
import { useCacheCall } from '../../../../../hooks';
import { useAccount as useEvmAccount } from '../../../../../hooks/useAccount';
import { bobGateway } from '../../BobGateway.utils';
import { useSendGatewayTransaction } from '../../hooks/useSendGatewayTransaction';

export const BobGatewayDeposit: FC = () => {
const [amount, setAmount] = useState('');
Expand All @@ -31,7 +28,6 @@ export const BobGatewayDeposit: FC = () => {
sendGatewayTransaction,
} = useSendGatewayTransaction({
toChain: 'bob',
strategyAddress: '0xBA67A0a0C2dd790182D1954B4C9788f9Ae43e604',
});

useEffect(() => {
Expand All @@ -51,9 +47,10 @@ export const BobGatewayDeposit: FC = () => {
}

const params = {
toToken: 'uniBTC',
toToken: 'wBTC',
evmAddress: account,
value: BigInt(parseUnits(amount, 8).toString()),
strategyAddress: '0xBA67A0a0C2dd790182D1954B4C9788f9Ae43e604',
};

console.log(params);
Expand Down
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 };

0 comments on commit 3ada817

Please sign in to comment.