Skip to content

Conversation

@SgtPooki
Copy link
Collaborator

@SgtPooki SgtPooki commented Dec 9, 2025

Summary

Expose Filecoin Pay funding logic as a reusable core API, refactor CLI funding to use it, and add coverage/docs. Remove legacy top-up calc in favor of the new planner.

What Changed

  • Added planFilecoinPayFunding, executeFilecoinPayFunding, and getFilecoinPayFundingInsights in src/core/payments/funding.ts, exported via core/payments.
  • Updated funding types (FilecoinPayFundingPlan/Execution/Insights, options) and refactored payments fund / auto-fund to consume the new API.
  • Documented the planner/executor in src/core/payments/README.md with usage examples.
  • Added unit tests for the planner (wallet shortfall, minimum/no-withdraw, zero-spend) and executor deposit path.

Removal

  • Removed calculateRequiredTopUp/formatTopUpReason path to avoid duplication; callers should use planFilecoinPayFunding + executeFilecoinPayFunding.

@SgtPooki SgtPooki requested a review from Copilot December 9, 2025 03:31
@FilOzzy FilOzzy added team/filecoin-pin "Filecoin Pin" project is a stakeholder for this work. team/fs-wg FOC working group is a stakeholder for this work, and thus wants to track it on their project board. labels Dec 9, 2025
@SgtPooki SgtPooki self-assigned this Dec 9, 2025
@FilOzzy FilOzzy added this to FS Dec 9, 2025
@github-project-automation github-project-automation bot moved this to 📌 Triage in FS Dec 9, 2025
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR introduces a comprehensive Filecoin Pay funding planner API that consolidates and improves upon the legacy top-up calculation logic. The new API provides a cleaner separation between planning (calculation) and execution phases, with detailed insights about current and projected funding states.

Key changes:

  • Added planFilecoinPayFunding, executeFilecoinPayFunding, and getFilecoinPayFundingInsights as the new core funding APIs
  • Removed legacy calculateRequiredTopUp and formatTopUpReason functions to eliminate duplication
  • Refactored CLI payments fund and auto-fund commands to use the new planner
  • Added comprehensive unit tests covering wallet shortfall detection, minimum mode, and zero-spend scenarios

Reviewed changes

Copilot reviewed 9 out of 10 changed files in this pull request and generated 7 comments.

Show a summary per file
File Description
src/core/payments/funding.ts New file introducing the funding planner with calculation, planning, and execution functions
src/core/payments/types.ts Added new types (FundingMode, FundingReasonCode, FilecoinPayFundingPlan, etc.) and updated documentation for existing types
src/core/payments/index.ts Exports new funding module (export * from './funding.js')
src/core/payments/top-up.ts Removed calculateRequiredTopUp and formatTopUpReason, kept executeTopUp for backward compatibility
src/core/payments/README.md Added documentation and usage examples for the new funding planner API
src/payments/types.ts Updated to re-export FundingMode from core, replaced deprecated FundMode type
src/payments/fund.ts Refactored to use planFilecoinPayFunding and executeFilecoinPayFunding, but contains a critical bug with parameter passing
upload-action/src/filecoin.js Migrated to use calculateFilecoinPayFundingPlan and formatFundingReason
upload-action/package-lock.json Version bump from 0.13.0 to 0.14.0 reflecting the minor version change
src/test/unit/payments-funding.test.ts New comprehensive unit tests for the funding planner functions
Files not reviewed (1)
  • upload-action/package-lock.json: Language not supported

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@SgtPooki SgtPooki requested a review from rvagg December 9, 2025 03:57
@SgtPooki SgtPooki marked this pull request as ready for review December 9, 2025 03:57
const { plan } = await planFilecoinPayFunding({
synapse,
targetRunwayDays: 30,
ensureAllowances: true, // also sets WarmStorage allowances
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
ensureAllowances: true, // also sets WarmStorage allowances
ensureAllowances: true, // also checks and sets WarmStorage allowances

I feel like there should be a stronger recommendation to always use this, could even be an opt-out instead of opt-in, but I'll leave it to you

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although .. depositUSDFC does it for you automatically, so maybe this is not even needed as an option here, we just do it by default anyway?

You might want to look through the flow in there and where it's called from your code in here to see if you're not doing too many calls. Probably not a big deal but also might be some flow optimisation that can be done.

status: PaymentStatus,
overrides?: { depositedBalance?: bigint; rateUsed?: bigint; lockupUsed?: bigint }
): FilecoinPayFundingInsights {
return buildFilecoinPayFundingInsights(status, overrides)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe just delete buildFilecoinPayFundingInsights and use getFilecoinPayFundingInsights internally

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

obviously I mean inline buildFilecoinPayFundingInsights into getFilecoinPayFundingInsights

case 'withdrawal-excess':
return 'Excess funds available for withdrawal'
default:
return 'Required funding'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not tempted to just throw in this case?

Comment on lines +189 to +190
if (days === 0) {
reasonCode = delta > 0n ? 'piece-upload' : 'none'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is this case? you want zero days runway but still need funding to onboard the piece? is this a normal path we experience?

Copy link
Member

@rvagg rvagg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this is OK, I haven't reviewed it all in depth, if it works in practice I'll take your word for it!

@github-project-automation github-project-automation bot moved this from 📌 Triage to ✔️ Approved by reviewer in FS Dec 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

team/filecoin-pin "Filecoin Pin" project is a stakeholder for this work. team/fs-wg FOC working group is a stakeholder for this work, and thus wants to track it on their project board.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants