Gas-Metered Functions
Larz covers Larzscript's gas metering: gas N declares a real, checkable cost on a function, and the --ledger flag prints the total gas a run actually spent.
gas N โ declaring a cost
Add gas N right after the parameters. It doesn't change what the function returns - just what it costs to call, something a host running your code can check:
fn add(a, b) gas 10 {
return a + b
}
fn mul(a, b) gas 25 {
return a * b
}
print(add(2, 3))
print(mul(4, 5))Output
5 20
--ledger โ metering what ran
Run the exact same file with larzscript --ledger and it prints the total gas spent - 10 for add, 25 for mul:
fn add(a, b) gas 10 {
return a + b
}
fn mul(a, b) gas 25 {
return a * b
}
print(add(2, 3))
print(mul(4, 5))Output
--- ledger (0) --- gas used: 35 5 20
Every snippet on this page is run through the real larzscript interpreter, not hand-typed โ what you see in the Output blocks is the actual output, for everything shown: a plain run and a --ledger run of the same gas-annotated code.
Related
Lesson 12: Building a Real CLI Toolargs and cli - what's next after gas-metered functions.→Lesson 10: Paywalls & Subscriptionspaywall, subscribe, and require has - what's next after wallet, price, pay.→Lesson 4: Functions & Recursionfn and return - gas N is just one more thing that goes after the parameter list.→How Gas Metering WorksThe full mechanics behind gas-annotated functions, including fail-closed enforcement, for more depth than a lesson video covers.→Learn Larzscript Cookbook29 short "how to X in Larzscript" recipes once you know the basics.→