mirror of
https://github.com/apotdevin/thunderhub.git
synced 2025-02-21 14:04:03 +01:00
chore: change pay fn
This commit is contained in:
parent
82b1b220e9
commit
b1f150e6e4
6 changed files with 115 additions and 3 deletions
|
@ -1,5 +1,6 @@
|
||||||
import { randomBytes, createHash } from 'crypto';
|
import { randomBytes, createHash } from 'crypto';
|
||||||
import {
|
import {
|
||||||
|
pay,
|
||||||
payViaRoutes,
|
payViaRoutes,
|
||||||
createInvoice,
|
createInvoice,
|
||||||
decodePaymentRequest,
|
decodePaymentRequest,
|
||||||
|
@ -11,11 +12,18 @@ import { ContextType } from 'server/types/apiTypes';
|
||||||
import { logger } from 'server/helpers/logger';
|
import { logger } from 'server/helpers/logger';
|
||||||
import { requestLimiter } from 'server/helpers/rateLimiter';
|
import { requestLimiter } from 'server/helpers/rateLimiter';
|
||||||
import { getErrorMsg } from 'server/helpers/helpers';
|
import { getErrorMsg } from 'server/helpers/helpers';
|
||||||
import { to } from 'server/helpers/async';
|
import { to, toWithError } from 'server/helpers/async';
|
||||||
import { CreateInvoiceType, DecodedType } from 'server/types/ln-service.types';
|
import { CreateInvoiceType, DecodedType } from 'server/types/ln-service.types';
|
||||||
|
|
||||||
const KEYSEND_TYPE = '5482373484';
|
const KEYSEND_TYPE = '5482373484';
|
||||||
|
|
||||||
|
type PayType = {
|
||||||
|
max_fee: Number;
|
||||||
|
max_paths: Number;
|
||||||
|
request: String;
|
||||||
|
out?: String[];
|
||||||
|
};
|
||||||
|
|
||||||
export const invoiceResolvers = {
|
export const invoiceResolvers = {
|
||||||
Query: {
|
Query: {
|
||||||
getInvoiceStatusChange: async (
|
getInvoiceStatusChange: async (
|
||||||
|
@ -176,6 +184,32 @@ export const invoiceResolvers = {
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
pay: async (
|
||||||
|
_: undefined,
|
||||||
|
{ max_fee, max_paths, out, request }: PayType,
|
||||||
|
context: ContextType
|
||||||
|
) => {
|
||||||
|
const { lnd } = context;
|
||||||
|
const props = {
|
||||||
|
request,
|
||||||
|
max_fee,
|
||||||
|
max_paths,
|
||||||
|
outgoing_channels: out || [],
|
||||||
|
};
|
||||||
|
|
||||||
|
logger.debug('Paying invoice with params: %o', props);
|
||||||
|
|
||||||
|
const [response, error] = await toWithError(pay({ lnd, ...props }));
|
||||||
|
|
||||||
|
if (error) {
|
||||||
|
logger.error('Error paying invoice: %o', error);
|
||||||
|
throw new Error(getErrorMsg(error));
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.debug('Paid invoice: %o', response);
|
||||||
|
return true;
|
||||||
|
},
|
||||||
payViaRoute: async (_: undefined, params: any, context: ContextType) => {
|
payViaRoute: async (_: undefined, params: any, context: ContextType) => {
|
||||||
await requestLimiter(context.ip, 'payViaRoute');
|
await requestLimiter(context.ip, 'payViaRoute');
|
||||||
|
|
||||||
|
|
|
@ -165,6 +165,12 @@ export const mutationTypes = gql`
|
||||||
includePrivate: Boolean
|
includePrivate: Boolean
|
||||||
): newInvoiceType
|
): newInvoiceType
|
||||||
circularRebalance(route: String!): Boolean
|
circularRebalance(route: String!): Boolean
|
||||||
|
pay(
|
||||||
|
max_fee: Int!
|
||||||
|
max_paths: Int!
|
||||||
|
out: [String]
|
||||||
|
request: String!
|
||||||
|
): Boolean
|
||||||
bosPay(
|
bosPay(
|
||||||
max_fee: Int!
|
max_fee: Int!
|
||||||
max_paths: Int!
|
max_paths: Int!
|
||||||
|
|
51
src/graphql/mutations/__generated__/pay.generated.tsx
generated
Normal file
51
src/graphql/mutations/__generated__/pay.generated.tsx
generated
Normal file
|
@ -0,0 +1,51 @@
|
||||||
|
/* eslint-disable */
|
||||||
|
import * as Types from '../../types';
|
||||||
|
|
||||||
|
import { gql } from '@apollo/client';
|
||||||
|
import * as Apollo from '@apollo/client';
|
||||||
|
const defaultOptions = {}
|
||||||
|
export type PayMutationVariables = Types.Exact<{
|
||||||
|
max_fee: Types.Scalars['Int'];
|
||||||
|
max_paths: Types.Scalars['Int'];
|
||||||
|
out?: Types.Maybe<Array<Types.Maybe<Types.Scalars['String']>> | Types.Maybe<Types.Scalars['String']>>;
|
||||||
|
request: Types.Scalars['String'];
|
||||||
|
}>;
|
||||||
|
|
||||||
|
|
||||||
|
export type PayMutation = { __typename?: 'Mutation', pay?: Types.Maybe<boolean> };
|
||||||
|
|
||||||
|
|
||||||
|
export const PayDocument = gql`
|
||||||
|
mutation Pay($max_fee: Int!, $max_paths: Int!, $out: [String], $request: String!) {
|
||||||
|
pay(max_fee: $max_fee, max_paths: $max_paths, out: $out, request: $request)
|
||||||
|
}
|
||||||
|
`;
|
||||||
|
export type PayMutationFn = Apollo.MutationFunction<PayMutation, PayMutationVariables>;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* __usePayMutation__
|
||||||
|
*
|
||||||
|
* To run a mutation, you first call `usePayMutation` within a React component and pass it any options that fit your needs.
|
||||||
|
* When your component renders, `usePayMutation` returns a tuple that includes:
|
||||||
|
* - A mutate function that you can call at any time to execute the mutation
|
||||||
|
* - An object with fields that represent the current status of the mutation's execution
|
||||||
|
*
|
||||||
|
* @param baseOptions options that will be passed into the mutation, supported options are listed on: https://www.apollographql.com/docs/react/api/react-hooks/#options-2;
|
||||||
|
*
|
||||||
|
* @example
|
||||||
|
* const [payMutation, { data, loading, error }] = usePayMutation({
|
||||||
|
* variables: {
|
||||||
|
* max_fee: // value for 'max_fee'
|
||||||
|
* max_paths: // value for 'max_paths'
|
||||||
|
* out: // value for 'out'
|
||||||
|
* request: // value for 'request'
|
||||||
|
* },
|
||||||
|
* });
|
||||||
|
*/
|
||||||
|
export function usePayMutation(baseOptions?: Apollo.MutationHookOptions<PayMutation, PayMutationVariables>) {
|
||||||
|
const options = {...defaultOptions, ...baseOptions}
|
||||||
|
return Apollo.useMutation<PayMutation, PayMutationVariables>(PayDocument, options);
|
||||||
|
}
|
||||||
|
export type PayMutationHookResult = ReturnType<typeof usePayMutation>;
|
||||||
|
export type PayMutationResult = Apollo.MutationResult<PayMutation>;
|
||||||
|
export type PayMutationOptions = Apollo.BaseMutationOptions<PayMutation, PayMutationVariables>;
|
12
src/graphql/mutations/pay.ts
Normal file
12
src/graphql/mutations/pay.ts
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
import { gql } from '@apollo/client';
|
||||||
|
|
||||||
|
export const PAY = gql`
|
||||||
|
mutation Pay(
|
||||||
|
$max_fee: Int!
|
||||||
|
$max_paths: Int!
|
||||||
|
$out: [String]
|
||||||
|
$request: String!
|
||||||
|
) {
|
||||||
|
pay(max_fee: $max_fee, max_paths: $max_paths, out: $out, request: $request)
|
||||||
|
}
|
||||||
|
`;
|
|
@ -217,6 +217,7 @@ export type Mutation = {
|
||||||
lnUrlWithdraw: Scalars['String'];
|
lnUrlWithdraw: Scalars['String'];
|
||||||
logout: Scalars['Boolean'];
|
logout: Scalars['Boolean'];
|
||||||
openChannel?: Maybe<OpenChannelType>;
|
openChannel?: Maybe<OpenChannelType>;
|
||||||
|
pay?: Maybe<Scalars['Boolean']>;
|
||||||
payViaRoute?: Maybe<Scalars['Boolean']>;
|
payViaRoute?: Maybe<Scalars['Boolean']>;
|
||||||
removePeer?: Maybe<Scalars['Boolean']>;
|
removePeer?: Maybe<Scalars['Boolean']>;
|
||||||
sendMessage?: Maybe<Scalars['Int']>;
|
sendMessage?: Maybe<Scalars['Int']>;
|
||||||
|
@ -389,6 +390,14 @@ export type MutationOpenChannelArgs = {
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
export type MutationPayArgs = {
|
||||||
|
max_fee: Scalars['Int'];
|
||||||
|
max_paths: Scalars['Int'];
|
||||||
|
out?: Maybe<Array<Maybe<Scalars['String']>>>;
|
||||||
|
request: Scalars['String'];
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
export type MutationPayViaRouteArgs = {
|
export type MutationPayViaRouteArgs = {
|
||||||
id: Scalars['String'];
|
id: Scalars['String'];
|
||||||
route: Scalars['String'];
|
route: Scalars['String'];
|
||||||
|
|
|
@ -2,7 +2,6 @@ import { toast } from 'react-toastify';
|
||||||
import { getErrorContent } from 'src/utils/error';
|
import { getErrorContent } from 'src/utils/error';
|
||||||
import { ColorButton } from 'src/components/buttons/colorButton/ColorButton';
|
import { ColorButton } from 'src/components/buttons/colorButton/ColorButton';
|
||||||
import { useEffect, useState } from 'react';
|
import { useEffect, useState } from 'react';
|
||||||
import { useBosPayMutation } from 'src/graphql/mutations/__generated__/bosPay.generated';
|
|
||||||
import { InputWithDeco } from 'src/components/input/InputWithDeco';
|
import { InputWithDeco } from 'src/components/input/InputWithDeco';
|
||||||
import { ChannelSelect } from 'src/components/select/specific/ChannelSelect';
|
import { ChannelSelect } from 'src/components/select/specific/ChannelSelect';
|
||||||
import { useDecodeRequestLazyQuery } from 'src/graphql/queries/__generated__/decodeRequest.generated';
|
import { useDecodeRequestLazyQuery } from 'src/graphql/queries/__generated__/decodeRequest.generated';
|
||||||
|
@ -12,6 +11,7 @@ import { Camera } from 'react-feather';
|
||||||
import Modal from 'src/components/modal/ReactModal';
|
import Modal from 'src/components/modal/ReactModal';
|
||||||
import dynamic from 'next/dynamic';
|
import dynamic from 'next/dynamic';
|
||||||
import { LoadingCard } from 'src/components/loading/LoadingCard';
|
import { LoadingCard } from 'src/components/loading/LoadingCard';
|
||||||
|
import { usePayMutation } from 'src/graphql/mutations/__generated__/pay.generated';
|
||||||
import { SingleLine, Separation } from '../../../../components/generic/Styled';
|
import { SingleLine, Separation } from '../../../../components/generic/Styled';
|
||||||
|
|
||||||
const QRCodeReader = dynamic(() => import('src/components/qrReader'), {
|
const QRCodeReader = dynamic(() => import('src/components/qrReader'), {
|
||||||
|
@ -39,7 +39,7 @@ export const Pay: React.FC<PayProps> = ({ predefinedRequest, payCallback }) => {
|
||||||
onError: () => toast.error('Error decoding invoice'),
|
onError: () => toast.error('Error decoding invoice'),
|
||||||
});
|
});
|
||||||
|
|
||||||
const [pay, { loading }] = useBosPayMutation({
|
const [pay, { loading }] = usePayMutation({
|
||||||
onCompleted: () => {
|
onCompleted: () => {
|
||||||
payCallback && payCallback();
|
payCallback && payCallback();
|
||||||
toast.success('Payment Sent');
|
toast.success('Payment Sent');
|
||||||
|
|
Loading…
Add table
Reference in a new issue