Wallet-connect resilience: contract costs are immutable constants, so bake verified wei fallbacks for when the wallet's internal RPC fails (MetaMask iOS 'Load failed'); friendlier retry message on network hiccups

join-now.js entry price + my.js upgrade table. Reported by Neville's team overnight 8/28.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
martbost
2026-08-28 04:03:18 -05:00
parent 21e105b953
commit ad5c5721aa
2 changed files with 29 additions and 8 deletions
+16 -2
View File
@@ -24,10 +24,17 @@ fetch('/api/public/config').then(function(r){return r.json()}).then(function(c){
async function req(method,params){ if(!eth) throw new Error('No wallet found'); return await eth.request({method:method,params:params||[]}); }
async function ethCall(data){ return await req('eth_call',[{to:CONTRACT,data:data},'latest']); }
// Immutable contract constant (getAllCosts premium[0], verified on-chain).
// Fallback when the WALLET's internal RPC hiccups (MetaMask iOS surfaces
// that as "Load failed") — prices can never change, so baking it is safe.
var REG_PREM_WEI=343406593406593400832n;
async function loadCosts(){
try{
var r=await ethCall(SEL_GETCOSTS); var d=r.slice(2), w=[];
for(var i=0;i<d.length;i+=64) w.push(BigInt('0x'+d.slice(i,i+64)));
costs={ regPrem:w.slice(8,16) }; return costs;
costs={ regPrem:w.slice(8,16) };
}catch(e){ costs={ regPrem:[REG_PREM_WEI] }; }
return costs;
}
function regValue(){ return costs.regPrem[0]*105n/100n; } // premium base * 1.05 (verified)
@@ -105,7 +112,14 @@ fetch('/api/public/config').then(function(r){return r.json()}).then(function(c){
await ensurePolygon();
await finishConnect();
log('Connected. Review the amount, then Join.','ok');
}catch(e){ log('Connect failed: '+(e.message||e),'err'); }
}catch(e){
var m=String((e&&e.message)||e);
if(/load failed|failed to fetch|network|timeout/i.test(m)){
log('Your wallet’s network connection hiccuped (this happens inside wallet apps sometimes) — tap <strong>Connect wallet</strong> once more and it usually goes right through.','err');
}else{
log('Connect failed: '+m,'err');
}
}
}
// Inside a wallet's in-app browser the wallet is often ALREADY connected —
// recognize it silently on load (eth_accounts never prompts) instead of
+9 -2
View File
@@ -229,11 +229,18 @@
try{await upEth.request({method:'wallet_switchEthereumChain',params:[{chainId:UPG.POLYGON}]});}
catch(e){if(e&&e.code===4902){await upEth.request({method:'wallet_addEthereumChain',params:[{chainId:UPG.POLYGON,chainName:'Polygon Mainnet',nativeCurrency:{name:'POL',symbol:'POL',decimals:18},rpcUrls:['https://polygon-rpc.com'],blockExplorerUrls:['https://polygonscan.com']}]});}else{throw e;}}
}
// exact cost from the contract (BigInt)
// exact cost from the contract (BigInt). Costs are immutable constants,
// so if the WALLET's internal RPC hiccups (MetaMask iOS: "Load failed")
// we fall back to the verified baked table instead of failing the flow.
upgLog('Reading the exact upgrade cost from the contract…');
const UP_STD=[310701203558346391552n,621402407116692783104n,1242804814233385566208n,2485609628466771132416n,4971219256933542264832n,9942438513867084529664n,19884877027734169059328n,0n];
const UP_PREM=[621402407116692783104n,1242804814233385566208n,2485609628466771132416n,4971219256933542264832n,9942438513867084529664n,19884877027734169059328n,39769754055468338118656n,0n];
let arr;
try{
const r=await upEth.request({method:'eth_call',params:[{to:UPG.CONTRACT,data:UPG.SEL_GETCOSTS},'latest']});
const words=[];for(let i=2;i<r.length;i+=64)words.push(BigInt('0x'+r.slice(i,i+64)));
const arr=(d.tier===2)?words.slice(24,32):words.slice(16,24);
arr=(d.tier===2)?words.slice(24,32):words.slice(16,24);
}catch(e){ arr=(d.tier===2)?UP_PREM:UP_STD; }
const val=arr[(d.level||1)-1];
if(!val||val<=0n){upgLog('Could not read the upgrade cost — try again in a minute.',true);return;}
upgLog('Upgrading to '+esc(LEVELS[ns.nextLevel-1]||'')+' for '+(Number(val)/1e18).toFixed(4)+' POL — confirm in your wallet…');