Weighted Average Cost - Perpetual

Created by Jay Geeth, Modified on Fri, 20 Jan 2023 at 07:03 PM by Jay Geeth

Crypto One supports two Weight Average Cost (WAC) methods. One is perpetual and the other one is periodic. This article will outline the behavior of the accounting algorithm. 


The algorithm moves from down the inventory in an increasing timestamp. Perpetual inventory systems require the cost of goods sold to be calculated each time there is a sale (send or sell transaction). Therefore, at the time of each sale, the algorithm calculates the weighted average cost of the units available in the inventory at the time of the sale. 


In this example, we will have the following transaction list:


Id
Flow
Date
Quantity
Price
1In2/15/2023 17:50
0.1
$927.9
2Out2/15/2023 18:51
0.001112455001

3Out2/15/2023 18:51
0.000090636000

4
In
2/26/2023 17:17
64.23440
$867.62
5
Out
2/15/2023 18:51
0.25
6
Out
2/26/2023 17:17
0.000084000000
7Out3/4/2023 17:281.00


The following transaction pool shows one inflow inventory 0.1 ETH coming in at a unit price of $927.95. Since it's the only inflow transaction the WAC calculated to be $927.95. The remaining outflow transactions are calculated at the said WAC value.


Id
Flow
Date
Quantity
Price
Cost
WAC
Inventory
1
In
2/15/2023 17:50
0.1
$927.95
$92.8
$927.95
0.1
2
Out
2/15/2023 18:51
0.001112455001
$1.03
$927.95
0.098887544999
3
Out
2/26/2023 17:17
0.000090636000

$0.084$927.95
0.098796908999


Then the following transactions occur. One inflow and three outflows. Based on the new remaining inventory and the new inventory the WAC is updated.


Id
Flow
Date
Quantity
 Price
4
In
2/26/23 17:17
64.23440
$867.62
5
Out
2/15/23 18:51
0.25
6
Out
2/26/23 17:17
0.000084000000
7
Out
3/4/23 17:28
1.00


WAC is calculate by getting all the inflow transactions with datetimestamps prior or equal outflow transaction. WAC Equation:


WAC = (Sum of cost basis / Sum of units available) 

Includes:
inflow transactions with timestamps <= outflow tranaction timestamp



Therefore WAC is recalculated the followiing way:
0.098796908999 (remaining inventory) x $927.95 (WAC) = $91.67859170562205
64.23440 (new inventory) x $867.62 (unit price) = $55731.050128


WAC = $55822.72871970562205 ÷ 64.3331969089989 (Total Inventory) = $867.712649173465258


The remaining outflows are calculated the following way:

Id
Flow
Date
Quantity
Price
Cost
WAC
Inventory
4
In
2/26/2023 17:17
64.23440
$867.62
$55,731.05
$867.71
64.3331969089989
5
Out
2/15/2023 18:51
0.25
$216.93
$867.71
64.0831969089989
6
Out
2/26/2023 17:17
0.000084

$0.07
$867.71
64.0831129089989
7
Out
3/4/2023 17:28
1.00
$867.71
$867.71
63.0831129089989


Special Scenario - Transactions having the same timestamp

Case #1 - Two consecutive outflows where the second outflow as an inflow with the same timetamp


IdFlowDateTimeNotes
11Out2/26/2023 11:11
13Out2/26/2023 12:12WAC does not use the inventory on transaction #14
14In2/26/2023 12:12



The WAC is recalculated everytime an inflow transaction occurs in the inventory. In the world of crypto, there is room for complications as inflow/outflow transactions can happen at the same time in transactions such as dex swaps, trades etc. Therefore If a outflow and a inflow transaction happens at the same time, this will trigger a race condition whether the recalculated WAC will apply to the outflow transaction thats in the same timestamp as the inflow.


In Crypto One, we handle this issue by assinging a priority to the ledger line item. If an outflow and an inflow transaction happened at the same time, the system gives priority to the ledger insertion order id. If the outflow contains a lower id than the inflow (regardless of having the same timestamp), the WAC used in the outflow transaction will not include the inventory of the inflow transaction. 


In the opposite scenario, If the inflow was inserted before the outflow (regardless of having the same timestamp), the WAC value used in the outflow transaction will be include the inventory of the inflow transaction.

Above scenario assumes, the transaction prior to the outflow is also an outflow.
 
Case #2 - Inflow transaction followed by an outflow and inflow pair with the same timestamp


In the following scenario, we have:


IdFlowDateTimeNotes
11Out2/26/2023 11:11
12In2/26/2023 11:11Receive transaction has a higher id than send. Does not use transaction #11 for WAC
13Out2/26/2023 12:12WAC contains previous inventory prior to #11, #12 and #14 (since #14 happened on a time <= to the #13 timestamp)
14In2/26/2023 12:12


Upon reaching transaction #11, transaction #12 will not be used to recalculate WAC regardless of having the same timestamp as #11. Recalculation of WAC will infact does not happen at all at transaction #11. It will continue to use the WAC calculated prior to transaction #11.

On reaching transaction #12, a trigger is sent to the algorithm to recalculate WAC upon next outflow. At transaction #13 the trigger is executed to recalculate WAC on the next outflow. Hence a new WAC is calculated at #13 by combining the WAC calculated on #11 + #12 and #14.

In summary, this special case occurs because #12 which is an inflow triggered a recalculation of WAC. Therefore #13 gets #14 included in the WAC calculation vs where #11 did not get #12 included in the WAC.


Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select atleast one of the reasons
CAPTCHA verification is required.

Feedback sent

We appreciate your effort and will try to fix the article