darwinia-network / collator-staking-ui

Collator Staking UI
https://collator-staking.darwinia.network
Apache License 2.0
0 stars 1 forks source link

Wrong formula #59

Closed AurevoirXavier closed 10 months ago

AurevoirXavier commented 10 months ago

https://github.com/darwinia-network/darwinia2.0-staking-ui/blame/8f9d88b4c874c36cedf140f0a288250507d3b293/src/utils/misc.ts#L12-L15

It makes things complicated.

  1. Begin with the initial equation:

$$ \left( \text{{stakingRing}} + \left( \text{{stakingKton}} \times \frac{{\text{{ringPool}}}}{{\text{{ktonPool}}}} \right) \right) \div \left( 2 \times \text{{ringPool}} \right) \times 10^9 $$

  1. Expand the equation:

$$ \frac{{\text{{stakingRing}} + \left( \text{{stakingKton}} \times \frac{{\text{{ringPool}}}}{{\text{{ktonPool}}}} \right)}}{{2 \times \text{{ringPool}}}} \times 10^9 $$

  1. Distribute the division:

$$ \left( \frac{{\text{{stakingRing}}}}{{2 \times \text{{ringPool}}}} + \frac{{\text{{stakingKton}} \times \frac{{\text{{ringPool}}}}{{\text{{ktonPool}}}}}}{{2 \times \text{{ringPool}}}} \right) \times 10^9 $$

  1. Simplify the equation further:

$$ 10^9 \times \frac{{\text{{stakingRing}}}}{{2 \times \text{{ringPool}}}} + 10^9 \times \frac{{\text{{stakingKton}} \times \frac{{\text{{ringPool}}}}{{\text{{ktonPool}}}}}}{{2 \times \text{{ringPool}}}} $$

It should be https://github.com/darwinia-network/darwinia/blob/2f0941d6f2a896bac2900d7c3f2d17a46ca6948b/pallet/staking/src/lib.rs#L778-L802.

JayJay1024 commented 10 months ago

Yeah, eventually equals to 500000000 * stakingRing / ringPool + 500000000 * stakingKton / ktonPool. So there is no problem with the formula, but it is complicated and can be optimized.

wuminzhe commented 10 months ago
def staking_to_power(staked_ring, staked_kton, ring_pool, kton_pool)
  raise 'require ring_pool > 0' if ring_pool <= 0

  power_of_ring = 500_000_000 * (staked_ring / ring_pool)
  power_of_kton = kton_pool > 0 ? 500_000_000 * (staked_kton / kton_pool) : 0

  power_of_ring + power_of_kton
end