wowok
Version:
Wowok Blockchain TypeScript API
1 lines • 18 kB
JavaScript
import{toTxObject,toTxString,isValidDescription,MAX_DESCRIPTION_LENGTH,isValidU8,PackageAddress,isValidU64,IsValidArgType,rewardFn}from'../common.js';import{W_ERROR,WErrors}from'../exception.js';import{parseObjectType,validObjects}from'../util.js';import{GetObjectExisted,GetObject,GetObjectDescription}from'../common.js';import{LocalMark}from'../local/local.js';import{CallBase,getCoinParam}from'./base.js';import{BuiltinPermissionIndex,newPermission}from'./permission.js';import{owner_receive}from'../util.js';import{AmountType,ObjectType}from'../query/object.js';import{GetAccountOrMark_Address}from'../local/index.js';import{create as a164a}from'./permission.js';import{query_received}from'../query/received.js';export class CallReward extends CallBase{['data'];['object_address']=undefined;['permission_address']=undefined;['new_permission_description']=undefined;['reward_type']=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']));if(this['object_address']){this['content']=undefined,await this['update_content'](a,ObjectType['Reward'],this['object_address']);if(!this['content'])W_ERROR(WErrors['InvalidParam'],'CallReward_Data.data.object:'+this['object_address']);this['permission_address']=this['content']['permission'],this['reward_type']=parseObjectType(this['content']['type_raw'],'reward::Reward'),!this['permission_address']&&W_ERROR(WErrors['InvalidParam'],'CallReward_Data.data.object.permission\x20is\x20empty'),!IsValidArgType(this['reward_type'])&&W_ERROR(WErrors['IsValidArgType'],'CallReward_Data.data.object.type_parameter\x20is\x20empty');}else{const b=GetObject(this['data']['object']);this['permission_address']=await GetObjectExisted(b?.['permission']),this['new_permission_description']=GetObjectDescription(b?.['permission']),this['reward_type']=b?.['type_parameter'],!IsValidArgType(this['reward_type'])&&W_ERROR(WErrors['IsValidArgType'],'CallReward_Data.data.object.type\x20invalid');}}async['call'](a){await this['prepare'](a);const b=[],c=[],d=this['content'],e=h=>{this['permission_address']&&!c['includes'](h)&&c['push'](h);};this['permission_address']&&!this['object_address']&&e(BuiltinPermissionIndex['REWARD_NEW']);if(this['data']?.['claim']!=null){const h=await LocalMark['Instance']()['get_address'](this['data']['claim']);if(h){if(d){const i=d['guards']['find'](j=>j['guard']===h);!i&&W_ERROR(WErrors['InvalidParam'],'Guard\x20'+h+'\x20is\x20not\x20fount\x20in\x20reward\x20guards\x20'+d['guards']['map'](j=>j['guard'])),i?.['amount']?.['type']===AmountType['Fixed']&&BigInt(i?.['amount']?.['value'])>BigInt(d['balance'])&&W_ERROR(WErrors['InvalidParam'],'Guard\x20'+h+'\x20amount\x20'+i?.['amount']?.['value']+'\x20is\x20greater\x20than\x20reward\x20balance\x20'+d['balance']);}this['data']['claim']=h,b['push'](h);}else W_ERROR(WErrors['IsValidArgType'],'CallReward_Data.data.claim\x20is\x20not\x20a\x20valid\x20guard\x20address');}this['permission_address']&&this['data']?.['description']!=null&&this['object_address']&&e(BuiltinPermissionIndex['REWARD_DESCRIPTION']);this['permission_address']&&this['data']?.['coin_add']!=null&&e(BuiltinPermissionIndex['REWARD_COIN_ADD']);this['permission_address']&&this['data']?.['receive']!=null&&e(BuiltinPermissionIndex['REWARD_RECEIVE']);this['permission_address']&&this['data']?.['guard_expiration_time']!==undefined&&e(BuiltinPermissionIndex['REWARD_GUARD_EXPIRATION_TIME_ADD']);this['permission_address']&&this['data']?.['guard_add']!=null&&e(BuiltinPermissionIndex['REWARD_GUARD']);this['permission_address']&&this['data']?.['guard_remove_expired']&&e(BuiltinPermissionIndex['DEMAND_GUARD']);this['permission_address']&&this['data']?.['owner_receive']!=null&&e(BuiltinPermissionIndex['REWARD_OWNER_RECEIVE']);this['permission_address']&&this['data']?.['um']!==undefined&&e(BuiltinPermissionIndex['REWARD_UM']);if(this['permission_address']||b['length']>0x0)return await this['check_permission_and_call'](a,this['permission_address'],c,b);return await this['exec'](a);}async['operate'](a,b,c,d){let e=this['permission_address'],f=this['object_address'];const g=this['content'];!this['object_address']&&(!e&&(e=newPermission(b,this['new_permission_description']??'')),e&&this['reward_type']&&(f=newReward(b,this['reward_type'],toTxObject(b,e),this['data']?.['description']??'',c)));(!f||!e||!this['reward_type'])&&W_ERROR(WErrors['InvalidParam'],'CallReward_Data.data.object:'+this['object_address']);this['data']?.['claim']!=null&&claim(b,f,this['reward_type'],this['data']['claim'],c);this['data']?.['description']!=null&&this['object_address']&&description_set(b,f,this['reward_type'],e,this['data']['description'],c);this['data']?.['coin_add']!=null&&await coin_add(a,b,f,this['reward_type'],e,this['data']['coin_add'],c);if(this['data']?.['receive']!=null&&this['object_address']){if(this['data']['receive']==='recently'){const h=await query_received({'name_or_address':{'name_or_address':this['object_address'],'local_mark_first':!![]},'all_type':!![],'network':a['network'],'no_cache':a['no_cache']});Array['isArray'](h)?W_ERROR(WErrors['InvalidParam'],'CallAllocation_Data.data.received_coins:\x20received\x20is\x20ReceivedNormal[]'):this['data']['receive']=h;}if(typeof this['data']['receive']==='object'&&'received'in this['data']['receive']&&this['data']['receive']['received']?.['length']>0x0){this['data']['receive']?.['token_type']!==this['reward_type']&&W_ERROR(WErrors['InvalidParam'],'CallReward_Data.data.receive.token_type:'+this['data']['receive']?.['token_type']);for(const i of this['data']['receive']['received']){const [j,k]=await Promise['all']([LocalMark['Instance']()['get_address'](i['id']),LocalMark['Instance']()['get_address'](i['payment'])]);k&&j?receive(b,f,this['reward_type'],e,toTxObject(b,j),toTxObject(b,k),c):W_ERROR(WErrors['InvalidParam'],'Payment\x20object\x20'+i['payment']+'\x20or\x20received\x20object\x20'+i['id']+'\x20is\x20not\x20valid');}}}this['data']?.['guard_add']!=null&&(g&&g['guard_not_added_expiration_time']!=null&&BigInt(g['guard_not_added_expiration_time'])>=BigInt(Date['now']())&&W_ERROR(WErrors['InvalidParam'],'The\x20time\x20lock\x20does\x20not\x20allow\x20the\x20addition\x20of\x20new\x20guards.\x20'+this['data']['guard_expiration_time']+'\x20\x20is\x20before\x20current\x20time\x20'+Date['now']()),this['data']['guard_add']['length']>MAX_GUARD_COUNT&&W_ERROR(WErrors['InvalidParam'],'guard_add\x20count\x20exceeds\x20maximum\x20limit\x20'+MAX_GUARD_COUNT),await Promise['all'](this['data']['guard_add']['map'](l=>guard_add(b,f,this['reward_type'],e,l,c))));this['data']['guard_remove_expired']&&guard_remove_expired(b,f,this['reward_type'],e,c);this['data']?.['guard_expiration_time']!==undefined&&guard_expiration_time_add(b,f,this['reward_type'],e,this['data']['guard_expiration_time'],c);this['data']?.['owner_receive']!=null&&this['object_address']&&(this['data']['owner_receive']==='recently'&&(this['data']['owner_receive']=await query_received({'name_or_address':{'name_or_address':this['object_address'],'local_mark_first':!![]},'all_type':!![],'network':a['network'],'no_cache':a['no_cache']})),await owner_receive(b,f,e,this['data']['owner_receive'],this['reward_type'],l=>PackageAddress+'::reward::'+l,c));if(this['data']['um']!==undefined){let l=null;this['data']['um']&&(l=await LocalMark['Instance']()['get_address'](this['data']['um'])||null,!l&&W_ERROR(WErrors['InvalidParam'],'CallReward_Data.data.um:'+this['data']['um'])),um_set(b,f,this['reward_type'],e,l,c);}if(!this['permission_address']){const m=GetObject(this['data']?.['object']);await this['new_with_mark'](a,b,ObjectType['Permission'],a164a(b,e),GetObject(m['permission']));}!this['object_address']&&await this['new_with_mark'](a,b,ObjectType['Reward'],create(b,f,this['reward_type']),GetObject(this['data']?.['object']));}}export const MAX_GUARD_COUNT=0xc8;export const MAX_RECORD_COUNT=0x1450;export function create(a,b,c){return validObjects([b]),!IsValidArgType(c)&&W_ERROR(WErrors['InvalidParam'],'Create\x20reward\x20type\x20'+c+'\x20is\x20not\x20valid'),a['moveCall']({'target':rewardFn('create'),'arguments':[toTxObject(a,b)],'typeArguments':[c]});}async function reward_guard(a,b){const c=await LocalMark['Instance']()['get_address'](b['guard']);c?b['guard']=c:W_ERROR(WErrors['InvalidParam'],'Guard\x20'+b['guard']+'\x20is\x20not\x20valid');validObjects([b['guard']]);if('GuardIdentifier'in b['recipient']){const d=b['recipient']['GuardIdentifier'];typeof d!=='number'&&W_ERROR(WErrors['InvalidParam'],'Recipient\x20GuardIdentifier\x20'+d+'\x20must\x20be\x20a\x20number');!isValidU8(d)&&W_ERROR(WErrors['InvalidParam'],'Recipient\x20GuardIdentifier\x20'+d+'\x20must\x20be\x20a\x20valid\x20u8');if(b['amount']['type']===AmountType['GuardU64Identifier'])return typeof b['amount']['value']!=='number'&&W_ERROR(WErrors['InvalidParam'],'Amount\x20GuardU64Identifier\x20value\x20must\x20be\x20a\x20number'),!isValidU8(b['amount']['value'])&&W_ERROR(WErrors['InvalidParam'],'Amount\x20GuardU64Identifier\x20'+b['amount']['value']+'\x20must\x20be\x20a\x20valid\x20u8'),a['moveCall']({'target':rewardFn('guard_u8_u8'),'arguments':[toTxObject(a,b['guard']),a['pure']['u8'](d),a['pure']['u8'](b['amount']['value']),a['pure']['option']('u64',b['expiration_time']??null),a['pure']['option']('u8',b['store_from_id']??null)]});else{if(b['amount']['type']===AmountType['Fixed'])return typeof b['amount']['value']!=='number'&&W_ERROR(WErrors['InvalidParam'],'Amount\x20Fixed\x20value\x20must\x20be\x20a\x20number'),!isValidU64(b['amount']['value'])&&W_ERROR(WErrors['InvalidParam'],'Amount\x20Fixed\x20'+b['amount']['value']+'\x20must\x20be\x20a\x20valid\x20u64'),a['moveCall']({'target':rewardFn('guard_u8_u64'),'arguments':[toTxObject(a,b['guard']),a['pure']['u8'](d),a['pure']['u64'](b['amount']['value']),a['pure']['option']('u64',b['expiration_time']??null),a['pure']['option']('u8',b['store_from_id']??null)]});}}else{if('Entity'in b['recipient']){const e=b['recipient']['Entity'],f=await GetAccountOrMark_Address(e);!f&&W_ERROR(WErrors['InvalidParam'],'Recipient\x20Entity\x20'+e+'\x20must\x20be\x20a\x20valid\x20address');if(b['amount']['type']===AmountType['GuardU64Identifier'])return typeof b['amount']['value']!=='number'&&W_ERROR(WErrors['InvalidParam'],'Amount\x20GuardU64Identifier\x20value\x20must\x20be\x20a\x20number'),!isValidU8(b['amount']['value'])&&W_ERROR(WErrors['InvalidParam'],'Amount\x20GuardU64Identifier\x20'+b['amount']['value']+'\x20must\x20be\x20a\x20valid\x20u8'),a['moveCall']({'target':rewardFn('guard_entity_u8'),'arguments':[toTxObject(a,b['guard']),a['pure']['address'](f),a['pure']['u8'](b['amount']['value']),a['pure']['option']('u64',b['expiration_time']??null),a['pure']['option']('u8',b['store_from_id']??null)]});else{if(b['amount']['type']===AmountType['Fixed'])return typeof b['amount']['value']!=='number'&&W_ERROR(WErrors['InvalidParam'],'Amount\x20Fixed\x20value\x20must\x20be\x20a\x20number'),!isValidU64(b['amount']['value'])&&W_ERROR(WErrors['InvalidParam'],'Amount\x20Fixed\x20'+b['amount']['value']+'\x20must\x20be\x20a\x20valid\x20u64'),a['moveCall']({'target':rewardFn('guard_entity_u64'),'arguments':[toTxObject(a,b['guard']),a['pure']['address'](f),a['pure']['u64'](b['amount']['value']),a['pure']['option']('u64',b['expiration_time']??null),a['pure']['option']('u8',b['store_from_id']??null)]});}}else{if('Signer'in b['recipient']){if(b['amount']['type']===AmountType['GuardU64Identifier'])return typeof b['amount']['value']!=='number'&&W_ERROR(WErrors['InvalidParam'],'Amount\x20GuardU64Identifier\x20value\x20must\x20be\x20a\x20number'),!isValidU8(b['amount']['value'])&&W_ERROR(WErrors['InvalidParam'],'Amount\x20GuardU64Identifier\x20'+b['amount']['value']+'\x20must\x20be\x20a\x20valid\x20u8'),a['moveCall']({'target':rewardFn('guard_signer_u8'),'arguments':[toTxObject(a,b['guard']),a['pure']['u8'](b['amount']['value']),a['pure']['option']('u64',b['expiration_time']??null),a['pure']['option']('u8',b['store_from_id']??null)]});else{if(b['amount']['type']===AmountType['Fixed'])return typeof b['amount']['value']!=='number'&&W_ERROR(WErrors['InvalidParam'],'Amount\x20Fixed\x20value\x20must\x20be\x20a\x20number'),!isValidU64(b['amount']['value'])&&W_ERROR(WErrors['InvalidParam'],'Amount\x20Fixed\x20'+b['amount']['value']+'\x20must\x20be\x20a\x20valid\x20u64'),a['moveCall']({'target':rewardFn('guard_signer_u64'),'arguments':[toTxObject(a,b['guard']),a['pure']['u64'](b['amount']['value']),a['pure']['option']('u64',b['expiration_time']??null),a['pure']['option']('u8',b['store_from_id']??null)]});}}}}return W_ERROR(WErrors['InvalidParam'],'Invalid\x20recipient\x20or\x20amount\x20type\x20combination'),undefined;}export function claim(a,b,c,d,e){return!e&&W_ERROR(WErrors['InvalidParam'],'CallReward_Data.data.claim:\x20passport\x20is\x20required'),validObjects([b,e,d]),!IsValidArgType(c)&&W_ERROR(WErrors['IsValidArgType'],'Invalid\x20reward\x20type\x20'+c),a['moveCall']({'target':rewardFn('claim'),'arguments':[toTxObject(a,b),toTxObject(a,e),toTxObject(a,d),a['object']['clock']()],'typeArguments':[c]});}export function newReward(a,b,c,d,e){return validObjects([c,e]),!IsValidArgType(b)&&W_ERROR(WErrors['InvalidParam'],'Invalid\x20reward\x20type\x20'+b),typeof d==='string'&&!isValidDescription(d)&&W_ERROR(WErrors['IsValidDescription'],'Description\x20exceeds\x20maximum\x20length\x20'+MAX_DESCRIPTION_LENGTH),e?a['moveCall']({'target':rewardFn('new_with_passport'),'arguments':[toTxObject(a,c),toTxObject(a,e),toTxString(a,d),a['object']['mutRegistrar'](),a['object']['clock']()],'typeArguments':[b]}):a['moveCall']({'target':rewardFn('new'),'arguments':[toTxObject(a,c),toTxString(a,d),a['object']['mutRegistrar'](),a['object']['clock']()],'typeArguments':[b]});}export function description_set(a,b,c,d,e,f){validObjects([b,d,f]),!IsValidArgType(c)&&W_ERROR(WErrors['InvalidParam'],'Invalid\x20reward\x20type\x20'+c),typeof e==='string'&&!isValidDescription(e)&&W_ERROR(WErrors['IsValidDescription'],'Description\x20exceeds\x20maximum\x20length\x20'+MAX_DESCRIPTION_LENGTH),f?a['moveCall']({'target':rewardFn('description_set_with_passport'),'arguments':[toTxObject(a,b),toTxObject(a,d),toTxObject(a,f),toTxString(a,e)],'typeArguments':[c]}):a['moveCall']({'target':rewardFn('description_set'),'arguments':[toTxObject(a,b),toTxObject(a,d),toTxString(a,e)],'typeArguments':[c]});}export async function guard_add(a,b,c,d,e,f){validObjects([b,d,f]);!IsValidArgType(c)&&W_ERROR(WErrors['InvalidParam'],'Invalid\x20reward\x20type\x20'+c);const g=await reward_guard(a,e);!g&&W_ERROR(WErrors['InvalidParam'],'CallReward_Data.data.guard_add\x20'+e+'\x20failed'),f?a['moveCall']({'target':rewardFn('guard_add_with_passport'),'arguments':[toTxObject(a,b),toTxObject(a,d),toTxObject(a,f),g,a['object']['mutRegistrar'](),a['object']['clock']()],'typeArguments':[c]}):a['moveCall']({'target':rewardFn('guard_add'),'arguments':[toTxObject(a,b),toTxObject(a,d),g,a['object']['mutRegistrar'](),a['object']['clock']()],'typeArguments':[c]});}export function guard_remove_expired(a,b,c,d,e){validObjects([b,d,e]),!IsValidArgType(c)&&W_ERROR(WErrors['InvalidParam'],'Invalid\x20reward\x20type\x20'+c),e?a['moveCall']({'target':rewardFn('guard_remove_expired_with_passport'),'arguments':[toTxObject(a,b),toTxObject(a,d),toTxObject(a,e),a['object']['mutRegistrar'](),a['object']['clock']()],'typeArguments':[c]}):a['moveCall']({'target':rewardFn('guard_remove_expired'),'arguments':[toTxObject(a,b),toTxObject(a,d),a['object']['mutRegistrar'](),a['object']['clock']()],'typeArguments':[c]});}export async function coin_add(a,b,d,e,f,g,h){validObjects([d,f,h]);!IsValidArgType(e)&&W_ERROR(WErrors['InvalidParam'],'Invalid\x20reward\x20type\x20'+e);const i=await getCoinParam(a,b,g,e);h?b['moveCall']({'target':rewardFn('coin_add_with_passport'),'arguments':[toTxObject(b,d),toTxObject(b,f),toTxObject(b,h),i],'typeArguments':[e]}):b['moveCall']({'target':rewardFn('coin_add'),'arguments':[toTxObject(b,d),toTxObject(b,f),i],'typeArguments':[e]});}export function receive(a,b,c,d,e,f,g){return validObjects([b,d,g,e,f]),!IsValidArgType(c)&&W_ERROR(WErrors['InvalidParam'],'Invalid\x20reward\x20type\x20'+c),g?a['moveCall']({'target':rewardFn('receive_with_passport'),'arguments':[toTxObject(a,b),toTxObject(a,d),toTxObject(a,g),toTxObject(a,e),toTxObject(a,f)],'typeArguments':[c]}):a['moveCall']({'target':rewardFn('receive'),'arguments':[toTxObject(a,b),toTxObject(a,d),toTxObject(a,e),toTxObject(a,f)],'typeArguments':[c]});}export function guard_expiration_time_add(a,b,c,d,e,f){validObjects([b,d,f]),!IsValidArgType(c)&&W_ERROR(WErrors['InvalidParam'],'Invalid\x20reward\x20type\x20'+c),f?a['moveCall']({'target':rewardFn('guard_expiration_time_add_with_passport'),'arguments':[toTxObject(a,b),toTxObject(a,d),toTxObject(a,f),a['pure']['option']('u64',e??null),a['object']['clock']()],'typeArguments':[c]}):a['moveCall']({'target':rewardFn('guard_expiration_time_add'),'arguments':[toTxObject(a,b),toTxObject(a,d),a['pure']['option']('u64',e??null),a['object']['clock']()],'typeArguments':[c]});}export function um_set(a,b,c,d,e,f){validObjects([b,d]),!IsValidArgType(c)&&W_ERROR(WErrors['InvalidParam'],'Invalid\x20reward\x20type\x20'+c),e?f?a['moveCall']({'target':rewardFn('um_set_with_passport'),'arguments':[toTxObject(a,b),toTxObject(a,d),f,toTxObject(a,e),a['object']['mutRegistrar'](),a['object']['clock']()],'typeArguments':[c]}):a['moveCall']({'target':rewardFn('um_set'),'arguments':[toTxObject(a,b),toTxObject(a,d),toTxObject(a,e),a['object']['mutRegistrar'](),a['object']['clock']()],'typeArguments':[c]}):f?a['moveCall']({'target':rewardFn('um_none_with_passport'),'arguments':[toTxObject(a,b),toTxObject(a,d),f,a['object']['mutRegistrar']()],'typeArguments':[c]}):a['moveCall']({'target':rewardFn('um_none'),'arguments':[toTxObject(a,b),toTxObject(a,d),a['object']['mutRegistrar']()],'typeArguments':[c]});}