Funding ticks and state synchronization
Be aware that in order to optimize resources, StarkEx updates the funding of a user’s position only when the user touches that position by executing some transaction.
When a user queries their position without actually executing a transaction, the application can update that user’s position based on a funding tick, without sending any transaction to StarkEx. Each funding tick is rounded to the next lowest integer. For example, -2.1 is rounded to -3.
When a user ultimately executes a transaction, StarkEx first updates the position, calculating the new position based on all the funding ticks that occurred since the last time the user touched their position.
At this point, the application’s state and StarkEx’s state can diverge.
On Monday, the collateral in Alice’s position is 100 USDC.
On Tuesday, the application submits a funding tick that updates the collateral in Alice’s position by -2.1, which is rounded to -3.
Alice queries her position, at which point the application applies the funding tick.
Her new collateral balance is 100-3=97 USDC.
On Wednesday, the application submits another funding tick that updates the collateral in Alice’s position by -1.9, which is rounded to -2.
She then executes a trade for some synthetic ETH she has in her position. This trade affects her synthetic ETH balance but not her collateral. But the funding tick changes apply.
Her new collateral balance is 97-2=95 USDC.
StarkEx applies the prior funding ticks, (-2.1) + (-1.9), to the original collateral balance as follows:
So in StarkEx, her new collateral balance is 96 USDC, while the application shows her collateral balance to be 95 USDC.
You need to consider this discrepancy when developing an application and ensure that the application’s state remains synchronized with StarkEx’s state.