wowok
Version:
Wowok Blockchain TypeScript API
1 lines • 14.2 kB
JavaScript
import{bcs}from'../bcs/index.js';import{isThisPackage,IsValidArgType,isValidU128,isValidU16,isValidU256,isValidU32,isValidU64,isValidU8,isValidValueType,PackageAddress,ValueType}from'./common.js';import{W_ERROR,WErrors}from'./exception.js';import{GetAccountOrMark_Address,GetManyAccountOrMark_Address}from'./local/index.js';import{toTxObject}from'./common.js';import{normalizeWowAddress}from'../utils/wow-types.js';import{LocalMark}from'./local/local.js';import{isValidAddress}from'./local/local.js';export const isValidValueContainer=a=>{try{return valueToBcsBytes(a['valueType'],a['value']),!![];}catch{return![];}};const toU64Value=a=>{if(typeof a==='string'){if(a['length']>0xf)return BigInt(a);else{const b=parseInt(a,0xa);return Number['isInteger'](b)?b:BigInt(a);}}return a;},toU128Value=a=>{if(typeof a==='bigint')return a;if(typeof a==='string')return BigInt(a);return BigInt(a);},toU256Value=a=>{if(typeof a==='bigint')return a;if(typeof a==='string')return BigInt(a);return BigInt(a);};export const valueToBcsBytes=async(a,b)=>{if(!isValidValueType(a))return W_ERROR(WErrors['InvalidParam'],'Invalid\x20ValueType:\x20'+a),new Uint8Array(0x0);const c=new Uint8Array(0x1);c[0x0]=a;try{let d;switch(a){case ValueType['Bool']:d=bcs['Bool']['serialize'](b)['toBytes']();break;case ValueType['Address']:{let l,m='';if(typeof b==='string')l=await GetAccountOrMark_Address({'name_or_address':b}),m='string:\x20'+b;else{if(b&&typeof b==='object'&&'name_or_address'in b){const n=b;l=await GetAccountOrMark_Address(n),m='AccountOrMark_Address:\x20'+JSON['stringify'](b);}else l=undefined,m='type:\x20'+typeof b+',\x20value:\x20'+JSON['stringify'](b);}if(l===undefined)return W_ERROR(WErrors['InvalidParam'],'Expected\x20valid\x20Address\x20for\x20ValueType.Address,\x20but\x20failed\x20to\x20resolve\x20'+m+'.\x20Address\x20may\x20not\x20exist\x20in\x20local\x20accounts\x20or\x20marks.'),new Uint8Array(0x0);d=bcs['Address']['serialize'](l)['toBytes']();break;}case ValueType['String']:d=bcs['String']['serialize'](b)['toBytes']();break;case ValueType['U8']:if(!isValidU8(b))return W_ERROR(WErrors['InvalidParam'],'Expected\x20valid\x20U8\x20for\x20ValueType.U8,\x20got\x20'+JSON['stringify'](b)),new Uint8Array(0x0);d=bcs['U8']['serialize'](b)['toBytes']();break;case ValueType['U16']:if(!isValidU16(b))return W_ERROR(WErrors['InvalidParam'],'Expected\x20valid\x20U16\x20for\x20ValueType.U16,\x20got\x20'+JSON['stringify'](b)),new Uint8Array(0x0);d=bcs['U16']['serialize'](b)['toBytes']();break;case ValueType['U32']:if(!isValidU32(b))return W_ERROR(WErrors['InvalidParam'],'Expected\x20valid\x20U32\x20for\x20ValueType.U32,\x20got\x20'+JSON['stringify'](b)),new Uint8Array(0x0);d=bcs['U32']['serialize'](b)['toBytes']();break;case ValueType['U64']:if(!isValidU64(b))return W_ERROR(WErrors['InvalidParam'],'Expected\x20valid\x20U64\x20for\x20ValueType.U64,\x20got\x20'+JSON['stringify'](b)),new Uint8Array(0x0);d=bcs['U64']['serialize'](toU64Value(b))['toBytes']();break;case ValueType['U128']:if(!isValidU128(b))return W_ERROR(WErrors['InvalidParam'],'Expected\x20valid\x20U128\x20for\x20ValueType.U128,\x20got\x20'+JSON['stringify'](b)),new Uint8Array(0x0);d=bcs['U128']['serialize'](toU128Value(b))['toBytes']();break;case ValueType['U256']:if(!isValidU256(b))return W_ERROR(WErrors['InvalidParam'],'Expected\x20valid\x20U256\x20for\x20ValueType.U256,\x20got\x20'+JSON['stringify'](b)),new Uint8Array(0x0);d=bcs['U256']['serialize'](toU256Value(b))['toBytes']();break;case ValueType['VecBool']:if(!Array['isArray'](b))return W_ERROR(WErrors['InvalidParam'],'Expected\x20array\x20for\x20ValueType.VecBool,\x20got\x20'+typeof b),new Uint8Array(0x0);for(const o of b){if(typeof o!=='boolean')return W_ERROR(WErrors['InvalidParam'],'Expected\x20boolean\x20in\x20VecBool,\x20got\x20'+typeof o),new Uint8Array(0x0);}d=bcs['vector'](bcs['Bool'])['serialize'](b)['toBytes']();break;case ValueType['VecAddress']:const f=await GetManyAccountOrMark_Address(b);d=bcs['vector'](bcs['Address'])['serialize'](f)['toBytes']();break;case ValueType['VecString']:if(!Array['isArray'](b))return W_ERROR(WErrors['InvalidParam'],'Expected\x20array\x20for\x20ValueType.VecString,\x20got\x20'+typeof b),new Uint8Array(0x0);for(const p of b){if(typeof p!=='string')return W_ERROR(WErrors['InvalidParam'],'Expected\x20string\x20in\x20VecString,\x20got\x20'+typeof p),new Uint8Array(0x0);}d=bcs['vector'](bcs['String'])['serialize'](b)['toBytes']();break;case ValueType['VecU8']:if(b instanceof Uint8Array)d=bcs['vector'](bcs['U8'])['serialize'](Array['from'](b))['toBytes']();else{if(Array['isArray'](b)){for(const q of b){if(!isValidU8(q))return W_ERROR(WErrors['InvalidParam'],'Expected\x20valid\x20U8\x20in\x20VecU8,\x20got\x20'+q),new Uint8Array(0x0);}d=bcs['vector'](bcs['U8'])['serialize'](b)['toBytes']();}else return W_ERROR(WErrors['InvalidParam'],'Expected\x20Uint8Array\x20or\x20array\x20for\x20ValueType.VecU8,\x20got\x20'+typeof b),new Uint8Array(0x0);}break;case ValueType['VecU16']:if(!Array['isArray'](b))return W_ERROR(WErrors['InvalidParam'],'Expected\x20array\x20for\x20ValueType.VecU16,\x20got\x20'+typeof b),new Uint8Array(0x0);for(const s of b){if(!isValidU16(s))return W_ERROR(WErrors['InvalidParam'],'Expected\x20valid\x20U16\x20in\x20VecU16,\x20got\x20'+s),new Uint8Array(0x0);}d=bcs['vector'](bcs['U16'])['serialize'](b)['toBytes']();break;case ValueType['VecU32']:if(!Array['isArray'](b))return W_ERROR(WErrors['InvalidParam'],'Expected\x20array\x20for\x20ValueType.VecU32,\x20got\x20'+typeof b),new Uint8Array(0x0);for(const t of b){if(!isValidU32(t))return W_ERROR(WErrors['InvalidParam'],'Expected\x20valid\x20U32\x20in\x20VecU32,\x20got\x20'+t),new Uint8Array(0x0);}d=bcs['vector'](bcs['U32'])['serialize'](b)['toBytes']();break;case ValueType['VecU64']:if(!Array['isArray'](b))return W_ERROR(WErrors['InvalidParam'],'Expected\x20array\x20for\x20ValueType.VecU64,\x20got\x20'+typeof b),new Uint8Array(0x0);for(const u of b){if(!isValidU64(u))return W_ERROR(WErrors['InvalidParam'],'Expected\x20valid\x20U64\x20in\x20VecU64,\x20got\x20'+u),new Uint8Array(0x0);}const g=b['map'](toU64Value);d=bcs['vector'](bcs['U64'])['serialize'](g)['toBytes']();break;case ValueType['VecU128']:if(!Array['isArray'](b))return W_ERROR(WErrors['InvalidParam'],'Expected\x20array\x20for\x20ValueType.VecU128,\x20got\x20'+typeof b),new Uint8Array(0x0);for(const v of b){if(!isValidU128(v))return W_ERROR(WErrors['InvalidParam'],'Expected\x20valid\x20U128\x20in\x20VecU128,\x20got\x20'+v),new Uint8Array(0x0);}const h=b['map'](toU128Value);d=bcs['vector'](bcs['U128'])['serialize'](h)['toBytes']();break;case ValueType['VecU256']:if(!Array['isArray'](b))return W_ERROR(WErrors['InvalidParam'],'Expected\x20array\x20for\x20ValueType.VecU256,\x20got\x20'+typeof b),new Uint8Array(0x0);for(const w of b){if(!isValidU256(w))return W_ERROR(WErrors['InvalidParam'],'Expected\x20valid\x20U256\x20in\x20VecU256,\x20got\x20'+w),new Uint8Array(0x0);}const i=b['map'](toU256Value);d=bcs['vector'](bcs['U256'])['serialize'](i)['toBytes']();break;case ValueType['VecVecU8']:if(!Array['isArray'](b))return W_ERROR(WErrors['InvalidParam'],'Expected\x20array\x20for\x20ValueType.VecVecU8,\x20got\x20'+typeof b),new Uint8Array(0x0);for(const x of b){if(!(x instanceof Uint8Array||Array['isArray'](x)))return W_ERROR(WErrors['InvalidParam'],'Expected\x20Uint8Array\x20or\x20array\x20in\x20VecVecU8,\x20got\x20'+typeof x),new Uint8Array(0x0);if(Array['isArray'](x))for(const y of x){if(!isValidU8(y))return W_ERROR(WErrors['InvalidParam'],'Expected\x20valid\x20U8\x20in\x20VecVecU8,\x20got\x20'+y),new Uint8Array(0x0);}}const j=b['map'](z=>z instanceof Uint8Array?Array['from'](z):z);d=bcs['vector'](bcs['vector'](bcs['U8']))['serialize'](j)['toBytes']();break;case ValueType['Value']:const k=b;if(!k||typeof k!=='object'||!('valueType'in k)||!('value'in k))return W_ERROR(WErrors['InvalidParam'],'Expected\x20ValueContainer\x20for\x20ValueType.Value,\x20got\x20'+JSON['stringify'](b)),new Uint8Array(0x0);if(!isValidValueType(k['valueType']))return W_ERROR(WErrors['InvalidParam'],'Invalid\x20inner\x20ValueType\x20in\x20ValueContainer:\x20'+k['valueType']),new Uint8Array(0x0);d=await valueToBcsBytes(k['valueType'],k['value']);break;default:W_ERROR(WErrors['InvalidParam'],'Unsupported\x20ValueType:\x20'+a);return new Uint8Array(0x0);}const e=new Uint8Array(c['length']+d['length']);return e['set'](c,0x0),e['set'](d,0x1),e;}catch(z){return W_ERROR(WErrors['InvalidParam'],'BCS\x20serialization\x20failed:\x20'+z['message']),new Uint8Array(0x0);}};export const bcsBytesToValue=(a,b)=>{if(a['length']===0x0)return W_ERROR(WErrors['InvalidParam'],'Empty\x20bytes\x20array'),undefined;const c=b??a[0x0];if(!isValidValueType(c))return W_ERROR(WErrors['InvalidParam'],'Invalid\x20ValueType\x20in\x20bytes:\x20'+c),undefined;if(a['length']<0x2)return undefined;const d=a['slice'](0x1);try{switch(c){case ValueType['Bool']:return bcs['Bool']['parse'](d);case ValueType['Address']:return bcs['Address']['parse'](d);case ValueType['String']:return bcs['String']['parse'](d);case ValueType['U8']:return bcs['U8']['parse'](d);case ValueType['U16']:return bcs['U16']['parse'](d);case ValueType['U32']:return bcs['U32']['parse'](d);case ValueType['U64']:return bcs['U64']['parse'](d);case ValueType['U128']:return bcs['U128']['parse'](d);case ValueType['U256']:return bcs['U256']['parse'](d);case ValueType['VecBool']:return bcs['vector'](bcs['Bool'])['parse'](d);case ValueType['VecAddress']:const e=bcs['vector'](bcs['Address'])['parse'](d);return{'entities':e['map'](f=>({'name_or_address':f}))};case ValueType['VecString']:return bcs['vector'](bcs['String'])['parse'](d);case ValueType['VecU8']:return bcs['vector'](bcs['U8'])['parse'](d);case ValueType['VecU16']:return bcs['vector'](bcs['U16'])['parse'](d);case ValueType['VecU32']:return bcs['vector'](bcs['U32'])['parse'](d);case ValueType['VecU64']:return bcs['vector'](bcs['U64'])['parse'](d);case ValueType['VecU128']:return bcs['vector'](bcs['U128'])['parse'](d);case ValueType['VecU256']:return bcs['vector'](bcs['U256'])['parse'](d);case ValueType['VecVecU8']:return bcs['vector'](bcs['vector'](bcs['U8']))['parse'](d);default:W_ERROR(WErrors['InvalidParam'],'Unsupported\x20ValueType\x20for\x20deserialization:\x20'+c);return undefined;}}catch(f){return W_ERROR(WErrors['InvalidParam'],'BCS\x20deserialization\x20failed:\x20'+f['message']),undefined;}};export const parseObjectType=(a,b='payment::Payment')=>{b+='<';if(a&&isThisPackage(a)){const c=a['indexOf'](b);if(c>0x0){const d=a['slice'](c+b['length'],a['length']-0x1);return d;}}return undefined;};export const parseTypeParameter=a=>{if(!a||!a['startsWith'](PackageAddress))return undefined;const b=a['indexOf']('<'),c=a['lastIndexOf']('>');if(b===-0x1||c===-0x1||b>=c)return undefined;return a['slice'](b+0x1,c);};export const extractCoinType=a=>{if(!a||!a['startsWith'](PackageAddress))return undefined;const b=PackageAddress['replace'](/[.*+?^${}()|[\]\\]/g,'\x5c$&'),c=new RegExp(b+'::[^:]+::[^<]+<'+b+'::coin::Coin<([^>]+)>>'),d=a['match'](c);if(d&&d[0x1])return d[0x1];return undefined;};export async function owner_receive(a,b,c,d,e,f,g){validObjects([b,c,g]);e&&!IsValidArgType(e)&&W_ERROR(WErrors['IsValidArgType'],'caller_type\x20'+e+'\x20is\x20invalid');if(Array['isArray'](d))for(const h of d){(!h['type']||!IsValidArgType(h['type']))&&W_ERROR(WErrors['IsValidArgType'],'type\x20'+h['type']+'\x20is\x20invalid');if(g){const i=await LocalMark['Instance']()['get_address'](h['id']);!i&&W_ERROR(WErrors['IsValidArgType'],'Received\x20'+h['id']+'\x20is\x20invalid'),a['moveCall']({'target':f('owner_receive_object_with_passport'),'arguments':[toTxObject(a,b),toTxObject(a,c),toTxObject(a,g),toTxObject(a,i)],'typeArguments':e?[e,h['type']]:[h['type']]});}else{const j=await LocalMark['Instance']()['get_address'](h['id']);!j&&W_ERROR(WErrors['IsValidArgType'],'Received\x20'+h['id']+'\x20is\x20invalid'),a['moveCall']({'target':f('owner_receive_object'),'arguments':[toTxObject(a,b),toTxObject(a,c),toTxObject(a,j)],'typeArguments':e?[e,h['type']]:[h['type']]});}}else{const k=d['token_type']['match'](/CoinWrapper<([^>]+)>/);!k&&W_ERROR(WErrors['IsValidArgType'],'Invalid\x20CoinWrapper\x20type:\x20'+d['token_type']);const l=k[0x1];!IsValidArgType(l)&&W_ERROR(WErrors['IsValidArgType'],'Invalid\x20CoinWrapper\x20type:\x20'+l);for(const m of d['received']){const [n,o]=await Promise['all']([LocalMark['Instance']()['get_address'](m['id']),LocalMark['Instance']()['get_address'](m['payment'])]);(!n||!o)&&W_ERROR(WErrors['IsValidArgType'],'Received\x20'+m['id']+'\x20or\x20Payment\x20'+m['payment']+'\x20is\x20invalid'),g?a['moveCall']({'target':f('owner_receive_with_passport'),'arguments':[toTxObject(a,b),toTxObject(a,c),toTxObject(a,g),toTxObject(a,n),toTxObject(a,o)],'typeArguments':e?[e,l]:[l]}):a['moveCall']({'target':f('owner_receive'),'arguments':[toTxObject(a,b),toTxObject(a,c),toTxObject(a,n),toTxObject(a,o)],'typeArguments':e?[e,l]:[l]});}}}export function permission_set(a,b,c,d,e,f){validObjects([b,c,d]),e&&!IsValidArgType(e)&&W_ERROR(WErrors['IsValidArgType'],'caller_type\x20'+e+'\x20is\x20invalid'),a['moveCall']({'target':f('permission_set'),'arguments':[toTxObject(a,b),toTxObject(a,c),toTxObject(a,d),a['object']['mutRegistrar'](),a['object']['clock']()],'typeArguments':e?[e]:[]});}export const validObjects=(a,b=!![])=>{a['forEach']((c,d)=>{typeof c==='string'&&c!=null?!isValidAddress(c)&&W_ERROR(WErrors['IsValidAddress'],'Address\x20is\x20invalid:\x20'+c):!b&&!c&&W_ERROR(WErrors['IsValidAddress'],'Address\x20is\x20required\x20at\x20index\x20'+d+'\x20in\x20objects\x20'+a);});};export const uint2address=a=>{return normalizeWowAddress(a['toString'](0x10));};export const parseRecipient=a=>{(!a||!a['variant'])&&W_ERROR(WErrors['ParseObjectContentFailed'],'Invalid\x20who\x20data:\x20'+JSON['stringify'](a));if(a['variant']==='GuardIdentifier'&&a['fields'])return{'GuardIdentifier':Number(a['fields']['pos0'])};else{if(a['variant']==='Entity'&&a['fields'])return{'Entity':{'name_or_address':a['fields']['pos0']}};else{if(a['variant']==='Signer')return{'Signer':null};else W_ERROR(WErrors['ParseObjectContentFailed'],'Invalid\x20who\x20variant:\x20'+a['variant']);}}};