web/website/node/pol-funded-rewards.mjs

22 lines
4.1 KiB
JavaScript

import {validFaithReward} from './faith-blessings.mjs';
import {createPOLQuestTreasuryPayments} from './quest-treasury-payments.mjs';
import {currentMentorPolicy} from './pol-mentor-rewards.mjs';
import {createSystemCharacters} from './system-characters.mjs';
import {OFFICIAL_ACCOUNT} from './official-account.mjs';
const jobs='jarvis_pol_quest_reward_jobs';
export function createPOLFundedRewards(central,bank,keys,{clock=Date.now}={}){
async function process(keyId){return central.transaction(async tx=>{
const job=await tx.first('SELECT * FROM '+jobs+' WHERE key_id=? FOR UPDATE',[keyId]);if(!job||job.state!=='queued')return 'unchanged';
const row=await tx.first('SELECT s.account_id,s.season_id,s.definition_id,s.state,c.reward_json,c.claimed_at,c.account_id claim_account,k.used_at,k.revoked_at,k.key_type,k.scope_id,k.account_id key_account,i.issuer_key_id,i.issuer_name FROM jarvis_pol_quest_states s JOIN jarvis_pol_quest_claims c ON c.key_id=s.key_id JOIN organisation_keys k ON k.id=s.key_id JOIN jarvis_pol_quest_issuers i ON i.quest_key_id=s.key_id WHERE s.key_id=? FOR UPDATE',[keyId]);
if(!row||row.state!=='claimed'||row.used_at==null||row.revoked_at!==null||row.key_type!=='quest'||row.key_account!==job.account_id||row.claim_account!==job.account_id||row.account_id!==job.account_id||row.season_id!==job.season_id||row.scope_id!==job.season_id+':POL_'+row.definition_id)throw Error('Consumed verified POL claim required');
const r=JSON.parse(row.reward_json);if(r.schema!=='pol-funded-reward-v1'||JSON.stringify(r)!==job.payload_json||r.definitionId!==row.definition_id||r.accountId!==row.account_id||r.seasonId!==row.season_id||r.issuerKeyId!==row.issuer_key_id||!Number.isSafeInteger(r.baseAmountMinor)||r.baseAmountMinor<1||r.baseAmountMinor>1000000)throw Error('Bound funded POL reward required');
const policy=await currentMentorPolicy(tx,{lock:true}),issuer=await createSystemCharacters(tx,keys,{authorizeOperator:async()=>false}).inspect(row.issuer_name),bps=issuer?.capabilities.includes('quest.reward-bonus')?policy?.dmBonusBps:0;
if(!policy||r.policyKeyId!==policy.keyId||!issuer?.capabilities.includes('quest.issue')||issuer.keyId!==row.issuer_key_id||r.bonusBps!==bps||r.amountMinor!==(r.baseAmountMinor+Number((BigInt(r.baseAmountMinor)*BigInt(bps)+9999n)/10000n))*(r.faith?2:1))throw Error('Current scoped reward policy and issuer required');
const blessed=!!r.faith;if(blessed&&(!Number.isSafeInteger(r.faithBaseAmountMinor)||r.faithBaseAmountMinor*2!==r.amountMinor||!await validFaithReward(tx,r.faith,{accountId:row.account_id,claimedAt:Number(row.claimed_at)}))||!blessed&&Object.hasOwn(r,'faithBaseAmountMinor'))throw Error('Verified faith reward required');
if(!await tx.first('SELECT id FROM organisation_keys WHERE id=? AND revoked_at IS NULL FOR UPDATE',[issuer.keyId]))throw Error('Current issuer key required');
const actor=await tx.first("SELECT username,role FROM site_accounts WHERE id=? AND password_hash NOT LIKE '!%'",[OFFICIAL_ACCOUNT.id]),player=await tx.first("SELECT id FROM site_accounts WHERE id=? AND password_hash NOT LIKE '!%' AND role NOT IN ('official_account','system_account','memorial') FOR UPDATE",[row.account_id]);if(!player||actor?.username!==OFFICIAL_ACCOUNT.username||actor.role!=='official_account')throw Error('Current payment identities required');
const payments=createPOLQuestTreasuryPayments(bank,{clock,blessed,authorize:async()=>{const current=await tx.first('SELECT used_at,revoked_at FROM organisation_keys WHERE id=? FOR UPDATE',[keyId]);return current?.used_at!=null&&current.revoked_at===null;}});
await payments.pay({keyId,accountId:row.account_id,amountMinor:r.amountMinor,memo:'Reputation reward '+keyId});await tx.query("UPDATE "+jobs+" SET state='paid' WHERE key_id=?",[keyId]);return 'paid';
});}
return Object.freeze({async drain(){const rows=await central.query("SELECT key_id FROM "+jobs+" WHERE state='queued' AND JSON_EXTRACT(payload_json,'$.schema')='pol-funded-reward-v1' ORDER BY created_at,key_id LIMIT 25"),results=[];for(const r of rows){try{results.push({keyId:r.key_id,state:await process(r.key_id)});}catch{results.push({keyId:r.key_id,state:'awaiting-funding-or-review'});}}return {attempted:rows.length,results};}});
}