dcrdex icon indicating copy to clipboard operation
dcrdex copied to clipboard

fiatrates: Implement a fiat rate oracle for tatanka

Open ukane-philemon opened this issue 10 months ago • 4 comments

Closes #2727

The new code in fiatrate pkg would eventually replace the existing providers we use elsewhere. That will be the focus of another effort.

ukane-philemon avatar Apr 14 '24 00:04 ukane-philemon

Do I need to wait 5 minutes for the first tick?

The rate messages are sent to subscribers every rateBroadcastInterval but until a rate has been requested, the fiat oracle does nothing. This ensures we are not fetching data that might not be used.

ukane-philemon avatar Apr 15 '24 10:04 ukane-philemon

This ensures we are not fetching data that might not be used.

Yeah that makes sense. Would be cool if there were a short circuit for new rates but I guess this is ok for now...

JoeGruffins avatar Apr 16 '24 01:04 JoeGruffins

429 looks like too many requests? I only tried once.

Ah I see, the second server is running at the same time.

ukane-philemon avatar Apr 25 '24 23:04 ukane-philemon

Demo test log

Testing fiat rates...
2024-04-26 01:19:45.562 [TRC] SRV[127.0.0.1:61369]: Tatanka node handling message. route = subscribe, payload = {"topic":"fiat_rate","subject":"zcl"}
2024-04-26 01:19:45.562 [TRC] SRV[127.0.0.1:61369]: Adding new subscription topic and subject fiat_rate -> zcl
2024-04-26 01:19:45.565 [TRC] SRV[127.0.0.1:61369]: Tatanka node handling message. route = subscribe, payload = {"topic":"fiat_rate","subject":"dcr"}
2024-04-26 01:19:45.567 [TRC] SRV[127.0.0.1:61369]: Tatanka node handling message. route = subscribe, payload = {"topic":"fiat_rate","subject":"btc"}
2024-04-26 01:19:45.568 [TRC] SRV[127.0.0.1:61369]: Tatanka node handling message. route = subscribe, payload = {"topic":"fiat_rate","subject":"ltc"}
2024-04-26 01:19:45.569 [TRC] SRV[127.0.0.1:61369]: Tatanka node handling message. route = subscribe, payload = {"topic":"fiat_rate","subject":"eth"}
2024-04-26 01:19:45.570 [TRC] SRV[127.0.0.1:61369]: Tatanka node handling message. route = subscribe, payload = {"topic":"fiat_rate","subject":"usdc.polygon"}
2024-04-26 01:19:45.571 [TRC] SRV[127.0.0.1:61369]: Tatanka node handling message. route = subscribe, payload = {"topic":"fiat_rate","subject":"bch"}
2024-04-26 01:19:45.572 [TRC] SRV[127.0.0.1:61369]: Tatanka node handling message. route = subscribe, payload = {"topic":"fiat_rate","subject":"dash"}
2024-04-26 01:19:45.573 [TRC] SRV[127.0.0.1:61369]: Tatanka node handling message. route = subscribe, payload = {"topic":"fiat_rate","subject":"dgb"}
2024-04-26 01:19:45.573 [TRC] SRV[127.0.0.1:61369]: Tatanka node handling message. route = subscribe, payload = {"topic":"fiat_rate","subject":"doge"}
2024-04-26 01:19:45.574 [TRC] SRV[127.0.0.1:61369]: Tatanka node handling message. route = subscribe, payload = {"topic":"fiat_rate","subject":"firo"}
2024-04-26 01:19:45.575 [TRC] SRV[127.0.0.1:61369]: Tatanka node handling message. route = subscribe, payload = {"topic":"fiat_rate","subject":"polygon"}
2024-04-26 01:19:45.576 [TRC] SRV[127.0.0.1:61369]: Tatanka node handling message. route = subscribe, payload = {"topic":"fiat_rate","subject":"zec"}
2024-04-26 01:19:45.580 [TRC] SRV[127.0.0.1:61370]: Tatanka node handling message from remote tatanka. route = relay_broadcast, payload = {"peerID":[3,11,29,163,207,237,127,233,64,180,182,17,111,158,144,247,17,193,117,32,213,232,224,8,12,27,228,194,3,113,154,105,181],"topic":"fiat_rate","subject":"zcl","messageType":"new_subscriber","stamp":"2024-04-26T01:19:45.562402+01:00"}
2024-04-26 01:19:45.582 [TRC] SRV[127.0.0.1:61370]: Tatanka node handling message from remote tatanka. route = relay_broadcast, payload = {"peerID":[3,11,29,163,207,237,127,233,64,180,182,17,111,158,144,247,17,193,117,32,213,232,224,8,12,27,228,194,3,113,154,105,181],"topic":"fiat_rate","subject":"dcr","messageType":"new_subscriber","stamp":"2024-04-26T01:19:45.56573+01:00"}
2024-04-26 01:19:45.592 [TRC] SRV[127.0.0.1:61370]: Tatanka node handling message from remote tatanka. route = relay_broadcast, payload = {"peerID":[3,11,29,163,207,237,127,233,64,180,182,17,111,158,144,247,17,193,117,32,213,232,224,8,12,27,228,194,3,113,154,105,181],"topic":"fiat_rate","subject":"btc","messageType":"new_subscriber","stamp":"2024-04-26T01:19:45.567623+01:00"}
2024-04-26 01:19:45.594 [TRC] SRV[127.0.0.1:61370]: Tatanka node handling message from remote tatanka. route = relay_broadcast, payload = {"peerID":[3,11,29,163,207,237,127,233,64,180,182,17,111,158,144,247,17,193,117,32,213,232,224,8,12,27,228,194,3,113,154,105,181],"topic":"fiat_rate","subject":"ltc","messageType":"new_subscriber","stamp":"2024-04-26T01:19:45.56874+01:00"}
2024-04-26 01:19:45.595 [TRC] SRV[127.0.0.1:61370]: Tatanka node handling message from remote tatanka. route = relay_broadcast, payload = {"peerID":[3,11,29,163,207,237,127,233,64,180,182,17,111,158,144,247,17,193,117,32,213,232,224,8,12,27,228,194,3,113,154,105,181],"topic":"fiat_rate","subject":"eth","messageType":"new_subscriber","stamp":"2024-04-26T01:19:45.570046+01:00"}
2024-04-26 01:19:45.595 [TRC] SRV[127.0.0.1:61370]: Tatanka node handling message from remote tatanka. route = relay_broadcast, payload = {"peerID":[3,11,29,163,207,237,127,233,64,180,182,17,111,158,144,247,17,193,117,32,213,232,224,8,12,27,228,194,3,113,154,105,181],"topic":"fiat_rate","subject":"usdc.polygon","messageType":"new_subscriber","stamp":"2024-04-26T01:19:45.570948+01:00"}
2024-04-26 01:19:45.596 [TRC] SRV[127.0.0.1:61370]: Tatanka node handling message from remote tatanka. route = relay_broadcast, payload = {"peerID":[3,11,29,163,207,237,127,233,64,180,182,17,111,158,144,247,17,193,117,32,213,232,224,8,12,27,228,194,3,113,154,105,181],"topic":"fiat_rate","subject":"bch","messageType":"new_subscriber","stamp":"2024-04-26T01:19:45.571649+01:00"}
2024-04-26 01:19:45.596 [TRC] SRV[127.0.0.1:61370]: Tatanka node handling message from remote tatanka. route = relay_broadcast, payload = {"peerID":[3,11,29,163,207,237,127,233,64,180,182,17,111,158,144,247,17,193,117,32,213,232,224,8,12,27,228,194,3,113,154,105,181],"topic":"fiat_rate","subject":"dash","messageType":"new_subscriber","stamp":"2024-04-26T01:19:45.572445+01:00"}
2024-04-26 01:19:45.596 [TRC] SRV[127.0.0.1:61370]: Tatanka node handling message from remote tatanka. route = relay_broadcast, payload = {"peerID":[3,11,29,163,207,237,127,233,64,180,182,17,111,158,144,247,17,193,117,32,213,232,224,8,12,27,228,194,3,113,154,105,181],"topic":"fiat_rate","subject":"dgb","messageType":"new_subscriber","stamp":"2024-04-26T01:19:45.5733+01:00"}
2024-04-26 01:19:45.597 [TRC] SRV[127.0.0.1:61370]: Tatanka node handling message from remote tatanka. route = relay_broadcast, payload = {"peerID":[3,11,29,163,207,237,127,233,64,180,182,17,111,158,144,247,17,193,117,32,213,232,224,8,12,27,228,194,3,113,154,105,181],"topic":"fiat_rate","subject":"doge","messageType":"new_subscriber","stamp":"2024-04-26T01:19:45.574149+01:00"}
2024-04-26 01:19:45.597 [TRC] SRV[127.0.0.1:61370]: Tatanka node handling message from remote tatanka. route = relay_broadcast, payload = {"peerID":[3,11,29,163,207,237,127,233,64,180,182,17,111,158,144,247,17,193,117,32,213,232,224,8,12,27,228,194,3,113,154,105,181],"topic":"fiat_rate","subject":"firo","messageType":"new_subscriber","stamp":"2024-04-26T01:19:45.574876+01:00"}
2024-04-26 01:19:45.598 [TRC] SRV[127.0.0.1:61370]: Tatanka node handling message from remote tatanka. route = relay_broadcast, payload = {"peerID":[3,11,29,163,207,237,127,233,64,180,182,17,111,158,144,247,17,193,117,32,213,232,224,8,12,27,228,194,3,113,154,105,181],"topic":"fiat_rate","subject":"polygon","messageType":"new_subscriber","stamp":"2024-04-26T01:19:45.57565+01:00"}
2024-04-26 01:19:45.598 [TRC] SRV[127.0.0.1:61370]: Tatanka node handling message from remote tatanka. route = relay_broadcast, payload = {"peerID":[3,11,29,163,207,237,127,233,64,180,182,17,111,158,144,247,17,193,117,32,213,232,224,8,12,27,228,194,3,113,154,105,181],"topic":"fiat_rate","subject":"zec","messageType":"new_subscriber","stamp":"2024-04-26T01:19:45.576766+01:00"}
2024-04-26 01:19:46.960 [ERR] SRV[127.0.0.1:61369]: failed to retrieve rate from KuCoin: unable to fetch fiat rates: Get "https://api.kucoin.com/api/v1/prices?currencies=ZCL,DCR,LTC,ETH,USDC,DASH,DGB,BTC,BCH,DOGE,FIRO,MATIC,ZEC": context deadline exceeded
2024-04-26 01:19:46.967 [ERR] SRV[127.0.0.1:61369]: failed to retrieve rate from Binance: unable to fetch fiat rates: Get "https://api3.binance.com/api/v3/ticker/price?symbols=[%22BTCUSDT%22%2C%22BCHUSDT%22%2C%22DOGEUSDT%22%2C%22FIROUSDT%22%2C%22MATICUSDT%22%2C%22ZECUSDT%22%2C%22DCRUSDT%22%2C%22LTCUSDT%22%2C%22ETHUSDT%22%2C%22USDCUSDT%22%2C%22DASHUSDT%22%2C%22DGBUSDT%22]": dial tcp: lookup api3.binance.com: no such host
2024-04-26 01:20:41.963 [TRC] tCL[0:127.0.0.1:61369][tTC]: Client handling message from tatanka node: route = rates, payload = {"topic":"fiat_rate","subject":"dash","value":30.529484061558378}
2024-04-26 01:20:41.964 [TRC] tCL[0:127.0.0.1:61369][tTC]: Client handling message from tatanka node: route = rates, payload = {"topic":"fiat_rate","subject":"dgb","value":0.01213908666581539}
2024-04-26 01:20:41.964 [TRC] tCL[0:127.0.0.1:61369][tTC]: Client handling message from tatanka node: route = rates, payload = {"topic":"fiat_rate","subject":"doge","value":0.1507924139217401}
2024-04-26 01:20:41.964 [TRC] tCL[0:127.0.0.1:61369][tTC]: Client handling message from tatanka node: route = rates, payload = {"topic":"fiat_rate","subject":"firo","value":1.682676418115035}
2024-04-26 01:20:41.965 [TRC] tCL[0:127.0.0.1:61369][tTC]: Client handling message from tatanka node: route = rates, payload = {"topic":"fiat_rate","subject":"polygon","value":0.7201772770279691}
2024-04-26 01:20:41.965 [TRC] tCL[0:127.0.0.1:61369][tTC]: Client handling message from tatanka node: route = rates, payload = {"topic":"fiat_rate","subject":"zec","value":23.235472219432538}
2024-04-26 01:20:41.965 [TRC] tCL[0:127.0.0.1:61369][tTC]: Client handling message from tatanka node: route = rates, payload = {"topic":"fiat_rate","subject":"zcl","value":0.10806431950319026}
2024-04-26 01:20:41.966 [TRC] tCL[0:127.0.0.1:61369][tTC]: Client handling message from tatanka node: route = rates, payload = {"topic":"fiat_rate","subject":"btc","value":64428.762814868955}
2024-04-26 01:20:41.966 [TRC] tCL[0:127.0.0.1:61369][tTC]: Client handling message from tatanka node: route = rates, payload = {"topic":"fiat_rate","subject":"ltc","value":83.97790266590093}
2024-04-26 01:20:41.966 [TRC] tCL[0:127.0.0.1:61369][tTC]: Client handling message from tatanka node: route = rates, payload = {"topic":"fiat_rate","subject":"eth","value":3152.9078322923297}
2024-04-26 01:20:41.967 [TRC] tCL[0:127.0.0.1:61369][tTC]: Client handling message from tatanka node: route = rates, payload = {"topic":"fiat_rate","subject":"usdc.polygon","value":1.000623811935815}
2024-04-26 01:20:41.967 [TRC] tCL[0:127.0.0.1:61369][tTC]: Client handling message from tatanka node: route = rates, payload = {"topic":"fiat_rate","subject":"bch","value":477.6043906819767}
2024-04-26 01:20:41.968 [TRC] tCL[0:127.0.0.1:61369][tTC]: Client handling message from tatanka node: route = rates, payload = {"topic":"fiat_rate","subject":"dcr","value":22.439650789531093}

