tsr-tezos-contract-statistics

Tezos contract stats

Script to pull contract data from tzkt indexer posgres db, calculate statistics and push resulting json files to website dataset storage.

The resulting datasets are used by thestackreport.xyz dashboards to enable historic views over time of Tezos smart contract usage.

This codebase is made open source for the purpose of data transparency and to serve as example code.

Statistics calculated

  • Daily contract calls per entrypoint
  • Accounts calling contract per day (split by wallet & contract account)
  • Transaction targets from contract per day
  • Top callers of the contract
  • Top targets by the contract

At the contract level, total numbers are stored directly in the document.
Full time series files are created as separate json/csv files and uploaded to the cloud storage layer.

Time series

Contract calls per day
Daily contract calls, split by entrypoint.

Usage per day

Accounts calling contract per day (split by wallet & contract account)

Xtz volume

  • Xtz volume to contract
    • from contract calls
    • from direct transactions
  • Xtz volume through contract
    • from contract calls
    • from direct transactions (in case of a pass-through contract)
  • Xtz volume from contract

Xtz volume to contract
Xtz volume towards contract can be further split into xtz volume sent towards contract through contract calls & ctz volume sent towards contract through direct xtz transactions.

Xtz volume through contract
Xtz volume through contract is the xtz volume from transactions that directly send through the xtz to another account within the same contract call transaction group.

Xtz volume from contract
xtz volume from contract is xtz volume that comes from the contract which was sent to the contract in an earlier contract call.

Xtz transaction attribution

Xtz transactions are transactions where the native Tezos xtz token is being transferred from one account to another. An xtz amount can also be included in a smart contract call. Depending on the smart contract code this xtz amount is used or sent in other transactions or becomes part of the current account balance for the smart contract.

An example of a smart contract call which sends xtz to the contract is for example placing an offer on the objkt.com marketplace contract. This sends the offered xtz amount to the contract. Then upon accepting the offer by calling the fulfill_offer entrypoint, the xtz is transferred from the contract to the person accepting the offer.

For xtz being transferred to the contract through contract calls it is straightforward to attribute the xtz to a specific contract call as it is part of that contract call transaction.

When xtz is being transferred from the contract it is a bit less straightforward because the xtz transaction often becomes a separate transaction from the contract to one or multiple wallets.

In some cases multiple contract calls are generating xtz transactions within the same operation group. So it is not simply a matter of looking to the contract call transaction in the transaction group.

Data attributes used in transaction attribution

Ophash

The hash of the entire operation group. Used to query the mongodb.

Counter

Used within a contract to validate that each operation is sequenced in the correct order. Unique at the contract level.

https://www.opentezos.com/tezos-basics/operations

Sender ID & Target ID

Used to identify the contract from which the transaction is sent or which it is targeting.

I’m trying to build a robust analysis for xtz transfers generated by smart contracts.

Is it possible to explicitly link an xtz transfer to a contract call transaction based on on-chain (& RPC available) data?

What I’ve got so far is I can implicitly link them by combining xtz transfers within an operation group towards the original contract call. This works for basic contract calls when only the direct contract code is executed. But when the code within a contract calls on other contracts within the same operation group, those other contracts can also in turn generate more xtz transactions.

Another implicit link could be to look at the sender of the xtz transfer because it must come from the contract executing the code. But then there might still be multiple contract calls towards that contract.

For example in the following operation group there are two calls to ctez_to_tez on the cTEZ-TEZ Plenty Stable swap contract:

[https://tzkt.io/ooMhUwWAoefamMft7oknf6DidEMn433ZtAoMnjy4GkaSRoNaoDr/66548700/81][1]

[https://tzkt.io/ooMhUwWAoefamMft7oknf6DidEMn433ZtAoMnjy4GkaSRoNaoDr/66548700/103][2]

And two xtz transactions from the contract to the original sender:

[https://tzkt.io/ooMhUwWAoefamMft7oknf6DidEMn433ZtAoMnjy4GkaSRoNaoDr/66548700/89][3]

[https://tzkt.io/ooMhUwWAoefamMft7oknf6DidEMn433ZtAoMnjy4GkaSRoNaoDr/66548700/111][4]

In this case another assumption could be made that each transfer or xtz send transaction is executed before the next contract call code starts.
That would mean in the above call that the xtz transactions are actually triggered by the following calls to ctez_to_tez_callback:

[https://tzkt.io/ooMhUwWAoefamMft7oknf6DidEMn433ZtAoMnjy4GkaSRoNaoDr/66548700/87][5]

[https://tzkt.io/ooMhUwWAoefamMft7oknf6DidEMn433ZtAoMnjy4GkaSRoNaoDr/66548700/109][6]

Method

In the case that there are more than one contract calls within an operation group.

Per contract call look at the nonce position in the operation group.

per xtz transaction determine the latest contract call to the contract that sent the transaction.

get call contract calls for the contract within the op group.

per xtz transaction coming from that contract within the op group:
Look for the highest contract call nonce value that is lower than the transaction nonce value itself.

Visit original content creator repository
https://github.com/The-Stack-Report/tsr-tezos-contract-statistics

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *