Numbers are at the heart of smart contracts. They hold immense importance because any mishap involving numbers can lead to significant losses, given that we’re dealing with financial transactions. Agoric is a platform that enables developers to write smart contracts in JavaScript, which is a tremendous advantage. Not only does it enable developers to write secure smart contracts, but it also provides a familiar language for many programmers.
So, how does Agoric handle numbers? One of the significant challenges pertaining to numbers is ensuring correct calculations. JavaScript, being a dynamic language, can induce errors at runtime. TypeScript and JSDoc can help to certain extent. They catch certain types of errors during development, they do not prevent runtime mishaps. Given that financial transactions are at stake, we must be cautious at runtime as well. Once a transaction is committed to the blockchain, it cannot be reversed, so any error can result in significant financial losses.
What is NAT?
When dealing with smart contracts, there is a data type called NAT, which refers to non-negative integers (0, 1, 2, 3, …). NAT ensures that values such as token amounts, balances, and other countable quantities are always non-negative. This is crucial for maintaining the integrity and correctness of smart contract logic, as it prevents negative values from causing unexpected behaviors or vulnerabilities in the contract.
Why is NAT Important?
Preventing Errors: NAT helps prevent errors that could arise from using negative numbers in contexts where only non-negative values make sense. For example, balances and token amounts should never be negative.
Security: Ensuring that only non-negative integers are used in critical parts of the code helps prevent certain types of vulnerabilities and attacks. For instance, negative values could be exploited to create underflows or other unexpected behaviors in smart contracts.
Clarity and Intent: When a value is declared as a NAT, it’s evident that the value is intended to be a non-negative integer. This clarity helps with code readability and maintainability.
Mathematical Consistency: Many mathematical operations and algorithms assume non-negative values. Enforcing the NAT type ensures that these operations work as intended.
Contract Integrity: In the context of financial contracts maintaining non-negative balances is important. NAT ensures that the logic for token transfers, payments, and other financial operations preserves this integrity.
What is endo/nat?
endo/nat
is a module within the Endo platform, which is Agoric’s secure JavaScript runtime environment. This module provides utilities for working with natural numbers in a secure and reliable manner. The endo/nat
module ensures that operations involving natural numbers are performed safely and correctly.
Example Code
import { Nat } from '@end/nat';
function placeBid(bidAmount) {
const validBid = Nat(bidAmount); // Ensures bidAmount is a natural number
// Further logic to place the bid
}
It is used to ensure that a given value is a non-negative integers. Using @endo/nat
, we get runtime validation for any inputs that cross trust boundaries. By enforcing non-negative integers using @endo/nat
, we can avoid many common pitfalls and vulnerabilities, ensuring that our smart contracts perform as expected in a secure and predictable manner.
Nat
is a function imported from the @endo/nat
package. It ensures that a number is a natural number (non-negative integer) and throws a RangeError
otherwise. It provides two functions: isNat(allegedNum)
to check if a value is a natural number, and Nat(allegedNum)
to coerce a value into a natural number or throw an error if it cannot be. The package is designed to handle both JavaScript numbers and bigints safely.