wowok 1.4.25 → 1.4.27

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wowok",
3
- "version": "1.4.25",
3
+ "version": "1.4.27",
4
4
  "description": "Create, collaborate, and transact on your own terms with the AI-driven web3 collaboration protocol.",
5
5
  "main": "./src/index.ts",
6
6
  "types": "./src/index.ts",
@@ -36,7 +36,10 @@
36
36
  "grantor",
37
37
  "grantee",
38
38
  "move language",
39
- "move"
39
+ "move",
40
+ "treasury",
41
+ "payment",
42
+ "Withholding"
40
43
  ],
41
44
  "author": "wowok",
42
45
  "license": "Apache-2.0",
package/src/demand.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { type TransactionResult, Transaction as TransactionBlock } from '@mysten/sui/transactions';
2
- import { FnCallType, Protocol, PassportObject, PermissionObject, GuardObject, DemandAddress, TxbObject, ResourceObject} from './protocol';
3
- import { IsValidDesription, IsValidAddress, IsValidArgType, IsValidU64, } from './utils'
2
+ import { FnCallType, Protocol, PassportObject, PermissionObject, GuardObject, DemandAddress, TxbObject } from './protocol';
3
+ import { IsValidDesription, IsValidAddress, IsValidArgType, IsValidU64, parseObjectType } from './utils'
4
4
  import { Errors, ERROR} from './exception'
5
5
 
6
6
  export class Demand {
@@ -250,16 +250,8 @@ export class Demand {
250
250
  })
251
251
  this.permission = new_permission
252
252
  }
