Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: liquidity webhooks #1782

Merged
merged 25 commits into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
3ab5d35
feat: add liquidityThreshold to asset
sabineschaller Aug 24, 2023
1368f44
Merge branch 'main' into s2-liquidity-webhooks
sabineschaller Aug 28, 2023
786ee62
feat: add liquidityThreshold to peer
sabineschaller Aug 28, 2023
f7eea5f
fix: test
sabineschaller Aug 28, 2023
d702d4d
feat: add onDebit
sabineschaller Sep 6, 2023
1a49ef6
feat: add webhook event
sabineschaller Sep 6, 2023
b054533
fix: circularity
sabineschaller Sep 7, 2023
be6fc57
test: asset.onDebit
sabineschaller Sep 7, 2023
db3b0f0
test: peer.onDebit
sabineschaller Sep 7, 2023
51c2d64
refactor: use promise.all instead of sequential promises
sabineschaller Sep 7, 2023
7722918
feat: add webhook schema, refactor events
sabineschaller Sep 7, 2023
0a9e36f
docs: add docs
sabineschaller Sep 8, 2023
a7a196f
fix: typo
sabineschaller Sep 8, 2023
782491f
feat: add liquidity threshold and webhook handling to mase
sabineschaller Sep 8, 2023
cc26e6c
fix: seed liquidity
sabineschaller Sep 8, 2023
619f83a
feat: postman changes
sabineschaller Sep 8, 2023
7be2879
fix: address feedback
sabineschaller Sep 11, 2023
13b4524
chore: new mirgration files
sabineschaller Sep 12, 2023
d611b55
refactor: rename webhook events
sabineschaller Sep 12, 2023
3ac325a
fis: formatting
sabineschaller Sep 12, 2023
834ca17
Revert "chore: new mirgration files"
sabineschaller Sep 12, 2023
3768f40
fix: migrations
sabineschaller Sep 12, 2023
4702ebe
chore: generate docs
sabineschaller Sep 12, 2023
a432aa5
Merge branch 'main' into s2-liquidity-webhooks
sabineschaller Sep 12, 2023
6f5611f
feat: add new event types to frontend filter
sabineschaller Sep 12, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions localenv/cloud-nine-wallet/seed.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,20 @@ assets:
- code: USD
scale: 2
liquidity: 1000000
liquidityThreshold: 100000
- code: EUR
scale: 2
liquidity: 1000000
liquidityThreshold: 100000
- code: MXN
scale: 2
liquidity: 1000000
liquidityThreshold: 100000
peers:
- initialLiquidity: '100000'
peerUrl: http://happy-life-bank-backend:3002
peerIlpAddress: test.happy-life-bank
liquidityThreshold: 10000
accounts:
- name: 'Grace Franklin'
path: accounts/gfranklin
Expand Down
4 changes: 4 additions & 0 deletions localenv/happy-life-bank/seed.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,20 @@ assets:
- code: USD
scale: 2
liquidity: 1000000
liquidityThreshold: 100000
- code: EUR
scale: 2
liquidity: 1000000
liquidityThreshold: 100000
- code: MXN
scale: 2
liquidity: 1000000
liquidityThreshold: 100000
peers:
- initialLiquidity: '1000000000000'
peerUrl: http://cloud-nine-wallet-backend:3002
peerIlpAddress: test.cloud-nine-wallet
liquidityThreshold: 100000
accounts:
- name: 'Philip Fry'
path: accounts/pfry
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export interface Asset {
code: string
scale: number
liquidity: number
liquidityThreshold: number
}

