wowok
Version:
Wowok Blockchain TypeScript API
1 lines • 10.9 kB
JavaScript
import{toTxObject,PackageAddress,GetObjectExisted,IsValidArgType,serviceFn}from'../common.js';import{W_ERROR,WErrors}from'../exception.js';import{GetAccountOrMark_Address,GetManyAccountOrMark_Address}from'../local/index.js';import{LocalMark}from'../local/local.js';import{ArbStatus,ObjectType,query_objects}from'../query/object.js';import{query_received}from'../query/received.js';import{parseObjectType,validObjects}from'../util.js';import{description_change,MAX_PROPOSITION_COUNT,objection,principal_confirm,proposition_change}from'./arb.js';import{CallBase}from'./base.js';import{accomplish,getMachineNodeForward,hold}from'./progress.js';export class CallOrder extends CallBase{['data'];['object_address']=undefined;['service']=undefined;constructor(a){super(),this['data']=a;}async['prepare'](a){await super['prepare'](a),!this['object_address']&&(this['object_address']=await GetObjectExisted(this['data']?.['object'])),this['object_address']?(this['content']=undefined,await this['update_content'](a,ObjectType['Order'],this['object_address']),!this['content']&&W_ERROR(WErrors['InvalidParam'],'CallOrder_Data.data.object:'+this['object_address'])):W_ERROR(WErrors['InvalidParam'],'CallOrder_Data.data.object:'+this['data']?.['object']);}async['call'](a){await this['prepare'](a);const b=[],c=e=>{!b['includes'](e)&&b['push'](e);},d=this['content'];if(this['data']['progress']!=null){(!d['machine']||!d['progress'])&&W_ERROR(WErrors['InvalidParam'],'CallOrder_Data.data.progress\x20error\x20on\x20progress\x20not\x20set');const e=await query_objects({'objects':[d['progress']],'no_cache':a['no_cache'],'network':a['network']});(!e['objects']||e['objects']['length']!==0x1||e['objects'][0x0]['type']!==ObjectType['Progress'])&&W_ERROR(WErrors['InvalidParam'],'CallOrder_Data.data.progress\x20fetch\x20error');const f=e['objects'][0x0],g=await getMachineNodeForward(a,f['machine'],f['current'],this['data']['progress']['operation']['next_node_name'],this['data']['progress']['operation']['forward']);g['error']&&W_ERROR(WErrors['InvalidParam'],'CallOrder_Data.data.progress.operation.forward:'+this['data']?.['progress']['operation']['forward']+'\x20validation\x20failed.\x20'+g['error']),g['forward']?.['guard']&&c(g['forward']['guard']['guard']);}if(b['length']>0x0)return await this['check_guard_and_call'](a,b);return await this['exec'](a);}async['get_service'](a){if(!this['service']&&this['content']){const b=await query_objects({'objects':[this['content']['service']],'no_cache':a['no_cache'],'network':a['network']});if(b['objects']&&b['objects']['length']===0x1){const c=b['objects'][0x0];(!c['type']||c['type']!==ObjectType['Service'])&&W_ERROR(WErrors['IsValidArgType'],'object\x20'+this['content']['service']+'\x20type\x20'+c['type']+'\x20is\x20not\x20Service');const d=parseObjectType(c['type_raw'],'service::Service');!d&&W_ERROR(WErrors['IsValidArgType'],'Service\x20'+this['content']['service']+'\x20type\x20'+c['type_raw']+'\x20is\x20not\x20valid'),this['service']={'object':c,'type':d};}}return!this['service']&&W_ERROR(WErrors['InvalidParam'],'CallOrder_Data.data.order_required_info\x20error\x20on\x20service\x20'+this['content']['service']),this['service'];}async['operate'](a,b,c,d){const e=this['content'];this['data']['agent']!=null&&agent_set(b,{'order':toTxObject(b,this['object_address']),'agents':await GetManyAccountOrMark_Address(this['data']['agent'])});this['data']['required_info']!==undefined&&await order_required_info_update(b,toTxObject(b,this['object_address']),this['data']['required_info']);if(this['data']['progress']!=null){!e['machine']&&W_ERROR(WErrors['InvalidParam'],'CallOrder_Data.data.progress\x20fail\x20with\x20machine\x20invalid\x20for\x20this\x20order');const f=await query_objects({'objects':[e['machine']],'no_cache':a['no_cache'],'network':a['network']});if(f['objects']&&f['objects']['length']===0x1&&f['objects'][0x0]['type']===ObjectType['Machine']){const g=f['objects'][0x0];'hold'in this['data']['progress']?hold(b,this['object_address'],g['object'],g['permission'],this['data']['progress']['operation']['next_node_name'],this['data']['progress']['operation']['forward'],this['data']['progress']['message']??'',this['data']['progress']['hold'],this['object_address'],c):accomplish(b,this['object_address'],g['object'],g['permission'],this['data']['progress']['operation']['next_node_name'],this['data']['progress']['operation']['forward'],this['data']['progress']['message']??'',this['object_address'],c);}else W_ERROR(WErrors['InvalidParam'],'CallOrder_Data.data.progress\x20fail\x20with\x20machine\x20invalid\x20for\x20this\x20order');}if(this['data']['arb_confirm']!=null){const h=await this['get_arb'](a,this['data']['arb_confirm']['arb']);this['check_arb'](e,h,ArbStatus['Principal_confirming']);if(this['data']['arb_confirm']['proposition']!==undefined)this['data']['arb_confirm']['proposition']['length']>MAX_PROPOSITION_COUNT&&W_ERROR(WErrors['InvalidParam'],'CallOrder_Data.data.arb_confirm.proposition\x20length\x20must\x20be\x20less\x20than\x20or\x20equal\x20to\x20'+MAX_PROPOSITION_COUNT),proposition_change(b,h['object']['object'],h['type'],toTxObject(b,this['object_address']),this['data']['arb_confirm']['proposition'],this['data']['arb_confirm']['description'],this['data']['arb_confirm']['confirm']);else this['data']['arb_confirm']['description']!==undefined?description_change(b,h['object']['object'],h['type'],toTxObject(b,this['object_address']),this['data']['arb_confirm']['description'],this['data']['arb_confirm']['confirm']):principal_confirm(b,h['object']['object'],h['type'],toTxObject(b,this['object_address']),this['data']['arb_confirm']['confirm']);}if(this['data']['arb_objection']!=null){const i=await this['get_arb'](a,this['data']['arb_objection']['arb']);this['check_arb'](e,i,ArbStatus['Arbitrated']),objection(b,i['object']['object'],i['type'],toTxObject(b,this['object_address']),this['data']['arb_objection']['objection']);}if(this['data']['arb_claim_compensation']){const j=await this['get_arb'](a,this['data']['arb_claim_compensation']['arb']);this['check_arb'](e,j,ArbStatus['Arbitrated']);const k=await this['get_service'](a);compensation_claim(b,{'service':k['object']['object'],'service_type':k['type'],'arbitration_object':j['object']['arbitration'],'arbitration_type':j['type'],'order':toTxObject(b,this['object_address']),'arb':j['object']['object']});}this['data']['receive']!=null&&this['object_address']&&(this['data']['receive']==='recently'&&(this['data']['receive']=await query_received({'name_or_address':{'name_or_address':this['object_address'],'local_mark_first':!![]},'all_type':!![],'no_cache':a['no_cache'],'network':a['network']})),await owner_receive(b,toTxObject(b,this['object_address']),this['data']['receive'])),this['data']['transfer_to']!=null&&await owner_change(b,{'order':toTxObject(b,this['object_address']),'new_owner':this['data']['transfer_to']});}async['get_arb'](a,b){const c=await query_objects({'objects':[b],'no_cache':a['no_cache'],'network':a['network']});(!c['objects']||c['objects']['length']!==0x1)&&W_ERROR(WErrors['InvalidParam'],'Arb\x20'+b+'\x20not\x20found');const d=c['objects'][0x0];(!d['type']||d['type']!==ObjectType['Arb'])&&W_ERROR(WErrors['IsValidArgType'],'object\x20'+b+'\x20type\x20'+d['type']+'\x20is\x20not\x20Arb');const e=parseObjectType(d['type_raw'],'arb::Arb');return!e&&W_ERROR(WErrors['IsValidArgType'],'Arb\x20'+b+'\x20type\x20'+d['type_raw']+'\x20is\x20not\x20valid'),{'object':d,'type':e};}['check_arb'](a,b,c){b['object']['status']!==c&&W_ERROR(WErrors['InvalidParam'],'arb\x20'+b['object']['object']+'\x20status\x20'+b['object']['status']+'\x20not\x20'+c),b['object']['order']!==this['object_address']&&W_ERROR(WErrors['InvalidParam'],'arb\x20'+b['object']['object']+'\x20order\x20'+b['object']['order']+'\x20not\x20'+this['object_address']),a['claimed_by']&&W_ERROR(WErrors['InvalidParam'],'arb\x20'+b['object']['object']+'\x20error\x20on\x20order\x20'+this['object_address']+'\x20already\x20disputed\x20by\x20'+a['claimed_by']);}}export function orderFn(a){return PackageAddress+'::order::'+a;}export const MAX_AGENT_COUNT=0xa;export const MAX_DISPUTE_COUNT=0xa;export function create(a,b){return validObjects([b]),a['moveCall']({'target':orderFn('create'),'arguments':[toTxObject(a,b)]});}export async function owner_change(a,b){const c=await GetAccountOrMark_Address(b['new_owner']);!c&&W_ERROR(WErrors['InvalidParam'],'CallOrder_Data.data.new_owner:'+b['new_owner']),validObjects([b['order']]),a['moveCall']({'target':orderFn('owner_change'),'arguments':[toTxObject(a,b['order']),a['pure']['address'](c)]});}export function agent_set(a,b){validObjects([b['order'],...b['agents']]),a['moveCall']({'target':orderFn('agent_set'),'arguments':[toTxObject(a,b['order']),a['pure']['vector']('address',b['agents'])]});}export async function owner_receive(a,b,c){validObjects([b]);if(Array['isArray'](c))for(const d of c){(!d['type']||!IsValidArgType(d['type']))&&W_ERROR(WErrors['IsValidArgType'],'type\x20'+d['type']+'\x20is\x20invalid'),validObjects([d['id']]),a['moveCall']({'target':orderFn('owner_receive_object'),'arguments':[toTxObject(a,b),toTxObject(a,d['id'])],'typeArguments':[d['type']]});}else{const e=c['token_type']['match'](/CoinWrapper<([^>]+)>/);!e&&W_ERROR(WErrors['IsValidArgType'],'Invalid\x20CoinWrapper\x20type:\x20'+c['token_type']);const f=e[0x1];!IsValidArgType(f)&&W_ERROR(WErrors['IsValidArgType'],'Invalid\x20CoinWrapper\x20type:\x20'+f);for(const g of c['received']){const [h,i]=await Promise['all']([LocalMark['Instance']()['get_address'](g['id']),LocalMark['Instance']()['get_address'](g['payment'])]);(!h||!i)&&W_ERROR(WErrors['IsValidArgType'],'Received\x20'+g['id']+'\x20or\x20Payment\x20'+g['payment']+'\x20is\x20invalid'),a['moveCall']({'target':orderFn('owner_receive'),'arguments':[toTxObject(a,b),toTxObject(a,h),toTxObject(a,i)],'typeArguments':[f]});}}}export async function order_required_info_update(a,b,c){validObjects([b]),c!==undefined&&(c=await LocalMark['Instance']()['get_address'](c)??null,!c&&W_ERROR(WErrors['InvalidParam'],'CallOrder_Data.data.required_info\x20'+c+'\x20not\x20found')),a['moveCall']({'target':orderFn('required_info_update'),'arguments':[toTxObject(a,b),a['pure']['option']('address',c)]});}export function compensation_claim(a,b){!IsValidArgType(b['service_type'])&&W_ERROR(WErrors['InvalidParam'],'Compensation\x20claim\x20service\x20type\x20'+b['service_type']+'\x20is\x20not\x20valid'),!IsValidArgType(b['arbitration_type'])&&W_ERROR(WErrors['InvalidParam'],'Compensation\x20claim\x20arbitration\x20type\x20'+b['arbitration_type']+'\x20is\x20not\x20valid'),validObjects([b['service'],b['arbitration_object'],b['order'],b['arb']]),a['moveCall']({'target':serviceFn('compensation_claim'),'arguments':[toTxObject(a,b['service']),toTxObject(a,b['arbitration_object']),toTxObject(a,b['order']),toTxObject(a,b['arb']),a['object']['clock']()],'typeArguments':[b['service_type'],b['arbitration_type']]});}