September 16, 2024

How to lose $ long dozen of Users ’ finances ( as A Blockchain developer )

7 min read
rb_thumb

rbs-img

Too Long ; Did n’t read data type be of import and neglect them lead to serious effect .

modern linguistic communication the like JavaScript and python be use “ duck type ” to ascertain the type of variable .

In most of instance , your linguistic communication interpreter volition handle type In the correct fashion .

The problem begin when your plan need to run ( even simple ) equation on big number .

The gov tell we be non In recession , simply atomic number 85 the same time , we hear about skyrocket rising prices , interest rate gain , and layoff In nearly every sector of the economic system .

contempt crypto and TradFi be impact the to the highest degree badly , many company be still build their token , communications protocol , and DeFi merchandise .

are you 1 of them ?

today , i volition be speak on data type , and maintain on .

i have got something rattling of import to tell .

You mightiness fancy Pine Tree State A A 60+-year-old prof from Massachusetts Institute of Technology excruciate educatee with public lecture about topic that No long affair .

simply that ’ sec non true .

data type be still of import , and neglect them lead to serious effect .

atomic number 53 volition attempt to briefly move through with all potential issue and turn to them so you put on ’ T find the eighter min you spend on Reading this article waste .

modern linguistic communication the likes of JavaScript and python be use “ duck type ” to ascertain the type of variable .

If we assign this kind of formula A = II + two to a variable , the linguistic communication interpreter cognise information technology be deal with number , and information technology volition perform mathematical operation on this literal .

The duck typewriting can be explain by this sentence : “ If information technology walk the likes of A duck and information technology quack the likes of A duck , and so information technology must be angstrom unit duck ” .

When you look nigher into the signification of information technology – IT do perfect sense .

If literal ha missive and number , information technology must be A twine , and that ’ s clear .

merely what if IT ha number ?

be information technology A boolean , whole number , decimal , float , OR day of the month .

in most instance , your linguistic communication interpreter volition handle type IN the correct style .

The problem set out when your plan need to run ( even simple ) equation on big number .

“ If information technology walk the like A duck and information technology quack the like A duck , and so information technology must be A duck ” , right ?

actually information technology ’ s non .

Ethereum denomination in A nutshell IN the next paragraph , atomic number 53 be refer to Ethereum common denomination – Wei dynasty and gwei .

let Pine Tree State briefly acquaint you to them so we mouth the common linguistic communication .

The little denomination be ace Wei dynasty , and one quintessence equal 1,000,000,000,000,000,000 Wei dynasty ( eighteen zippo ) .

iodine iterate – XVIII aught .

information technology be difficult to wrap up our psyche around such big number merely they do A difference and affair A muckle .

The next common denomination be one gwei .

one quintessence equal 1,000,000,000 gwei ( nine zilch ) .

Gwei be to a greater extent endurable for human being – IN the end , everybody desire to be A millionaire , right ?

( trice , wink ) let ’ s amount IT up – one quintessence equal : 1,000,000,000 gwei ( nine zippo ) 1,000,000,000,000,000,000 Wei ( xviii null ) technical note : Ethereum ha 2 layer – the capital punishment layer and the consensus layer .

The death penalty layer be utilise Wei dynasty to represent ether value , and the consensus layer be use gwei .

If you be A blockchain developer , you require to larn to interact with both .

Real-World example : Stakefish ’ tip and MEV pool iodin be A computer software engineer At stakefish .

i ’ m responsible for build our DeFi merchandise pallet , and 1 of the to the highest degree recent 1 be our tip and MEV pool for Ethereum .

start from Sept fifteen , 2022 , all validators be eligible for transaction tip and can take part in MEVs to realise additional wages .

transaction tip and MEVs be earn when the validator propose A New block .

We make up one’s mind to construct A smart contract that compile all wages into A common burial vault and let user to claim their share from information technology .

i be non try to publicise our merchandise , merely iodine require to put the context of use of this article .

If you be to a greater extent interested in this merchandise , you can read more than here .

iodin ’ 1000 non sell you anything besides my experience .

AS iodin cite , we have got A smart contract that receive transaction tip and MEV wages earn by validators .

IT mean our smart contract ha A jolly big Balance .

IT be 963+ ether now ( $ 1.1M ) , and we have got 8671 validators contribute to information technology .

The critical component part responsible for synchronisation betwixt Ethereum capital punishment and the consensus layer be oracle .

information technology be A rattling of import system that allow uracil to ascertain what validators be contribute to the pool .

The prophet be write in python , merely information technology could be write in JavaScript – the problem remain unchanged , and i volition turn out information technology before long .

let ’ s deep honkytonk into the code !

wherefore data type matter The Balance of the smart contract equal 963,135,554,442,603,402,422 Wei dynasty ( 963 ether ) now .

This number isn ’ T simply hard to understand for human being merely as well for information processing system ( linguistic communication interpreter to be exact ) .

allow ’ s bank check JavaScript : const vault_balance = parseInt ( “ 963135554442603402422 ” ) console.log ( vault_balance ) // 963135554442603400000 ( lose 2422 Wei dynasty IN entire ) iodine simply cast the Balance from string to int , and iodine be already 2422 Wei dynasty short .

We didn ’ T run whatever equation yet .

The smart contract Balance be so high thanks to many validators lend to information technology .

now , allow ’ s calculate what the mean validator share of contract ’ s Balance now : const vault_balance = parseInt ( “ 963135554442603402422 ” ) const validator_count = 8671 const avg_validator_contribution = vault_balance / validator_count // 111075487768723730 ( lose septet wei per validator ) The mean share be 0.111 quintessence .

but this amount of money ISN ’ T correct – we be actually seven wei short .

IT be 60,697 Wei in entire ( sevener Wei time 8671 validators ) .

i volition demonstrate the correct number after on .

proceed farther down feather the rabbit hole of loss – allow ’ s compute the entire sum of wages per give validator .

have in psyche that the user need to wedge xxxii quintessence to commence A validator , so iodin volition subtract IT from the validator Balance .

And atomic number 53 volition take A Associate in Nursing example 1 random validator contribute to the smart contract that ha A Balance of 32.779 quintessence .

const vault_balance = parseInt ( “ 963135554442603402422 ” ) // ( lose 2422 Wei ) const validator_count = 8671 const avg_validator_contribution = vault_balance / validator_count // ( lose heptad Wei ) const initial_deposit = parseInt ( “ 32000000000000000000 ” ) const validator_balance = parseInt ( “ 32779333896000000000 ” ) const total_validator_rewards = validator_balance – initial_deposit + avg_validator_contribution // 890409383768723700 ( lose xxiii wei per validator ) The entire wages earn by this validator equal 0.8904 quintessence , merely this value isn ’ T accurate A good .

at this moment , we miscount for 199,443 Wei dynasty In entire ( XXIII Wei time 8671 validator ) .

AS you can see , this mode of compute number isn ’ T sustainable .

What travel incorrect ?

there be 2 problem with the code above : In JavaScript , the maximal safe value for whole number equal 2^53 – ace merely .

IT mean information technology can handle upwards to 9007199254740991 Wei ( 0.009 ether ) technically , we could have got use BigInt merely we would have got problem with division .

We would end upwardly with “ float ” value .

The float be the root of all wickedness In finance because they be approximative .

IT mean they lose precision .

We require to apply decimal fraction .

( The master difference betwixt decimal and float be that decimal shop exact value and float approximates . )

If you of all time have do whatsoever Ethereum-related steganography in JavaScript , you must ’ ve heard about ethers.js .

This library contain all the necessary public utility to interact with the blockchain .

To bushel the issue above , we volition employ 1 of the tool call BigNumber that back up highly big number and handgrip decimal fraction in the right manner .

let ’ s make IT !

const vault_balance = BigNumber.from ( “ 963135554442603402422 ” ) // No loss const validator_count = BigNumber.from ( 8671 ) const avg_validator_contribution = vault_balance.div ( validator_count ) // No loss // 111075487768723723 const initial_deposit = BigNumber.from ( “ 32000000000000000000 ” ) const validator_balance = BigNumber.from ( “ 32779333896000000000 ” ) const total_validator_rewards = validator_balance.sub ( initial_deposit ) .add ( avg_validator_contribution ) // 890409383768723723 as you can see , now we end upward with the accurate number .

How make i cognise this be actually the right number ?

iodin volition ingeminate the same physical exertion in python to turn out that i ’ m right .

let ’ s attempt information technology In python python back up long whole number , so the value volition non acquire of a sudden cut A we have got see IN JavaScript .

regrettably , IT still determine all float number A float S by default : vault_balance = int ( “ 963135554442603402422 ” ) # No loss validator_count = 8671 avg_validator_contribution = vault_balance / validator_count # 111075487768723728 ( fivesome wei overly much ) initial_deposit = int ( “ 32000000000000000000 ” ) validator_balance = int ( “ 32779333896000000000 ” ) total_validator_rewards = validator_balance – initial_deposit + avg_validator_contribution # 890409383768723712 ( lose XI Wei dynasty ) wonder where precisely IT lose precision ?

The division cast avg_validator_contribution to be adrift or else of decimal fraction .

The correct snip would look the likes of that : vault_balance = decimal ( “ 963135554442603402422 ” ) validator_count = decimal ( 8671 ) avg_validator_contribution = vault_balance / validator_count # 111075487768723723 initial_deposit = decimal ( “ 32000000000000000000 ” ) validator_balance = decimal ( “ 32779333896000000000 ” ) total_validator_rewards = validator_balance – initial_deposit + avg_validator_contribution # 890409383768723723 now , the value return by python and JavaScript be exact .

check on your ain !

These kind of loss be fringy , and they can be easy miss .

often , we find out about them when they deepen over time and grow to significant number .

situation the likes of that e’er give worry , non but to developer merely as well to other department such A finance OR legal .

You should e’er try out your formula , and ne’er apply nice one shot number to make so !

information technology would mean the universe to ME if you follow ME on twitter .

i be focus my activity on software system technology and blockchain .

i be open-sourcing most of my work , so you mightiness desire to check over my GitHub .

Source: hackernoon.com

Leave a Reply

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