Rate found for zcl
Rate found for dcr
Rate found for btc
Rate found for ltc
Rate found for eth
Rate found for usdc.polygon
Rate found for bch
Rate found for dash
Rate found for dgb
Rate found for doge
Rate found for firo
Rate found for polygon
Rate found for zec!!!!!!!! Test Success !!!!!!!!
2024-04-26 01:20:41.969 [INF] tCL[0:127.0.0.1:61369][tTC][TCP]: WebSockets client for ws://127.0.0.1:61369/ws has disconnected
2024-04-26 01:20:41.969 [DBG] tCL[0:127.0.0.1:61369][tTC][TCP][TC]: Sending close 1000 (normal) message.
2024-04-26 01:20:41.970 [INF] tCL[1:127.0.0.1:61370][tTC][TCP]: WebSockets client for ws://127.0.0.1:61370/ws has disconnected
2024-04-26 01:20:41.970 [DBG] tCL[1:127.0.0.1:61370][tTC][TCP][TC]: Sending close 1000 (normal) message.
2024-04-26 01:20:41.972 [TRC] COMMS[WS]: Sent 20 and dropped 0 messages to 127.0.0.1 before shutdown.
2024-04-26 01:20:41.972 [TRC] COMMS: Disconnected websocket client 127.0.0.1
2024-04-26 01:20:41.972 [TRC] COMMS[WS]: Sent 34 and dropped 0 messages to 127.0.0.1 before shutdown.
2024-04-26 01:20:41.972 [TRC] COMMS: Disconnected websocket client 127.0.0.1
2024-04-26 01:20:41.973 [INF] COMMS: Server shutting down...
2024-04-26 01:20:41.974 [DBG] COMMS: RPC listener done for 127.0.0.1:61370
2024-04-26 01:20:41.974 [DBG] CL[ws://127.0.0.1:61369]: Sending close 1000 (normal) message.
2024-04-26 01:20:41.974 [TRC] COMMS[WS]: Sent 6 and dropped 0 messages to 127.0.0.1 before shutdown.
2024-04-26 01:20:41.974 [TRC] COMMS: Disconnected websocket client 127.0.0.1
2024-04-26 01:20:41.974 [INF] COMMS: Server shutdown complete
2024-04-26 01:20:41.971 [INF] COMMS: Server shutting down...
2024-04-26 01:20:41.976 [DBG] COMMS: RPC listener done for 127.0.0.1:61369
2024-04-26 01:20:41.976 [INF] COMMS: Server shutdown complete

ukane-philemon avatar Apr 26 '24 00:04 ukane-philemon

The demo is failing before fmt.Println("Testing fiat rates...")

JoeGruffins avatar May 08 '24 02:05 JoeGruffins

Allow clients to ask for a send from server of some info.

We can just reply with the current rates (if any) for every subscription.

ukane-philemon avatar May 09 '24 00:05 ukane-philemon

We can just reply with the current rates (if any) for every subscription.

That sounds great to me.

JoeGruffins avatar May 09 '24 00:05 JoeGruffins

The demo just ends with this now:

One of issue was what you've described, new subscription missed the first notification cuz they subscribed after and will have to wait for the next rate notification. https://github.com/decred/dcrdex/pull/2736/commits/1751a1634c4cb79c63269d6953c53fcc5e0db363 makes it possible to reply to a subscription so that new fiat rate subscribers always get the current rates before the next notification.

Thanks for the review @JoeGruffins & @buck54321.

ukane-philemon avatar May 09 '24 10:05 ukane-philemon