Code Monkey home page Code Monkey logo

Comments (17)

habanoz avatar habanoz commented on June 5, 2024 1

Your concerns are valid. However, use RPC API requires a lot of programming/testing effort. I need to see more demand before adding RPC based reward calculations.

I will leave this issue open to see if there will be more demand.

from tezos-reward-distributor.

utdrmac avatar utdrmac commented on June 5, 2024 1

Found it. Credit to @DefinitelyNotAGoat golang tezos library.

The pattern:

  • get level for cycle X (cycle X * 4096 + 1)
  • get block for level (/chains/main/blocks/" + headHash + "~" + (current level - level))
  • get roll snapshot index for block (/chains/main/blocks/" + block.Hash + "/context/raw/json/cycle/" + strCycle)
  • get snapshot block for payment cycle X (((cycleX - 7) * 4096) + (RollSnapShot+1)*256)
  • get staking balance of delegate using snapshot block (/chains/main/blocks/" + snapshot block
    hash + "/context/delegates/" + delegatePhk + "/staking_balance)
  • get delegates at cycle X (/chains/main/blocks/" + snapshot block hash + "/context/delegates/" + delegatePhk + "/delegated_contracts
  • get rewards for delegate at cycle X (/chains/main/blocks/head/context/raw/json/contracts/index/" + delegatePhk + "/frozen_balance/" + X
  • foreach delegates, get delegate balance at snapshot (/chains/main/blocks/" + snapshot block hash + "/context/contracts/" + delegate KT1 + "/balance"
    -- calculate % of total staking per delegatee
    -- calculate reward for delegatee

from tezos-reward-distributor.

utdrmac avatar utdrmac commented on June 5, 2024 1

Nope. I asked lead dev and Aurthor in bakers-slack. Waiting for response.

from tezos-reward-distributor.

goodpen avatar goodpen commented on June 5, 2024

+1

from tezos-reward-distributor.

utdrmac avatar utdrmac commented on June 5, 2024

What needs to be fetched from RPC?

  • List of delegators
  • list of 'active' delegators
  • Balance of each delegator
  • Staking balance of baker
  • total balance of baker

The hardest thing I cannot find is for which cycle a delegator activates.

# Alphanet
$ ./tezos-client rpc get /chains/main/blocks/head/context/delegates/tz1LdZ6S8ScNMgaCLqrekDvbBWhLqtUebk23
{ "balance": "13594924319", "frozen_balance": "0",
  "frozen_balance_by_cycle": [], "staking_balance": "32345701585",
  "delegated_contracts":
    [ "KT1WyMWHxGt8HLgWG9LZEqxG5u2kEpHNyE8D",
      "KT1Ua68oUfTwykAC8MRh8JZm9Ekd8wp57rm9",
      "KT1MTZGjAZo9ZYNU2y72vxwpg3iqGUWpZyfm",
      "KT1Lg9R2b2UjJhJdhvRnNovdpaQF41o2jvgw" ],
  "delegated_balance": "18750777266", "deactivated": false,
  "grace_period": 7 }

You can see a list of all delegates but no info on their staked balance, nor any indication to which cycle their delegation amount unfreezes and becomes part of the staked balance. This is typically 7 cycles, but I was expecting there to be an RPC call to find out which specific block/level.

The staking balance noted above is incorrect. Each of those delegates were added in cycle 1 (alphanet reset on Nov 30th) and should not be considered part of the staked balance for 7 cycles.

from tezos-reward-distributor.

utdrmac avatar utdrmac commented on June 5, 2024

This RPC validates the KT1 is a delegate of our baker, tz1LdZ, and gives us the staked balance.

./tezos-client rpc get /chains/main/blocks/head/context/contracts/KT1XfTzTJWFMsEukKXXVoKbwsBFbZvYS3hkn
{ "manager": "tz1Sfe7Nm2MdmkCopA7DVBUHHEd8G73TWwfN",
  "balance": "14378132380", "spendable": true,
  "delegate":
    { "setable": false, "value": "tz1LdZ6S8ScNMgaCLqrekDvbBWhLqtUebk23" },
  "counter": "0" }

from tezos-reward-distributor.

habanoz avatar habanoz commented on June 5, 2024

/chains/main/blocks/head/context/delegates/
balance + delegated_balance - rewars = staking_balance.

For a specific cycle/block we will need to use blockhash instead of 'head'. In order to get that we will need to find snapshot chosen for the cycle.

from tezos-reward-distributor.

utdrmac avatar utdrmac commented on June 5, 2024

Been looking for snapshot information in RPC. Not documented fully.

from tezos-reward-distributor.

habanoz avatar habanoz commented on June 5, 2024

Yeah, documentation is not great.

/chains/main/blocks/head/context/raw/json/rolls/owner/snapshot/

returns selected snapshot index <sn_idx> for

block index for which snapshot taken can be calculated. Remember in each block there are 16 snapshot thus 256 blocks between snapshots. Eventually:

block_index = sn_idx*256 + * 4096 (roughly)

This block_index can be converted to block_hash. Once we have block hash we can query delegators/stake state for the specific block.

from tezos-reward-distributor.

utdrmac avatar utdrmac commented on June 5, 2024

So this gives me snapshots I can access:

$ ./tezos-client rpc get /chains/main/blocks/head/context/raw/json/rolls/owner/snapshot
[ 7, 6, 5, 4, 3, 2, 1, 0 ]

Then I can query a specific (the current) cycle:

$ ./tezos-client rpc get /chains/main/blocks/head | jq -r '.metadata.level.cycle'
2
$ ./tezos-client rpc get /chains/main/blocks/head/context/raw/json/rolls/owner/snapshot/2/
[ 0 ]

What does that 0 mean here?
If I look at mainnet, current cycle 51:

https://rpc.tezrpc.me/chains/main/blocks/head/context/raw/json/rolls/owner/snapshot/51/
"[1]"

Fetching that:

$ ./tezos-client rpc get /chains/main/blocks/head/context/raw/json/rolls/owner/snapshot/2/0/
[ 2403, 2147, 1891, 1635, 1379, 1123, 867, 611, 2659, 355, 99, 2402, 2146,
  1890, 1634, 1378, 1122, 866, 610, 2658, 354, 98, 2401, 2145, 1889, 1633,
...

Fetching one of these:

$ ./tezos-client rpc get /chains/main/blocks/head/context/raw/json/rolls/owner/snapshot/2/0/596/
"p2pk66EmFoQS6b2mYLvCrwjXs7XT1A2znX26HcT9YMiGsyCHyDvsLaF"

And that is?

from tezos-reward-distributor.

habanoz avatar habanoz commented on June 5, 2024

How long does

./tezos-client rpc get /chains/main/blocks/head/context/raw/json/rolls/owner/snapshot/2/

take on your mainnet node?

from tezos-reward-distributor.

utdrmac avatar utdrmac commented on June 5, 2024

https://rpc.tezrpc.me/chains/main/blocks/head/context/raw/json/rolls/owner/snapshot/55/
Takes about 10 seconds

from tezos-reward-distributor.

utdrmac avatar utdrmac commented on June 5, 2024

The block-snapshot calculation is different on alphanet (-1) vs mainnet (-2)

PRESERVED_CYCLES=3
BLOCKS_IN_CYCLE=2048
(cycle - PRESERVED_CYCLES - 1) * BLOCKS_IN_CYCLE) + (snapShotQuery.RollSnapShot + 1) * 256

from tezos-reward-distributor.

habanoz avatar habanoz commented on June 5, 2024

Any idea why it is different? Reserved cycles parameter is already different in alpha and mainnet e.g. 2 vs 5.

from tezos-reward-distributor.

issie81 avatar issie81 commented on June 5, 2024

+1 add dependance on the node

from tezos-reward-distributor.

habanoz avatar habanoz commented on June 5, 2024

@amzid implemented this feature.

from tezos-reward-distributor.

habanoz avatar habanoz commented on June 5, 2024

Work of @amzid is merged. Now RPC based calculations are supported.

from tezos-reward-distributor.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.