State between two blocks? Help would be greatly appreciated

Hi, hope everyone is well! Happy new year :slight_smile:

I’ve got a question I’ve been searching the answer to without success for multiple months.

Do smart contracts (and accounts) states have an “intermediate” state between two blocks?

I understand that the beginning of the blocks are a single source of truth for accounts and applications states, but what happens between blocks?

For example, imagine two wallets.
Wallet A has 10 algos, wallet B has 0 algos.
Wait for the beginning of a new block, then send a pay transaction from A to B for 10 algos.

If I query the account balance of B immediately after I submit this transaction, it would say that it has zero algos, until the beginning of next block.

That would make sense. But then consider this new scenario:

Wallet A has 10 algos, wallet B has 0 algos.
Wait for the beginning of a new block, then send a pay transaction from A to B for 10 algos, THEN immediately send a pay transaction from B to A for 10 algos.
It… works. Why?

Do wallet B immediately knows that it WILL receive 10 algos? If so, how? Why doesn’t it show in the account balance BUT is usable to transact with?
Are smart contracts aware of this “intermediary state”?

Thanks a million to people that may help me with this.

Have a good day!

The question is, is THAT possible? I don’t think so. You send only spend what you HAVE. So, B can send the 10 Algos only one block after (s)he received it.

That is not true… You can send algos or other tokens, or can make inner app calls within the block.

It works the way that node with which you communicate either accepts or denies your transaction. It accepts your second transaction if it has added previous transaction to the tx pool. You cannot rely on this tx pool because the committee which creates block might not accept it for example if you attempt to do double spending, but it is technically possible to track these transactions and give use instant blockchain feeling.

There are no events in the official algorand code. So the wallet can know this only if it fetches current block and parses txs in the pool. To parse txs in pool is so much complicated (because account can receive assets for example from inner transactions) that it is not very efficient to spend time on coding it, and 3 second delay is usually good for everyone.

Yes. Everyone can do smart contract call to do arbitrage right after someone makes app call to swap some assets. Both swaps will be executed in single block if you are fast enough…

2 Likes

Thanks for your answer! It’s a little bit more clear to me.
However, your last sentence was confusing.

Everyone can do smart contract call to do arbitrage right after someone makes app call to swap some assets.

Swapping and profitability of arbitrage trades is only based upon two variables: the reserves of the asset A and the asset B of the application (usually an AMM pool).
Since these variables won’t be “officially” updated between two blocks, how somebody could know that there is an opportunity to seize between two blocks?

Let’s say you query this information with algod, it will only get updated at the beginning of new blocks…
That’s what I can’t wrap my head around… :sweat_smile:

For example at Pact the reserves are stored in the global app variables. You may calculate it with the inflow of money, or i think also there should be available information from the app transaction object about this effect… (I am not 100% sure right now)

1 Like

Disclaimer: I’m speaking as someone who has tinkered with submitting txns and monitoring the txn pool, not as someone who understands how Algorand smart contracts work and how state is computed. I could be totally wrong here.

THEN immediately send a pay transaction from B to A for 10 algos.
It… works. Why?

The intermediate state is the txn pool. Imagine it like so

  1. [Block N] [empty txn pool for Block N+1]
  2. [Block N] [ (A sends 10 ALGO to B) ]
  3. [Block N] [ (A sends 10 ALGO to B) (B sends 10 ALGO to A) ]
  4. [Block N] [Block N+1] [empty txn pool for Block N+2]

Step 2 succeeds because in Block N, wallet A has 10 ALGO. So it is able to send 10 ALGO to B.
Step 3 succeeds because in the pending txn pool, B received 10 ALGO from A. (this is the intermediate state). So B has enough ALGO to send 10 ALGO to A.

Since these variables won’t be “officially” updated between two blocks, how somebody could know that there is an opportunity to seize between two blocks?

Monitor the txn pool and compute the intermediate state yourself. There’s a REST endpoint that returns the txn pool.

I can give one more interesting idea…

You can have USDC on your account, and borrow Algo. With algo you perform for example arbitrage on some tokens, and then repay the loan. Everything within one block. And infrastracture is already out there…

Diclaimer… I don not work for Folks :slight_smile:

Thanks you guys! Guess I was not that subtle in my original question so you understood my intentions :smiley:

Since you all answered honestly and been very helpful, I cannot remain “secretive” and I’ll respect you, so please find some info below :wink:

So… I already built all of that. As of now, I’m capable of reading and predicting with 99% accuracy the future states of AMM pools and find the optimal amount to do arbitrage between pools & amms, with dual, triangular, quadratic arbitrage, you name it. I’ve spent weeks on this.
I find the opportunities and, I think, manage to do the whole thing (search/find/send txns) extremely fast (~1ms).
But I’m still too slow in regards of the other arbitragers.

