# Details on the Smart Contract Ranking Algorithm Part 1

May 17, 2018 · 6 min read

Generally, it’s very difficult to say if a DApp is good or not, since there are many metrics to judge. For example, whether a DApp is interaction design smooth, whether the design is artful, or whether a DApp solves some real problem. In the final analysis, the way we rank the quality of a DApp boils down to a subjective judgement. Even algorithms are designed by humans, after all. Consequently, judging the quality of a DApp can is a controversial affair, if not a technically impossible to do perfectly. That being said, as a starting point to rank the quality of DApps on Nebulas, we choose another widely-used metric, popularity, as the metric of “goodness” for each DApp.

# Computing the popularity of smart contract

For a DApp, the algorithm evaluates the quality of a DApp by smart contract calling, taking into consideration the importance of different account addresses that call the DApp. In short, the greater the number of different account addresses calling a DApp, the better the DApp’s ranking.

Consider the DApp in to the following figure. It is called by three account addresses. To describe our methodology in simple terms, we could evaluate the popularity of this smart contract as “3”, because each account address contributes a unit of popularity to the smart contract.

However, an account address may invoke multiple different smart contracts, which makes the contribution of the account address to the popularity of smart contracts different. Let’s review the so-called “popularity” metric accordingly. In fact, our calculation of popularity is to calculate how many users can be attracted by a smart contract. For the above picture, if a user’s attention is 1, then the smart contract attracts 3 attention altogether.

However, user attention is limited. When an account calls n smart contracts, the attention that each smart contract can attract is only 1/n. This is intuitive, because a person’s time and energy are limited. We can only devote all our time and energy to a task if we do that one task all day. When we do n things, we can only devote 1/n to every thing on average.

As shown in the following figure, account addresses A and B call two different smart contracts respectively, and then their contribution to the popularity of each smart contract is reduced by ½. For smart contract S_1, the popularity is ½ + ½ + 1 = 2, and for smart contract S_2, its popularity is ½ + ½ = 1.

Let’s consider a more general case, that is, an account address may call multiple smart contracts, and each smart contract may also be called for multiple times. How should we calculate it at this time? An intuitive way is to divide the user attention according to the number of calls, where each smart contract gets the attention of the corresponding number of calls, or contribution. This process can described in mathematical language:

1 ) Construct a bipartite graph, with nodes at one side being ordinary addresses and the other side’s nodes being contracts.

2 ) For the contract u and the ordinary address v, calculate the edge weight between them:

, where

is the number of times that the ordinary address u directly or indirectly calls the contract v.

3) Compute the score of contract v:

# Calculating the importance of account addresses

If you are careful enough, you may have realized that in the previous calculation, we have a hypothesis that each account is the same and there is no difference between accounts. In the final mathematical description, we give a parameter P_u, because the importance of each account address is different. There are different considerations about the importance of accounts in every system, which is related to the design philosophy of a system. In a system adopting PoW algorithm, like Bitcoin, an account or node with strong hash power will be considered as more important. For other systems, like EOS, an account holding a large number of tokens will be considered as more important. In Nebulas, we think that a more frequently used account address is more important, because we believe that frequent usage of Nebulas network is what ultimately gives Nebulas value. Therefore, our consideration of the importance of account address is mainly based on “whether this user frequently use Nebulas blockchain.”

We consider the transfer behaviors of the following two account addresses in history, where A makes multiple transfers and has a large amount of account balance, while B has only one entry, so there is no longer activity recorded and its balance is relatively small. We can think that the account address A is more important than B.

Evaluating the importance of account address is complicated. To some extent, it is similar to the evaluation method for Nebulas rank. For an account address v, the formula to calculate its importance is:

Where S_v is the median balance of the account address in a time period, and R_v is the liquidity indicator of the account address. Parameters a, b, c, d, \ mu, \ lambda are parameters adjusted according to the actual situation.

The interpretation of this formula can be divided into two parts.

1. In the balance part of the account address, this factor is mainly introduced for cheating-proofness’ sake, that is, to increase the cost of cheating. If an adversary wants to cheat, they need to hold a certain amount of money, but money can’t cheat. At the same time, in order to reduce the influence of excessive account balances, we introduced the formula ( s _ v * a / ( s _ v + b ) ). The curve of this latter formula is shown in the following figure:

When the account balance is infinite, the value of this formula approaches a approximately. It gets flatter when b is bigger, and steeper in the opposite case. This reflects that in reality, account balance is a factor that distinguishes the importance of an address.

2. The assets circulated of the account address is the second part of the interpretation of account importance formula. This metric — the activity of an address — as measured as soon as a Nebulas account is in its bootstrap stage. We think that when an account consistently transfers assets in and out, it is more important for Nebulas during bootstrap stage. Therefore, the calculation formula of r _ v is as follows:

In this formula, x is the transfer-in amount of the account, y is the transfer-out amount of the account, and the function is a surface, which is shown as follows:

In this function, when the transfer-in and transfer-out values are equal, the maximum value is obtained. In this case, liquidity is increased to the greatest extent.

It should be noted that although the value of R_v will increase as the balance increases, the influence of R_v is limited because of the introduction of an upper limit. This is a similar mechanism as the one used to constrain the influence of account balances.

Please read the analysis of a actual situation and the treatment of cheating in the next episode.

Official website: Nebulas.io
Github: github.com/nebulasio/go-nebulas
Slack: nebulasio.herokuapp.com
Telegram(EN): t.me/nebulasio

## Nebulasio

Nebulas is an autonomous metanet.

## Nebulasio

Nebulas is an autonomous metanet. The future of collaboration is coming.

Written by

## Nebulas

nebulas.io / Nebulas is a next generation public blockchain, aiming for a continuously improving ecosystem.

## Nebulasio

Nebulas is an autonomous metanet. The future of collaboration is coming.