wowok_agent 0.0.1
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/LICENSE +201 -0
- package/README.md +23 -0
- package/package.json +52 -0
- package/src/account.ts +198 -0
- package/src/cache.ts +54 -0
- package/src/call/arbitration.ts +169 -0
- package/src/call/call.ts +133 -0
- package/src/call/demand.ts +133 -0
- package/src/call/machine.ts +209 -0
- package/src/call/permission.ts +101 -0
- package/src/call/personal.ts +87 -0
- package/src/call/repository.ts +129 -0
- package/src/call/service.ts +334 -0
- package/src/call/treasury.ts +149 -0
- package/src/events.ts +100 -0
- package/src/index.ts +13 -0
- package/src/objects.ts +661 -0
- package/src/permission.ts +52 -0
- package/tsconfig.json +111 -0
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import { TransactionBlock, CallResponse} from 'wowok';
|
|
2
|
+
import { PassportObject, IsValidAddress, Errors, ERROR, Permission, PermissionIndex,
|
|
3
|
+
PermissionIndexType, DepositParam, Treasury, Treasury_WithdrawMode, WithdrawParam, WitnessFill
|
|
4
|
+
} from 'wowok';
|
|
5
|
+
import { OBJECT_QUERY, ObjectTreasury } from '../objects';
|
|
6
|
+
import { CallBase } from "./call";
|
|
7
|
+
|
|
8
|
+
export class CallTreasury extends CallBase {
|
|
9
|
+
type_parameter: string;
|
|
10
|
+
permission_new?: string;
|
|
11
|
+
description?: string;
|
|
12
|
+
withdraw_mode?: Treasury_WithdrawMode;
|
|
13
|
+
withdraw_guard?: {op:'add' | 'set'; data:{guard:string, amount:string}[]} | {op:'remove', guards:string[]} | {op:'removeall'};
|
|
14
|
+
deposit_guard?: string;
|
|
15
|
+
deposit?: {data:DepositParam, guard?:string | 'fetch'};
|
|
16
|
+
receive?: {payment:string; received_object:string};
|
|
17
|
+
withdraw?:WithdrawParam;
|
|
18
|
+
constructor(type_parameter:string, object: string | 'new' = 'new') {
|
|
19
|
+
super(object)
|
|
20
|
+
this.type_parameter = type_parameter;
|
|
21
|
+
}
|
|
22
|
+
async call(account?:string) : Promise<WitnessFill[] | CallResponse | undefined> {
|
|
23
|
+
if (!this.type_parameter) ERROR(Errors.InvalidParam, 'type_parameter');
|
|
24
|
+
var checkOwner = false; const guards : string[] = [];
|
|
25
|
+
const perms : PermissionIndexType[] = []; var obj: ObjectTreasury | undefined ;
|
|
26
|
+
|
|
27
|
+
if (this?.permission && IsValidAddress(this.permission)) {
|
|
28
|
+
if (this?.object === 'new') {
|
|
29
|
+
perms.push(PermissionIndex.treasury)
|
|
30
|
+
}
|
|
31
|
+
if (this?.permission_new !== undefined) {
|
|
32
|
+
checkOwner = true;
|
|
33
|
+
}
|
|
34
|
+
if (this?.description !== undefined && this.object !== 'new') {
|
|
35
|
+
perms.push(PermissionIndex.treasury_descritption)
|
|
36
|
+
}
|
|
37
|
+
if (this?.withdraw_mode !== undefined) {
|
|
38
|
+
perms.push(PermissionIndex.treasury_withdraw_mode)
|
|
39
|
+
}
|
|
40
|
+
if (this?.withdraw_guard == undefined) { // publish is an irreversible one-time operation
|
|
41
|
+
perms.push(PermissionIndex.treasury_withdraw_guard)
|
|
42
|
+
}
|
|
43
|
+
if (this?.deposit_guard !== undefined) {
|
|
44
|
+
perms.push(PermissionIndex.treasury_deposit_guard)
|
|
45
|
+
}
|
|
46
|
+
if (this?.deposit_guard !== undefined) {
|
|
47
|
+
perms.push(PermissionIndex.treasury_deposit_guard)
|
|
48
|
+
}
|
|
49
|
+
if (this?.deposit?.guard !== undefined) {
|
|
50
|
+
if (IsValidAddress(this.deposit.guard)) {
|
|
51
|
+
guards.push(this.deposit.guard)
|
|
52
|
+
} 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;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
if (obj?.deposit_guard) {
|
|
60
|
+
guards.push(obj?.deposit_guard)
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
if (this?.receive !== undefined) {
|
|
65
|
+
perms.push(PermissionIndex.treasury_receive)
|
|
66
|
+
}
|
|
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 {
|
|
71
|
+
if (!obj) {
|
|
72
|
+
const r = await OBJECT_QUERY.objects({objects:[this.object], showContent:true});
|
|
73
|
+
if (r?.objects && r.objects[0].type === 'Treasury') {
|
|
74
|
+
obj = r.objects[0] as ObjectTreasury;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
if (typeof(obj?.withdraw_guard) === 'string') {
|
|
78
|
+
guards.push(obj?.withdraw_guard)
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
} else {
|
|
82
|
+
perms.push(PermissionIndex.treasury_withdraw)
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
return await this.check_permission_and_call(this.permission, perms, guards, checkOwner, undefined, account)
|
|
86
|
+
}
|
|
87
|
+
return this.exec(account);
|
|
88
|
+
}
|
|
89
|
+
protected async operate (txb:TransactionBlock, passport?:PassportObject) {
|
|
90
|
+
let obj : Treasury | undefined ; let permission: any;
|
|
91
|
+
if (this.object === 'new' && this?.type_parameter) {
|
|
92
|
+
if (!this?.permission || !IsValidAddress(this?.permission)) {
|
|
93
|
+
permission = Permission.New(txb, '');
|
|
94
|
+
}
|
|
95
|
+
obj = Treasury.New(txb, this.type_parameter, permission ?? this?.permission, this?.description??'', permission?undefined:passport)
|
|
96
|
+
} 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)
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
if (obj) {
|
|
103
|
+
if (this?.description !== undefined && this.object !== 'new') {
|
|
104
|
+
obj?.set_description(this.description, passport);
|
|
105
|
+
}
|
|
106
|
+
if (this?.deposit_guard !== undefined) {
|
|
107
|
+
obj?.set_deposit_guard(this.deposit_guard, passport);
|
|
108
|
+
}
|
|
109
|
+
if (this?.withdraw_mode !== undefined) {
|
|
110
|
+
obj?.set_withdraw_mode(this.withdraw_mode, passport)
|
|
111
|
+
}
|
|
112
|
+
if (this?.withdraw_guard !== undefined) {
|
|
113
|
+
switch (this.withdraw_guard.op) {
|
|
114
|
+
case 'add':
|
|
115
|
+
this.withdraw_guard.data.forEach(v => obj?.add_withdraw_guard(v.guard, BigInt(v.amount), passport))
|
|
116
|
+
break;
|
|
117
|
+
case 'remove':
|
|
118
|
+
obj?.remove_withdraw_guard(this.withdraw_guard.guards, false, passport)
|
|
119
|
+
break;
|
|
120
|
+
case 'set':
|
|
121
|
+
obj?.remove_withdraw_guard([], true, passport)
|
|
122
|
+
this.withdraw_guard.data.forEach(v => obj?.add_withdraw_guard(v.guard, BigInt(v.amount), passport))
|
|
123
|
+
break;
|
|
124
|
+
case 'removeall':
|
|
125
|
+
obj?.remove_withdraw_guard([], true, passport)
|
|
126
|
+
break;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
if (this?.withdraw !== undefined) {
|
|
130
|
+
obj?.withdraw(this.withdraw, passport)
|
|
131
|
+
}
|
|
132
|
+
if (this?.receive !== undefined) {
|
|
133
|
+
obj?.receive(this.receive.payment, this.receive.received_object, passport);
|
|
134
|
+
}
|
|
135
|
+
if (this.deposit !== undefined) {
|
|
136
|
+
obj?.deposit(this.deposit.data, passport)
|
|
137
|
+
}
|
|
138
|
+
if (this?.permission_new !== undefined) {
|
|
139
|
+
obj?.change_permission(this.permission_new);
|
|
140
|
+
}
|
|
141
|
+
if (permission) {
|
|
142
|
+
permission.launch();
|
|
143
|
+
}
|
|
144
|
+
if (this.object === 'new') {
|
|
145
|
+
obj?.launch();
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
package/src/events.ts
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
|
|
2
|
+
/**
|
|
3
|
+
* Provide AI with Basic WoWok event queries:
|
|
4
|
+
* for real-time detail tracking.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { Protocol } from 'wowok';
|
|
8
|
+
|
|
9
|
+
export interface EventQueryOption {
|
|
10
|
+
/** optional paging cursor */
|
|
11
|
+
cursor?: {eventSeq: string; txDigest: string} | null | undefined;
|
|
12
|
+
/** maximum number of items per page, default to [QUERY_MAX_RESULT_LIMIT] if not specified. */
|
|
13
|
+
limit?: number | null | undefined;
|
|
14
|
+
/** query result ordering, default to false (ascending order), oldest record first. */
|
|
15
|
+
order?: 'ascending' | 'descending' | null | undefined;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface EventBase {
|
|
19
|
+
id: {eventSeq: string; txDigest: string};
|
|
20
|
+
sender: string;
|
|
21
|
+
type: string | 'NewArbEvent' | 'NewOrderEvent' | 'NewProgressEvent' | 'PresentServiceEvent';
|
|
22
|
+
type_raw: string;
|
|
23
|
+
time: string;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface NewArbEvent extends EventBase {
|
|
27
|
+
arb: string,
|
|
28
|
+
arbitration: string,
|
|
29
|
+
order: string,
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface NewOrderEvent extends EventBase {
|
|
33
|
+
order: string,
|
|
34
|
+
service: string,
|
|
35
|
+
progress?: string | null,
|
|
36
|
+
amount: string,
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface NewProgressEvent extends EventBase {
|
|
40
|
+
progress: string,
|
|
41
|
+
machine: string,
|
|
42
|
+
task?: string | null,
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export interface PresentServiceEvent extends EventBase {
|
|
46
|
+
demand: string,
|
|
47
|
+
service: string,
|
|
48
|
+
recommendation: string,
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export interface EventAnswer {
|
|
52
|
+
data: EventBase[];
|
|
53
|
+
hasNextPage: boolean;
|
|
54
|
+
nextCursor?: {eventSeq: string; txDigest: string} | null;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export namespace EVENT_QUERY {
|
|
58
|
+
export const newArbEvents = async(option?:EventQueryOption) : Promise<EventAnswer> => {
|
|
59
|
+
return await queryEvents(Protocol.Instance().package('wowok') + '::arb::NewArbEvent', option)
|
|
60
|
+
}
|
|
61
|
+
export const presentServiceEvents = async(option?:EventQueryOption) : Promise<EventAnswer> => {
|
|
62
|
+
return await queryEvents(Protocol.Instance().package('wowok') + '::demand::PresentEvent', option)
|
|
63
|
+
}
|
|
64
|
+
export const newProgressEvents = async(option?:EventQueryOption) : Promise<EventAnswer> => {
|
|
65
|
+
return await queryEvents(Protocol.Instance().package('wowok') + '::progress::NewProgressEvent', option)
|
|
66
|
+
}
|
|
67
|
+
export const newOrderEvents = async(option?:EventQueryOption) : Promise<EventAnswer> => {
|
|
68
|
+
return await queryEvents(Protocol.Instance().package('wowok') + '::order::NewOrderEvent', option)
|
|
69
|
+
}
|
|
70
|
+
const queryEvents = async(type:string, option?:EventQueryOption) : Promise<EventAnswer> => {
|
|
71
|
+
const res = await Protocol.Client().queryEvents({query:{MoveEventType:type}, cursor:option?.cursor, limit:option?.limit, order:option?.order});
|
|
72
|
+
const data = res?.data?.map((v:any) => {
|
|
73
|
+
if (v?.packageId === Protocol.Instance().package('wowok')) {
|
|
74
|
+
if (v?.type?.includes('::order::NewOrderEvent')) {
|
|
75
|
+
return {
|
|
76
|
+
id: v?.id, time: v?.timestampMs, type_raw:v?.type, sender:v?.sender, type:'NewOrderEvent',
|
|
77
|
+
order: v?.parsedJson?.object, service: v?.parsedJson?.service, progress: v?.parsedJson?.progress, amount: v?.parsedJson?.amount
|
|
78
|
+
} as NewOrderEvent
|
|
79
|
+
} else if (v?.type?.includes('::demand::PresentEvent')) {
|
|
80
|
+
return {
|
|
81
|
+
id: v?.id, time: v?.timestampMs, type_raw:v?.type, sender:v?.sender, type:'NewOrderEvent',
|
|
82
|
+
demand:v?.parsedJson?.object, service: v?.parsedJson?.service, recommendation:v?.parsedJson?.tips
|
|
83
|
+
} as PresentServiceEvent
|
|
84
|
+
} else if (v?.type?.includes('::progress::NewProgressEvent')) {
|
|
85
|
+
return {
|
|
86
|
+
id: v?.id, time: v?.timestampMs, type_raw:v?.type, sender:v?.sender, type:'NewOrderEvent',
|
|
87
|
+
progress:v?.parsedJson?.object, machine: v?.parsedJson?.machine, task:v?.parsedJson?.task
|
|
88
|
+
} as NewProgressEvent
|
|
89
|
+
} else if (v?.type?.includes('::arb::NewArbEvent')) {
|
|
90
|
+
return {
|
|
91
|
+
id: v?.id, time: v?.timestampMs, type_raw:v?.type, sender:v?.sender, type:'NewOrderEvent',
|
|
92
|
+
arb:v?.parsedJson?.object, arbitration:v?.parsedJson?.arbitration, order:v?.parsedJson?.order
|
|
93
|
+
} as NewArbEvent
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return {id: v?.id, time: v?.timestampMs, type_raw:v?.type, sender:v?.sender, type:'',}
|
|
97
|
+
})
|
|
98
|
+
return {data:data, hasNextPage:res?.hasNextPage, nextCursor:res?.nextCursor}
|
|
99
|
+
}
|
|
100
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export * from './objects'
|
|
2
|
+
export * from './permission'
|
|
3
|
+
export * from './events'
|
|
4
|
+
export * from './cache'
|
|
5
|
+
export * from './call/call'
|
|
6
|
+
export * from './call/permission'
|
|
7
|
+
export * from './call/arbitration'
|
|
8
|
+
export * from './call/treasury'
|
|
9
|
+
export * from './call/permission'
|
|
10
|
+
export * from './call/demand'
|
|
11
|
+
export * from './call/machine'
|
|
12
|
+
export * from './call/repository'
|
|
13
|
+
export * from './call/service'
|