threshold-network / merkle-distribution

Threshold Network rewards generation and distribution
https://threshold.network
1 stars 6 forks source link

Rewards calculation for tBTCv2 #35

Closed dimpar closed 1 year ago

dimpar commented 1 year ago

This PR is copied from the keep-core repository. The original PR and reviews were done here

Here we calculate rewards for the beta tBTCv2 operators. The output rewards JSON file should be used to generate a Merkle distribution tree.

There are 5 requirements for an operator to receive rewards in a given rewards interval:

E.g. 1

<operator_address>:
  "instances":
        "10.102.1.33:9701": {
          "upTimePercent": 50,
          "preParams": 400,
          "version": "v2.0.0-m1-9-g4049dc015"
        }
        "10.102.2.20:9601": {
          "upTimePercent": 46,
          "preParams": 600,
          "version": "v2.0.0-m1-9-g4049dc015"
        }
}
"upTimePercent" - total: 96% meets a 96% req across all the instances
"preParams" - total avg. 500 meets a 500 avg across all the instances

E.g. 2

<operator_address>:
  "instances":
        "10.102.1.33:9701": {
          "upTimePercent": 50,
          "preParams": 399,
          "version": "v2.0.0-m1-9-g4049dc015"
        }
        "10.102.2.20:9601": {
          "upTimePercent": 45,
          "preParams": 600,
          "version": "v2.0.0-m1-9-g4049dc015"
        }
}
"upTimePercent" - total: 95% does not meet a 96% req across all the instances
"preParams" - total avg. 499.5 does not meet a 500 avg across all the instances

rewards.ts will:

uptimeCoeficient: off-chain client's uptime beaconWeightedAuthorization = 100k 10/30 + 150k 20/30 tbtcWeightedAuthorization = 100k 10/30 + 150k 20/30 weightedAuthorization = uptimeCoeficient * min(beaconWeightedAuthorization, tbtcWeightedAuthorization)

- query [Prometheus service](https://monitoring.test.threshold.network/prometheus/graph) for bootstrap peers' info
- aggregate and validate rewards requirements for each operator
- save rewards data for eligible operators

<details>
  <summary>Example operator details to calculate final rewards</summary>

"0x5cD847903Bb7F29de77Eecc135628cA5b104A355": { "applications": { "beaconAuthorization": "800000000000000000000000", "tbtcAuthorization": "800000000000000000000000" }, "instances": { "10.102.1.19:9601": { "upTimePercent": 74.52969075397857, "avgPreParams": 922, "version": "v2.0.0-m1-9-g4049dc015" }, "10.102.1.77:9601": { "upTimePercent": 24.81376522052997, "avgPreParams": 971, "version": "v2.0.0-m1-9-g4049dc015" } }, "requirements": { "isBeaconAuthorized": true, "isTbtcAuthorized": true, "isUptimeSatisfied": true, "isPreParamsSatisfied": true, "isVersionSatisfied": true } }


</details>

If all of the requirements are satisfied, then the rewards can be calculated with the following formula: 

amount = uptimeCoeficient min(beaconWeightedAuthorization, tbtcWeightedAuthorization) APR/12

APR = 15%

Example output format that is used to generate a merkle distribution tree. A parent key is a staking provider address, not operator. 

{ "0x0eC14BC7cCA82c942Cf276F6BbD0413216dDB2bE": { "beneficiary": "0x0eC14BC7cCA82c942Cf276F6BbD0413216dDB2bE", "amount": "9900000000000000000000" } },


### How to run

./rewards.sh --rewards-start-date Y-m-d --rewards-end-date Y-m-d --etherscan-token ""



For more details that include optional flags please run `./rewards.sh --help`