Browse Source

accounting table, query types

utxos
Evan Feenstra 4 years ago
parent
commit
f3d9f4449e
  1. 2
      src/constants.ts
  2. 5
      src/controllers/index.ts
  3. 79
      src/controllers/queries.ts
  4. 3
      src/models/index.ts
  5. 38
      src/models/ts/accounting.ts

2
src/constants.ts

@ -57,6 +57,8 @@ const constants = {
"heartbeat_confirmation": 27,
"keysend": 28, // no e2e
"boost": 29,
"query": 30,
"query_response": 31
},
network_types: {
"lightning": 0,

5
src/controllers/index.ts

@ -13,6 +13,7 @@ import * as subcriptions from './subscriptions'
import * as uploads from './uploads'
import * as confirmations from './confirmations'
import * as actions from './api'
import * as queries from './queries'
import { checkTag } from '../utils/gitinfo'
import * as timers from '../utils/timers'
import * as builtInBots from '../builtin'
@ -96,6 +97,8 @@ export async function set(app) {
app.get('/info', details.getNodeInfo)
app.get('/route', details.checkRoute)
app.get('/query/onchain_address', queries.queryOnchainAddres)
app.post('/action', actions.processAction)
app.get('/bots', bots.getBots)
app.post('/bot', bots.createBot)
@ -152,4 +155,6 @@ export const ACTIONS = {
[msgtypes.heartbeat]: confirmations.receiveHeartbeat,
[msgtypes.heartbeat_confirmation]: confirmations.receiveHeartbeatConfirmation,
[msgtypes.boost]: messages.receiveBoost,
[msgtypes.query]: queries.receiveQuery,
[msgtypes.query_response]: queries.receiveQueryResponse,
}

79
src/controllers/queries.ts

@ -0,0 +1,79 @@
import { success, failure } from '../utils/res'
import { models } from '../models'
import * as network from '../network'
import constants from '../constants'
import * as short from 'short-uuid'
type QueryType = 'onchain_address'
export interface Query {
type: QueryType
uuid: string
result?: string
}
let queries: { [k: string]: Query } = {}
const gameb = '023d70f2f76d283c6c4e58109ee3a2816eb9d8feb40b23d62469060a2b2867b77f'
export async function queryOnchainAddres(req, res) {
const uuid = short.generate()
const owner = await models.Contact.findOne({ where: { isOwner: true } })
const query:Query = {
type:'onchain_address',
uuid
}
const opts = {
amt: constants.min_sat_amount,
dest: gameb,
data: <network.Msg>{
type: constants.message_types.query,
message: {
content: JSON.stringify(query)
},
sender: { pub_key: owner.publicKey }
}
}
try {
await network.signAndSend(opts)
} catch (e) {
failure(res, e)
return
}
let i = 0
let interval = setInterval(() => {
if (i >= 15) {
clearInterval(interval)
delete queries[uuid]
failure(res, 'no response received')
return
}
if (queries[uuid]) {
success(res, queries[uuid].result)
clearInterval(interval)
delete queries[uuid]
return
}
i++
}, 1000)
}
export const receiveQuery = async (payload) => {
const dat = payload.content || payload
const sender_pub_key = dat.sender.pub_key
const content = dat.message.content
}
export const receiveQueryResponse = async (payload) => {
const dat = payload.content || payload
// const sender_pub_key = dat.sender.pub_key
const content = dat.message.content
try {
const q:Query = JSON.parse(content)
queries[q.uuid] = q
} catch(e) {
console.log("=> ERROR receiveQueryResponse,",e)
}
}

3
src/models/index.ts

@ -11,6 +11,7 @@ import Timer from './ts/timer'
import Bot from './ts/bot'
import ChatBot from './ts/chatBot'
import BotMember from './ts/botMember'
import Accounting from './ts/accounting'
const env = process.env.NODE_ENV || 'development';
const config = require(path.join(__dirname, '../../config/config.json'))[env]
@ -18,7 +19,7 @@ const config = require(path.join(__dirname, '../../config/config.json'))[env]
const sequelize = new Sequelize({
...config,
logging: process.env.SQL_LOG === 'true' ? console.log : false,
models: [Chat, Contact, Invite, Message, Subscription, MediaKey, ChatMember, Timer, Bot, ChatBot, BotMember]
models: [Chat, Contact, Invite, Message, Subscription, MediaKey, ChatMember, Timer, Bot, ChatBot, BotMember, Accounting]
})
const models = sequelize.models

38
src/models/ts/accounting.ts

@ -0,0 +1,38 @@
import { Table, Column, Model, DataType } from 'sequelize-typescript';
@Table({ tableName: 'sphinx_accountings', underscored: true })
export default class Accounting extends Model<Accounting> {
@Column({
type: DataType.BIGINT,
primaryKey: true,
unique: true,
autoIncrement: true
})
id: number
@Column
date: Date
@Column
pubkey: string
@Column
onchainAddress: string
@Column(DataType.DECIMAL)
amount: number
@Column
sourceApp: string
@Column(DataType.BIGINT)
status: number
@Column
error: string
@Column(DataType.BIGINT)
chanId: number
}
Loading…
Cancel
Save