export interface Fees {
Expand All @@ -22,6 +23,7 @@ export interface Fees {
}

export interface Peering {
liquidityThreshold: number
peerUrl: string
peerIlpAddress: string
initialLiquidity: string
Expand Down
14 changes: 10 additions & 4 deletions localenv/mock-account-servicing-entity/app/lib/requesters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ export interface GraphqlResponseElement {

export async function createAsset(
code: string,
scale: number
scale: number,
liquidityThreshold: number
): Promise<AssetMutationResponse> {
const createAssetMutation = gql`
mutation CreateAsset($input: CreateAssetInput!) {
Expand All @@ -39,14 +40,16 @@ export async function createAsset(
id
code
scale
liquidityThreshold
}
}
}
`
const createAssetInput = {
input: {
code,
scale
scale,
liquidityThreshold
}
}
return apolloClient
Expand All @@ -67,7 +70,8 @@ export async function createPeer(
outgoingEndpoint: string,
assetId: string,
assetCode: string,
name: string
name: string,
liquidityThreshold: number
): Promise<CreatePeerMutationResponse> {
const createPeerMutation = gql`
mutation CreatePeer($input: CreatePeerInput!) {
Expand All @@ -79,6 +83,7 @@ export async function createPeer(
id
staticIlpAddress
name
liquidityThreshold
}
}
}
Expand All @@ -91,7 +96,8 @@ export async function createPeer(
outgoing: { endpoint: outgoingEndpoint, authToken: `test-${assetCode}` }
},
assetId,
name
name,
liquidityThreshold
}
}
return apolloClient
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ import { Asset } from 'generated/graphql'

export async function setupFromSeed(config: Config): Promise<void> {
const assets: Record<string, Asset> = {}
for (const { code, scale, liquidity } of config.seed.assets) {
const { asset } = await createAsset(code, scale)
for (const { code, scale, liquidity, liquidityThreshold } of config.seed
.assets) {
const { asset } = await createAsset(code, scale, liquidityThreshold)
if (!asset) {
throw new Error('asset not defined')
}
Expand All @@ -39,7 +40,8 @@ export async function setupFromSeed(config: Config): Promise<void> {
peer.peerUrl,
asset.id,
asset.code,
peer.name
peer.name,
peer.liquidityThreshold
).then((response) => response.peer)
if (!peerResponse) {
throw new Error('peer response not defined')
Expand Down
24 changes: 22 additions & 2 deletions localenv/mock-account-servicing-entity/app/lib/webhooks.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import type { Amount } from './transactions.server'
import { mockAccounts } from './accounts.server'
import { apolloClient } from './apolloClient'
import { v4 as uuid } from 'uuid'
import { createPaymentPointer } from './requesters'
import {
addAssetLiquidity,
addPeerLiquidity,
createPaymentPointer
} from './requesters'
import { CONFIG } from './parse_config.server'

export enum EventType {
Expand All @@ -14,7 +18,9 @@ export enum EventType {
OutgoingPaymentCreated = 'outgoing_payment.created',
OutgoingPaymentCompleted = 'outgoing_payment.completed',
OutgoingPaymentFailed = 'outgoing_payment.failed',
PaymentPointerNotFound = 'payment_pointer.not_found'
PaymentPointerNotFound = 'payment_pointer.not_found',
LiquidityAsset = 'asset.liquidity_low',
LiquidityPeer = 'peer.liquidity_low'
}

export interface WebHook {
Expand Down Expand Up @@ -194,3 +200,17 @@ export async function handlePaymentPointerNotFound(wh: WebHook) {
paymentPointer.url
)
}

export async function handleLowLiquidity(wh: WebHook) {
const id = wh.data['id'] as string | undefined

if (!id) {
throw new Error('id not found')
}

if (wh.type == 'asset.liquidity_low') {
await addAssetLiquidity(id, 1000000, uuid())
} else {
await addPeerLiquidity(id, '1000000', uuid())
}
}
10 changes: 9 additions & 1 deletion localenv/mock-account-servicing-entity/app/routes/webhooks.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import type { ActionArgs } from '@remix-run/node'
import { json } from '@remix-run/node'
import { handlePaymentPointerNotFound, WebHook } from '~/lib/webhooks.server'
import {
handleLowLiquidity,
handlePaymentPointerNotFound,
WebHook
} from '~/lib/webhooks.server'
import {
handleOutgoingPaymentCreated,
handleOutgoingPaymentCompletedFailed,
Expand Down Expand Up @@ -34,6 +38,10 @@ export async function action({ request }: ActionArgs) {
case EventType.PaymentPointerNotFound:
await handlePaymentPointerNotFound(wh)
break
case EventType.LiquidityAsset:
case EventType.LiquidityPeer:
await handleLowLiquidity(wh)
break
default:
console.log(`unknown event type: ${wh.type}`)
return json(undefined, { status: 400 })
Expand Down
22 changes: 18 additions & 4 deletions localenv/mock-account-servicing-entity/generated/graphql.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions localenv/mock-account-servicing-entity/seed.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@ self:
graphqlUrl: http://localhost:3001/graphql
hostname: 'backend'
mapHostname: 'primary-mase'
assets:
- code: USD
scale: 2
liquidity: 1000000
liquidityThreshold: 100000
peers:
- asset: USD
scale: 2
initialLiquidity: '100000'
peerUrl: http://peer-backend:3002
peerIlpAddress: test.peer
liquidityThreshold: 10000
accounts:
- name: 'Grace Franklin'
url: http://backend/accounts/gfranklin
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.up = function (knex) {
return knex.schema.alterTable('assets', (table) => {
table.bigInteger('liquidityThreshold').nullable()
})
}

/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.down = function (knex) {
return knex.schema.alterTable('assets', (table) => {
table.dropColumn('liquidityThreshold')
})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.up = function (knex) {
return knex.schema.alterTable('peers', (table) => {
table.bigInteger('liquidityThreshold').nullable()
})
}

/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.down = function (knex) {
return knex.schema.alterTable('peers', (table) => {
table.dropColumn('liquidityThreshold')
})
}
2 changes: 2 additions & 0 deletions packages/backend/src/accounting/psql/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ export async function createTransfer(
postTransfers: async (transferRefs) => postTransfers(deps, transferRefs),
getAccountReceived: async (accountRef) =>
getAccountTotalReceived(deps, accountRef),
getAccountBalance: async (accountRef) =>
getLiquidityAccountBalance(deps, accountRef),
createPendingTransfers: async (transfersToCreate) => {
const [
sourceAccount,
Expand Down
Loading