chore: change pay fn

This commit is contained in:
apotdevin 2021-09-11 20:09:36 +02:00
parent 82b1b220e9
commit b1f150e6e4
No known key found for this signature in database
GPG key ID: 4403F1DFBE779457
6 changed files with 115 additions and 3 deletions

View file

@ -1,5 +1,6 @@
import { randomBytes, createHash } from 'crypto';
import {
pay,
payViaRoutes,
createInvoice,
decodePaymentRequest,
@ -11,11 +12,18 @@ import { ContextType } from 'server/types/apiTypes';
import { logger } from 'server/helpers/logger';
import { requestLimiter } from 'server/helpers/rateLimiter';
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';
const KEYSEND_TYPE = '5482373484';
type PayType = {
max_fee: Number;
max_paths: Number;
request: String;
out?: String[];
};
export const invoiceResolvers = {
Query: {
getInvoiceStatusChange: async (
@ -176,6 +184,32 @@ export const invoiceResolvers = {
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) => {
await requestLimiter(context.ip, 'payViaRoute');

View file

@ -165,6 +165,12 @@ export const mutationTypes = gql`
includePrivate: Boolean
): newInvoiceType
circularRebalance(route: String!): Boolean
pay(
max_fee: Int!
max_paths: Int!
out: [String]
request: String!
): Boolean
bosPay(
max_fee: Int!
max_paths: Int!

View 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>;

View 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)
}
`;

View file

@ -217,6 +217,7 @@ export type Mutation = {
lnUrlWithdraw: Scalars['String'];
logout: Scalars['Boolean'];
openChannel?: Maybe<OpenChannelType>;
pay?: Maybe<Scalars['Boolean']>;
payViaRoute?: Maybe<Scalars['Boolean']>;
removePeer?: Maybe<Scalars['Boolean']>;
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 = {
id: Scalars['String'];
route: Scalars['String'];

View file

@ -2,7 +2,6 @@ import { toast } from 'react-toastify';
import { getErrorContent } from 'src/utils/error';
import { ColorButton } from 'src/components/buttons/colorButton/ColorButton';
import { useEffect, useState } from 'react';
import { useBosPayMutation } from 'src/graphql/mutations/__generated__/bosPay.generated';
import { InputWithDeco } from 'src/components/input/InputWithDeco';
import { ChannelSelect } from 'src/components/select/specific/ChannelSelect';
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 dynamic from 'next/dynamic';
import { LoadingCard } from 'src/components/loading/LoadingCard';
import { usePayMutation } from 'src/graphql/mutations/__generated__/pay.generated';
import { SingleLine, Separation } from '../../../../components/generic/Styled';
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'),
});
const [pay, { loading }] = useBosPayMutation({
const [pay, { loading }] = usePayMutation({
onCompleted: () => {
payCallback && payCallback();
toast.success('Payment Sent');