Money
Amounts: NEAR, yocto & U128
wei becomes yocto-NEAR (10^24). NearToken does the unit math, and amounts cross the ABI as U128 strings to keep their precision.
Money is the same idea with more zeros and a wrapper type. Where Solidity counts in wei (10¹⁸ per ETH), NEAR counts in yocto-NEAR (10²⁴ per NEAR). You rarely write the zeros yourself — NearToken builds and does math on amounts.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
contract Money {
function oneEther() external pure returns (uint256) {
return 1 ether; // 1e18 wei
}
function toWei(uint256 amount) external pure returns (uint256) {
return amount * 1 ether;
}
}use near_sdk::{near, NearToken};
use near_sdk::json_types::U128;
#[near(contract_state)]
#[derive(Default)]
pub struct Money {}
#[near]
impl Money {
pub fn one_near(&self) -> U128 {
// 1 NEAR = 1e24 yocto
U128(NearToken::from_near(1).as_yoctonear())
}
pub fn to_yocto(&self, near_amount: u8) -> U128 {
U128(NearToken::from_near(near_amount as u128).as_yoctonear())
}
}wei (10¹⁸) ↔ yocto-NEAR (10²⁴) — both the smallest unit, NEAR just has six more zeros.
1 ether ↔ NearToken::from_near(1) (also from_millinear, from_yoctonear). NearToken carries the units and offers checked_add / saturating_mul, so you don’t hand-roll big-integer math.
Across the method boundary, amounts travel as U128 — a string in JSON — to dodge the 2^53 precision limit; read the inner value with .0. env::attached_deposit() hands you a NearToken directly.