-
Notifications
You must be signed in to change notification settings - Fork 117
Custom Contract Fees Mechanism #208
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
cschuchardt88
wants to merge
12
commits into
neo-project:master
Choose a base branch
from
cschuchardt88:add/contract-fee
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
6712688
Init Draft
cschuchardt88 2386c52
fixed up text
cschuchardt88 71d0844
Added Specification
cschuchardt88 9273b19
typoes
cschuchardt88 abec27c
more typoes
cschuchardt88 7be9feb
more typoes
cschuchardt88 c6e87ad
Updated spec more
cschuchardt88 50c2f31
fixed formating
cschuchardt88 4ecd34e
formatting
cschuchardt88 1e8f026
formatting
cschuchardt88 e080459
removed some formatting
cschuchardt88 e5765e3
Add contract
cschuchardt88 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,186 @@ | ||
| # **NEP: Custom Contract Fees Mechanism** | ||
|
|
||
| **NEP**: TBD | ||
| **Title:** Custom Contract Fees with ABI Declaration and Runtime Enforcement | ||
| **Author:** Chris Schuchardt <cschuchardt88@gmail.com> | ||
| **Type:** Standard | ||
| **Status:** Draft | ||
| **Created:** 2025-10-27 | ||
|
|
||
| --- | ||
|
|
||
| ## **Abstract** | ||
|
|
||
| This NEP introduces a standardized mechanism for smart contracts to declare **custom execution fees** in addition to standard network gas. These fees are paid by the caller to the contract owner (or designated beneficiary) upon successful invocation. The fee structure is declared in the contract's ABI using a new `"fee"` metadata field, supported by updated DevPack tooling and enforced by the Neo VM at runtime. | ||
|
|
||
| --- | ||
|
|
||
| ## **Motivation** | ||
|
|
||
| Many dApps require **micro-transactions**, **subscription models**, or **usage-based billing** directly within smart contracts. While GAS covers network costs, there is no native way to charge **contract-level fees** in NEO or NEP-17 tokens. This NEP enables: | ||
|
|
||
| - Pay-per-use contracts | ||
| - Premium feature access | ||
| - Decentralized SaaS models | ||
| - Fair compensation for contract maintainers | ||
|
|
||
| --- | ||
|
|
||
| ## **Specification** | ||
|
|
||
| ### 1. **ABI Extension: Fee Declaration** | ||
|
|
||
| A new optional `"fee"` object is added to method definitions in the ABI. | ||
|
|
||
| ```json | ||
| { | ||
| "methods": [ | ||
| { | ||
| "name": "queryData", | ||
| "parameters": [...], | ||
| "returnType": "String", | ||
| "offset": 123, | ||
| "safe": true, | ||
| "fee": { | ||
| "asset": "NEO" | "GAS" | "0x..." (NEP-17 hash), | ||
| "amount": 100000000, // 8 decimals (1.0 fixed-point) | ||
| "beneficiary": "NM7g6DAeN3Nx8iK53GqY7fpeyqX5bUq5fJ", // optional, defaults to contract owner | ||
| "mode": "fixed" | "dynamic", // fixed = static, dynamic = computed at runtime | ||
| "dynamicScriptHash": "0x..." // required if mode=dynamic | ||
| } | ||
| } | ||
| ] | ||
| } | ||
| ``` | ||
|
|
||
| #### Field Details: | ||
|
|
||
| | Field | Type | Required | Description | | ||
| |------|------|----------|-----------| | ||
| | `asset` | string | Yes | `"NEO"`, `"GAS"`, or NEP-17 contract hash | | ||
| | `amount` | integer | Yes (if `mode=fixed`) | Amount in smallest unit (8 decimals) | | ||
| | `beneficiary` | string | No | Address to receive fee. Defaults to contract owner | | ||
| | `mode` | string | Yes | `"fixed"` or `"dynamic"` | | ||
| | `dynamicScriptHash` | string | Yes (if `mode=dynamic`) | Contract that computes fee via `calculateFee(...)` | | ||
|
|
||
| > **Note**: Dynamic fees allow complex logic (e.g., tiered pricing, time-based rates). | ||
|
|
||
| --- | ||
|
|
||
| ### 2. **Dynamic Fee Computation Interface** | ||
|
|
||
| Contracts declaring `mode: "dynamic"` must reference a fee calculator contract implementing: | ||
|
|
||
| ```csharp | ||
| public interface IFeeCalculator | ||
| { | ||
| BigInteger CalculateFee(ByteString method, object[] args); | ||
| } | ||
| ``` | ||
|
|
||
| - Called by VM before execution | ||
| - Must be **Safe** (no state changes) | ||
| - Returns fee in smallest unit | ||
|
|
||
| --- | ||
|
|
||
| ### 3. **Decorators** | ||
|
|
||
| #### C# Attributes | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Implementation detail not relevant to the VM/runtime spec. |
||
|
|
||
| ```csharp | ||
| [Fee(Asset = "GAS", Amount = 50000000, Mode = FeeMode.Fixed)] // 0.5 GAS | ||
| [Fee(Asset = "0xd2a4cff31913016155e38e474a2c06d08be276cf", Amount = 100, Mode = FeeMode.Fixed)] | ||
| public static string QueryData(string key) { ... } | ||
|
|
||
| [Fee( | ||
| Asset = "GAS", | ||
| Mode = FeeMode.Dynamic, | ||
| Calculator = "0xb2a4cff31913016155e38e474a2c06d08be296cf" | ||
| )] | ||
| public static UInt160 GetReport(uint userId) { ... } | ||
| ``` | ||
|
|
||
| #### DevPack Export Logic | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here. |
||
|
|
||
| - `[Fee]` attribute is parsed during ABI generation | ||
| - Populates `fee` field in method ABI | ||
| - Validates: | ||
| - Asset is valid (NEO, GAS, or deployed NEP-17) | ||
| - `Calculator` implements `IFeeCalculator` if `mode=dynamic` | ||
| - Emits warning if fee > 10 GAS (configurable) | ||
|
|
||
| --- | ||
|
|
||
| ### 4. **Runtime Enforcement (Neo VM)** | ||
|
|
||
| #### Invocation Flow | ||
|
|
||
| ```text | ||
| 1. Caller invokes contract.method(args) | ||
| 2. VM loads ABI | ||
| 3. If method has .fee: | ||
| a. If fixed → deduct exact amount from caller | ||
| b. If dynamic → invoke calculator.CalculateFee(method, args) | ||
| → deduct returned amount | ||
| 4. Transfer fee to beneficiary (or contract owner) | ||
| 5. Proceed with execution | ||
| 6. On failure → refund fee (but not network GAS) | ||
| ``` | ||
|
|
||
| #### Safety Guarantees | ||
|
|
||
| - **Atomicity**: Fee deduction and transfer are atomic with execution | ||
| - **Reentrancy-safe**: Uses `ExecutionEngine` snapshot | ||
| - **No double-spend**: Fee checked before execution | ||
| - **Refund on revert**: Fee returned if contract throws | ||
|
|
||
| #### Syscall Additions | ||
|
|
||
| ```csharp | ||
| // Pseudo-syscalls (internal) | ||
| bool ChargeFee(UInt160 asset, BigInteger amount, UInt160 beneficiary); | ||
| BigInteger QueryDynamicFee(UInt160 calculator, string method, object[] args); | ||
| ``` | ||
|
|
||
| --- | ||
|
|
||
| ### 5. **Contract Owner & Beneficiary** | ||
|
|
||
| - Contract owner = deployer address (stored in manifest) | ||
| - `beneficiary` overrides owner per method | ||
| - If omitted → owner | ||
|
|
||
| --- | ||
|
|
||
| ## **Security Considerations** | ||
|
|
||
| - **DoS via high dynamic fees**: Mitigated by gas cap on calculator | ||
| - **Calculator griefing**: Limit execution to 100k gas | ||
| - **Fee spoofing**: ABI is signed in manifest | ||
| - **Replay attacks**: Nonce not needed (fee per call) | ||
|
|
||
| --- | ||
|
|
||
| ## **Backwards Compatibility** | ||
|
|
||
| - Existing contracts without `[Fee]` → no fee charged | ||
| - Old DevPack versions → ignore attribute | ||
| - VM ignores unknown ABI fields | ||
|
|
||
| --- | ||
|
|
||
| ## **Appendix** | ||
|
|
||
| ### Example Contract | ||
|
|
||
| ```csharp | ||
| public class MyContract : SmartContract | ||
| { | ||
| [Fee(Asset = "GAS", Amount = 100000000, Beneficiary = "NM7...")] // 1 GAS | ||
| public static string GetPrice(string symbol) { ... } | ||
|
|
||
| [Fee(Asset = "0xabc...", Mode = FeeMode.Dynamic, Calculator = "0xb2a4cff31913016155e38e474a2c06d08be296cf")] | ||
| public static UInt160 GetReport(uint tier) { ... } | ||
| } | ||
| ``` | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Methods are defined using NEP-25 JSON, not using C#, see any other NEP defining methods for example.