Skip to content

feat!: overhaul continuous payments logic#1066

Merged
sidvishnoi merged 6 commits into
mainfrom
payment-streams-continuous
Jun 10, 2025
Merged

feat!: overhaul continuous payments logic#1066
sidvishnoi merged 6 commits into
mainfrom
payment-streams-continuous

Conversation

@sidvishnoi

@sidvishnoi sidvishnoi commented Jun 6, 2025

Copy link
Copy Markdown
Member

Context

Closes #976

Changes proposed in this pull request

Overview

Major change: This affects our handling of continuous payments when there are more than 1 monetization link tags on page.

  • Loop through PaymentSessions in a staggered manner while making continuous payments.
    • Earlier, on page load, we used to pay all sessions at once, then adding a longer wait on next payments. Now, we pay one session at a time, and give other sessions smaller wait than before.
    • This respects user's rate of pay and prevents abuse by websites as user won't end up paying 100 units on page load if there are 100 link tags on page.
    • MonetizationEvent is fired more often than earlier, giving more continuous payments feel.
  • Payments are made only in multiple of PaymentSession.minSendAmount, making payments fairer to sender and ASEs (no rounding loss/gains due to currency conversion).
  • There's a MIN_PAYMENT_WAIT of 2s, to reduce load of OP servers.

Changes

  • Move payment timing and amount logic from individual payment sessions (per link tag) to PaymentManager (per tab).
    • There's no PaymentSession.start(), PaymentSession.stop() etc. anymore
    • Some changes in math and timing of payment amounts
      • Added concept of PaymentManager.pendingAmount which increments as per units-duration defined by user's rate of pay.
      • When pendingAmount reaches next payable session's minSendAmount (or higher), pay appropriate amount to that session and subtract paid amount from pendingAmount, and move onto next payable session.
      • Essentially, pendingAmount is like a bucket, and there's a FIFO queue of sessions waiting to empty it as per their needs.
    • Payment streams looping flow and demo: see below in details
  • Made overpaying logic state and logic simpler
    • overpaying wait etc. no longer depends on walletAddress, but only of tab's URL.
    • walletAddress info is still used to emit the older monetization event
  • Renamed PaymentSession.pay(amount) to PaymentSession.payOneTime(amount) for clarity.
  • Made PaymentSession.payContinuous() public (and renamed to .pay(amount), and added a helper (payWithRetry(amount)) that retries this payment on certain errors.
  • Made concept of "stop" and "pause" streaming more distinguishable.
  • Made use of a pausable Timer class to keep timings sane.

Follow-up

  • Some parts of code are not as efficient as they could be. e.g. we loop through sessions a lot, even when they may not be changed since last call (finding sessions to pay, size of sessions). Some things can be improved while still preserving code readability - like maintain a count of size on add/remove session, instead of iterating all frames and adding size of each.
  • Update E2E tests and add more unit tests.

Payment streams looping flow and demo

  • First go through all payable link tags on main website, one by one
  • Then pay first link in first iframe, then first link in second iframe
  • Then again go through all payable link tags on main website
  • Then pay second link of first iframe, then second link (if doesn't exist, then first again) of second iframe
  • Then again go through all payable link tags on main website
  • Then again pay first link in first iframe, then first link in second iframe, and so on

In this demo, there are 2 iframes. Each frame (including main site) has 2 link tags in same order: MXN and then ZAR.

Most importantly, payment rate is now respected! As an example, our rate of pay says 1c every 2s.
Earlier: when we had 6 link tags, we’d emit 6 monetization events on page load. Spending 6c on page load. Then we’d wait 12s for next payment, and pay 6c again.
Now: We’d emit one monetization event every 2s!

Of course, this is the case when sending wallet and receiving wallet have 1c = 1c exchange rate. When sending from MXN to USD, even though sender’s rate is 0.01MXN every 2s, USD wallet cannot receive an amount this small. It needs at least 0.20MXN to receive 0.01USD. So, we’ll wait 40s (such that amount is now 0.20MXN) for the next payment!

Video: https://drive.google.com/file/d/1LtcdAJmMnYuT6wCNCFLp4aGkJY9U5g9j/view?usp=sharing

@github-actions github-actions Bot added area: content Improvements or additions to extension content script area: background Improvements or additions to extension background script area: popup Improvements or additions to extension popup area: tests Improvements or additions to tests area: pages Changes to any of extension's pages area: shared Changes to shared libraries and utilities labels Jun 6, 2025
@github-actions

github-actions Bot commented Jun 6, 2025

Copy link
Copy Markdown
Contributor

Extension builds preview

Name Link
Latest commit f937ecd
Latest job logs Run #15558377854
Chrome (991.69KB)Download
Firefox (991.71KB)Download

@sidvishnoi sidvishnoi force-pushed the payment-streams-continuous branch from 4ee1d6a to 8941964 Compare June 9, 2025 10:21
@github-actions github-actions Bot removed the area: tests Improvements or additions to tests label Jun 9, 2025
@sidvishnoi sidvishnoi force-pushed the payment-streams-continuous branch 5 times, most recently from 27c0b0e to b19d17d Compare June 9, 2025 11:31
@sidvishnoi sidvishnoi requested a review from DarianM June 9, 2025 12:28
@github-actions github-actions Bot removed area: popup Improvements or additions to extension popup area: pages Changes to any of extension's pages labels Jun 9, 2025
@sidvishnoi sidvishnoi marked this pull request as ready for review June 9, 2025 12:28
Comment thread src/shared/helpers/misc.ts
@sidvishnoi sidvishnoi merged commit d3e0d45 into main Jun 10, 2025
9 checks passed
@sidvishnoi sidvishnoi deleted the payment-streams-continuous branch June 10, 2025 12:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: background Improvements or additions to extension background script area: content Improvements or additions to extension content script area: shared Changes to shared libraries and utilities

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Payment streams overhaul

2 participants