Generate wallet mutation from fields
This commit is contained in:
		
							parent
							
								
									0a0085fe82
								
							
						
					
					
						commit
						00f78daadc
					
				@ -1,4 +1,22 @@
 | 
				
			|||||||
import { gql } from 'graphql-tag'
 | 
					import { gql } from 'graphql-tag'
 | 
				
			||||||
 | 
					import { SERVER_WALLET_DEFS } from '@/components/wallet'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function walletTypeDefs () {
 | 
				
			||||||
 | 
					  const typeDefs = SERVER_WALLET_DEFS.map(
 | 
				
			||||||
 | 
					    (w) => {
 | 
				
			||||||
 | 
					      let args = 'id: ID, '
 | 
				
			||||||
 | 
					      args += w.fields.map(f => {
 | 
				
			||||||
 | 
					        let arg = `${f.name}: String`
 | 
				
			||||||
 | 
					        if (!f.optional) {
 | 
				
			||||||
 | 
					          arg += '!'
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        return arg
 | 
				
			||||||
 | 
					      }).join(', ')
 | 
				
			||||||
 | 
					      args += ', settings: AutowithdrawSettings!'
 | 
				
			||||||
 | 
					      return `${w.server.resolverName}(${args}): Boolean`
 | 
				
			||||||
 | 
					    }).join('\n')
 | 
				
			||||||
 | 
					  return typeDefs
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export default gql`
 | 
					export default gql`
 | 
				
			||||||
  extend type Query {
 | 
					  extend type Query {
 | 
				
			||||||
@ -19,7 +37,7 @@ export default gql`
 | 
				
			|||||||
    sendToLnAddr(addr: String!, amount: Int!, maxFee: Int!, comment: String, identifier: Boolean, name: String, email: String): Withdrawl!
 | 
					    sendToLnAddr(addr: String!, amount: Int!, maxFee: Int!, comment: String, identifier: Boolean, name: String, email: String): Withdrawl!
 | 
				
			||||||
    cancelInvoice(hash: String!, hmac: String!): Invoice!
 | 
					    cancelInvoice(hash: String!, hmac: String!): Invoice!
 | 
				
			||||||
    dropBolt11(id: ID): Withdrawl
 | 
					    dropBolt11(id: ID): Withdrawl
 | 
				
			||||||
    upsertWalletLND(id: ID, socket: String!, macaroon: String!, cert: String, settings: AutowithdrawSettings!): Boolean
 | 
					    ${walletTypeDefs()}
 | 
				
			||||||
    upsertWalletCLN(id: ID, socket: String!, rune: String!, cert: String, settings: AutowithdrawSettings!): Boolean
 | 
					    upsertWalletCLN(id: ID, socket: String!, rune: String!, cert: String, settings: AutowithdrawSettings!): Boolean
 | 
				
			||||||
    upsertWalletLNAddr(id: ID, address: String!, settings: AutowithdrawSettings!): Boolean
 | 
					    upsertWalletLNAddr(id: ID, address: String!, settings: AutowithdrawSettings!): Boolean
 | 
				
			||||||
    removeWallet(id: ID!): Boolean
 | 
					    removeWallet(id: ID!): Boolean
 | 
				
			||||||
 | 
				
			|||||||
@ -9,13 +9,15 @@ import * as lnbits from '@/components/wallet/lnbits'
 | 
				
			|||||||
import * as nwc from '@/components/wallet/nwc'
 | 
					import * as nwc from '@/components/wallet/nwc'
 | 
				
			||||||
import * as lnc from '@/components/wallet/lnc'
 | 
					import * as lnc from '@/components/wallet/lnc'
 | 
				
			||||||
import * as lnd from '@/components/wallet/lnd'
 | 
					import * as lnd from '@/components/wallet/lnd'
 | 
				
			||||||
import { useApolloClient, useQuery } from '@apollo/client'
 | 
					import { gql, useApolloClient, useQuery } from '@apollo/client'
 | 
				
			||||||
import { REMOVE_WALLET, WALLET_BY_TYPE } from '@/fragments/wallet'
 | 
					import { REMOVE_WALLET, WALLET_BY_TYPE } from '@/fragments/wallet'
 | 
				
			||||||
import { autowithdrawInitial } from '../autowithdraw-shared'
 | 
					import { autowithdrawInitial } from '../autowithdraw-shared'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// wallet definitions
 | 
					// wallet definitions
 | 
				
			||||||
export const WALLET_DEFS = [lnbits, nwc, lnc, lnd]
 | 
					export const WALLET_DEFS = [lnbits, nwc, lnc, lnd]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export const SERVER_WALLET_DEFS = WALLET_DEFS.filter(w => w.server)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export const Status = {
 | 
					export const Status = {
 | 
				
			||||||
  Initialized: 'Initialized',
 | 
					  Initialized: 'Initialized',
 | 
				
			||||||
  Enabled: 'Enabled',
 | 
					  Enabled: 'Enabled',
 | 
				
			||||||
@ -177,8 +179,9 @@ function useServerConfig (wallet) {
 | 
				
			|||||||
    ...config
 | 
					    ...config
 | 
				
			||||||
  }) => {
 | 
					  }) => {
 | 
				
			||||||
    try {
 | 
					    try {
 | 
				
			||||||
 | 
					      const mutation = generateMutation(wallet)
 | 
				
			||||||
      return await client.mutate({
 | 
					      return await client.mutate({
 | 
				
			||||||
        mutation: wallet.server.mutation,
 | 
					        mutation,
 | 
				
			||||||
        variables: {
 | 
					        variables: {
 | 
				
			||||||
          id: walletId,
 | 
					          id: walletId,
 | 
				
			||||||
          ...config,
 | 
					          ...config,
 | 
				
			||||||
@ -211,6 +214,28 @@ function useServerConfig (wallet) {
 | 
				
			|||||||
  return [config, saveConfig, clearConfig]
 | 
					  return [config, saveConfig, clearConfig]
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function generateMutation (wallet) {
 | 
				
			||||||
 | 
					  const { resolverName } = wallet.server
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  let headerArgs = '$id: ID, '
 | 
				
			||||||
 | 
					  headerArgs += wallet.fields.map(f => {
 | 
				
			||||||
 | 
					    let arg = `$${f.name}: String`
 | 
				
			||||||
 | 
					    if (!f.optional) {
 | 
				
			||||||
 | 
					      arg += '!'
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    return arg
 | 
				
			||||||
 | 
					  }).join(', ')
 | 
				
			||||||
 | 
					  headerArgs += ', $settings: AutowithdrawSettings!'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  let inputArgs = 'id: $id, '
 | 
				
			||||||
 | 
					  inputArgs += wallet.fields.map(f => `${f.name}: $${f.name}`).join(', ')
 | 
				
			||||||
 | 
					  inputArgs += ', settings: $settings'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  return gql`mutation ${resolverName}(${headerArgs}) {
 | 
				
			||||||
 | 
					    ${resolverName}(${inputArgs})
 | 
				
			||||||
 | 
					  }`
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export function getWalletByName (name) {
 | 
					export function getWalletByName (name) {
 | 
				
			||||||
  return WALLET_DEFS.find(def => def.name === name)
 | 
					  return WALLET_DEFS.find(def => def.name === name)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -1,4 +1,3 @@
 | 
				
			|||||||
import { UPSERT_WALLET_LND } from '@/fragments/wallet'
 | 
					 | 
				
			||||||
import { LNDAutowithdrawSchema } from '@/lib/validate'
 | 
					import { LNDAutowithdrawSchema } from '@/lib/validate'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export const name = 'lnd'
 | 
					export const name = 'lnd'
 | 
				
			||||||
@ -44,5 +43,5 @@ export const schema = LNDAutowithdrawSchema
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
export const server = {
 | 
					export const server = {
 | 
				
			||||||
  walletType: 'LND',
 | 
					  walletType: 'LND',
 | 
				
			||||||
  mutation: UPSERT_WALLET_LND
 | 
					  resolverName: 'upsertWalletLND'
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -107,13 +107,6 @@ mutation upsertWalletLNAddr($id: ID, $address: String!, $settings: AutowithdrawS
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
`
 | 
					`
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export const UPSERT_WALLET_LND =
 | 
					 | 
				
			||||||
gql`
 | 
					 | 
				
			||||||
mutation upsertWalletLND($id: ID, $socket: String!, $macaroon: String!, $cert: String, $settings: AutowithdrawSettings!) {
 | 
					 | 
				
			||||||
  upsertWalletLND(id: $id, socket: $socket, macaroon: $macaroon, cert: $cert, settings: $settings)
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
`
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
export const UPSERT_WALLET_CLN =
 | 
					export const UPSERT_WALLET_CLN =
 | 
				
			||||||
gql`
 | 
					gql`
 | 
				
			||||||
mutation upsertWalletCLN($id: ID, $socket: String!, $rune: String!, $cert: String, $settings: AutowithdrawSettings!) {
 | 
					mutation upsertWalletCLN($id: ID, $socket: String!, $rune: String!, $cert: String, $settings: AutowithdrawSettings!) {
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user