Why anchor.js provide different results from querying lcd.terra.dev?

I am about to do some lightweight widget for anchor protocol and I would like to use simple rest calls instead of using whole anchor.js
However, if I query using anchor.js, I got correct result, eg. aquire pending rewards

anchorcli q staking reward-info terra1se7ml7wt7pqzjhajhw5ma9lnstcs429lmj0wzd
{
  staker: 'terra1se7ml7wt7pqzjhajhw5ma9lnstcs429lmj0wzd',
  reward_index: '0.283797841612686875',
  bond_amount: '20280038284',
  pending_reward: '1576571'
}

However, if I query contract using lcd.terra.dev, like
curl -X GET "https://lcd.terra.dev/wasm/contracts/terra1897an2xux840p9lrh6py3ryankc6mspw49xse3/store?query_msg=%7B%22staker_info%22%3A%20%7B%20%22staker%22%3A%20%22terra1se7ml7wt7pqzjhajhw5ma9lnstcs429lmj0wzd%22%7D%7D" -H "accept: application/json"

I got

{"height":"2642567","result":{"staker":"terra1se7ml7wt7pqzjhajhw5ma9lnstcs429lmj0wzd","reward_index":"0.283722242961145227","bond_amount":"20280038284","pending_reward":"43428"}}

Which is weird as block number corresponds to current block number, but pending_reward using lcd is wrong. Any suggestions?

@saad.najafi @kjessec Can you give this a look?

The difference is there because your LCD query didn’t include block_height in your query. Specifying the optional parameter, block_height, will give you a simulated reward as of the block height, whereas if you omitted it’ll give you only the stored state.

https://docs.anchorprotocol.com/smart-contracts/anchor-token/staking#stakerinfo

Since staking rewards are given out in periods, the contract cannot give you the total amount of reward you have earned so far since the last period state change. When you specify the block_height, it’ll try to extrapolate how much you would’ve earned since the last state change using the current reward factor, giving you a better outlook of what the reward is actually like.

If you were to do the anchorcli command and lcd query using the same block height (i.e. curl -X GET “https://lcd.terra.dev/wasm/contracts/terra1897an2xux840p9lrh6py3ryankc6mspw49xse3/store?query_msg={“staker_info”%3A%20{%20"staker"%3A%20"terra1se7ml7wt7pqzjhajhw5ma9lnstcs429lmj0wzd"%2C"block_height"%3A2694201}}” -H “accept: application/json”) it’ll yield the same result.

Please note that the block height parameter can only be bigger than the latest known (=current) block height. Specifying a block height smaller than the current height will result in a negative Uint256 operation which will give you an error.

Thanks! That was it, I’ve never tried higher than current block number.