253
- static parseObjectType = (chain_type:string) : string => {
254
- if (chain_type) {
255
- const s = 'demand::Demand<'
256
- const i = chain_type.indexOf(s);
257
- if (i > 0) {
258
- let r = chain_type.slice(i + s.length, chain_type.length-1);
259
- return r
260
- }
261
- }
262
- return '';
253
+ static parseObjectType = (chain_type?:string | null) : string => {
254
+ return parseObjectType(chain_type, 'demand::Demand<')
263
255
  }
264
256
 
265
257
  static MAX_BOUNTY_COUNT = 300;
package/src/guard.ts CHANGED
@@ -263,19 +263,19 @@ export class Guard {
263
263
  [MODULES.vote, 'Voted Time by Address', 1113, [ValueType.TYPE_ADDRESS], ValueType.TYPE_U64, 'The time of whether an address has been voted on.', ['adddress']],
264
264
 
265
265
  [MODULES.payment, 'Sender', 1200, [], ValueType.TYPE_ADDRESS, 'Payment originator address.', []],
266
- [MODULES.payment, 'Total Amount', 1201, [], ValueType.TYPE_U64, "Payment amount.", []],
267
- [MODULES.payment, 'Tips', 1202, [], ValueType.TYPE_STRING, 'Payment remarks.', ['address']],
266
+ [MODULES.payment, 'Total Amount', 1201, [], ValueType.TYPE_U128, "Payment amount.", []],
267
+ [MODULES.payment, 'Remark', 1202, [], ValueType.TYPE_STRING, 'Payment remark.', ['address']],
268
268
  [MODULES.payment, 'Has Guard for Perpose', 1203, [], ValueType.TYPE_BOOL, 'Whether the payment references a Guard?', []],
269
269
  [MODULES.payment, 'Has Object for Perpose', 1204, [], ValueType.TYPE_BOOL, 'Whether the payment references an Object?', []],
270
270
  [MODULES.payment, 'Guard for Perpose', 1205, [], ValueType.TYPE_ADDRESS, 'The Guard referenced by this payment.', []],
271
271
  [MODULES.payment, 'Object for Perpose', 1206, [], ValueType.TYPE_ADDRESS, "The Object referenced by this payment.", []],
272
- [MODULES.payment, 'Number of Transfer', 1207, [], ValueType.TYPE_U64, 'Number of recipients to receive payment from.', []],
272
+ [MODULES.payment, 'Number of Recipients', 1207, [], ValueType.TYPE_U64, 'Number of recipients to receive payment from.', []],
273
273
  [MODULES.payment, 'Is a Recipient', 1208, [ValueType.TYPE_ADDRESS], ValueType.TYPE_BOOL, 'Is a recipient received the payment?', ['address']],
274
274
  [MODULES.payment, 'Amount for a Recipient', 1209, [ValueType.TYPE_ADDRESS], ValueType.TYPE_U64, 'The amount of payment received by an address.', ['address']],
275
275
  [MODULES.payment, 'Time', 1210, [], ValueType.TYPE_U64, 'Payment time', []],
276
276
  [MODULES.payment, 'Is from Treasury', 1211, [], ValueType.TYPE_BOOL, 'Whether the payment comes from a Treasury?', []],
277
277
  [MODULES.payment, 'Treasury Address', 1212, [], ValueType.TYPE_ADDRESS, 'The Treasury from which the payment comes.', []],
278
- [MODULES.payment, 'Index', 1213, [], ValueType.TYPE_U64, 'Payment index or id.', []],
278
+ [MODULES.payment, 'Biz-ID', 1213, [], ValueType.TYPE_U64, 'Bisiness ID number of the payment.', []],
279
279
 
280
280
  [MODULES.withholding, 'Amount', 1300, [], ValueType.TYPE_U64, 'Total amount deposited with withholding.' , []],
281
281
  [MODULES.withholding, 'Original Type Deposited', 1301, [], ValueType.TYPE_STRING, "Original type name of asserts deposited.", []],
@@ -296,11 +296,10 @@ export class Guard {
296
296
  [MODULES.treasury, 'Permission', 1400, [], ValueType.TYPE_ADDRESS, 'Permission object address.', []],
297
297
  [MODULES.treasury, 'Balance', 1401, [], ValueType.TYPE_U64, "Treasury balance.", []],
298
298
  [MODULES.treasury, 'Number of Flow Records', 1402, [], ValueType.TYPE_U64, 'Number of treasury transactions.', []],
299
- [MODULES.treasury, 'Deposit Flow', 1403, [], ValueType.TYPE_U128, 'Treasury inflow amount.', []],
300
- [MODULES.treasury, 'Withdraw Flow', 1404, [], ValueType.TYPE_U128, 'Treasury outflow amount.', []],
301
- [MODULES.treasury, 'Flow over a Period of Time', 1405, [ValueType.TYPE_U64, ValueType.TYPE_U64], ValueType.TYPE_U128, 'The flow amount between certain periods.', ['begin time', 'end time']],
302
- [MODULES.treasury, 'Deposit Flow over a Period of Time', 1406, [ValueType.TYPE_U64, ValueType.TYPE_U64], ValueType.TYPE_U128, "The inflow amount between certain periods.", ['begin time', 'end time']],
303
- [MODULES.treasury, 'Withdraw Flow over a Period of Time', 1407, [ValueType.TYPE_U64, ValueType.TYPE_U64], ValueType.TYPE_U128, 'The outflow amount between certain periods.', ['begin time', 'end time']],
299
+ [MODULES.treasury, 'Inflow Amount', 1403, [], ValueType.TYPE_U128, 'Treasury inflow amount.', []],
300
+ [MODULES.treasury, 'Outflow Amount', 1404, [], ValueType.TYPE_U128, 'Treasury outflow amount.', []],
301
+ [MODULES.treasury, 'Number of Payment', 1405, [ValueType.TYPE_ADDRESS], ValueType.TYPE_U64, 'The number of flows generated by a payment.', ['payment address']],
302
+ [MODULES.treasury, 'Can anyone deposit ', 1406, [], ValueType.TYPE_BOOL, "Whether to allow any address to deposit?", []],
304
303
  ];
305
304
 
306
305
  static BoolCmd = Guard.QUERIES.filter(q => q[4] === ValueType.TYPE_BOOL);
package/src/index.ts CHANGED
@@ -14,4 +14,6 @@ export * from './graphql'
14
14
  export * from './entity'
15
15
  export * from './wowok'
16
16
  export * from './resource'
17
- export * from './treasury'
17
+ export * from './treasury'
18
+ export * from './payment'
19
+ export * from './withholding'
package/src/passport.ts CHANGED
@@ -94,7 +94,7 @@ export class GuardParser {
94
94
 
95
95
  // console.log(res[0].data?.content);
96
96
  let content = res[0].data!.content as any;
97
- if (content?.type != protocol.Package() + '::guard::Guard') {
97
+ if (content?.type != protocol.Package('base') + '::guard::Guard') {
98
98
  ERROR(Errors.Fail, 'GuardObject object invalid')
99
99
  }
100
100
 
@@ -260,7 +260,7 @@ export class GuardParser {
260
260
  if (index === -1) ERROR(Errors.Fail, 'Parse_Guard_Helper invalid type: ' + c.type);
261
261
 
262
262
 
263
- if (c.fields.input.type === (protocol.Package() + '::bcs::BCS')) {
263
+ if (c.fields.input.type === (protocol.Package('base') + '::bcs::BCS')) {
264
264
  const constants = GuardParser.parse_constant(c.fields.constants); // MUST first
265
265
  const inputs = GuardParser.parse_bcs(constants, Uint8Array.from(c.fields.input.fields.bytes));
266
266
  me.guard_list.push({id: c.fields.id.id, input:[...inputs], constant:[...constants], digest:r.data?.digest??'', version:r.data?.version ?? ''});
package/src/payment.ts ADDED
@@ -0,0 +1,63 @@
1
+ import { CoinObject, FnCallType, PaymentAddress, Protocol, TxbObject} from './protocol';
2
+ import { IsValidDesription, IsValidAddress, IsValidArray, } from './utils';
3
+ import { ERROR, Errors } from './exception';
4
+ import { Transaction as TransactionBlock} from '@mysten/sui/transactions';
5
+
6
+ export interface Payment_Receiver {
7
+ address: string;
8
+ coin: CoinObject;
9
+ }
10
+
11
+ export interface PaymentParam {
12
+ receiver: Payment_Receiver[];
13
+ business_index: bigint;
14
+ business_remark: string;
15
+ for_object?: string;
16
+ for_guard?: string;
17
+ }
18
+
19
+ export const PAYMENT_MAX_RECEIVER_COUNT = 200;
20
+ export function create_payment(txb:TransactionBlock, pay_token_type:string, param:PaymentParam) : PaymentAddress {
21
+ if (!pay_token_type) ERROR(Errors.InvalidParam, 'Payment.New.pay_token_type');
22
+
23
+ if (param.receiver.length > PAYMENT_MAX_RECEIVER_COUNT) {
24
+ ERROR(Errors.InvalidParam, 'Payment.New.param.receiver');
25
+ }
26
+
27
+ if (!IsValidArray(param.receiver, (item:Payment_Receiver) => { return IsValidAddress(item.address) && Protocol.IsValidObjects([item.coin])})) {
28
+ ERROR(Errors.IsValidArray, 'Payment.New.param.receiver');
29
+ }
30
+
31
+ if (param?.for_object && !IsValidAddress(param.for_object)) {
32
+ ERROR(Errors.IsValidAddress, 'Payment.New.param.for_object')
33
+ }
34
+ if (param?.for_guard && !IsValidAddress(param.for_guard)) {
35
+ ERROR(Errors.IsValidAddress, 'Payment.New.param.for_guard')
36
+ }
37
+ if (param?.business_remark && !IsValidDesription(param?.business_remark)) {
38
+ ERROR(Errors.IsValidDesription, 'Payment.New.param.business_remark')
39
+ }
40
+
41
+ let obj = txb.pure.option('address', param.for_object ? param.for_object : undefined);
42
+ const clock = txb.sharedObjectRef(Protocol.CLOCK_OBJECT);
43
+
44
+ if (param.for_guard) {
45
+ return txb.moveCall({
46
+ target:Protocol.Instance().PaymentFn('create_withGuard') as FnCallType,
47
+ arguments:[txb.pure.vector('address', param.receiver.map((i)=>i.address)), txb.makeMoveVec({elements:param.receiver.map((i)=>i.coin)}),
48
+ obj, txb.object(param.for_guard), txb.pure.u64(param.business_index), txb.pure.string(param.business_remark), txb.object(clock)],
49
+ typeArguments:[pay_token_type],
50
+ })
51
+ } else {
52
+ return txb.moveCall({
53
+ target:Protocol.Instance().PaymentFn('new') as FnCallType,
54
+ arguments:[txb.pure.vector('address', param.receiver.map((i)=>i.address)), txb.makeMoveVec({elements:param.receiver.map((i)=>i.coin)}),
55
+ obj, txb.pure.u64(param.business_index), txb.pure.string(param.business_remark), txb.object(clock)],
56
+ typeArguments:[pay_token_type],
57
+ })
58
+ }
59
+ }
60
+
61
+
62
+
63
+
package/src/permission.ts CHANGED
@@ -79,10 +79,11 @@ export enum PermissionIndex {
79
79
  progress_parent = 655,
80
80
 
81
81
  treasury = 700,
82
- treasury_deposit = 701,
83
- treasury_withdraw = 702,
84
- treasury_free_deposit_set = 703,
82
+ treasury_receive = 701,
83
+ treasury_deposit = 702,
84
+ treasury_withdraw = 703,
85
85
  treasury_descritption = 704,
86
+ treasury_free_deposit_set = 705,
86
87
  user_defined_start = 1000,
87
88
  }
88
89
 
@@ -183,6 +184,7 @@ export const PermissionInfo : PermissionInfoType[] = [
183
184
 
184
185
  {index:PermissionIndex.treasury, name: 'Treasury', description:'Launch new Treasury', module: 'treasury'},
185
186
  {index:PermissionIndex.treasury_deposit, name: 'Deposit', description:'Deposit coins', module: 'treasury'},
187
+ {index:PermissionIndex.treasury_receive, name: 'Receive', description:'Receive coins from some address sent', module: 'treasury'},
186
188
  {index:PermissionIndex.treasury_withdraw, name: 'Withdraw', description:'Withdraw coins', module: 'treasury'},
187
189
  {index:PermissionIndex.treasury_free_deposit_set, name: 'Be Free Deposit', description:'Set whether deposits can be made without permission', module: 'treasury'},
188
190
  {index:PermissionIndex.treasury_descritption, name: 'Description', description:'Set Treasury description', module: 'treasury'},
package/src/progress.ts CHANGED
@@ -78,7 +78,6 @@ export class Progress {
78
78
  }
79
79
 
80
80
  launch() : ProgressAddress {
81
-
82
81
  return this.txb.moveCall({
83
82
  target:Protocol.Instance().ProgressFn('create') as FnCallType,
84
83
  arguments: [Protocol.TXB_OBJECT(this.txb, this.object)],
package/src/protocol.ts CHANGED
@@ -11,7 +11,6 @@ export enum MODULES {
11
11
  machine = 'machine',
12
12
  node = 'node',
13
13
  progress = 'progress',
14
- community = 'community',
15
14
  repository = 'repository',
16
15
  permission = 'permission',
17
16
  passport = 'passport',
@@ -56,6 +55,11 @@ export type ResourceObject = TransactionResult | string | TransactionArgument;
56
55
  export type ResourceAddress = TransactionResult;
57
56
  export type EntityObject = TransactionResult | string | TransactionArgument;
58
57
  export type EntityAddress = TransactionResult;
58
+ export type TreasuryAddress = TransactionResult;
59
+ export type PaymentObject = TransactionResult | string | TransactionArgument;
60
+ export type PaymentAddress = TransactionResult;
61
+ export type ReceivedObject = TransactionResult | string | TransactionArgument;
62
+ export type CoinWrapperObject = TransactionResult;
59
63
 
60
64
  export type TxbObject = string | TransactionResult | TransactionArgument | GuardObject | RepositoryObject | PermissionObject | MachineObject | PassportObject |
61
65
  DemandObject | ServiceObject | RewardObject | OrderObject | DiscountObject | VoteObject | DemandObject | ResourceObject | EntityObject;
@@ -204,20 +208,22 @@ export enum ENTRYPOINT {
204
208
  }
205
209
  /*
206
210
  const TESTNET = {
207
- package: "0xbd3d0929072f7647e521bf72851ccdc7e2169052b22bfdc5b49439c48cfb119a",
211
+ wowok: "0xbd3d0929072f7647e521bf72851ccdc7e2169052b22bfdc5b49439c48cfb119a",
208
212
  wowok_object: '0xb0a521a287e9d5e08932b3984dbe6ce159e836179c41bd08c556ef77ecdb7439',
209
213
  entity_object: '0x16aab98920e7341d1dc19631031253234b2b71fc2ab8c32d65ee3ded8072acef',
210
214
  treasury_cap:'0xb75a2ca2f651755c134ad521175f33f9e3f9008ad44340f76b3229e1f30cfdff',
211
215
  }
212
216
  */
213
217
  const TESTNET = {
214
- package: "0x0a0a86d4d364ba566d5392cd8ba7d980f4d65fc1e1586e5b2e49b0ecd8dfeae4",
215
- wowok_object: '0x8e5424a7899597dc75b456d597dd0a741296c1363282a72d52421b1c85d2460e',
216
- entity_object: '0xd88aaf554750437ab4148acca27e0ba51cff97c13882722a4a89289e57a7b8fa',
217
- treasury_cap:'0x01079f2baa2c320afc52e0f2680167c44bbaad9a6a21514bfd2e56356028be9d',
218
+ wowok: "0xb82ef16ea48fb4f732310c3a0c86c34d0b8711f4dac1c4ca693005345047bfa4",
219
+ base: '0x74fb58a0b148f19852a721293a797fdeb59dec5da71ca31838fc810af2d32fdd',
220
+ wowok_object: '0x41c93c2903227632a33394d37fd3b7f62a33bc72b16a7f9aafc8161b78b232be',
221
+ entity_object: '0x0b34c975a4b179755dabdec5c50505bd21c0439c463d171a54fa44f215e3bb57',
222
+ treasury_cap:'0xacb8b1d380d4f75a802dd7412142290bebd7f6266785690de2daf68f6a7ba9ce',
218
223
  }
219
224
  const MAINNET = {
220
- package: "",
225
+ wowok: "",
226
+ base:"",
221
227
  wowok_object: '',
222
228
  entity_object: '',
223
229
  treasury_cap:'',
@@ -231,7 +237,7 @@ export interface CoinTypeInfo {
231
237
  }
232
238
  export class Protocol {
233
239
  protected network = '';
234
- protected package = '';
240
+ protected package = new Map<string, string>();
235
241
  protected signer = '';
236
242
  protected wowok_object = '';
237
243
  protected entity_object = '';
@@ -262,14 +268,16 @@ export class Protocol {
262
268
  case ENTRYPOINT.devnet:
263
269
  break;
264
270
  case ENTRYPOINT.testnet:
265
- this.package = TESTNET.package;
271
+ this.package.set('wowok', TESTNET.wowok);
272
+ this.package.set('base', TESTNET.base);
266
273
  this.wowok_object = TESTNET.wowok_object;
267
274
  this.entity_object= TESTNET.entity_object;
268
275
  this.treasury_cap = TESTNET.treasury_cap;
269
276
  this.graphql = 'https://sui-testnet.mystenlabs.com/graphql';
270
277
  break;
271
278
  case ENTRYPOINT.mainnet:
272
- this.package = MAINNET.package;
279
+ this.package.set('wowok', MAINNET.wowok);
280
+ this.package.set('base', MAINNET.base);
273
281
  this.wowok_object = MAINNET.wowok_object;
274
282
  this.entity_object= MAINNET.entity_object;
275
283
  this.treasury_cap = MAINNET.treasury_cap;
@@ -277,7 +285,10 @@ export class Protocol {
277
285
  break;
278
286
  };
279
287
  }
280
- Package(): string { return this.package }
288
+ Package(type:string): string {
289
+ return this.package.get(type) ?? ''
290
+ }
291
+
281
292
  WowokObject(): string { return this.wowok_object }
282
293
  EntityObject(): string { return this.entity_object }
283
294
  TreasuryCap() : string { return this.treasury_cap }
@@ -296,22 +307,23 @@ export class Protocol {
296
307
  }; return "";
297
308
  };
298
309
 
299
- MachineFn = (fn:any) => { return `${this.package}::${MODULES.machine}::${fn}`};
300
- ProgressFn = (fn:any) => { return `${this.package}::${MODULES.progress}::${fn}`};
301
- CommunityFn = (fn: any) => { return `${this.package}::${MODULES.community}::${fn}`};
302
- RepositoryFn = (fn:any) => { return `${this.package}::${MODULES.repository}::${fn}`};
303
- PermissionFn = (fn: any) => { return `${this.package}::${MODULES.permission}::${fn}`};
304
- PassportFn = (fn:any) => { return `${this.package}::${MODULES.passport}::${fn}`};
305
- GuardFn = (fn: any) => { return `${this.package}::${MODULES.guard}::${fn}`};
306
- VoteFn = (fn:any) => { return `${this.package}::${MODULES.vote}::${fn}`};
307
- DemandFn = (fn: any) => { return `${this.package}::${MODULES.demand}::${fn}`};
308
- OrderFn = (fn:any) => { return `${this.package}::${MODULES.order}::${fn}`};
309
- RewardFn = (fn: any) => { return `${this.package}::${MODULES.reward}::${fn}`};
310
- ServiceFn = (fn: any) => { return `${this.package}::${MODULES.service}::${fn}`};
311
- ResourceFn = (fn: any) => { return `${this.package}::${MODULES.resource}::${fn}`};
312
- EntityFn = (fn: any) => { return `${this.package}::${MODULES.entity}::${fn}`};
313
- WowokFn = (fn: any) => { return `${this.package}::${MODULES.wowok}::${fn}`};
314
- TreasuryFn = (fn: any) => { return `${this.package}::${MODULES.treasury}::${fn}`};
310
+ MachineFn = (fn:any) => { return `${this.package.get('wowok')}::${MODULES.machine}::${fn}`};
311
+ ProgressFn = (fn:any) => { return `${this.package.get('wowok')}::${MODULES.progress}::${fn}`};
312
+ RepositoryFn = (fn:any) => { return `${this.package.get('wowok')}::${MODULES.repository}::${fn}`};
313
+ PermissionFn = (fn: any) => { return `${this.package.get('wowok')}::${MODULES.permission}::${fn}`};
314
+ PassportFn = (fn:any) => { return `${this.package.get('wowok')}::${MODULES.passport}::${fn}`};
315
+ GuardFn = (fn: any) => { return `${this.package.get('base')}::${MODULES.guard}::${fn}`};
316
+ VoteFn = (fn:any) => { return `${this.package.get('wowok')}::${MODULES.vote}::${fn}`};
317
+ DemandFn = (fn: any) => { return `${this.package.get('wowok')}::${MODULES.demand}::${fn}`};
318
+ OrderFn = (fn:any) => { return `${this.package.get('wowok')}::${MODULES.order}::${fn}`};
319
+ RewardFn = (fn: any) => { return `${this.package.get('wowok')}::${MODULES.reward}::${fn}`};
320
+ ServiceFn = (fn: any) => { return `${this.package.get('wowok')}::${MODULES.service}::${fn}`};
321
+ ResourceFn = (fn: any) => { return `${this.package.get('wowok')}::${MODULES.resource}::${fn}`};
322
+ EntityFn = (fn: any) => { return `${this.package.get('wowok')}::${MODULES.entity}::${fn}`};
323
+ WowokFn = (fn: any) => { return `${this.package.get('wowok')}::${MODULES.wowok}::${fn}`};
324
+ TreasuryFn = (fn: any) => { return `${this.package.get('wowok')}::${MODULES.treasury}::${fn}`};
325
+ PaymentFn = (fn: any) => { return `${this.package.get('wowok')}::${MODULES.payment}::${fn}`};
326
+ WithholdingFn = (fn: any) => { return `${this.package.get('wowok')}::${MODULES.withholding}::${fn}`};
315
327
 
316
328
  Query = async (objects: Query_Param[], options:SuiObjectDataOptions={showContent:true}) : Promise<SuiObjectResponse[]> => {
317
329
  const client = new SuiClient({ url: this.NetworkUrl() });
@@ -342,7 +354,6 @@ export class Protocol {
342
354
 
343
355
  const privkey = fromHEX(priv_key);
344
356
  const keypair = Ed25519Keypair.fromSecretKey(privkey);
345
-
346
357
  const response = await client.signAndExecuteTransaction({
347
358
  transaction: this.CurrentSession(),
348
359
  signer: keypair,
@@ -356,8 +367,8 @@ export class Protocol {
356
367
  static SUI_TOKEN_TYPE = '0x0000000000000000000000000000000000000000000000000000000000000002::sui::SUI'; // TOKEN_TYPE
357
368
  // used in demand, reward, ...
358
369
  static SUI_COIN_TYPE = '0x0000000000000000000000000000000000000000000000000000000000000002::coin::Coin<0x2::sui::SUI>'; // COIN TYPE
359
- WOWOK_TOKEN_TYPE = () => { return this.package + '::wowok::WOWOK' }
360
- WOWOK_COIN_TYPE = () => { return '0x2::coin::Coin<' + this.package + '::wowok::WOWOK>'}
370
+ WOWOK_TOKEN_TYPE = () => { return this.package.get('wowok') + '::wowok::WOWOK' }
371
+ WOWOK_COIN_TYPE = () => { return '0x2::coin::Coin<' + this.package.get('wowok') + '::wowok::WOWOK>'}
361
372
  COINS_TYPE = () => {
362
373
  switch(this.network) {
363
374
  case ENTRYPOINT.testnet:
@@ -392,13 +403,13 @@ export class Protocol {
392
403
  CoinTypes_Testnet:CoinTypeInfo[] = [
393
404
  {symbol:'SUI', type:'0x0000000000000000000000000000000000000000000000000000000000000002::sui::SUI', decimals:9, alias:true},
394
405
  {symbol:'SUI', type:'0x2::sui::SUI', decimals:9, },
395
- {symbol:'WOW', type:TESTNET.package + '::wowok::WOWOK', decimals:9},
406
+ {symbol:'WOW', type:TESTNET.wowok + '::wowok::WOWOK', decimals:9},
396
407
  ];
397
408
 
398
409
  CoinTypes_Mainnet:CoinTypeInfo[] = [
399
410
  {symbol:'SUI', type:'0x0000000000000000000000000000000000000000000000000000000000000002::sui::SUI', decimals:9, alias:true},
400
411
  {symbol:'SUI', type:'0x2::sui::SUI', decimals:9, },
401
- {symbol:'WOW', type:TESTNET.package + '::wowok::WOWOK', decimals:9},
412
+ {symbol:'WOW', type:TESTNET.wowok + '::wowok::WOWOK', decimals:9},
402
413
  {symbol:'USDT', type:'0xc060006111016b8a020ad5b33834984a437aaa7d3c74c18e09a95d48aceab08c::coin::COIN', decimals:6},
403
414
  {symbol:'USDC', type:'0x5d4b302506645c37ff133b98c4b50a5ae14841659738d6d733d59d0d217a93bf::coin::COIN', decimals:6},
404
415
  {symbol:'WETH', type:'0xaf8cd5edc19c4512f4259f0bee101a40d41ebed738ade5874359610ef8eeced5::coin::COIN', decimals:8},
@@ -438,13 +449,20 @@ export class Protocol {
438
449
  })
439
450
  }
440
451
  WOWOK_OBJECTS_TYPE = () => (Object.keys(MODULES) as Array<keyof typeof MODULES>).map((key) =>
441
- { let i = this.package + '::' + key + '::'; return i + capitalize(key); })
452
+ { let i = this.package.get('wowok') + '::' + key + '::'; return i + capitalize(key); })
442
453
  WOWOK_OBJECTS_PREFIX_TYPE = () => (Object.keys(MODULES) as Array<keyof typeof MODULES>).map((key) =>
443
- { return this.package + '::' + key + '::'; })
454
+ { return this.package.get('wowok') + '::' + key + '::'; })
455
+ private hasPackage(pack:string) : boolean {
456
+ for (let value of this.package.values()) {
457
+ if (value === pack) {
458
+ return true;
459
+ }
460
+ } return false;
461
+ }
444
462
  object_name_from_type_repr = (type_repr:string) : string => {
445
463
  if (!type_repr) return ''
446
464
  let i = type_repr.indexOf('::');
447
- if (i > 0 && type_repr.slice(0, i) === this.package) {
465
+ if (i > 0 && this.hasPackage(type_repr.slice(0, i))) {
448
466
  i = type_repr.indexOf('<');
449
467
  if (i > 0) {
450
468
  type_repr = type_repr.slice(0, i);
@@ -457,6 +475,22 @@ export class Protocol {
457
475
  }
458
476
  return ''
459
477
  }
478
+ module_object_name_from_type_repr = (type_repr:string) : string => {
479
+ if (!type_repr) return ''
480
+ let i = type_repr.indexOf('::');
481
+ if (i > 0 && this.hasPackage(type_repr.slice(0, i))) {
482
+ i = type_repr.indexOf('<');
483
+ if (i > 0) {
484
+ type_repr = type_repr.slice(0, i);
485
+ }
486
+
487
+ let n = type_repr.indexOf('::');
488
+ if (n > 0) {
489
+ return type_repr.slice(n+2);
490
+ }
491
+ }
492
+ return ''
493
+ }
460
494
  }
461
495
 
462
496
  export class RpcResultParser {
@@ -471,8 +505,7 @@ export class RpcResultParser {
471
505
  if (response?.objectChanges) {
472
506
  response.objectChanges.forEach((change) => {
473
507
  RpcResultParser.Object_Type_Extra().forEach((name) => {
474
- let type = protocol.Package() + '::' + name;
475
- if (change.type == 'created' && change.objectType.includes(type)) {
508
+ if (change.type == 'created' && protocol.module_object_name_from_type_repr(change.objectType)===name) {
476
509
  if (ret.has(name)) {
477
510
  ret.get(name)?.push(change.objectId);
478
511
  } else {
package/src/repository.ts CHANGED
@@ -2,7 +2,7 @@ import { Protocol, FnCallType, ValueType, RepositoryValueType, RepositoryAddress
2
2
  import { PermissionIndexType, Permission } from './permission'
3
3
  import { Bcs, array_unique, IsValidDesription, IsValidAddress, IsValidArray, IsValidName, ValueTypeConvert} from './utils';
4
4
  import { ERROR, Errors } from './exception';
5
- import { MAX_U8, MAX_U128, MAX_U256, MAX_U64, stringToUint8Array } from './utils';
5
+ import { MAX_U8, MAX_U128, MAX_U256, MAX_U64, parseObjectType } from './utils';
6
6
  import { type TransactionResult, Transaction as TransactionBlock } from '@mysten/sui/transactions';
7
7
 
8
8
  export enum Repository_Policy_Mode {
@@ -394,16 +394,8 @@ export class Repository {
394
394
  static IsValidValue = (value:Uint8Array) => {
395
395
  return value.length < Repository.MAX_VALUE_LENGTH;
396
396
  }
397
- static parseObjectType = (chain_type:string) : string => {
398
- if (chain_type) {
399
- const s = 'repository::Repository<'
400
- const i = chain_type.indexOf(s);
401
- if (i > 0) {
402
- let r = chain_type.slice(i + s.length, chain_type.length-1);
403
- return r
404
- }
405
- }
406
- return '';
397
+ static parseObjectType = (chain_type?:string | null) : string => {
398
+ return parseObjectType(chain_type, 'repository::Repository<');
407
399
  }
408
400
 
409
401
  static rpc_de_data(fields:any) : RepData [] {
package/src/resource.ts CHANGED
@@ -21,6 +21,8 @@ export class Resource {
21
21
  static LikeName = "like";
22
22
  static DislikeName = "dislike";
23
23
  static FavorName = "favor";
24
+ static LaunchName = 'launch';
25
+ static OrderName = 'order';
24
26
 
25
27
  protected object:TxbObject;
26
28
  protected txb;
@@ -55,14 +57,14 @@ export class Resource {
55
57
  });
56
58
  }
57
59
 
58
- add2(object:string, name:string[]) {
59
- if (!IsValidAddress(object)) ERROR(Errors.IsValidAddress, 'add2');
60
+ add2(object:TxbObject, name:string[]) {
61
+ if (typeof(object) === 'string' && !IsValidAddress(object)) ERROR(Errors.IsValidAddress, 'add2');
60
62
  if (!IsValidArray(name, IsValidName)) ERROR(Errors.IsValidArray, 'add2');
61
63
  if (!name) return
62
64
 
63
65
  this.txb.moveCall({
64
66
  target:Protocol.Instance().ResourceFn('add2') as FnCallType,
65
- arguments:[Protocol.TXB_OBJECT(this.txb, this.object), this.txb.pure.address(object),
67
+ arguments:[Protocol.TXB_OBJECT(this.txb, this.object), typeof(object) === 'string' ? this.txb.pure.address(object) : object,
66
68
  this.txb.pure.vector('string', name)]
67
69
  });
68
70
  }
package/src/reward.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { TransactionArgument, Transaction as TransactionBlock, type TransactionResult, } from '@mysten/sui/transactions';
2
2
  import { FnCallType, GuardObject, PassportObject, PermissionObject, RewardAddress, Protocol, TxbObject, } from './protocol';
3
- import { array_unique, IsValidAddress, IsValidArgType, IsValidArray, IsValidDesription, IsValidU64} from './utils';
3
+ import { array_unique, IsValidAddress, IsValidArgType, IsValidArray, IsValidDesription, IsValidU64, parseObjectType} from './utils';
4
4
  import { ERROR, Errors } from './exception';
5
5
 
6
6
  export type CoinReward = TransactionResult;
@@ -277,7 +277,6 @@ export class Reward {
277
277
  }
278
278
 
279
279
  allow_claim(bAllowClaim: boolean, passport?:PassportObject) {
280
-
281
280
  if (passport) {
282
281
  this.txb.moveCall({
283
282
  target:Protocol.Instance().RewardFn('allow_claim_with_passport') as FnCallType,
@@ -298,7 +297,6 @@ export class Reward {
298
297
  if (!Protocol.IsValidObjects([new_permission])) {
299
298
  ERROR(Errors.IsValidObjects)
300
299
  }
301
-
302
300
 
303
301
  this.txb.moveCall({
304
302
  target:Protocol.Instance().RewardFn('permission_set') as FnCallType,
@@ -308,15 +306,7 @@ export class Reward {
308
306
  this.permission = new_permission
309
307
  }
310
308
  static parseObjectType = (chain_type:string) : string => {
311
- if (chain_type) {
312
- const s = 'reward::Reward<'
313
- const i = chain_type.indexOf(s);
314
- if (i > 0) {
315
- let r = chain_type.slice(i + s.length, chain_type.length-1);
316
- return r
317
- }
318
- }
319
- return '';
309
+ return parseObjectType(chain_type, 'reward::Reward<')
320
310
  }
321
311
  static MAX_PORTIONS_COUNT = 600;
322
312
  static MAX_GUARD_COUNT = 16;
package/src/service.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { IsValidArray, IsValidPercent, IsValidName_AllowEmpty, Bcs, array_unique, IsValidTokenType, IsValidDesription,
1
+ import { IsValidArray, IsValidPercent, IsValidName_AllowEmpty, parseObjectType, array_unique, IsValidTokenType, IsValidDesription,
2
2
  IsValidAddress, IsValidEndpoint, IsValidU64, } from './utils'
3
3
  import { FnCallType, GuardObject, PassportObject, PermissionObject, RepositoryObject, MachineObject, ServiceAddress,
4
4
  ServiceObject, DiscountObject, OrderObject, OrderAddress, CoinObject, Protocol, ValueType,
@@ -1031,25 +1031,11 @@ export class Service {
1031
1031
  }
1032
1032
 
1033
1033
  static parseObjectType = (chain_type:string | undefined | null) : string => {
1034
- if (chain_type) {
1035
- const s = 'service::Service<'
1036
- const i = chain_type.indexOf(s);
1037
- if (i > 0) {
1038
- return chain_type.slice(i + s.length, chain_type.length-1);
1039
- }
1040
- }
1041
- return '';
1034
+ return parseObjectType(chain_type, 'service::Service<')
1042
1035
  }
1043
1036
 
1044
1037
  static parseOrderObjectType = (chain_type:string | undefined | null) : string => {
1045
- if (chain_type) {
1046
- const s = 'order::Order<'
1047
- const i = chain_type.indexOf(s);
1048
- if (i > 0) {
1049
- return chain_type.slice(i + s.length, chain_type.length-1);
1050
- }
1051
- }
1052
- return '';
1038
+ return parseObjectType(chain_type, 'order::Order<')
1053
1039
  }
1054
1040
 
1055
1041
  static endpoint = (service_endpoint:string, item_endpoint:string, item_name:string) => {
@@ -1062,7 +1048,7 @@ export class Service {
1062
1048
 
1063
1049
  static DiscountObjects = (owner:string, handleDiscountObject:handleDiscountObject) => {
1064
1050
  Protocol.Client().getOwnedObjects({owner:owner,
1065
- filter:{MoveModule:{module:'order', package:Protocol.Instance().Package()}},
1051
+ filter:{MoveModule:{module:'order', package:Protocol.Instance().Package('wowok')}},
1066
1052
  options:{showContent:true, showType:true}}).then((res) => {
1067
1053
  handleDiscountObject(owner, res.data.map((v)=>v.data));
1068
1054
  }).catch((e) => {
package/src/treasury.ts CHANGED
@@ -1,8 +1,28 @@
1
1
  import { type TransactionResult, Transaction as TransactionBlock } from '@mysten/sui/transactions';
2
- import { FnCallType, Protocol, PassportObject, PermissionObject, DemandAddress, TxbObject, CoinObject} from './protocol';
3
- import { IsValidDesription, IsValidU64, IsValidAddress, IsValidArgType, } from './utils'
2
+ import { FnCallType, Protocol, PassportObject, PermissionObject, TreasuryAddress, TxbObject, CoinObject, PaymentObject,
3
+ ReceivedObject } from './protocol';
4
+ import { IsValidDesription, IsValidU64, IsValidAddress, IsValidArgType, IsValidArray, parseObjectType} from './utils'
4
5
  import { Errors, ERROR} from './exception'
5
6
 
7
+ export interface DepositParam {
8
+ coin: CoinObject,
9
+ index: bigint,
10
+ remark: string,
11
+ for_object?: string,
12
+ for_guard?: string,
13
+ }
14
+
15
+ export interface WithdrawItem {
16
+ address: string,
17
+ amount: bigint,
18
+ }
19
+ export interface WithdrawParam {
20
+ items: WithdrawItem[],
21
+ index: bigint,
22
+ remark: string,
23
+ for_object?: string,
24
+ for_guard?: string,
25
+ }
6
26
  export class Treasury {
7
27
  protected token_type;
8
28
  protected permission ;
@@ -53,7 +73,7 @@ export class Treasury {
53
73
  return d
54
74
  }
55
75
 
56
- launch() : DemandAddress {
76
+ launch() : TreasuryAddress {
57
77
  return this.txb.moveCall({
58
78
  target:Protocol.Instance().TreasuryFn('create') as FnCallType,
59
79
  arguments:[Protocol.TXB_OBJECT(this.txb, this.object)],
@@ -69,125 +89,176 @@ export class Treasury {
69
89
  })
70
90
  }
71
91
 
72
- set_free_deposits(bFreeDeposit:boolean, passport?:PassportObject) {
92
+ set_free_deposit(bFreeDeposit:boolean, passport?:PassportObject) {
73
93
  if (passport) {
74
94
  this.txb.moveCall({
75
- target:Protocol.Instance().TreasuryFn('free_deposits_set_with_passport') as FnCallType,
95
+ target:Protocol.Instance().TreasuryFn('free_deposit_set_with_passport') as FnCallType,
76
96
  arguments:[passport, Protocol.TXB_OBJECT(this.txb, this.object),
77
97
  this.txb.pure.bool(bFreeDeposit), Protocol.TXB_OBJECT(this.txb, this.permission)],
78
98
  typeArguments:[this.token_type],
79
99
  })
80
100
  } else {
81
101
  this.txb.moveCall({
82
- target:Protocol.Instance().TreasuryFn('free_deposits_set') as FnCallType,
102
+ target:Protocol.Instance().TreasuryFn('free_deposit_set') as FnCallType,
83
103
  arguments:[Protocol.TXB_OBJECT(this.txb, this.object), this.txb.pure.bool(bFreeDeposit), Protocol.TXB_OBJECT(this.txb, this.permission)],
84
104
  typeArguments:[this.token_type],
85
105
  })
86
106
  }
87
107
  }
88
108
 
89
- deposit(coin:CoinObject, tips:string, for_object?:string, index?:bigint, from_object?:string, passport?:PassportObject) :TxbObject {
90
- if (!Protocol.IsValidObjects([coin])) {
91
- ERROR(Errors.IsValidObjects, 'deposit.coin')
109
+ deposit(param:DepositParam, bFreeDeposit:boolean=false, passport?:PassportObject) :TxbObject {
110
+ if (!Protocol.IsValidObjects([param.coin])) {
111
+ ERROR(Errors.IsValidObjects, 'deposit.param.coin')
92
112
  }
93
- if (!IsValidDesription(tips)) {
94
- ERROR(Errors.IsValidDesription, 'deposit.tips')
113
+ if (!IsValidDesription(param.remark)) {
114
+ ERROR(Errors.IsValidDesription, 'deposit.param.remark')
95
115
  }
96
- if (for_object && !IsValidAddress(for_object)) {
97
- ERROR(Errors.IsValidAddress, 'deposit.tips')
116
+ if (param?.for_object && !IsValidAddress(param.for_object)) {
117
+ ERROR(Errors.IsValidAddress, 'deposit.param.for_object')
98
118
  }
99
- if (from_object && !IsValidAddress(from_object)) {
100
- ERROR(Errors.IsValidAddress, 'deposit.from_object')
119
+ if (param?.for_guard && !IsValidAddress(param.for_guard)) {
120
+ ERROR(Errors.IsValidAddress, 'deposit.param.for_guard')
101
121
  }
102
- if (index !== undefined && !IsValidU64(index)) {
103
- ERROR(Errors.InvalidParam, 'deposit.index')
122
+ if (param.index !== undefined && !IsValidU64(param.index)) {
123
+ ERROR(Errors.InvalidParam, 'deposit.param.index')
104
124
  }
105
- const for_obj = this.txb.pure.option('address', for_object ?? undefined);
106
- const idx = this.txb.pure.option('u64', index ?? undefined);
107
- const from_obj = this.txb.pure.option('address', from_object ?? undefined);
125
+
126
+ const for_obj = this.txb.pure.option('address', param.for_object ?? undefined);
108
127
  const clock = this.txb.sharedObjectRef(Protocol.CLOCK_OBJECT);
109
128
 
129
+ if (bFreeDeposit) {
130
+ if (param.for_guard) {
131
+ return this.txb.moveCall({
132
+ target:Protocol.Instance().TreasuryFn('free_deposit_forGuard') as FnCallType,
133
+ arguments:[Protocol.TXB_OBJECT(this.txb, this.object), Protocol.TXB_OBJECT(this.txb, param.coin), this.txb.pure.u64(param.index),
134
+ this.txb.pure.string(param.remark), for_obj, this.txb.object(param.for_guard), this.txb.object(clock)],
135
+ typeArguments:[this.token_type],
136
+ })
137
+ } else {
138
+ return this.txb.moveCall({
139
+ target:Protocol.Instance().TreasuryFn('free_deposit') as FnCallType,
140
+ arguments:[Protocol.TXB_OBJECT(this.txb, this.object), Protocol.TXB_OBJECT(this.txb, param.coin), this.txb.pure.u64(param.index),
141
+ this.txb.pure.string(param.remark), for_obj, this.txb.object(clock)],
142
+ typeArguments:[this.token_type],
143
+ })
144
+ }
145
+ } else {
146
+ if (passport) {
147
+ if (param.for_guard) {
148
+ return this.txb.moveCall({
149
+ target:Protocol.Instance().TreasuryFn('deposit_forGuard_with_passport') as FnCallType,
150
+ arguments:[passport, Protocol.TXB_OBJECT(this.txb, this.object), Protocol.TXB_OBJECT(this.txb, param.coin), this.txb.pure.u64(param.index),
151
+ this.txb.pure.string(param.remark), for_obj, this.txb.object(param.for_guard), this.txb.object(clock), Protocol.TXB_OBJECT(this.txb, this.permission)],
152
+ typeArguments:[this.token_type],
153
+ })
154
+ } else {
155
+ return this.txb.moveCall({
156
+ target:Protocol.Instance().TreasuryFn('deposit_with_passport') as FnCallType,
157
+ arguments:[passport, Protocol.TXB_OBJECT(this.txb, this.object), Protocol.TXB_OBJECT(this.txb, param.coin), this.txb.pure.u64(param.index),
158
+ this.txb.pure.string(param.remark), for_obj, this.txb.object(clock), Protocol.TXB_OBJECT(this.txb, this.permission)],
159
+ typeArguments:[this.token_type],
160
+ })
161
+ }
162
+ } else {
163
+ if (param.for_guard) {
164
+ return this.txb.moveCall({
165
+ target:Protocol.Instance().TreasuryFn('deposit_forGuard') as FnCallType,
166
+ arguments:[Protocol.TXB_OBJECT(this.txb, this.object), Protocol.TXB_OBJECT(this.txb, param.coin), this.txb.pure.u64(param.index),
167
+ this.txb.pure.string(param.remark), for_obj, this.txb.object(param.for_guard), this.txb.object(clock), Protocol.TXB_OBJECT(this.txb, this.permission)],
168
+ typeArguments:[this.token_type],
169
+ })
170
+ } else {
171
+ return this.txb.moveCall({
172
+ target:Protocol.Instance().TreasuryFn('deposit') as FnCallType,
173
+ arguments:[Protocol.TXB_OBJECT(this.txb, this.object), Protocol.TXB_OBJECT(this.txb, param.coin), this.txb.pure.u64(param.index),
174
+ this.txb.pure.string(param.remark), for_obj, this.txb.object(clock), Protocol.TXB_OBJECT(this.txb, this.permission)],
175
+ typeArguments:[this.token_type],
176
+ })
177
+ }
178
+ }
179
+ }
180
+ }
181
+
182
+ receive(payment:PaymentObject, received:ReceivedObject, passport?:PassportObject) {
183
+ if (!Protocol.IsValidObjects([payment, received])) {
184
+ ERROR(Errors.IsValidArray, 'receive.payment&received');
185
+ }
186
+ const clock = this.txb.sharedObjectRef(Protocol.CLOCK_OBJECT);
110
187
  if (passport) {
111
188
  return this.txb.moveCall({
112
- target:Protocol.Instance().TreasuryFn('deposit_with_passport') as FnCallType,
113
- arguments:[passport, Protocol.TXB_OBJECT(this.txb, this.object), Protocol.TXB_OBJECT(this.txb, coin),
114
- this.txb.pure.string(tips), for_obj, idx, from_obj, this.txb.object(clock), Protocol.TXB_OBJECT(this.txb, this.permission)],
189
+ target:Protocol.Instance().TreasuryFn('receive_with_passport') as FnCallType,
190
+ arguments:[passport, Protocol.TXB_OBJECT(this.txb, this.object), this.txb.object(received), this.txb.object(payment),
191
+ this.txb.object(clock), Protocol.TXB_OBJECT(this.txb, this.permission)],
115
192
  typeArguments:[this.token_type],
116
193
  })
117
194
  } else {
118
195
  return this.txb.moveCall({
119
- target:Protocol.Instance().TreasuryFn('deposit') as FnCallType,
120
- arguments:[Protocol.TXB_OBJECT(this.txb, this.object), Protocol.TXB_OBJECT(this.txb, coin),
121
- this.txb.pure.string(tips), for_obj, idx, from_obj, this.txb.object(clock), Protocol.TXB_OBJECT(this.txb, this.permission)],
196
+ target:Protocol.Instance().TreasuryFn('receive') as FnCallType,
197
+ arguments:[Protocol.TXB_OBJECT(this.txb, this.object), this.txb.object(received), this.txb.object(payment),
198
+ this.txb.object(clock), Protocol.TXB_OBJECT(this.txb, this.permission)],
122
199
  typeArguments:[this.token_type],
123
- })
124
- }
125
- }
126
-
127
- free_deposit(coin:CoinObject, tips:string, for_object?:string, index?:number, from_object?:string) : TxbObject {
128
- if (!Protocol.IsValidObjects([coin])) {
129
- ERROR(Errors.IsValidObjects, 'free_deposit.coin')
130
- }
131
- if (!IsValidDesription(tips)) {
132
- ERROR(Errors.IsValidDesription, 'free_deposit.tips')
133
- }
134
- if (for_object && !IsValidAddress(for_object)) {
135
- ERROR(Errors.IsValidAddress, 'free_deposit.for_object')
136
- }
137
- if (from_object && !IsValidAddress(from_object)) {
138
- ERROR(Errors.IsValidAddress, 'free_deposit.from_object')
139
- }
140
- if (index !== undefined && !IsValidU64(index)) {
141
- ERROR(Errors.InvalidParam, 'free_deposit.index')
200
+ })
142
201
  }
143
- const for_obj = this.txb.pure.option('address', for_object ?? undefined);
144
- const idx = this.txb.pure.option('u64', index ?? undefined);
145
- const from_obj = this.txb.pure.option('address', from_object ?? undefined);
146
- const clock = this.txb.sharedObjectRef(Protocol.CLOCK_OBJECT);
147
-
148
- return this.txb.moveCall({
149
- target:Protocol.Instance().TreasuryFn('deposit2') as FnCallType,
150
- arguments:[Protocol.TXB_OBJECT(this.txb, this.object), Protocol.TXB_OBJECT(this.txb, coin),
151
- this.txb.pure.string(tips), for_obj, idx, from_obj, this.txb.object(clock)],
152
- typeArguments:[this.token_type],
153
- })
154
202
  }
155
203
 
156
- withdraw(amount:bigint, tips:string, for_object?:string, index?:bigint, to_object?:string, passport?:PassportObject) : CoinObject {
157
- if (!IsValidU64(amount)) {
158
- ERROR(Errors.IsValidObjects, 'withdraw.amount')
204
+ withdraw(param:WithdrawParam, passport?:PassportObject) {
205
+ if (param.items.length === 0) return undefined;
206
+ if (!IsValidArray(param.items, (item:WithdrawItem) => IsValidU64(item.amount) && IsValidAddress(item.address))) {
207
+ ERROR(Errors.IsValidArray, 'withdraw.param.items')
159
208
  }
160
- if (!IsValidDesription(tips)) {
161
- ERROR(Errors.IsValidDesription, 'withdraw.tips')
209
+ if (!IsValidDesription(param.remark)) {
210
+ ERROR(Errors.IsValidDesription, 'withdraw.param.remark')
162
211
  }
163
- if (for_object && !IsValidAddress(for_object)) {
164
- ERROR(Errors.IsValidAddress, 'withdraw.for_object')
212
+ if (!IsValidU64(param.index)) {
213
+ ERROR(Errors.IsValidU64, 'withdraw.param.index')
165
214
  }
166
- if (to_object && !IsValidAddress(to_object)) {
167
- ERROR(Errors.IsValidAddress, 'withdraw.to_object')
215
+ if (param?.for_guard && !IsValidAddress(param.for_guard)) {
216
+ ERROR(Errors.IsValidAddress, 'withdraw.param.for_guard')
168
217
  }
169
- if (index !== undefined && !IsValidU64(index)) {
170
- ERROR(Errors.InvalidParam, 'withdraw.index')
218
+ if (param?.for_object && !IsValidAddress(param.for_object)) {
219
+ ERROR(Errors.IsValidAddress, 'withdraw.param.for_object')
171
220
  }
172
- const for_obj = this.txb.pure.option('address', for_object ?? undefined);
173
- const idx = this.txb.pure.option('u64', index ?? undefined);
174
- const to_obj = this.txb.pure.option('address', to_object ?? undefined);
221
+
222
+ const for_obj = this.txb.pure.option('address', param.for_object ?? undefined);
175
223
  const clock = this.txb.sharedObjectRef(Protocol.CLOCK_OBJECT);
176
224
 
177
225
  if (passport) {
178
- return this.txb.moveCall({
179
- target:Protocol.Instance().TreasuryFn('withdraw_with_passport') as FnCallType,
180
- arguments:[passport, Protocol.TXB_OBJECT(this.txb, this.object), this.txb.pure.u64(amount),
181
- this.txb.pure.string(tips), for_obj, idx, to_obj, this.txb.object(clock), Protocol.TXB_OBJECT(this.txb, this.permission)],
182
- typeArguments:[this.token_type],
183
- })
226
+ if (param.for_guard) {
227
+ return this.txb.moveCall({
228
+ target:Protocol.Instance().TreasuryFn('withdraw_forGuard_with_passport') as FnCallType,
229
+ arguments:[passport, Protocol.TXB_OBJECT(this.txb, this.object), this.txb.pure.vector('address', param.items.map(v=>v.address)),
230
+ this.txb.pure.vector('u64', param.items.map(v=>v.amount)), this.txb.pure.u64(param.index),
231
+ this.txb.pure.string(param.remark), for_obj, this.txb.object(param.for_guard), this.txb.object(clock), Protocol.TXB_OBJECT(this.txb, this.permission)],
232
+ typeArguments:[this.token_type],
233
+ })
234
+ } else {
235
+ return this.txb.moveCall({
236
+ target:Protocol.Instance().TreasuryFn('withdraw_with_passport') as FnCallType,
237
+ arguments:[passport, Protocol.TXB_OBJECT(this.txb, this.object), this.txb.pure.vector('address', param.items.map(v=>v.address)),
238
+ this.txb.pure.vector('u64', param.items.map(v=>v.amount)), this.txb.pure.u64(param.index),
239
+ this.txb.pure.string(param.remark), for_obj, this.txb.object(clock), Protocol.TXB_OBJECT(this.txb, this.permission)],
240
+ typeArguments:[this.token_type],
241
+ })
242
+ }
184
243
  } else {
185
- return this.txb.moveCall({
186
- target:Protocol.Instance().TreasuryFn('withdraw') as FnCallType,
187
- arguments:[Protocol.TXB_OBJECT(this.txb, this.object), this.txb.pure.u64(amount),
188
- this.txb.pure.string(tips), for_obj, idx, to_obj, this.txb.object(clock), Protocol.TXB_OBJECT(this.txb, this.permission)],
189
- typeArguments:[this.token_type],
190
- })
244
+ if (param.for_guard) {
245
+ return this.txb.moveCall({
246
+ target:Protocol.Instance().TreasuryFn('withdraw_forGuard') as FnCallType,
247
+ arguments:[Protocol.TXB_OBJECT(this.txb, this.object), this.txb.pure.vector('address', param.items.map(v=>v.address)),
248
+ this.txb.pure.vector('u64', param.items.map(v=>v.amount)), this.txb.pure.u64(param.index),
249
+ this.txb.pure.string(param.remark), for_obj, this.txb.object(param.for_guard), this.txb.object(clock), Protocol.TXB_OBJECT(this.txb, this.permission)],
250
+ typeArguments:[this.token_type],
251
+ })
252
+ } else {
253
+ console.log(param)
254
+ return this.txb.moveCall({
255
+ target:Protocol.Instance().TreasuryFn('withdraw') as FnCallType,
256
+ arguments:[Protocol.TXB_OBJECT(this.txb, this.object), this.txb.pure.vector('address', param.items.map(v=>v.address)),
257
+ this.txb.pure.vector('u64', param.items.map(v=>v.amount)), this.txb.pure.u64(param.index),
258
+ this.txb.pure.string(param.remark), for_obj, this.txb.object(clock), Protocol.TXB_OBJECT(this.txb, this.permission)],
259
+ typeArguments:[this.token_type],
260
+ })
261
+ }
191
262
  }
192
263
  }
193
264
 
@@ -225,15 +296,11 @@ export class Treasury {
225
296
  this.permission = new_permission
226
297
  }
227
298
  static parseObjectType = (chain_type:string) : string => {
228
- if (chain_type) {
229
- const s = 'treasury::Treasury<'
230
- const i = chain_type.indexOf(s);
231
- if (i > 0) {
232
- let r = chain_type.slice(i + s.length, chain_type.length-1);
233
- return r
234
- }
235
- }
236
- return '';
299
+ return parseObjectType(chain_type, 'treasury::Treasury<')
237
300
  }
301
+
302
+ static OP_WITHDRAW = 0;
303
+ static OP_DEPOSIT = 1;
304
+ static OP_RECEIVE = 2;
238
305
  }
239
306
 
package/src/utils.ts CHANGED
@@ -119,6 +119,17 @@ export const concatenate = (resultConstructor:any, ...arrays:any[]) => {
119
119
  return result;
120
120
  }
121
121
 
122
+ export const parseObjectType = (chain_type:string | null | undefined, header:string='payment::Payment<') : string => {
123
+ if (chain_type) {
124
+ const i = chain_type.indexOf(header);
125
+ if (i > 0) {
126
+ let r = chain_type.slice(i + header.length, chain_type.length-1);
127
+ return r
128
+ }
129
+ }
130
+ return '';
131
+ }
132
+
122
133
  export const array_equal = (arr1: any[], arr2: any[]) => {
123
134
  if (arr1.length !== arr2.length) {
124
135
  return false;
@@ -463,19 +474,40 @@ export const ResolveU64 = (value:bigint) : bigint => {
463
474
  }
464
475
  }
465
476
 
477
+ function removeTrailingZeros(numberString: string): string {
478
+ const trimmedString = numberString.trim();
479
+ const decimalIndex = trimmedString.indexOf('.');
480
+
481
+ if (decimalIndex !== -1) {
482
+ let endIndex = trimmedString.length - 1;
483
+
484
+ while (trimmedString[endIndex] === '0') {
485
+ endIndex--;
486
+ }
487
+
488
+ if (trimmedString[endIndex] === '.') {
489
+ endIndex--; // 如果小数点后面全是零,也去掉小数点
490
+ }
491
+
492
+ return trimmedString.slice(0, endIndex + 1);
493
+ }
494
+
495
+ return trimmedString;
496
+ }
497
+
466
498
  export const ResolveBalance = (balance:string, decimals:number) : string => {
467
499
  if (!balance) return ''
468
500
  if (balance === '0') return '0'
469
501
  if (decimals <= 0) return balance;
470
502
  var pos = decimals - balance.length;
471
503
  if (pos === 0) {
472
- return '.' + balance;
504
+ return removeTrailingZeros('.' + (balance));
473
505
  } else if (pos < 0) {
474
506
  let start = balance.slice(0, Math.abs(pos));
475
507
  let end = balance.slice(Math.abs(pos));
476
- return start + '.' + end;
508
+ return removeTrailingZeros(start + '.' + end);
477
509
  } else {
478
- return '.' + balance.padStart(decimals, '0');
510
+ return removeTrailingZeros('.' + balance.padStart(decimals, '0'));
479
511
  }
480
512
  }
481
513
 
@@ -0,0 +1,164 @@
1
+ import { FnCallType, PaymentObject, ReceivedObject, PaymentAddress, Protocol, TxbObject, CoinObject, PassportObject} from './protocol';
2
+ import { IsValidDesription, IsValidAddress, Bcs, array_unique, IsValidArray, IsValidName, IsValidU64, IsValidU256, IsValidU8 } from './utils';
3
+ import { ERROR, Errors } from './exception';
4
+ import { DepositParam, WithdrawParam, WithdrawItem } from './treasury';
5
+ import { Transaction as TransactionBlock} from '@mysten/sui/transactions';
6
+
7
+ export interface WithholdingGuard {
8
+ guard: string,
9
+ amount: bigint,
10
+ }
11
+ export interface WithholdingParam {
12
+ guards: WithholdingGuard[]
13
+ }
14
+
15
+ export class Withholding {
16
+ protected object : TxbObject;
17
+ protected pay_token_type;
18
+ protected txb;
19
+
20
+ get_pay_type() { return this.pay_token_type }
21
+ get_object() { return this.object }
22
+ private constructor(txb:TransactionBlock, pay_token_type:string, ) {
23
+ this.object = '';
24
+ this.pay_token_type = pay_token_type;
25
+ this.txb = txb;
26
+ }
27
+
28
+ static From(txb:TransactionBlock, pay_token_type:string, object:TxbObject) : Withholding {
29
+ if (!pay_token_type) ERROR(Errors.InvalidParam, 'Withholding.From.pay_token_type');
30
+ let v = new Withholding(txb, pay_token_type);
31
+ v.object = Protocol.TXB_OBJECT(txb, object)
32
+ return v
33
+ }
34
+
35
+ static New(txb:TransactionBlock, pay_token_type:string, param:WithholdingParam) : Withholding {
36
+ if (!pay_token_type) ERROR(Errors.InvalidParam, 'Withholding.New_fromAddress.pay_token_type');
37
+ if (param.guards.length === 0 || param.guards.length > Withholding.MAX_GUARD_COUNT) {
38
+ ERROR(Errors.InvalidParam, 'Withholding.New.param.guards length')
39
+ }
40
+ if (!IsValidArray(param.guards, (item:WithholdingGuard) => IsValidAddress(item.guard) && IsValidU64(item.amount))) {
41
+ ERROR(Errors.InvalidParam, 'Withholding.New.param.guards')
42
+ }
43
+
44
+ let v = new Withholding(txb, pay_token_type);
45
+ const clock = txb.sharedObjectRef(Protocol.CLOCK_OBJECT);
46
+
47
+ v.object = txb.moveCall({
48
+ target:Protocol.Instance().WithholdingFn('new') as FnCallType,
49
+ arguments:[],
50
+ typeArguments:[pay_token_type],
51
+ })
52
+
53
+ param.guards.forEach((i) => {
54
+ txb.moveCall({
55
+ target:Protocol.Instance().WithholdingFn('add_guard') as FnCallType,
56
+ arguments:[txb.object(v.object), txb.object(i.guard), txb.pure.u64(i.amount)],
57
+ typeArguments:[pay_token_type],
58
+ })
59
+ })
60
+ return v
61
+ }
62
+
63
+ launch() : PaymentAddress {
64
+ return this.txb.moveCall({
65
+ target:Protocol.Instance().WithholdingFn('create') as FnCallType,
66
+ arguments:[Protocol.TXB_OBJECT(this.txb, this.object)],
67
+ typeArguments:[this.pay_token_type],
68
+ })
69
+ }
70
+
71
+ receive(payment:PaymentObject, received:ReceivedObject) {
72
+ if (!Protocol.IsValidObjects([payment, received])) {
73
+ ERROR(Errors.IsValidArray, 'receive.payment&received');
74
+ }
75
+ const clock = this.txb.sharedObjectRef(Protocol.CLOCK_OBJECT);
76
+
77
+ return this.txb.moveCall({
78
+ target:Protocol.Instance().WithholdingFn('receive') as FnCallType,
79
+ arguments:[Protocol.TXB_OBJECT(this.txb, this.object), this.txb.object(received), this.txb.object(payment),
80
+ this.txb.object(clock)],
81
+ typeArguments:[this.pay_token_type],
82
+ })
83
+ }
84
+
85
+ deposit(param:DepositParam) {
86
+ if (!Protocol.IsValidObjects([param.coin])) {
87
+ ERROR(Errors.IsValidObjects, 'deposit.param.coin')
88
+ }
89
+ if (!IsValidDesription(param.remark)) {
90
+ ERROR(Errors.IsValidDesription, 'deposit.param.remark')
91
+ }
92
+ if (param?.for_object && !IsValidAddress(param.for_object)) {
93
+ ERROR(Errors.IsValidAddress, 'deposit.param.for_object')
94
+ }
95
+ if (param?.for_guard && !IsValidAddress(param.for_guard)) {
96
+ ERROR(Errors.IsValidAddress, 'deposit.param.for_guard')
97
+ }
98
+ if (param.index !== undefined && !IsValidU64(param.index)) {
99
+ ERROR(Errors.InvalidParam, 'deposit.param.index')
100
+ }
101
+
102
+ const for_obj = this.txb.pure.option('address', param.for_object ?? undefined);
103
+ const clock = this.txb.sharedObjectRef(Protocol.CLOCK_OBJECT);
104
+
105
+ if (param.for_guard) {
106
+ return this.txb.moveCall({
107
+ target:Protocol.Instance().WithholdingFn('deposit_forGuard') as FnCallType,
108
+ arguments:[Protocol.TXB_OBJECT(this.txb, this.object), Protocol.TXB_OBJECT(this.txb, param.coin), this.txb.pure.u64(param.index),
109
+ this.txb.pure.string(param.remark), for_obj, this.txb.object(param.for_guard), this.txb.object(clock)],
110
+ typeArguments:[this.pay_token_type],
111
+ })
112
+ } else {
113
+ return this.txb.moveCall({
114
+ target:Protocol.Instance().WithholdingFn('deposit') as FnCallType,
115
+ arguments:[Protocol.TXB_OBJECT(this.txb, this.object), Protocol.TXB_OBJECT(this.txb, param.coin), this.txb.pure.u64(param.index),
116
+ this.txb.pure.string(param.remark), for_obj, this.txb.object(clock)],
117
+ typeArguments:[this.pay_token_type],
118
+ })
119
+ }
120
+ }
121
+
122
+ // param.treasury -> coins ; param.receiver -> null
123
+ withdraw(guard:string, param:WithdrawParam, passport:PassportObject) {
124
+ if (param.items.length === 0) return undefined;
125
+ if (!IsValidArray(param.items, (item:WithdrawItem) => IsValidU64(item.amount) && IsValidAddress(item.address))) {
126
+ ERROR(Errors.IsValidArray, 'withdraw.param.items')
127
+ }
128
+ if (!IsValidDesription(param.remark)) {
129
+ ERROR(Errors.IsValidDesription, 'withdraw.param.remark')
130
+ }
131
+ if (!IsValidU64(param.index)) {
132
+ ERROR(Errors.IsValidU64, 'withdraw.param.index')
133
+ }
134
+ if (param?.for_guard && !IsValidAddress(param.for_guard)) {
135
+ ERROR(Errors.IsValidAddress, 'withdraw.param.for_guard')
136
+ }
137
+ if (param?.for_object && !IsValidAddress(param.for_object)) {
138
+ ERROR(Errors.IsValidAddress, 'withdraw.param.for_object')
139
+ }
140
+
141
+ const for_obj = this.txb.pure.option('address', param.for_object ?? undefined);
142
+ const clock = this.txb.sharedObjectRef(Protocol.CLOCK_OBJECT);
143
+
144
+ if (param.for_guard) {
145
+ return this.txb.moveCall({
146
+ target:Protocol.Instance().WithholdingFn('withdraw_forGuard') as FnCallType,
147
+ arguments:[Protocol.TXB_OBJECT(this.txb, this.object), this.txb.object(guard), this.txb.object(passport),
148
+ this.txb.pure.vector('address', param.items.map(i=>i.address)), this.txb.pure.vector('u64', param.items.map(i=>i.amount)),
149
+ this.txb.pure.u64(param.index), this.txb.pure.string(param.remark), for_obj, this.txb.object(param.for_guard), this.txb.object(clock)],
150
+ typeArguments:[this.pay_token_type],
151
+ })
152
+ } else {
153
+ return this.txb.moveCall({
154
+ target:Protocol.Instance().WithholdingFn('withdraw') as FnCallType,
155
+ arguments:[Protocol.TXB_OBJECT(this.txb, this.object), this.txb.object(guard), this.txb.object(passport),
156
+ this.txb.pure.vector('address', param.items.map(i=>i.address)), this.txb.pure.vector('u64', param.items.map(i=>i.amount)),
157
+ this.txb.pure.u64(param.index), this.txb.pure.string(param.remark), for_obj, this.txb.object(clock)],
158
+ typeArguments:[this.pay_token_type],
159
+ })
160
+ }
161
+ }
162
+
163
+ static MAX_GUARD_COUNT = 16;
164
+ }