fix(shade-contract-template): refund register deposit above the storage cost - #103
fix(shade-contract-template): refund register deposit above the storage cost#103PiVortex wants to merge 3 commits into
Conversation
…ge cost (#64) register_agent kept any overpayment and only checked the deposit for first-time registrations. It now computes the required deposit (the storage cost for a new agent, 0 for a re-registration), requires the attached deposit to be at least that, and refunds the excess. Overpayment can no longer be lost, and a caller can safely attach more than the minimum. A failed registration (insufficient deposit, or an invalid attestation) panics, so NEAR reverts the tx and auto-refunds the full attached deposit; the explicit refund only runs on success for the overpaid excess. Set the shade-agent-js default register deposit to the exact storage cost (0.00486 NEAR) and document that a higher deposit is allowed (refunded). Update the agent-contract / api docs and add unit tests asserting the refund amounts. Removal still does not refund the deposit, so a removed agent re-registering pays again (issue #64 point 2) — kept as-is and documented on remove_agent.
|
/claude-review |
There was a problem hiding this comment.
Pull request overview
Updates the Shade agent registration flow to avoid silently keeping excess attached deposits: the contract now charges only the required storage stake (or 0 on re-registration) and refunds any overpayment, while the JS client/docs/tests are updated to reflect the new default and semantics.
Changes:
- Reworks
register_agentto compute arequired_deposit(storage cost for new agents, 0 for re-registrations), requireattached >= required, and refundattached - requiredon success. - Updates
shade-agent-jsdefault register deposit to the current storage cost (0.00486 NEAR) and clarifies that attaching more is safe (excess refunded). - Updates docs and adds/updates unit tests to cover refund behavior and the new error message wording.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| shade-contract-template/src/lib.rs | Implements required_deposit logic and refunds any excess deposit after successful registration. |
| shade-contract-template/src/internal/unit_tests.rs | Adds unit tests covering overpay refund, exact-deposit no-refund, and re-register full refund; updates panic expectations. |
| shade-agent-js/src/api.ts | Updates DEFAULT_REGISTER_DEPOSIT_YOCTO to 0.00486 NEAR and updates JSDoc to document refund-safe over-attachment. |
| shade-agent-js/tests/unit/api.test.ts | Updates the expected default deposit fixture and test wording to match the new default. |
| docs/reference/api.md | Updates register() documentation to describe “>= + refund” semantics and the new default deposit. |
| docs/reference/agent-contract.md | Updates contract documentation and example snippet to describe required-deposit and refund behavior, including re-registration requiring 0. |
Code reviewReviewed the Found 1 issue:
shade-agent-framework/shade-contract-template/src/lib.rs Lines 137 to 140 in 53467a3 Design notes
|
#103) Address Claude review finding [MEDIUM:55]: the refund was only unit-tested via a scheduled receipt. Add a sandbox integration test that over-deposits 1 NEAR on registration and asserts the agent's on-chain balance drops by only ~storage + gas (well under 0.1 NEAR), proving the excess is refunded. Runs in the /run-e2e gate; the refund is attestation-independent so the sandbox is sufficient.
|
Addressed in |
|
/claude-review |
Code reviewFound 1 issue:
shade-agent-framework/shade-contract-template/README.md Lines 56 to 68 in ce27db7 Otherwise clean: attestation gating, owner access control, signing, and secret handling are untouched; Design notes
|
#103) Address Claude review finding [LOW:60]: add the refund integration test to the Integration tests table and the three refund unit tests to the Unit tests prose, so the per-test inventory tracks this PR's additions.
|
Fixed in the latest push — added |
|
/claude-review |
Code reviewNo issues found. Re-reviewed the current HEAD ( Verified clean:
Design notes
|
|
Reviews passed! |
|
/run-e2e |
|
Running the e2e suite (contract integration + tests-in-tee): https://github.com/NearDeFi/shade-agent-framework/actions/runs/27932686818 |
Closes #64
What & why
register_agentpreviously requiredattached_deposit >= storage_costfor first-time registrations and kept any overpayment; re-registrations were unchecked. This reworks it to:required_deposit= the storage cost for a new agent,0for a re-registration (reuses the existing slot);attached >= required_deposit;attached − required) to the agent.So overpayment can no longer be lost, and a caller can safely attach more than the minimum — which removes the brittle exact-match coupling between the client default and the contract (the issue that dogged the earlier exact-deposit attempt).
Failure handling
A failed registration panics — insufficient deposit (
require!) or an invalid attestation (verify_attestation). On panic NEAR reverts the whole tx and auto-refunds the full attached deposit, so there's no manual refund on the failure path and no need for a non-panic path. The explicitPromise::transferrefund runs only on success, for the overpaid excess. The error surfaces via the panic message →register()→call()→account.callFunction()rejects →toThrowablerethrows it, so the agent gets a clear, specific error.Files changed
shade-contract-template/src/lib.rs—register_agentreworked (required_deposit+>=+ refund excess viaPromise::transfer(..).detach()); keptSTORAGE_BYTES_TO_REGISTERwith a short fork/mirror comment.shade-agent-js/src/api.ts—DEFAULT_REGISTER_DEPOSIT_YOCTO→4860000000000000000000(0.00486 NEAR, the storage cost); JSDoc notes a higher deposit is allowed (refunded) and thatforceDeposit: trueis now safe on re-registration.docs/reference/agent-contract.md,docs/reference/api.md— updated for>= + refund, the re-registration-needs-0 rule, and "you may attach more (refunded)".shade-agent-js/tests/unit/api.test.ts— default-deposit fixture → 0.00486.shade-contract-template/src/internal/unit_tests.rs— added refund tests (overpay → 0.00014 refund; exact → no refund; re-register → full refund); updated the insufficient-deposit panic-message expectation.Tests
cargo test --lib(54 pass, incl. 3 new refund tests);cargo clippy --all-targetsclean; integration tests still compile (cargo test --no-run) and need no deposit changes since0.005 ≥ 0.00486.shade-agent-jsbuild + 275 unit tests;shade-agent-templatetscclean.Design decisions / Accepted tradeoffs
>= + refundchosen over exact-==(avoids the client↔contract coupling / version-skew the reviews flagged) and over runtime-measured storage (kept the constant for simplicity, per maintainer decision).remove_agent.Release impact
@neardefi/shade-agent-js— minor. The default register deposit changed (0.005 → 0.00486); backward-compatible (the contract refunds excess, so any over-attach is safe). Version bump handled onmainper the release process.Follow-up
tests-in-tee(/run-e2e) must be run by a maintainer — the only end-to-end proof of the registration flow under attestation. Itstest-different-account-idscenario needs no change (0.005 still ≥ the cost).🤖 Generated with Claude Code