Skip to content

Commit

Permalink
refactor: rename to useSendGatewayTransaction
Browse files Browse the repository at this point in the history
Signed-off-by: Gregory Hill <[email protected]>
  • Loading branch information
gregdhill committed Sep 6, 2024
1 parent 174f67c commit 4b6391e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
2 changes: 1 addition & 1 deletion packages/sats-wagmi/src/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ export * from './useFeeRate';
export * from './useFeeEstimate';
export * from './useSendTransaction';
export * from './useSignMessage';
export * from './useSendToGateway';
export * from './useSendGatewayTransaction';
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,34 @@ import { GatewayQuoteParams, GatewaySDK } from '@gobob/bob-sdk';

import { useAccount } from './useAccount';

type SendToGatewayParams = {
type SendGatewayTransactionParams = {
toToken: string;
evmAddress: string;
value: bigint;
};

type UseSendToGatewayProps = Omit<
type UseSendGatewayTransactionProps = Omit<
{ gatewaySDK?: GatewaySDK } & Omit<
Optional<
GatewayQuoteParams,
'fromUserAddress' | 'toUserAddress' | 'amount' | 'toToken' | 'fromChain' | 'fromToken'
>,
'toChain'
> & { toChain: 'bob' | 'bob-sepolia' } & UseMutationOptions<string | undefined, Error, SendToGatewayParams, unknown>,
> & { toChain: 'bob' | 'bob-sepolia' } & UseMutationOptions<
string | undefined,
Error,
SendGatewayTransactionParams,
unknown
>,
'mutationKey' | 'mutationFn'
>;

const useSendToGateway = ({ gatewaySDK, toChain = 'bob', ...props }: UseSendToGatewayProps) => {
const useSendGatewayTransaction = ({ gatewaySDK, toChain = 'bob', ...props }: UseSendGatewayTransactionProps) => {
const { address: btcAddress, publicKey: btcPublicKey, connector } = useAccount();

const { mutate, mutateAsync, ...result } = useMutation({
mutationKey: ['sats-gateway', btcAddress],
mutationFn: async ({ toToken, evmAddress, value }: SendToGatewayParams) => {
mutationFn: async ({ toToken, evmAddress, value }: SendGatewayTransactionParams) => {
if (!connector) return undefined;
if (!btcAddress) return undefined;

Expand Down Expand Up @@ -58,9 +63,9 @@ const useSendToGateway = ({ gatewaySDK, toChain = 'bob', ...props }: UseSendToGa

return {
...result,
sendToGateway: mutate,
sendToGatewayAsync: mutateAsync
sendGatewayTransaction: mutate,
sendGatewayTransactionAsync: mutateAsync
};
};

export { useSendToGateway };
export { useSendGatewayTransaction };
11 changes: 8 additions & 3 deletions playgrounds/vite-react/src/Gateway.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
import type { FormEvent } from 'react';

import { useSendToGateway } from '@gobob/sats-wagmi';
import { useSendGatewayTransaction } from '@gobob/sats-wagmi';
import { type Hex, parseUnits } from 'viem';

function Gateway() {
const { data: hash, error, isPending, sendToGateway } = useSendToGateway({ toChain: 'bob-sepolia' });
const {
data: hash,
error,
isPending,
sendGatewayTransaction
} = useSendGatewayTransaction({ toChain: 'bob-sepolia' });

async function submit(e: FormEvent<HTMLFormElement>) {
e.preventDefault();
const formData = new FormData(e.target as HTMLFormElement);
const evmAddress = formData.get('address') as Hex;
const value = formData.get('value') as string;

sendToGateway({ toToken: 'tBTC', evmAddress, value: parseUnits(value, 8) });
sendGatewayTransaction({ toToken: 'tBTC', evmAddress, value: parseUnits(value, 8) });
}

return (
Expand Down

0 comments on commit 4b6391e

Please sign in to comment.