@nibgate/sdk 0.2.28 → 0.2.30

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nibgate/sdk",
3
- "version": "0.2.28",
3
+ "version": "0.2.30",
4
4
  "description": "Framework-agnostic browser and server package for creator-owned gated content, unlock events, and receipts.",
5
5
  "type": "module",
6
6
  "sideEffects": [
@@ -224,8 +224,8 @@ export function renderDefaultUnlockUI(container, resource, options = {}) {
224
224
  const cleanup = () => { document.removeEventListener('mouseup', cancelHold); document.removeEventListener('touchend', cancelHold); };
225
225
  card.addEventListener('remove', cleanup);
226
226
 
227
- // ── Gateway deposit icon ──────────────────────────────────────────────
228
- let gwOverlay = null;
227
+ // ── Gateway balance + deposit icon ─────────────────────────────────────
228
+ let balEl = null, gwOverlay = null, balTimer = null;
229
229
 
230
230
  function depIcon() {
231
231
  return '<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" style="display:inline;vertical-align:middle"><path d="M12 17V3"/><path d="m6 11 6 6 6-6"/><path d="M19 21H5"/></svg>';
@@ -246,24 +246,40 @@ export function renderDefaultUnlockUI(container, resource, options = {}) {
246
246
  }
247
247
  function onDepKey(e) { if (e.key === 'Escape' && gwOverlay) { gwOverlay.remove(); gwOverlay = null; document.removeEventListener('keydown', onDepKey); } }
248
248
 
249
- let balEl = null;
250
249
  function ensureBal() {
251
250
  if (balEl && balEl.isConnected) return balEl;
252
251
  balEl = el('span', { 'data-nibgate-bal': '', style: 'margin-left:4px;cursor:pointer;white-space:nowrap;color:var(--accent,#7c9a6d)' },
253
- '\u00b7\u00a0|\u00a0' + depIcon());
252
+ '\u00b7\u00a0<span data-nibgate-bal-txt></span>\u00a0|\u00a0' + depIcon());
254
253
  balEl.addEventListener('click', showDeposit);
255
- if (label.parentNode) label.parentNode.insertBefore(balEl, label.nextSibling);
254
+ label.appendChild(balEl);
256
255
  return balEl;
257
256
  }
258
257
 
258
+ async function refreshBal() {
259
+ if (!card.isConnected || !window.ethereum || !options.gatewayBalanceUrl) return;
260
+ try {
261
+ const accts = await window.ethereum.request({ method: 'eth_accounts' });
262
+ const addr = Array.isArray(accts) && accts[0] ? accts[0] : null;
263
+ if (!addr) return;
264
+ const t = ensureBal().querySelector('[data-nibgate-bal-txt]');
265
+ if (!t) return;
266
+ const r = await fetch(options.gatewayBalanceUrl, {
267
+ method: 'POST',
268
+ headers: { 'Content-Type': 'application/json' },
269
+ body: JSON.stringify({ address: addr }),
270
+ });
271
+ const data = await r.json();
272
+ t.textContent = data?.balance || '';
273
+ } catch {}
274
+ }
275
+
259
276
  if (window.ethereum) {
260
- setTimeout(() => {
261
- const accts = window.ethereum.request({ method: 'eth_accounts' }).catch(() => []);
262
- accts.then(a => { if (Array.isArray(a) && a[0]) ensureBal(); });
263
- }, 1000);
277
+ balTimer = setInterval(refreshBal, 3000);
278
+ setTimeout(refreshBal, 1000);
279
+ window.ethereum.on('accountsChanged', refreshBal);
264
280
  }
265
281
 
266
- return { ...ctrl, element: card, destroy: () => { cleanup(); card.remove(); if (gwOverlay) { gwOverlay.remove(); gwOverlay = null; } } };
282
+ return { ...ctrl, element: card, destroy: () => { cleanup(); card.remove(); if (balTimer) clearInterval(balTimer); if (gwOverlay) { gwOverlay.remove(); gwOverlay = null; } } };
267
283
  }
268
284
 
269
285
  export function renderDefaultRatingUI(container, resource, options = {}) {