wowok
Version:
Wowok Blockchain TypeScript API
1 lines • 4.5 kB
JavaScript
import{normalizeWowAddress}from'../utils/wow-types.js';export class AsyncCache{async['getObject'](a){const [b,c]=await Promise['all']([this['get']('OwnedObject',a),this['get']('SharedOrImmutableObject',a)]);return b??c??null;}async['getObjects'](a){return Promise['all'](a['map'](b=>this['getObject'](b)));}async['addObject'](a){return a['owner']?await this['set']('OwnedObject',a['objectId'],a):await this['set']('SharedOrImmutableObject',a['objectId'],a),a;}async['addObjects'](a){await Promise['all'](a['map'](async b=>this['addObject'](b)));}async['deleteObject'](a){await Promise['all']([this['delete']('OwnedObject',a),this['delete']('SharedOrImmutableObject',a)]);}async['deleteObjects'](a){await Promise['all'](a['map'](b=>this['deleteObject'](b)));}async['getMoveFunctionDefinition'](a){const b=normalizeWowAddress(a['package'])+'::'+a['module']+'::'+a['function'];return this['get']('MoveFunction',b);}async['addMoveFunctionDefinition'](a){const b=normalizeWowAddress(a['package']),c=b+'::'+a['module']+'::'+a['function'],d={...a,'package':b};return await this['set']('MoveFunction',c,d),d;}async['deleteMoveFunctionDefinition'](a){const b=normalizeWowAddress(a['package'])+'::'+a['module']+'::'+a['function'];await this['delete']('MoveFunction',b);}async['getCustom'](a){return this['get']('Custom',a);}async['setCustom'](a,b){return this['set']('Custom',a,b);}async['deleteCustom'](a){return this['delete']('Custom',a);}}export class InMemoryCache extends AsyncCache{#caches={'OwnedObject':new Map(),'SharedOrImmutableObject':new Map(),'MoveFunction':new Map(),'Custom':new Map()};async['get'](a,b){return this.#caches[a]['get'](b)??null;}async['set'](a,b,c){this.#caches[a]['set'](b,c);}async['delete'](a,b){this.#caches[a]['delete'](b);}async['clear'](a){if(a)this.#caches[a]['clear']();else for(const b of Object['values'](this.#caches)){b['clear']();}}}export class ObjectCache{#cache;#onEffects;constructor({cache:cache=new InMemoryCache(),onEffects:a}){this.#cache=cache,this.#onEffects=a;}['asPlugin'](){return async(a,b,c)=>{const d=a['inputs']['filter'](g=>g['UnresolvedObject'])['map'](g=>g['UnresolvedObject']['objectId']),e=(await this.#cache['getObjects'](d))['filter'](g=>g!==null),f=new Map(e['map'](g=>[g['objectId'],g]));for(const g of a['inputs']){if(!g['UnresolvedObject'])continue;const h=f['get'](g['UnresolvedObject']['objectId']);if(!h)continue;h['initialSharedVersion']&&!g['UnresolvedObject']['initialSharedVersion']?g['UnresolvedObject']['initialSharedVersion']=h['initialSharedVersion']:(h['version']&&!g['UnresolvedObject']['version']&&(g['UnresolvedObject']['version']=h['version']),h['digest']&&!g['UnresolvedObject']['digest']&&(g['UnresolvedObject']['digest']=h['digest']));}await Promise['all'](a['commands']['map'](async i=>{if(i['MoveCall']){const j=await this['getMoveFunctionDefinition']({'package':i['MoveCall']['package'],'module':i['MoveCall']['module'],'function':i['MoveCall']['function']});j&&(i['MoveCall']['_argumentTypes']=j['parameters']);}})),await c(),await Promise['all'](a['commands']['map'](async i=>{i['MoveCall']?.['_argumentTypes']&&await this.#cache['addMoveFunctionDefinition']({'package':i['MoveCall']['package'],'module':i['MoveCall']['module'],'function':i['MoveCall']['function'],'parameters':i['MoveCall']['_argumentTypes']});}));};}async['clear'](){await this.#cache['clear']();}async['getMoveFunctionDefinition'](a){return this.#cache['getMoveFunctionDefinition'](a);}async['getObjects'](a){return this.#cache['getObjects'](a);}async['deleteObjects'](a){return this.#cache['deleteObjects'](a);}async['clearOwnedObjects'](){await this.#cache['clear']('OwnedObject');}async['clearCustom'](){await this.#cache['clear']('Custom');}async['getCustom'](a){return this.#cache['getCustom'](a);}async['setCustom'](a,b){return this.#cache['setCustom'](a,b);}async['deleteCustom'](a){return this.#cache['deleteCustom'](a);}async['applyEffects'](a){if(!a['V2'])throw new Error('Unsupported\x20transaction\x20effects\x20version\x20'+a['$kind']);const {lamportVersion:b,changedObjects:c}=a['V2'],d=[],e=[];c['forEach'](([f,g])=>{if(g['outputState']['NotExist'])d['push'](f);else{if(g['outputState']['ObjectWrite']){const [h,i]=g['outputState']['ObjectWrite'];e['push']({'objectId':f,'digest':h,'version':b,'owner':i['AddressOwner']??i['ObjectOwner']??null,'initialSharedVersion':i['Shared']?.['initialSharedVersion']??null});}}}),await Promise['all']([this.#cache['addObjects'](e),this.#cache['deleteObjects'](d),this.#onEffects?.(a)]);}}