feat: onchain mutations

This commit is contained in:
AP 2019-12-09 07:48:17 +01:00
parent 058bb84ab8
commit 5de9502f58
4 changed files with 123 additions and 4 deletions

View file

@ -41,12 +41,12 @@ export const getErrorMsg = (error: any[]): string => {
const code = error[0];
const msg = error[1];
let details = ''
let details = '';
if (error.length > 2) {
if (error[2].err) {
details = error[2].err.details
} else if(error[2].details) {
details = error[2].details
details = error[2].err.details;
} else if (error[2].details) {
details = error[2].details;
}
}

View file

@ -0,0 +1,19 @@
import {
GraphQLObjectType,
GraphQLString,
GraphQLInt,
GraphQLBoolean,
} from 'graphql';
export const SendToType = new GraphQLObjectType({
name: 'sendToType',
fields: () => {
return {
confirmationCount: { type: GraphQLString },
id: { type: GraphQLString },
isConfirmed: { type: GraphQLBoolean },
isOutgoing: { type: GraphQLBoolean },
tokens: { type: GraphQLInt },
};
},
});

View file

@ -0,0 +1,36 @@
import { createChainAddress } from 'ln-service';
import { logger } from '../../../helpers/logger';
import { requestLimiter } from '../../../helpers/rateLimiter';
import { GraphQLNonNull, GraphQLString, GraphQLBoolean } from 'graphql';
import { getErrorMsg, getAuthLnd } from '../../../helpers/helpers';
interface AddressProps {
address: string;
}
export const createAddress = {
type: GraphQLString,
args: {
auth: { type: new GraphQLNonNull(GraphQLString) },
nested: { type: GraphQLBoolean },
},
resolve: async (root: any, params: any, context: any) => {
await requestLimiter(context.ip, params, 'createAddress', 1, '1s');
const lnd = getAuthLnd(params.auth);
const format = params.nested ? 'np2wpkh' : 'p2wpkh';
try {
const address: AddressProps = await createChainAddress({
lnd: lnd,
is_unused: true,
format,
});
return address.address;
} catch (error) {
logger.error('Error creating address: %o', error);
throw new Error(getErrorMsg(error));
}
},
};

View file

@ -0,0 +1,64 @@
import { sendToChainAddress } from 'ln-service';
import { logger } from '../../../helpers/logger';
import { requestLimiter } from '../../../helpers/rateLimiter';
import {
GraphQLNonNull,
GraphQLString,
GraphQLBoolean,
GraphQLInt,
} from 'graphql';
import { getErrorMsg, getAuthLnd } from '../../../helpers/helpers';
import { SendToType } from '../../../schemaTypes/mutation.ts/onchain/sentToAddress';
interface SendProps {
confirmation_count: number;
id: string;
is_confirmed: boolean;
is_outgoing: boolean;
tokens: number;
}
export const sentToAddress = {
type: SendToType,
args: {
auth: { type: new GraphQLNonNull(GraphQLString) },
address: { type: new GraphQLNonNull(GraphQLString) },
tokens: { type: new GraphQLNonNull(GraphQLInt) },
fee: { type: GraphQLInt },
target: { type: GraphQLInt },
sendAll: { type: GraphQLBoolean },
},
resolve: async (root: any, params: any, context: any) => {
await requestLimiter(context.ip, params, 'createAddress', 1, '1s');
const lnd = getAuthLnd(params.auth);
const props = params.fee
? { fee_tokens_per_vbyte: params.fee }
: params.target
? { target_confirmations: params.target }
: {};
const sendAll = params.sendAll ? { is_send_all: true } : {};
try {
const send: SendProps = await sendToChainAddress({
lnd: lnd,
address: params.address,
tokens: params.tokens,
...props,
...sendAll,
});
return {
confirmationCount: send.confirmation_count,
id: send.id,
isConfirmed: send.is_confirmed,
isOutgoing: send.is_outgoing,
tokens: send.tokens,
};
} catch (error) {
logger.error('Error creating address: %o', error);
throw new Error(getErrorMsg(error));
}
},
};