You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/blockchain-development-tutorials/forte/fixed-point-128-bit-math.md
+24-25Lines changed: 24 additions & 25 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -16,10 +16,9 @@ keywords:
16
16
sidebar_label: DeFi Math Utils
17
17
---
18
18
19
-
# High-Precision Fixed-Point 128 Bit Math
19
+
# High-Precision Fixed-Point 128 Bit Math
20
20
21
-
## Overview
22
-
Dealing with decimals is a notorious issue for most developers on other chains, especially when working with decentralized finance (DeFi). Blockchains are deterministic systems and floating-point arithmetic is non-deterministic across different compilers and architectures, which is why blockchains use fixed-point arithmetic via integers (scaling numbers by a fixed factor).
21
+
Dealing with decimals is a notorious issue for most developers on other chains, especially when working with decentralized finance (DeFi). Blockchains are deterministic systems and floating-point arithmetic is non-deterministic across different compilers and architectures, which is why blockchains use fixed-point arithmetic via integers (scaling numbers by a fixed factor).
23
22
24
23
The issue with this is that these fixed-point integers tend to be very imprecise when using various mathematical operations on them. The more operations you apply to these numbers, the more imprecise these numbers become. However [`DeFiActionsMathUtils`] provides a standardized library for high-precision mathematical operations in DeFi applications on Flow. The contract extends Cadence's native 8-decimal precision (`UFix64`) to 24 decimals using `UInt128` for intermediate calculations, ensuring accuracy in complex financial computations while maintaining deterministic results across the network.
25
24
@@ -54,15 +53,15 @@ let output = afterFee * price // More precision lost
54
53
let finalAmount = output / someRatio // Even more precision lost
55
54
```
56
55
57
-
After three-to-four sequential operations, significant cumulative rounding errors can occur, especially when dealing with large amounts. Assuming a rounding error with eight decimals (1.234567885 rounds up to 1.23456789, causing a rounding error of 0.000000005), then after 100 operations with this error and dealing with one million dollars USDF, the protocol loses $0.5 in revenue from this lack of precision. This might not seem like a lot, but if we consider the TVL of Aave, which is around 40 billion USD, then that loss results in $20,000 USD!
56
+
After three-to-four sequential operations, significant cumulative rounding errors can occur, especially when dealing with large amounts. Assuming a rounding error with eight decimals (1.234567885 rounds up to 1.23456789, causing a rounding error of 0.000000005), then after 100 operations with this error and dealing with one million dollars USDF, the protocol loses $0.5 in revenue from this lack of precision. This might not seem like a lot, but if we consider the TVL of Aave, which is around 40 billion USD, then that loss results in $20,000 USD!
58
57
59
58
## The Solution: 24-Decimal Precision
60
59
61
60
[`DeFiActionsMathUtils`] solves this with `UInt128` to represent fixed-point numbers with 24 decimal places (scaling factor of 10^24). This provides 16 additional decimal places for intermediate calculations, dramatically reducing precision loss.
62
61
63
62
:::Warning
64
63
65
-
There is still some precision loss occurring, but it is much smaller than with eight decimals.
64
+
There is still some precision loss occurring, but it is much smaller than with eight decimals.
66
65
67
66
:::
68
67
@@ -147,7 +146,7 @@ let protocolFee = DeFiActionsMathUtils.toUFix64RoundUp(calculatedFee)
147
146
let displayValue = DeFiActionsMathUtils.toUFix64Round(calculatedValue)
148
147
```
149
148
150
-
**RoundEven** - Select this for scenarios with many repeated calculations where you want to minimize systematic bias. Also known as "[banker's rounding]", this mode rounds ties (exactly 0.5) to the nearest even number, which statistically balances out over many operations, making it ideal for large-scale distributions or statistical calculations.
149
+
**RoundEven** - Select this for scenarios with many repeated calculations where you want to minimize systematic bias. Also known as "[banker's rounding]", this mode rounds ties (exactly 0.5) to the nearest even number, which statistically balances out over many operations, making it ideal for large-scale distributions or statistical calculations.
151
150
152
151
```cadence
153
152
// For repeated operations where bias matters
@@ -304,23 +303,23 @@ access(all) fun calculateSwapOutput(
304
303
let reserveOut = DeFiActionsMathUtils.toUInt128(outputReserve)
305
304
let fee = DeFiActionsMathUtils.toUInt128(feeBasisPoints)
306
305
let basisPoints = DeFiActionsMathUtils.toUInt128(10000.0)
Copy file name to clipboardExpand all lines: docs/blockchain-development-tutorials/forte/flow-actions/basic-combinations.md
+13-24Lines changed: 13 additions & 24 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -11,16 +11,6 @@ keywords:
11
11
12
12
# Composing Workflows with Flow Actions
13
13
14
-
:::warning
15
-
16
-
We are reviewing and finalizing Flow Actions in [FLIP 339]. The specific implementation may change as a part of this process.
17
-
18
-
We will update these tutorials, but you may need to refactor your code if the implementation changes.
19
-
20
-
:::
21
-
22
-
## Overview
23
-
24
14
Flow Actions are designed to be **composable**, which means you can chain them together like LEGO blocks to build complex strategies. Each primitive has a standardized interface that works consistently across all protocols and eliminates the need to learn multiple APIs. This composability allows atomic execution of multi-step workflows within single transactions, ensuring either complete success or safe failure. When developers combine these primitives, they create sophisticated decentralized finance (DeFi) strategies like automated yield farming, cross-protocol arbitrage, and portfolio rebalancing. The [5 Flow Actions Primitives] are:
25
15
26
16
-**Source** → Provides tokens on demand by withdrawing from vaults or claiming rewards. Sources respect minimum balance constraints and return empty vaults gracefully when nothing is available.
@@ -321,7 +311,6 @@ This advanced workflow demonstrates the power of combining VaultSource with Zapp
assert(result.success, message: "Workflow should complete successfully")
553
541
}
@@ -564,14 +552,14 @@ test("Strategy should handle price volatility") {
564
552
priceOracle: mockPriceOracle,
565
553
swapper: mockSwapper
566
554
)
567
-
555
+
568
556
// Simulate price changes
569
557
mockPriceOracle.setPrice(1.0)
570
558
let result1 = strategy.execute()
571
-
559
+
572
560
mockPriceOracle.setPrice(2.0)
573
561
let result2 = strategy.execute()
574
-
562
+
575
563
assert(result1 != result2, message: "Strategy should adapt to price changes")
576
564
}
577
565
```
@@ -591,5 +579,6 @@ In this tutorial, you learned how to combine Flow Actions primitives to create s
591
579
Composability is the core strength of Flow Actions. These examples demonstrate how Flow Actions primitives can be combined to create powerful, automated workflows that integrate multiple protocols seamlessly. The framework's standardized interfaces enable developers to chain operations together like LEGO blocks, focusing on strategy implementation rather than protocol-specific integration details.
0 commit comments