Namely this one : Algorand Account

So I was thinking that I was missing a piece of the puzzle, hence my original question, but apparently not. So I guess this (these?) guy(s) are too strong for me :smiley:

Time to let go I guess (now that’s a skill I’ve got to be working on!)

PS: The account I linked in my previous post is so fast that he is confident enough to arbitrage without atomic transactions. Yep. Time to let go :smiley:

What’s your infrastructure setup look like? Where is your code running? Do you have multiple instances around the world? How many relays are you connecting to?

Optimal networking is just as important as optimal code.

1 Like

Would you mind sharing the code? If you’re not gonna use it, I will :slight_smile:

@cerf @guanzo

lets cooperate

i also see it as the infrastructure challange

we already have events from the algod node in redis pubsub in customized algod node

Also the zero risk smart contract to do arbitrage is in progress: GitHub - thaddavis/arbitage_bot_challenge: Coding Challenge presented by Ludovit Sholz and Laurens Michiels van Kessenich to Tad Duval on Dec. 19th at 10:00am EST

Even Deflex protocol does not calculate the prices correctly… If i want to swap 1000 usdc to Vote Coin it gives me currently these routes:

https://app.alammex.com/api/quote?chain=mainnet&fromASAID=31566704&toASAID=452399768&atomicOnly=false&amount=1000000000&type=fixed-input&disabledProtocols=&referrerAddress=AWALLETCPHQPJGCZ6AHLIFPHWBHUEHQ7VBYJVVGQRRY4MEIGWUBKCQYP4Y
{
   "quote":76744854019,
   "profit":{
      "amount":7276733820,
      "asa":{
         "id":452399768,
         "name":"Vote Coin",
         "unit_name":"Vote",
         "url":"https://www.vote-coin.com/",
         "decimals":6,
         "is_deleted":0,
         "is_wrapped":0,
         "is_pool_asset":0,
         "is_verified":1,
         "verification_tier_pera":1,
         "verification_tier_algoexplorer":1,
         "is_nft":0,
         "supply_circulating":"3548714942152",
         "creator_address":"P65LXHA5MEDMOJ2ZAITLZWYSU6W25BF2FCXJ5KQRDUB2NT2T7DPAAFYT3U",
         "reserve_address":"P65LXHA5MEDMOJ2ZAITLZWYSU6W25BF2FCXJ5KQRDUB2NT2T7DPAAFYT3U",
         "clawback_address":null,
         "freeze_address":null,
         "supply_total":"1000000000000000",
         "total_holders":241,
         "total_swaps":143696,
         "total_transactions":542032,
         "creation_round":17787025,
         "creation_round_time":1638475683,
         "price_algo":0.05830459,
         "tvl_algo":230977.25,
         "market_cap_algo":206906.31,
         "market_cap_fully_diluted_algo":58304590,
         "price_usd":0.012084326,
         "tvl_usd":47872.807,
         "market_cap_usd":42883.816,
         "market_cap_fully_diluted_usd":12084326
      }
   },
   "priceBaseline":0.012038220593928494,
   "route":[
      {
         "percentage":74,
         "path":[
            {
               "name":"humble: 0.3% fee tier ",
               "class":[
                  [
                     "HumbleSwapper"
                  ]
               ],
               "in":{
                  "id":31566704,
                  "name":"USDC",
                  "unit_name":"USDC",
                  "url":"https://www.centre.io/usdc",
                  "decimals":6,
                  "is_deleted":0,
                  "is_wrapped":1,
                  "is_pool_asset":0,
                  "is_verified":1,
                  "verification_tier_pera":2,
                  "verification_tier_algoexplorer":2,
                  "is_nft":0,
                  "supply_circulating":"122420390868644",
                  "creator_address":"2UEQTE5QDNXPI7M3TU44G6SYKLFWLPQO7EBZM7K7MHMQQMFI4QJPLHQFHM",
                  "reserve_address":"2UEQTE5QDNXPI7M3TU44G6SYKLFWLPQO7EBZM7K7MHMQQMFI4QJPLHQFHM",
                  "clawback_address":null,
                  "freeze_address":"3ERES6JFBIJ7ZPNVQJNH2LETCBQWUPGTO4ROA6VFUR25WFSYKGX3WBO5GE",
                  "supply_total":"18446744073709551615",
                  "total_holders":33091,
                  "total_swaps":2296469,
                  "total_transactions":6386268,
                  "creation_round":8874561,
                  "creation_round_time":1599252577,
                  "price_algo":4.847283,
                  "tvl_algo":37897390,
                  "market_cap_algo":593406270,
                  "market_cap_fully_diluted_algo":89416588000000,
                  "price_usd":1.0046576,
                  "tvl_usd":7854688.9,
                  "market_cap_usd":122990570,
                  "market_cap_fully_diluted_usd":18532661000000
               },
               "out":{
                  "id":0,
                  "name":"Algorand",
                  "unit_name":"ALGO",
                  "url":"https://algorand.org",
                  "decimals":6,
                  "is_deleted":0,
                  "is_wrapped":0,
                  "is_pool_asset":0,
                  "is_verified":1,
                  "verification_tier_pera":2,
                  "verification_tier_algoexplorer":2,
                  "is_nft":0,
                  "supply_circulating":"7203367518352945",
                  "creator_address":null,
                  "reserve_address":null,
                  "clawback_address":null,
                  "freeze_address":null,
                  "supply_total":"10000000000000000",
                  "total_holders":0,
                  "total_swaps":8237287,
                  "total_transactions":3,
                  "creation_round":null,
                  "creation_round_time":null,
                  "price_algo":1,
                  "tvl_algo":null,
                  "market_cap_algo":7110760100,
                  "market_cap_fully_diluted_algo":10000000000,
                  "price_usd":0.207262,
                  "tvl_usd":null,
                  "market_cap_usd":1473790400,
                  "market_cap_fully_diluted_usd":2072620000
               }
            },
            {
               "name":"pact: 0.01% fee tier ",
               "class":[
                  [
                     "PactConstantProductSwapper"
                  ]
               ],
               "in":{
                  "id":0,
                  "name":"Algorand",
                  "unit_name":"ALGO",
                  "url":"https://algorand.org",
                  "decimals":6,
                  "is_deleted":0,
                  "is_wrapped":0,
                  "is_pool_asset":0,
                  "is_verified":1,
                  "verification_tier_pera":2,
                  "verification_tier_algoexplorer":2,
                  "is_nft":0,
                  "supply_circulating":"7203367518352945",
                  "creator_address":null,
                  "reserve_address":null,
                  "clawback_address":null,
                  "freeze_address":null,
                  "supply_total":"10000000000000000",
                  "total_holders":0,
                  "total_swaps":8237287,
                  "total_transactions":3,
                  "creation_round":null,
                  "creation_round_time":null,
                  "price_algo":1,
                  "tvl_algo":null,
                  "market_cap_algo":7110760100,
                  "market_cap_fully_diluted_algo":10000000000,
                  "price_usd":0.207262,
                  "tvl_usd":null,
                  "market_cap_usd":1473790400,
                  "market_cap_fully_diluted_usd":2072620000
               },
               "out":{
                  "id":452399768,
                  "name":"Vote Coin",
                  "unit_name":"Vote",
                  "url":"https://www.vote-coin.com/",
                  "decimals":6,
                  "is_deleted":0,
                  "is_wrapped":0,
                  "is_pool_asset":0,
                  "is_verified":1,
                  "verification_tier_pera":1,
                  "verification_tier_algoexplorer":1,
                  "is_nft":0,
                  "supply_circulating":"3548714942152",
                  "creator_address":"P65LXHA5MEDMOJ2ZAITLZWYSU6W25BF2FCXJ5KQRDUB2NT2T7DPAAFYT3U",
                  "reserve_address":"P65LXHA5MEDMOJ2ZAITLZWYSU6W25BF2FCXJ5KQRDUB2NT2T7DPAAFYT3U",
                  "clawback_address":null,
                  "freeze_address":null,
                  "supply_total":"1000000000000000",
                  "total_holders":241,
                  "total_swaps":143696,
                  "total_transactions":542032,
                  "creation_round":17787025,
                  "creation_round_time":1638475683,
                  "price_algo":0.05830459,
                  "tvl_algo":230977.25,
                  "market_cap_algo":206906.31,
                  "market_cap_fully_diluted_algo":58304590,
                  "price_usd":0.012084326,
                  "tvl_usd":47872.807,
                  "market_cap_usd":42883.816,
                  "market_cap_fully_diluted_usd":12084326
               }
            }
         ]
      },
      {
         "percentage":26,
         "path":[
            {
               "name":"pact: 0.01% fee tier ",
               "class":[
                  [
                     "PactConstantProductSwapper"
                  ]
               ],
               "in":{
                  "id":31566704,
                  "name":"USDC",
                  "unit_name":"USDC",
                  "url":"https://www.centre.io/usdc",
                  "decimals":6,
                  "is_deleted":0,
                  "is_wrapped":1,
                  "is_pool_asset":0,
                  "is_verified":1,
                  "verification_tier_pera":2,
                  "verification_tier_algoexplorer":2,
                  "is_nft":0,
                  "supply_circulating":"122420390868644",
                  "creator_address":"2UEQTE5QDNXPI7M3TU44G6SYKLFWLPQO7EBZM7K7MHMQQMFI4QJPLHQFHM",
                  "reserve_address":"2UEQTE5QDNXPI7M3TU44G6SYKLFWLPQO7EBZM7K7MHMQQMFI4QJPLHQFHM",
                  "clawback_address":null,
                  "freeze_address":"3ERES6JFBIJ7ZPNVQJNH2LETCBQWUPGTO4ROA6VFUR25WFSYKGX3WBO5GE",
                  "supply_total":"18446744073709551615",
                  "total_holders":33091,
                  "total_swaps":2296469,
                  "total_transactions":6386268,
                  "creation_round":8874561,
                  "creation_round_time":1599252577,
                  "price_algo":4.847283,
                  "tvl_algo":37897390,
                  "market_cap_algo":593406270,
                  "market_cap_fully_diluted_algo":89416588000000,
                  "price_usd":1.0046576,
                  "tvl_usd":7854688.9,
                  "market_cap_usd":122990570,
                  "market_cap_fully_diluted_usd":18532661000000
               },
               "out":{
                  "id":452399768,
                  "name":"Vote Coin",
                  "unit_name":"Vote",
                  "url":"https://www.vote-coin.com/",
                  "decimals":6,
                  "is_deleted":0,
                  "is_wrapped":0,
                  "is_pool_asset":0,
                  "is_verified":1,
                  "verification_tier_pera":1,
                  "verification_tier_algoexplorer":1,
                  "is_nft":0,
                  "supply_circulating":"3548714942152",
                  "creator_address":"P65LXHA5MEDMOJ2ZAITLZWYSU6W25BF2FCXJ5KQRDUB2NT2T7DPAAFYT3U",
                  "reserve_address":"P65LXHA5MEDMOJ2ZAITLZWYSU6W25BF2FCXJ5KQRDUB2NT2T7DPAAFYT3U",
                  "clawback_address":null,
                  "freeze_address":null,
                  "supply_total":"1000000000000000",
                  "total_holders":241,
                  "total_swaps":143696,
                  "total_transactions":542032,
                  "creation_round":17787025,
                  "creation_round_time":1638475683,
                  "price_algo":0.05830459,
                  "tvl_algo":230977.25,
                  "market_cap_algo":206906.31,
                  "market_cap_fully_diluted_algo":58304590,
                  "price_usd":0.012084326,
                  "tvl_usd":47872.807,
                  "market_cap_usd":42883.816,
                  "market_cap_fully_diluted_usd":12084326
               }
            }
         ]
      }
   ],
   "flattenedRoute":{
      "humble":0.37,
      "pact":0.63
   },
   "quotes":[
      {
         "class":"PactConstantProductSwapper",
         "name":"pact: 0.01% fee tier ",
         "value":69468120199
      },
      {
         "class":"TinymanSwapper",
         "name":"tinyman: 0.3% fee tier",
         "value":43443902428
      },
      {
         "class":"TinymanV2Swapper",
         "name":"tinyman-V2: 0.3% fee tier ",
         "value":34690654680
      }
   ],
   "txnPayload":{
      "iv":"7003350cf7e66cbee5a72a42a47193f7",
      "data":""
   },
   "requiredAppOptIns":[
      
   ],
   "protocolFees":{
      "humble":85442348,
      "pact":4835408,
      "deflex":30710225
   },
   "fromASAID":31566704,
   "toASAID":452399768,
   "type":"fixed-input"
}
Swap 1000 USDC for at least 75977.405479 Vote on deflex 31566704-0-452399768: 0.0004% fee tier (74%)
74% USDC -> Humble USDC/Algo -> Algo->Vote pact: 0.01% fee tier
26% USDC/Vote pact: 0.01% fee tier

So if we move the price of vote coin up by 1.2%, there must be arbitrage opportunities out there in minimum with the Vote->GoBTC 0,01% pool or any of not used Vote/Algo pools namely on tinyman.

Source: Vote Coin – Vestige

I don’t think arbitrage works well on pools with low TVL. Your swap amounts cause too much price impact which at best negates the arb profit and at worst you suffer a loss. Sure you can swap smaller amounts but at that point the profit isn’t worth it.

I dont get you… You cannot suffer any loss with doing arbitrage through either group txs or inner txs from dapp. Algorand is already ready to do risk free arbitrages with minimum price requirements and good profits because people use pool directly or deflex kind protocols are not evolved enough.

Sorry yes you’re right, you can guarantee profits with slippage and flash loans. I’ve been doing CEX arb recently so I got used to taking losses. Anyways, my point was that price impact still disporportionaly affects pools with small TVL, so your txn will usually fail.

if you do calculations properly it should not fail… if you are affecting small pool to move the price above the sliperage it seems to me that your calculations might get better to do better arbitrage

btw i have little doubt to flash loans by folks as they charge i think 0,1% … to make enough profit in arbitrage to cover this fee is quite difficult… it might be just easier to swap from base currency do few swaps and then swap to the base currency back