wowok_agent 0.0.1 → 0.1.3

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.
@@ -1,12 +1,14 @@
1
- import { TransactionBlock, CallResponse} from 'wowok';
1
+ import { TransactionBlock, CallResponse, IsValidArgType} from 'wowok';
2
2
  import { PassportObject, IsValidAddress, Errors, ERROR, Permission, PermissionIndex,
3
3
  PermissionIndexType, DepositParam, Treasury, Treasury_WithdrawMode, WithdrawParam, WitnessFill
4
4
  } from 'wowok';
5
- import { OBJECT_QUERY, ObjectTreasury } from '../objects';
6
- import { CallBase } from "./call";
5
+ import { query_objects, ObjectTreasury } from '../objects';
6
+ import { CallBase, CallResult } from "./base";
7
7
 
8
- export class CallTreasury extends CallBase {
9
- type_parameter: string;
8
+ export interface CallTreasury_Data {
9
+ object?: string; // undefined for creating a new object
10
+ permission?: string;
11
+ type_parameter?: string;
10
12
  permission_new?: string;
11
13
  description?: string;
12
14
  withdraw_mode?: Treasury_WithdrawMode;
@@ -15,61 +17,75 @@ export class CallTreasury extends CallBase {
15
17
  deposit?: {data:DepositParam, guard?:string | 'fetch'};
16
18
  receive?: {payment:string; received_object:string};
17
19
  withdraw?:WithdrawParam;
18
- constructor(type_parameter:string, object: string | 'new' = 'new') {
19
- super(object)
20
- this.type_parameter = type_parameter;
20
+ }
21
+ export class CallTreasury extends CallBase {
22
+ data: CallTreasury_Data;
23
+ constructor(data:CallTreasury_Data) {
24
+ super();
25
+ this.data = data;
21
26
  }
22
- async call(account?:string) : Promise<WitnessFill[] | CallResponse | undefined> {
23
- if (!this.type_parameter) ERROR(Errors.InvalidParam, 'type_parameter');
27
+
28
+ async call(account?:string) : Promise<CallResult> {
29
+ if (!this.data.type_parameter || !IsValidArgType(this.data.type_parameter)) {
30
+ ERROR(Errors.IsValidArgType, 'treasury.type_parameter');
31
+ }
32
+
24
33
  var checkOwner = false; const guards : string[] = [];
25
34
  const perms : PermissionIndexType[] = []; var obj: ObjectTreasury | undefined ;
26
35
 
27
- if (this?.permission && IsValidAddress(this.permission)) {
28
- if (this?.object === 'new') {
36
+ if (this.data?.permission && IsValidAddress(this.data.permission)) {
37
+ if (!this.data?.object) {
29
38
  perms.push(PermissionIndex.treasury)
30
39
  }
31
- if (this?.permission_new !== undefined) {
40
+ if (this.data?.permission_new !== undefined) {
32
41
  checkOwner = true;
33
42
  }
34
- if (this?.description !== undefined && this.object !== 'new') {
43
+ if (this.data?.description !== undefined && this.data.object) {
35
44
  perms.push(PermissionIndex.treasury_descritption)
36
45
  }
37
- if (this?.withdraw_mode !== undefined) {
46
+ if (this.data?.withdraw_mode !== undefined) {
38
47
  perms.push(PermissionIndex.treasury_withdraw_mode)
39
48
  }
40
- if (this?.withdraw_guard == undefined) { // publish is an irreversible one-time operation
49
+ if (this.data?.withdraw_guard == undefined) { // publish is an irreversible one-time operation
41
50
  perms.push(PermissionIndex.treasury_withdraw_guard)
42
51
  }
43
- if (this?.deposit_guard !== undefined) {
52
+ if (this.data?.deposit_guard !== undefined) {
44
53
  perms.push(PermissionIndex.treasury_deposit_guard)
45
54
  }
46
- if (this?.deposit_guard !== undefined) {
55
+ if (this.data?.deposit_guard !== undefined) {
47
56
  perms.push(PermissionIndex.treasury_deposit_guard)
48
57
  }
49
- if (this?.deposit?.guard !== undefined) {
50
- if (IsValidAddress(this.deposit.guard)) {
51
- guards.push(this.deposit.guard)
58
+ if (this.data?.deposit?.guard !== undefined) {
59
+ if (IsValidAddress(this.data.deposit.guard)) {
60
+ guards.push(this.data.deposit.guard)
52
61
  } else {
53
- if (!obj) {
54
- const r = await OBJECT_QUERY.objects({objects:[this.object], showContent:true});
55
- if (r?.objects && r.objects[0].type === 'Treasury') {
56
- obj = r.objects[0] as ObjectTreasury;
62
+ if (!this.data.object) {
63
+ if (this.data?.deposit_guard && IsValidAddress(this.data?.deposit_guard)) {
64
+ guards.push(this.data.deposit_guard)
57
65
  }
66
+ } else {
67
+ if (!obj) {
68
+ const r = await query_objects({objects:[this.data.object], showContent:true});
69
+ if (r?.objects && r.objects[0].type === 'Treasury') {
70
+ obj = r.objects[0] as ObjectTreasury;
71
+ }
72
+ }
73
+ if (obj?.deposit_guard) {
74
+ guards.push(obj?.deposit_guard)
75
+ }
58
76
  }
59
- if (obj?.deposit_guard) {
60
- guards.push(obj?.deposit_guard)
61
- }
77
+
62
78
  }
63
79
  }
64
- if (this?.receive !== undefined) {
80
+ if (this.data?.receive !== undefined) {
65
81
  perms.push(PermissionIndex.treasury_receive)
66
82
  }
67
- if (this?.withdraw?.withdraw_guard !== undefined) {
68
- if (typeof(this.withdraw.withdraw_guard) === 'string' && IsValidAddress(this.withdraw.withdraw_guard)) {
69
- guards.push(this.withdraw.withdraw_guard)
70
- } else {
83
+ if (this.data?.withdraw?.withdraw_guard !== undefined) {
84
+ if (typeof(this.data.withdraw.withdraw_guard) === 'string' && IsValidAddress(this.data.withdraw.withdraw_guard)) {
85
+ guards.push(this.data.withdraw.withdraw_guard)
86
+ } else if (this.data.object) {
71
87
  if (!obj) {
72
- const r = await OBJECT_QUERY.objects({objects:[this.object], showContent:true});
88
+ const r = await query_objects({objects:[this.data.object], showContent:true});
73
89
  if (r?.objects && r.objects[0].type === 'Treasury') {
74
90
  obj = r.objects[0] as ObjectTreasury;
75
91
  }
@@ -82,66 +98,66 @@ export class CallTreasury extends CallBase {
82
98
  perms.push(PermissionIndex.treasury_withdraw)
83
99
  }
84
100
 
85
- return await this.check_permission_and_call(this.permission, perms, guards, checkOwner, undefined, account)
101
+ return await this.check_permission_and_call(this.data.permission, perms, guards, checkOwner, undefined, account)
86
102
  }
87
103
  return this.exec(account);
88
104
  }
89
105
  protected async operate (txb:TransactionBlock, passport?:PassportObject) {
90
106
  let obj : Treasury | undefined ; let permission: any;
91
- if (this.object === 'new' && this?.type_parameter) {
92
- if (!this?.permission || !IsValidAddress(this?.permission)) {
107
+ if (!this.data.object) {
108
+ if (!this.data?.permission || !IsValidAddress(this.data?.permission)) {
93
109
  permission = Permission.New(txb, '');
94
110
  }
95
- obj = Treasury.New(txb, this.type_parameter, permission ?? this?.permission, this?.description??'', permission?undefined:passport)
111
+ obj = Treasury.New(txb, this.data.type_parameter!, permission ?? this.data?.permission, this.data?.description??'', permission?undefined:passport)
96
112
  } else {
97
- if (IsValidAddress(this.object) && this.type_parameter && this.permission && IsValidAddress(this?.permission)) {
98
- obj = Treasury.From(txb, this.type_parameter, this.permission, this.object)
113
+ if (IsValidAddress(this.data.object) && this.data.type_parameter && this.data.permission && IsValidAddress(this.data?.permission)) {
114
+ obj = Treasury.From(txb, this.data.type_parameter, this.data.permission, this.data.object)
99
115
  }
100
116
  }
101
117
 
102
118
  if (obj) {
103
- if (this?.description !== undefined && this.object !== 'new') {
104
- obj?.set_description(this.description, passport);
119
+ if (this.data?.description !== undefined && this.data.object) {
120
+ obj?.set_description(this.data.description, passport);
105
121
  }
106
- if (this?.deposit_guard !== undefined) {
107
- obj?.set_deposit_guard(this.deposit_guard, passport);
122
+ if (this.data?.deposit_guard !== undefined) {
123
+ obj?.set_deposit_guard(this.data.deposit_guard, passport);
108
124
  }
109
- if (this?.withdraw_mode !== undefined) {
110
- obj?.set_withdraw_mode(this.withdraw_mode, passport)
125
+ if (this.data?.withdraw_mode !== undefined) {
126
+ obj?.set_withdraw_mode(this.data.withdraw_mode, passport)
111
127
  }
112
- if (this?.withdraw_guard !== undefined) {
113
- switch (this.withdraw_guard.op) {
128
+ if (this.data?.withdraw_guard !== undefined) {
129
+ switch (this.data.withdraw_guard.op) {
114
130
  case 'add':
115
- this.withdraw_guard.data.forEach(v => obj?.add_withdraw_guard(v.guard, BigInt(v.amount), passport))
131
+ this.data.withdraw_guard.data.forEach(v => obj?.add_withdraw_guard(v.guard, BigInt(v.amount), passport))
116
132
  break;
117
133
  case 'remove':
118
- obj?.remove_withdraw_guard(this.withdraw_guard.guards, false, passport)
134
+ obj?.remove_withdraw_guard(this.data.withdraw_guard.guards, false, passport)
119
135
  break;
120
136
  case 'set':
121
137
  obj?.remove_withdraw_guard([], true, passport)
122
- this.withdraw_guard.data.forEach(v => obj?.add_withdraw_guard(v.guard, BigInt(v.amount), passport))
138
+ this.data.withdraw_guard.data.forEach(v => obj?.add_withdraw_guard(v.guard, BigInt(v.amount), passport))
123
139
  break;
124
140
  case 'removeall':
125
141
  obj?.remove_withdraw_guard([], true, passport)
126
142
  break;
127
143
  }
128
144
  }
129
- if (this?.withdraw !== undefined) {
130
- obj?.withdraw(this.withdraw, passport)
145
+ if (this.data?.withdraw !== undefined) {
146
+ obj?.withdraw(this.data.withdraw, passport)
131
147
  }
132
- if (this?.receive !== undefined) {
133
- obj?.receive(this.receive.payment, this.receive.received_object, passport);
148
+ if (this.data?.receive !== undefined) {
149
+ obj?.receive(this.data.receive.payment, this.data.receive.received_object, passport);
134
150
  }
135
- if (this.deposit !== undefined) {
136
- obj?.deposit(this.deposit.data, passport)
151
+ if (this.data.deposit !== undefined) {
152
+ obj?.deposit(this.data.deposit.data, passport)
137
153
  }
138
- if (this?.permission_new !== undefined) {
139
- obj?.change_permission(this.permission_new);
154
+ if (this.data?.permission_new !== undefined) {
155
+ obj?.change_permission(this.data.permission_new);
140
156
  }
141
157
  if (permission) {
142
158
  permission.launch();
143
159
  }
144
- if (this.object === 'new') {
160
+ if (!this.data.object) {
145
161
  obj?.launch();
146
162
  }
147
163
  }
package/src/call.ts ADDED
@@ -0,0 +1,70 @@
1
+
2
+ /**
3
+ * Provide a this interface for AI
4
+ * Operation sequence Priority: common operation > Guard change > permission change
5
+ * Recommended: Changes to guard and permission are committed on-chain separately to avoid permission dependencies for other operations.
6
+ */
7
+
8
+ import { CallArbitration, CallArbitration_Data } from "./call/arbitration";
9
+ import { CallDemand, CallDemand_Data } from "./call/demand";
10
+ import { CallMachine, CallMachine_Data } from "./call/machine";
11
+ import { CallPermission, CallPermission_Data } from "./call/permission";
12
+ import { CallPersonal, CallPersonal_Data } from "./call/personal";
13
+ import { CallRepository, CallRepository_Data } from "./call/repository";
14
+ import { CallService, CallService_Data } from "./call/service";
15
+ import { CallTreasury, CallTreasury_Data } from "./call/treasury";
16
+ import { CallBase, CallResult, CallWithWitnessParam } from "./call/base";
17
+ import { CallGuard, CallGuard_Data } from "./call/guard";
18
+
19
+
20
+ export interface CallObjectData {
21
+ type: 'Demand' | 'Service' | 'Machine' | 'Treasury' | 'Arbitration' | 'Guard' | 'Repository' | 'Personal' | 'Permission';
22
+ data: CallDemand_Data | CallMachine_Data | CallArbitration_Data | CallPermission_Data | CallPermission_Data
23
+ | CallTreasury_Data | CallService_Data | CallPermission_Data | CallRepository_Data;
24
+ account?: string;
25
+ witness?: CallWithWitnessParam;
26
+ }
27
+
28
+ export const call_object_json = async (json: string) : Promise<string> => {
29
+ try {
30
+ const c : CallObjectData = JSON.parse(json);
31
+ return JSON.stringify({data:await call_object(c)});
32
+ } catch (e) {
33
+ return JSON.stringify({error:e?.toString()})
34
+ }
35
+ }
36
+
37
+ export const call_object = async (call: CallObjectData) : Promise<CallResult> => {
38
+ var obj = call_object_new(call);
39
+
40
+ if (obj) {
41
+ if (call.witness) {
42
+ return obj.call_with_witness(call.witness);
43
+ } else {
44
+ return obj.call(call.account);
45
+ }
46
+ }
47
+ }
48
+
49
+ function call_object_new (call: CallObjectData) : CallBase | undefined {
50
+ switch (call.type) {
51
+ case 'Demand':
52
+ return new CallDemand(call.data as CallDemand_Data);
53
+ case 'Service':
54
+ return new CallService(call.data as CallService_Data);
55
+ case 'Machine':
56
+ return new CallMachine(call.data as CallMachine_Data);
57
+ case 'Treasury':
58
+ return new CallTreasury(call.data as CallTreasury_Data);
59
+ case 'Arbitration':
60
+ return new CallArbitration(call.data as CallArbitration_Data);
61
+ case 'Guard':
62
+ return new CallGuard(call.data as CallGuard_Data);
63
+ case 'Repository':
64
+ return new CallRepository(call.data as CallRepository_Data);
65
+ case 'Personal':
66
+ return new CallPersonal(call.data as CallPersonal_Data);
67
+ case 'Permission':
68
+ return new CallPermission(call.data as CallPermission_Data);
69
+ }
70
+ }
package/src/index.ts CHANGED
@@ -2,7 +2,7 @@ export * from './objects'
2
2
  export * from './permission'
3
3
  export * from './events'
4
4
  export * from './cache'
5
- export * from './call/call'
5
+ export * from './call/base'
6
6
  export * from './call/permission'
7
7
  export * from './call/arbitration'
8
8
  export * from './call/treasury'
@@ -11,3 +11,5 @@ export * from './call/demand'
11
11
  export * from './call/machine'
12
12
  export * from './call/repository'
13
13
  export * from './call/service'
14
+ export * from './call/guard'
15
+ export * from './account'