Skip to content
This repository has been archived by the owner on Jun 4, 2024. It is now read-only.

Feat/exchange account schema #11

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion Feeds Daemon.postman_collection.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
"header": [],
"body": {
"mode": "raw",
"raw": "{\n \"method\":\"updateFeed\",\n \"params\": {\n \"feed_id\":\"satoshi\",\n \"fields\": [\n {\n \"name\": \"Bitcoin\",\n \"value\": 1.243\n }\n ]\n }\n}",
"raw": "{\n \"method\":\"updateFeed\",\n \"params\": {\n \"feed_id\":\"satoshi\",\n \"fields\": [\n {\n \"name\": \"total balance\",\n \"value\": 11\n },\n {\n \"name\": \"total open pnl\",\n \"value\": 12\n },\n {\n \"name\": \"total open pnl and total balance\",\n \"value\": { \"balance\": 10, \"absolute_pnl\": 1, \"relative_pnl\": 10 }\n }\n ]\n }\n}",
"options": {
"raw": {
"language": "json"
Expand Down
48 changes: 29 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ node start.js

## Test & Development
There are tests for all methods located in `./test` dir. You can run them by `npm run test`
There examples in `./examples` folder. They require running daemon. Run `npm run start` to start daemon and in separate terminal run `npm run example:account` to start generating account feed. See [examples readme](./example/README.md) for more details
There examples in `./examples` folder. They require running daemon. Run `npm run start` to start daemon and in separate terminal run `npm run example:exchange:account` to start generating account feed. See [examples readme](./example/README.md) for more details

## RPC API

Expand All @@ -78,14 +78,13 @@ curl --location --request POST 'http://localhost:8787/v0.1/rpc' \
Response
``` json
{
"jsonrpc": "2.0",
"id": "925b10c27f4ad350ab3ef7e027605fd83388c999a890cfdd8e6061656b5a5513",
"result": {
"slashdrive": {
"key": "<string>",
"encryption_key": "<string>"
}
}
"jsonrpc": "2.0",
"id": "<call id>",
"result": {
"url": ":<slashfeed url>",
"feed_key": "<public key>",
"encrypt_key": "<private key>"
}
}
```

Expand All @@ -95,14 +94,22 @@ Update feed request
curl --location --request POST 'http://localhost:8787/v0.1/rpc' \
--header 'Content-Type: application/json' \
--data-raw '{
"method":"updateFeedBalance",
"method":"updateFeed",
"params": {
"feed_id":"satoshi123",
"fields": [
{
"name": "Bitcoin",
"value": 1.442
}
{
"name": "total balance",
"value": 11
},
{
"name": "total open pnl",
"value": 12
},
{
"name": "total open pnl and total balance",
"value": { "balance": 10, "absolute_pnl": 1, "relative_pnl": 10 }
}
]
}
}'
Expand All @@ -111,8 +118,10 @@ Response
``` json
{
"jsonrpc": "2.0",
"id": "4fefad839fa440cc2a85d8178d1d895fa1044460080b8fe1a26b4942aa86c07f",
"result": true
"id": "<call id>",
"result": {
"updated": true
}
}
```

Expand All @@ -129,10 +138,11 @@ Response
``` json
{
"jsonrpc": "2.0",
"id": "c6ccd88f842330ab60153b5fb512101d2ab76824189eee5690f9070ebe18cb87",
"id": "call id",
"result": {
"feed_key": "<string>",
"encrypt_key": "<string>"
"url": ":<slashfeed url>",
"feed_key": "<public key>",
"encrypt_key": "<private key>"
}
}
```
Expand Down
22 changes: 17 additions & 5 deletions example/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<h3 align="center">Account feeds example</h3>
<h3 align="center">Exchange account feeds example</h3>


## Start account feeds daemon
Expand Down Expand Up @@ -45,7 +45,7 @@ $ npm run start

## Feed generation

Open a separate terminal window and execute `npm run example:account` from the root directory. This will create three hyperdrives. Each hyperdrive feeds the data for a particular customer account. The drive contents are accessible by knowing the discovery key and the encryption key. This information can be shared by [slashfeed URL](https://github.com/synonymdev/slashtags/tree/master/packages/url), which has the following format: `slashfeed:<id>#encryptionKey=<key>`.
Open a separate terminal window and execute `npm run example:exchange:account` from the root directory. This will create three hyperdrives. Each hyperdrive feeds the data for a particular customer account. The drive contents are accessible by knowing the discovery key and the encryption key. This information can be shared by [slashfeed URL](https://github.com/synonymdev/slashtags/tree/master/packages/url), which has the following format: `slashfeed:<id>#encryptionKey=<key>`.

```sh
example Starting account feed +0ms
Expand All @@ -64,9 +64,21 @@ The data will update periodically.

```sh
example Updating account feeds +5s
example:abcde123 Updated feed: [ 'Bitcoin: "7.40"', 'Bitcoin P/L: "-99.27"' ] +0ms
example:satoshi9191 Updated feed: [ 'Bitcoin: "9.73"', 'Bitcoin P/L: "-51.02"' ] +0ms
example:synonymxyz Updated feed: [ 'Bitcoin: "8.52"', 'Bitcoin P/L: "54.86"' ] +0ms
example:abcde123 Updated feed: [
'total balance: "-5443472.07061947"',
'total open pnl: {"absolute":"-9039929.42534387","relative":"11.30"}',
'total open pnl and total balance: {"value":"8112571.67253644","absolute_pnl":"2471980.91819883","relative_pnl":"-26.32"}'
] +0ms
example:satoshi9191 Updated feed: [
'total balance: "-719298.06843400"',
'total open pnl: {"absolute":"-331530.44059873","relative":"-61.52"}',
'total open pnl and total balance: {"value":"-7741750.18537790","absolute_pnl":"-9805918.76897961","relative_pnl":"-17.91"}'
] +0ms
example:synonymxyz Updated feed: [
'total balance: "9146804.61864919"',
'total open pnl: {"absolute":"1281327.62853056","relative":"-70.31"}',
'total open pnl and total balance: {"value":"-797822.55459577","absolute_pnl":"6331277.87150443","relative_pnl":"10.34"}'
] +0ms
...
```

Expand Down
23 changes: 18 additions & 5 deletions example/accountFeed.js → example/exchangeAccountFeed.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,26 @@ async function updateAccounts (accountIds) {
for (const accountId of accountIds) {
const update = [
{
name: 'Bitcoin',
value: faker.finance.amount(5, 10, 2)
name: 'total balance',
value: faker.finance.amount(-10000000, 10000000, 8),
},
{
name: 'Bitcoin P/L',
value: faker.finance.amount(-100, 100, 2)
}
name: "total open pnl",
value: {
absolute: faker.finance.amount(-10000000, 10000000, 8),
relative: faker.finance.amount(-100, 100, 2)
},
},
{
name: "total open pnl and total balance",
value: {
value: faker.finance.amount(-10000000, 10000000, 8),
absolute_pnl: faker.finance.amount(-10000000, 10000000, 8),
relative_pnl: faker.finance.amount(-100, 100, 2)
},
},


]
await updateFeed(accountId, update)
accountLogger(accountId)('Updated feed:', update.map(u => `${u.name}: ${JSON.stringify(u.value)}`))
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"scripts": {
"test": "mocha ./test/",
"start": "DEBUG=stfeed* node start.js",
"example:account": "DEBUG=example* node example/accountFeed.js"
"example:exchange:account": "DEBUG=example* node example/exchangeAccountFeed.js"
},
"repository": {
"type": "git",
Expand Down
31 changes: 19 additions & 12 deletions schemas/slashfeed.json
Original file line number Diff line number Diff line change
@@ -1,25 +1,32 @@
{
"name": "Bitfinex",
"description": "Bitfinex account feed",
"type": "account_feed",
"type": "exchange_account_feed",
"version": "0.0.1",
"icons": {
"48": "data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMTAyNHB4IiBoZWlnaHQ9IjEwMjRweCIgdmlld0JveD0iMCAwIDEwMjQgMTAyNCIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj4KICAgPGNpcmNsZSBjeD0iNTEyIiBjeT0iNTEyIiByPSI1MTIiIHN0eWxlPSJmaWxsOiMwM2NhOWIiLz4KICAgPHBhdGggZD0iTTczMi44MSAyNzVjLTEuNjctLjczLTE3My41MS0yNC42LTM0My4zOSA4Ni44OEMyODQgNDMxLjIyIDI3MCA1MzIuNjggMjc0LjgxIDYwMC4zNmMyNDcuMDgtMjcuODkgNDUyLjQxLTMxNy4yOSA0NTgtMzI1LjM2ek0yOTEuNTcgNjc1LjUzYzIxLjI0IDM4LjE0IDE4MS43NyAxMjQuNiAzMjMuODcgNS4zNVM3NDUuNjIgMzQ0LjY2IDczMi44MSAyNzVjLTQuNDcgMTAuMTEtMTU5LjYyIDM1Ni44OS00NDEuMjQgNDAwLjUxIiBzdHlsZT0iZmlsbDojZmZmIi8+Cjwvc3ZnPgo="
},
"fields": [
{
"name": "Bitcoin",
"description": "Bitcoin balance",
"main": "/feed/bitcoin/",
"type": "currency",
"units": "btc"
"type": "balance",
"name": "total balance",
"description": "Total balance",
"main": "/feed/total-balance/",
"units": "sats"
},
{
"name": "Bitcoin P/L",
"description": "Bitcoin relative balance change",
"main": "/feed/bitcoin-p-l/",
"type": "delta",
"units": "%"
"type": "pnl",
"name": "total open pnl",
"description": "Total open Profit and Loss",
"main": "/feed/total-open-pnl/",
"units": "sats"
},
{
"type": "pnl_and_balance",
"name": "total open pnl and total balance",
"description": "Bitcoin open Profit and Loss and Total balance",
"main": "/feed/total-open-pnl-and-total-balance/",
"units": "sats"
}
]
}
}
11 changes: 10 additions & 1 deletion src/BaseUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ module.exports = (name, fileName) => {
errName: `${name}_ERROR`,
fileName
}),
log: Log(name)
log: Log(name),
getFileName: (fieldName) => {
const regex = /[^a-z0-9]+/gi
const trailing = /-+$/

return `/${fieldName.toLowerCase().trim().replace(regex, '-').replace(trailing, '')}/`
},
snakeToCamel: (str) => {
return str.toLowerCase().replace(/([-_][a-z])/g, group => group.toUpperCase().replace('-', '').replace('_', ''))
}
}
}
Loading