wowok
Version:
Wowok Blockchain TypeScript API
1 lines • 3.28 kB
JavaScript
import{bigint,object,parse,string}from'valibot';import{bcs}from'../../bcs/index.js';import{normalizeStructTag}from'../../utils/wow-types.js';import{Commands}from'../Commands.js';import{Inputs}from'../Inputs.js';import{getClient}from'../resolve.js';const COIN_WITH_BALANCE='CoinWithBalance',WOW_TYPE=normalizeStructTag('0x2::wow::WOW');export function coinWithBalance({type:type=WOW_TYPE,balance:a,useGasCoin:useGasCoin=!![]}){let b=null;return c=>{if(b)return b;c['addIntentResolver'](COIN_WITH_BALANCE,resolveCoinBalance);const d=type==='gas'?type:normalizeStructTag(type);return b=c['add'](Commands['Intent']({'name':COIN_WITH_BALANCE,'inputs':{},'data':{'type':d===WOW_TYPE&&useGasCoin?'gas':d,'balance':BigInt(a)}})),b;};}const CoinWithBalanceData=object({'type':string(),'balance':bigint()});async function resolveCoinBalance(a,b,c){const d=new Set(),e=new Map();if(!a['sender'])throw new Error('Sender\x20must\x20be\x20set\x20to\x20resolve\x20CoinWithBalance');for(const j of a['commands']){if(j['$kind']==='$Intent'&&j['$Intent']['name']===COIN_WITH_BALANCE){const {type:k,balance:l}=parse(CoinWithBalanceData,j['$Intent']['data']);k!=='gas'&&l>0x0n&&d['add'](k),e['set'](k,(e['get'](k)??0x0n)+l);}}const f=new Set();for(const m of a['inputs']){m['Object']?.['ImmOrOwnedObject']&&f['add'](m['Object']['ImmOrOwnedObject']['objectId']),m['UnresolvedObject']?.['objectId']&&f['add'](m['UnresolvedObject']['objectId']);}const g=new Map(),h=getWowClient(b);await Promise['all']([...d]['map'](async n=>{g['set'](n,await getCoinsOfType({'coinType':n,'balance':e['get'](n),'client':h,'owner':a['sender'],'usedIds':f}));}));const i=new Map();i['set']('gas',{'$kind':'GasCoin','GasCoin':!![]});for(const [n,o]of a['commands']['entries']()){if(o['$kind']!=='$Intent'||o['$Intent']['name']!==COIN_WITH_BALANCE)continue;const {type:p,balance:q}=o['$Intent']['data'];if(q===0x0n&&p!=='gas'){a['replaceCommand'](n,Commands['MoveCall']({'target':'0x2::coin::zero','typeArguments':[p]}));continue;}const r=[];if(!i['has'](p)){const [s,...t]=g['get'](p)['map'](u=>a['addInput']('object',Inputs['ObjectRef']({'objectId':u['coinObjectId'],'digest':u['digest'],'version':u['version']})));t['length']>0x0&&r['push'](Commands['MergeCoins'](s,t)),i['set'](p,s);}r['push'](Commands['SplitCoins'](i['get'](p),[a['addInput']('pure',Inputs['Pure'](bcs['u64']()['serialize'](q)))])),a['replaceCommand'](n,r),a['mapArguments'](u=>{if(u['$kind']==='Result'&&u['Result']===n)return{'$kind':'NestedResult','NestedResult':[n+r['length']-0x1,0x0]};return u;});}return c();}async function getCoinsOfType({coinType:a,balance:b,client:c,owner:d,usedIds:e}){let f=b;const g=[];return h();async function h(i=null){const {data:j,hasNextPage:k,nextCursor:l}=await c['getCoins']({'owner':d,'coinType':a,'cursor':i}),m=j['sort']((n,o)=>Number(BigInt(o['balance'])-BigInt(n['balance'])));for(const n of m){if(e['has'](n['coinObjectId']))continue;const o=BigInt(n['balance']);g['push'](n),f-=o;if(f<=0x0)return g;}if(k)return h(l);throw new Error('Not\x20enough\x20coins\x20of\x20type\x20'+a+'\x20to\x20satisfy\x20requested\x20balance');}}export function getWowClient(a){const b=getClient(a);if(!b['jsonRpc'])throw new Error('CoinWithBalance\x20intent\x20currently\x20only\x20works\x20with\x20WowClient');return b;}