bch-rpc-explorer icon indicating copy to clipboard operation
bch-rpc-explorer copied to clipboard

Parse AnyHedge transactions

Open dagurval opened this issue 4 years ago • 1 comments

Like we do for flipstarter and voter.cash transactions, we can identify AnyHedge transactions.

This is possible to parse from a transaction (quoting @rkalis from AnyHedge telegram):

Let me put it in pseudocode.

From the parsed tx, you have:

hedge payout satoshis long payout satoshis settlement price funding transaction (outpoint)

From that you get:

total contract satoshis = hedge payout satoshis + long payout satoshis hedge USD payout = (hedge payout satoshis / 1e8) * settlement price long USD payout = (long payout satoshis / 1e8) * settlement price

By definition:

hedge USD payin = hedge USD payout

Then you look up the funding transaction's block timestamp (1 rpc call). Then you look up the approximate BCH price at the time of funding (e.g. coingecko API). Note that this is an approximation, because we cannot know the exact price used in the contract funding.

From there you calculate:

approx hedge payin satoshis = (hedge USD payin / approx funding price) * 1e8 approx long payin satoshis = total contract satoshis - approx hedge payin satoshis approx long USD payin = (approx long payin satoshis / 1e8) * approx funding price

From there you can calculate the long's approx profit/loss in terms of sats, $ or %.

Does that make sense? So it should be 1 rpc call + 1 price api call

We don't need to provide all that info, but that is what is possible.

dagurval avatar Jan 06 '21 06:01 dagurval

See https://gitlab.com/GeneralProtocols/anyhedge/library/-/blob/development/examples/parse-settlement-transaction.js for a code example of how to use the AnyHedge library to parse settlement transactions.

rkalis avatar Jan 06 '21 09:01 rkalis