0x1a

@00011010

Joined on Jul 17, 2023

  • As someone who is implementing sth like this for the first time, 3 differnt approaches pops up, where none of them is optimal: Indexer Running a node Scraping the mintscan website for data Indexer The V4 Indexer is a set of microservices which provides a set of rich APIs and websocket channels for a dYdX V4 application. It is the easiest way to query trader profile, and there are two ways to do it. Either by running a local v4 node or connecting to a remote v4 node. Unfortunatly, the instructions on running a local v4 node is an outdated link on dydx github page https://github.com/dydxprotocol/v4#running-the-chain-locally, which forces us to connect to a remote node to query the data. Additionally, to test our implementation, we can first use the https://indexer.dydx.trade/v4/perpetualPositions endpoint to query the data that we intend to get using the Indexer v4. Unfortunatly, this approach can't be used for the main product since there is a rate limit. Therefore one should set up an Indexer follwoing the link https://github.com/dydxprotocol/v4-chain/tree/main/indexer. Before discussing the possible implementation, here is the API calls that are available through the Indexer https://indexer.dydx.trade/docs/. Indexer also gives us access to off-chain data which we don't actually need currently, but can be used to create a heat map since limit orders are not stored on chain. Indexer does not give us an API that would help us to detect new events therefore using indexer will cause a big overhead that is proportioanl to number of active traders. Since we don't have an API for new events, we need to find a realtivly efficient algorithm to update the profiles with not that big of an overhead. One can initially query all the data for addresses and store the postiions as well as bids, the bids for each pair are stored in a list where we sort them based on prices. After that we have pairs many lists. Since, as mentioned, we don't have access to new events, what we do is we track the price of each pair and iterate over each list based on the price (in this case lists with the iterator serve as an outdated orderbook). We then set a integer threshold $k$ which will simply serve as an indicator. Whenever price movement causes an iteration over $k$ many orders or $\Delta t$ time has passed since the last refresh, we query each address one more time to refresh the data starting from the once with the most orders. Of course this is a very suboptimal approach, but the best using the indexer. PSA: A code tat can be used to set up the profiles https://gist.github.com/kingbaldwin-iv/7e7ff68081f75dae454e7cb76a119d0b. (Setting up the profiles can be done very easily using the indexer, the main issue is processing new transactions.) Node/Validator
     Like  Bookmark
  • What are we given? hash (= tx-hash) block_number block_timestamp log_index hash_log_index (=hash||'_' || log_index) address (=GMX Vault CA "0x489ee077994B6658eAfA855C308275EAd8097C4A") protocol (="GMX") contract (="Vault") event_name (UpdatePosition, IncreasePosition, LiquidatePosition, DecreasePosition, ClosePosition)
     Like  Bookmark
  • During the Algorithms and Probability class, which I took this semester, I got introduced to the concept of probabilistic dynamic programming. At the start, it felt very unintiuitve but after some time studying, I think I have a decent understanding of it. But to improve my understanding I decided to write a short post about the problems that I've seen and detailed explanation on how to solve them. Ok too much talk, let's start with the first problem. Vanishing Bag DESCRIPTION: We have a special bag from which the objects can vanish. Initially there are some number of white and black stones in the bag. The game proceeds by two players, you and your friend, alternatively choosing a stone from the bag and removing it. The players are required to do this without looking into the bag, so each stone is considered to be drawn uniformly at random. However, this on its own would not be as fun, so there is a catch. Every time someone draws a stone from the bag, another stone chosen uniformly at random vanishes. The game ends once a white stone is drawn or the bag becomes empty. The winner is the player who draws the white stone or if the bag becomes empty the winner is the player who played second. Out of courtesy, your friend draws first and begins the game (thus, if the bag becomes empty you win). What is the probability of you winning the game? So before getting into how to construct the dp-table and fill it, let's approach the problem from purely theoretical perspective. Let $X_{i,j,k}$ be the event that represents us winning the game considering we have $i ≤ w$ white stones, $j ≤ b$ black stones left in the bag and current player is player $k \in {0,1}$. $k=0$ implies that the current player is the friend and k=1 implies it is our turn. Let $Y/\overline Y$ represent the occurance "A black/white stone vanished from the bag.". Let $Z/\overline Z$ represent "Current player picked a black/white stone.". We have the following recursive relationship: $$ Pr[X_{i,j,0}] = Pr[Y \cap Z]Pr[X_{i,j,0}|Y \cap Z] +Pr[Y\cap \overline Z]Pr[X_{i,j,0}|Y\cap \overline Z]+\Pr[\overline Y\cap Z]Pr[X_{i,j,0}|\overline Y\cap Z]+Pr[\overline Y \cap \overline Z]Pr[X_{i,j,0}|\overline Y\cap \overline Z]\ =Pr[Z]Pr[Y|Z]Pr[X_{i,j,0}|Y \cap Z] +Pr[\overline Z]Pr[Y| \overline Z]Pr[X_{i,j,0}|Y\cap \overline Z]+\Pr[Z]Pr[\overline Y| Z]Pr[X_{i,j,0}|\overline Y\cap Z]+Pr[\overline Z]Pr[\overline Y | \overline Z]Pr[X_{i,j,0}|\overline Y\cap \overline Z]\=\frac{j}{i+j}\cdot\frac{j-1}{i+j-1}\cdot Pr[X_{i,j-2,1}]+\frac{i}{i+j}\cdot\frac{j}{i+j-2}\cdot 0 + \ \frac{j}{i+j}\cdot\frac{i}{i+j-1}\cdot Pr[X_{i-1,j-1,1}]+ \frac{i}{i+j}\cdot \frac{i-1}{i+j-1}\cdot 0 \= \frac{j}{i+j}\cdot\frac{j-1}{i+j-1}\cdot Pr[X_{i,j-2,1}]+\frac{j}{i+j}\cdot\frac{i}{i+j-1}\cdot Pr[X_{i-1,j-1,1}]$$
     Like 1 Bookmark
  • I thought this was relatively harder so wanted to analyse this as well. The task goes as follows: The inhabitants of an island are divided into three tribes: Bears, Hunters, and Ninjas. They are very hostile, and whenever two members of different tribes meet, the stronger one always eliminates the weaker one. It is known that a bear is stronger than a ninja, a ninja is stronger than a hunter, a hunter is stronger than a bear. The hostility goes on until only one tribe remains. What is the probability that each tribe is the last one standing, given initial population size? Once again, before coding let's get into the calculation and without the loss of generality, let's consider the probability chance of bear. Let $Pr[B_{i,j,k}]$ be the survival probaility of bears under the assumption that $i$ bears, $j$ ninjas and $k$ bears left. Also let us consider "meetings" as uniformaly consectivly choosen two inhabitants. Let $X_i/Y_i/Z_i$ be $i\in{1,2}$-th choice bear/ninja/hunter. The following equality consists of union of pairwise disjunkt occurances and it holds. $$Pr[(Y_2\cap X_1) \cup (Z_2\cap X_1)\cup (X_2\cap X_1)\cup(Y_2\cap Y_1)\cup (Z_2\cap Y_1)\cup (X_2\cap Y_1)\cup(Z_2\cap Z_1)\\cup (X_2\cap Z_1)\cup(Y_2\cap Z_1)]=Pr[(X_1\cap(Y_2\cup Z_2\cup X_2))\cup (Y_1\cap(Y_2\cup Z_2\cup X_2))\\cup (Z_1\cap(Y_2\cup Z_2\cup X_2))]=Pr[(Y_2\cup Z_2\cup X_2)\cap(X_1\cup Y_1\cup Z_1)]\=Pr[X_1\cup Y_1\cup Z_1]Pr[(Y_2\cup Z_2\cup X_2)|(X_1\cup Y_1\cup Z_1)]=1$$ We can use it to construct an equation for $Pr[B_{i,j,k}]$: $$Pr[B_{i,j,k}] = Pr[X_1]Pr[Y_2|X_1]Pr[B_{i,j,k}|Y_2\cap X_1]+Pr[X_1]Pr[Z_2|X_1]Pr[B_{i,j,k}|Z_2\cap X_1]\+Pr[X_1]Pr[X_2|X_1]Pr[B_{i,j,k}|X_2\cap X_1]+Pr[Y_1]Pr[Y_2|Y_1]Pr[B_{i,j,k}|Y_2\cap Y_1]\+Pr[Y_1]Pr[Z_2|Y_1]Pr[B_{i,j,k}|Z_2\cap Y_1]+Pr[Y_1]Pr[X_2|Y_1]Pr[B_{i,j,k}|X_2\cap Y_1]\+Pr[Z_1]Pr[Z_2|Z_1]Pr[B_{i,j,k}|Z_2\cap Z_1]+Pr[Z_1]Pr[X_2|Z_1]Pr[B_{i,j,k}|X_2\cap Z_1]\+Pr[Z_1]Pr[Y_2|Z_1]Pr[B_{i,j,k}|Y_2\cap Z_1]$$
     Like  Bookmark