Guest User

Polymarket Commission Email Filter - Google Apps Script

a guest
Oct 24th, 2026
1
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. Polymarket Commission Email Filter - Google Apps Script
  3.  
  4. Archives matching commission emails strictly below LIMIT_DOLLARS.
  5. The default threshold is $10; change it to your preferred amount.
  6. Messages remain available in Gmail's All Mail.
  7.  
  8. SETUP
  9. 1. Create a project at https://script.google.com/ and paste this code into Code.gs.
  10. 2. Set LIMIT_DOLLARS to your preferred threshold.
  11. 3. In the editor, go to Services > + > Gmail API > Add.
  12. 4. Run archiveSmallCommissions once and authorize Gmail access.
  13. 5. Go to Triggers > Add Trigger, select archiveSmallCommissions,
  14.    choose Time-driven, and set your preferred schedule.
  15. 6. If you use a standard Google Cloud project, also enable the Gmail API
  16.    in that project's Google Cloud console.
  17. */
  18.  
  19. const LIMIT_DOLLARS = 10;
  20. const DUB_SENDER = '[email protected]';
  21.  
  22. function archiveSmallCommissions() {
  23.   const lock = LockService.getScriptLock();
  24.   if (!lock.tryLock(1000)) return;
  25.  
  26.   try {
  27.     // Collect messages first, then archive them:
  28.     // this prevents inbox changes from affecting pagination.
  29.     const messageIds = [];
  30.     let pageToken;
  31.  
  32.     do {
  33.       const options = {
  34.         q: `in:inbox from:${DUB_SENDER} subject:commission subject:Polymarket`,
  35.         maxResults: 500
  36.       };
  37.       if (pageToken) options.pageToken = pageToken;
  38.  
  39.       const page = Gmail.Users.Messages.list('me', options);
  40.       for (const message of page.messages || []) {
  41.         messageIds.push(message.id);
  42.       }
  43.       pageToken = page.nextPageToken;
  44.     } while (pageToken);
  45.  
  46.     let archived = 0;
  47.  
  48.     for (const id of messageIds) {
  49.       const message = Gmail.Users.Messages.get('me', id, {
  50.         format: 'metadata',
  51.         metadataHeaders: ['Subject', 'From']
  52.       });
  53.  
  54.       if (!(message.labelIds || []).includes('INBOX')) continue;
  55.  
  56.       const headers = message.payload.headers || [];
  57.       const getHeader = name =>
  58.         (headers.find(h => h.name.toLowerCase() === name) || {}).value || '';
  59.  
  60.       // Double-check the sender's exact email address.
  61.       const from = getHeader('from').trim();
  62.       const address = (from.match(/<([^<>]+)>/) || [null, from])[1];
  63.       if (address.trim().toLowerCase() !== DUB_SENDER) continue;
  64.  
  65.       // Read only the commission amount from the subject,
  66.       // not the purchase amount from the message body.
  67.       const subject = getHeader('subject').trim();
  68.       const match = subject.match(
  69.         /^You['’]ve made a \$([0-9]+(?:,[0-9]{3})*)(?:\.([0-9]{2}))? commission from Polymarket$/i
  70.       );
  71.  
  72.       // Leave messages with unrecognized subjects unchanged.
  73.       if (!match) continue;
  74.  
  75.       const cents =
  76.         Number(match[1].replace(/,/g, '')) * 100 +
  77.         Number(match[2] || '0');
  78.  
  79.       if (!Number.isSafeInteger(cents)) continue;
  80.  
  81.       if (cents < LIMIT_DOLLARS * 100) {
  82.         // Removing the INBOX label archives the message.
  83.         Gmail.Users.Messages.modify(
  84.           { removeLabelIds: ['INBOX'] },
  85.           'me',
  86.           id
  87.         );
  88.         archived++;
  89.       }
  90.     }
  91.  
  92.     console.log(`Messages archived: ${archived}`);
  93.   } finally {
  94.     lock.releaseLock();
  95.   }
  96. }
  97.  
Advertisement
Add Comment
Please, Sign In to add comment