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,169 @@
|
|
|
1
|
+
import { TransactionBlock, CallResponse} from 'wowok';
|
|
2
|
+
import { PassportObject, IsValidAddress, Errors, ERROR, Permission, PermissionIndex, PermissionIndexType, Treasury,
|
|
3
|
+
Arbitration, Dispute, Feedback, Vote, VotingGuard, WithdrawFee, WitnessFill
|
|
4
|
+
} from 'wowok';
|
|
5
|
+
import { OBJECT_QUERY, ObjectArbitration, } from '../objects';
|
|
6
|
+
import { CallBase } from "./call";
|
|
7
|
+
|
|
8
|
+
export class CallArbitration extends CallBase {
|
|
9
|
+
type_parameter: string;
|
|
10
|
+
permission_new?: string;
|
|
11
|
+
description?: string;
|
|
12
|
+
bPaused?: boolean;
|
|
13
|
+
endpoint?: string;
|
|
14
|
+
fee?: string;
|
|
15
|
+
fee_treasury?: string;
|
|
16
|
+
usage_guard?: string;
|
|
17
|
+
voting_guard?: {op:'add' | 'set'; data:VotingGuard[]} | {op:'remove', guards:string[]} | {op:'removeall'};
|
|
18
|
+
arb_new?: {data: Dispute; guard?:string | 'fetch'}; // dispute an order, and a new Arb launched.
|
|
19
|
+
arb_withdraw_fee?: {arb:string; data:WithdrawFee};
|
|
20
|
+
arb_vote?: Vote;
|
|
21
|
+
arb_arbitration?: Feedback;
|
|
22
|
+
constructor(type_parameter:string, object: string | 'new' = 'new') {
|
|
23
|
+
super(object)
|
|
24
|
+
this.type_parameter = type_parameter;
|
|
25
|
+
}
|
|
26
|
+
async call(account?:string) : Promise<WitnessFill[] | CallResponse | undefined> {
|
|
27
|
+
if (!this.type_parameter) ERROR(Errors.InvalidParam, 'type_parameter')
|
|
28
|
+
|
|
29
|
+
var checkOwner = false; const guards : string[] = [];
|
|
30
|
+
const perms : PermissionIndexType[] = []; var obj: ObjectArbitration | undefined ;
|
|
31
|
+
|
|
32
|
+
if (this?.permission && IsValidAddress(this.permission)) {
|
|
33
|
+
if (this?.object === 'new') {
|
|
34
|
+
perms.push(PermissionIndex.arbitration)
|
|
35
|
+
}
|
|
36
|
+
if (this?.permission_new !== undefined) {
|
|
37
|
+
checkOwner = true;
|
|
38
|
+
}
|
|
39
|
+
if (this?.description !== undefined && this.object !== 'new') {
|
|
40
|
+
perms.push(PermissionIndex.arbitration_description)
|
|
41
|
+
}
|
|
42
|
+
if (this?.bPaused !== undefined) {
|
|
43
|
+
perms.push(PermissionIndex.arbitration_pause)
|
|
44
|
+
}
|
|
45
|
+
if (this?.endpoint == undefined) { // publish is an irreversible one-time operation
|
|
46
|
+
perms.push(PermissionIndex.arbitration_endpoint)
|
|
47
|
+
}
|
|
48
|
+
if (this?.fee !== undefined && this.object !== 'new') {
|
|
49
|
+
perms.push(PermissionIndex.arbitration_fee)
|
|
50
|
+
}
|
|
51
|
+
if (this?.fee_treasury !== undefined && this.object !== 'new') {
|
|
52
|
+
perms.push(PermissionIndex.arbitration_treasury)
|
|
53
|
+
}
|
|
54
|
+
if (this?.usage_guard !== undefined) {
|
|
55
|
+
perms.push(PermissionIndex.arbitration_guard)
|
|
56
|
+
}
|
|
57
|
+
if (this?.voting_guard !== undefined) {
|
|
58
|
+
perms.push(PermissionIndex.arbitration_voting_guard)
|
|
59
|
+
}
|
|
60
|
+
if (this?.arb_arbitration !== undefined) {
|
|
61
|
+
perms.push(PermissionIndex.arbitration_arbitration)
|
|
62
|
+
}
|
|
63
|
+
if (this?.arb_new?.guard !== undefined) {
|
|
64
|
+
if (IsValidAddress(this.arb_new.guard)) {
|
|
65
|
+
guards.push(this.arb_new.guard)
|
|
66
|
+
} else {
|
|
67
|
+
if (!obj) {
|
|
68
|
+
const r = await OBJECT_QUERY.objects({objects:[this.object], showContent:true});
|
|
69
|
+
if (r?.objects && r.objects[0].type === 'Treasury') {
|
|
70
|
+
obj = r.objects[0] as ObjectArbitration;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
if (obj?.usage_guard) {
|
|
74
|
+
guards.push(obj?.usage_guard)
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
if (this?.arb_vote !== undefined) {
|
|
79
|
+
perms.push(PermissionIndex.treasury_receive)
|
|
80
|
+
}
|
|
81
|
+
if (typeof(this?.arb_vote?.voting_guard) === 'string' && IsValidAddress(this?.arb_vote?.voting_guard)) {
|
|
82
|
+
guards.push(this?.arb_vote?.voting_guard)
|
|
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 : Arbitration | undefined ; let permission: any; let withdraw_treasury:any;
|
|
91
|
+
if (this.object === 'new' && this?.type_parameter) {
|
|
92
|
+
if (!this?.permission || !IsValidAddress(this?.permission)) {
|
|
93
|
+
permission = Permission.New(txb, '');
|
|
94
|
+
}
|
|
95
|
+
if (!this?.fee_treasury || !IsValidAddress(this?.fee_treasury)) {
|
|
96
|
+
withdraw_treasury = Treasury.New(txb, this?.type_parameter, permission ?? this?.permission, '', permission?undefined:passport);
|
|
97
|
+
}
|
|
98
|
+
obj = Arbitration.New(txb, this.type_parameter, permission ?? this?.permission, this?.description??'',
|
|
99
|
+
BigInt(this?.fee ?? 0), withdraw_treasury??this.fee_treasury, permission?undefined:passport);
|
|
100
|
+
} else {
|
|
101
|
+
if (IsValidAddress(this.object) && this.type_parameter && this.permission && IsValidAddress(this?.permission)) {
|
|
102
|
+
obj = Arbitration.From(txb, this.type_parameter, this.permission, this.object)
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
if (obj) {
|
|
107
|
+
if (this?.description !== undefined && this.object !== 'new') {
|
|
108
|
+
obj?.set_description(this.description, passport);
|
|
109
|
+
}
|
|
110
|
+
if (this?.bPaused !== undefined) {
|
|
111
|
+
obj?.pause(this.bPaused, passport);
|
|
112
|
+
}
|
|
113
|
+
if (this?.endpoint !== undefined) {
|
|
114
|
+
obj?.set_endpoint(this.endpoint, passport)
|
|
115
|
+
}
|
|
116
|
+
if (this?.fee !== undefined && this.object !== 'new') {
|
|
117
|
+
obj?.set_fee(BigInt(this.fee), passport)
|
|
118
|
+
}
|
|
119
|
+
if (this.fee_treasury !== undefined && this.object !== 'new') {
|
|
120
|
+
obj?.set_withdrawTreasury(this.fee_treasury, passport)
|
|
121
|
+
}
|
|
122
|
+
if (this.usage_guard !== undefined) {
|
|
123
|
+
obj?.set_guard(this.usage_guard, passport)
|
|
124
|
+
}
|
|
125
|
+
if (this?.voting_guard !== undefined) {
|
|
126
|
+
switch (this.voting_guard.op) {
|
|
127
|
+
case 'add':
|
|
128
|
+
obj?.add_voting_guard(this.voting_guard.data, passport)
|
|
129
|
+
break;
|
|
130
|
+
case 'remove':
|
|
131
|
+
obj?.remove_voting_guard(this.voting_guard.guards, false, passport)
|
|
132
|
+
break;
|
|
133
|
+
case 'set':
|
|
134
|
+
obj?.remove_voting_guard([], true, passport)
|
|
135
|
+
obj?.add_voting_guard(this.voting_guard.data, passport)
|
|
136
|
+
break;
|
|
137
|
+
case 'removeall':
|
|
138
|
+
obj?.remove_voting_guard([], true, passport)
|
|
139
|
+
break;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
if (this?.arb_new !== undefined) {
|
|
144
|
+
obj?.dispute(this.arb_new.data, passport)
|
|
145
|
+
}
|
|
146
|
+
if (this?.arb_arbitration !== undefined) {
|
|
147
|
+
obj?.arbitration(this.arb_arbitration, passport)
|
|
148
|
+
}
|
|
149
|
+
if (this?.arb_vote !== undefined) {
|
|
150
|
+
obj?.vote(this.arb_vote, passport)
|
|
151
|
+
}
|
|
152
|
+
if (this?.arb_withdraw_fee !== undefined) {
|
|
153
|
+
obj?.withdraw_fee(this.arb_withdraw_fee.arb, this.arb_withdraw_fee.data, passport)
|
|
154
|
+
}
|
|
155
|
+
if (this?.permission_new !== undefined) {
|
|
156
|
+
obj?.change_permission(this.permission_new);
|
|
157
|
+
}
|
|
158
|
+
if (withdraw_treasury) {
|
|
159
|
+
withdraw_treasury.launch();
|
|
160
|
+
}
|
|
161
|
+
if (permission) {
|
|
162
|
+
permission.launch();
|
|
163
|
+
}
|
|
164
|
+
if (this.object === 'new') {
|
|
165
|
+
obj?.launch();
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
package/src/call/call.ts
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Provide a this interface for AI
|
|
3
|
+
* Operation sequence Priority: common operation > Guard change > permission change
|
|
4
|
+
* Recommended: Changes to guard and permission are committed on-chain separately to avoid permission dependencies for other operations.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { Protocol, TransactionBlock, CallResponse} from 'wowok';
|
|
8
|
+
import { PassportObject, Errors, ERROR, Permission,
|
|
9
|
+
PermissionIndexType, GuardParser, Passport, WitnessFill
|
|
10
|
+
} from 'wowok';
|
|
11
|
+
import { PERMISSION_QUERY } from '../permission';
|
|
12
|
+
import { Account } from '../account';
|
|
13
|
+
import { ObjectBase } from '../objects';
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
export interface ResponseData extends ObjectBase {
|
|
17
|
+
change:'created' | 'mutated' | string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export class CallBase {
|
|
21
|
+
object: string | 'new' ;
|
|
22
|
+
permission?: string;
|
|
23
|
+
constructor(object: string | 'new' = 'new') {
|
|
24
|
+
this.object = object;
|
|
25
|
+
}
|
|
26
|
+
// operation implementation for a call
|
|
27
|
+
protected async operate(txb:TransactionBlock, passport?:PassportObject) {};
|
|
28
|
+
|
|
29
|
+
// return WitnessFill to resolve filling witness, and than 'call_with_witness' to complete the call;
|
|
30
|
+
// return ResponseData when the call has completed;
|
|
31
|
+
// throw an exception when errors.
|
|
32
|
+
async call(account?:string) : Promise<WitnessFill[] | CallResponse | undefined> { return undefined };
|
|
33
|
+
async call_with_witness (guards: string[], fill?: WitnessFill[], account?:string) : Promise<CallResponse | undefined> {
|
|
34
|
+
if (guards.length > 0) { // prepare passport
|
|
35
|
+
const p: GuardParser | undefined = await GuardParser.Create([...guards]);
|
|
36
|
+
const pair = Account.Instance().get_pair(account, true);
|
|
37
|
+
if (!pair) ERROR(Errors.Fail, 'account invalid')
|
|
38
|
+
|
|
39
|
+
if (p) {
|
|
40
|
+
const query = await p.done(fill);
|
|
41
|
+
if (query) {
|
|
42
|
+
const txb = new TransactionBlock();
|
|
43
|
+
const passport = new Passport(txb, query!);
|
|
44
|
+
this.operate(new TransactionBlock(), passport?.get_object())
|
|
45
|
+
passport.destroy();
|
|
46
|
+
|
|
47
|
+
return await Protocol.Client().signAndExecuteTransaction({
|
|
48
|
+
transaction: txb,
|
|
49
|
+
signer: pair!,
|
|
50
|
+
options:{showObjectChanges:true},
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
} else {
|
|
54
|
+
ERROR(Errors.Fail, 'guard finish_passport')
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
protected async check_permission_and_call (permission:string, permIndex: PermissionIndexType[], guards_needed: string[],
|
|
60
|
+
checkOwner?:boolean, checkAdmin?:boolean, account?:string) : Promise<WitnessFill[] | CallResponse> {
|
|
61
|
+
var guards : string[] = [];
|
|
62
|
+
const pair = Account.Instance().get_pair(account, true);
|
|
63
|
+
if (!pair) ERROR(Errors.Fail, 'account invalid')
|
|
64
|
+
|
|
65
|
+
if (permIndex.length > 0 || checkOwner) {
|
|
66
|
+
const p = await PERMISSION_QUERY.permission({permission_object:permission, address:pair!.toSuiAddress()});
|
|
67
|
+
if (checkOwner && !p.owner) ERROR(Errors.noPermission, 'owner');
|
|
68
|
+
if (checkAdmin && !p.admin) ERROR(Errors.noPermission, 'admin');
|
|
69
|
+
|
|
70
|
+
permIndex.forEach(v => {
|
|
71
|
+
const r = Permission.HasPermission(p, v);
|
|
72
|
+
if (!r?.has) ERROR(Errors.noPermission, v);
|
|
73
|
+
if (r?.guard) guards.push(r.guard)
|
|
74
|
+
})
|
|
75
|
+
}
|
|
76
|
+
if (guards_needed.length > 0) {
|
|
77
|
+
guards = guards.concat(guards_needed);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
if (guards.length > 0) { // prepare passport
|
|
81
|
+
const p: GuardParser | undefined = await GuardParser.Create([...guards]);
|
|
82
|
+
const futures = p ? p.future_fills() : [];
|
|
83
|
+
|
|
84
|
+
if (!p) ERROR(Errors.Fail, 'guard parse')
|
|
85
|
+
|
|
86
|
+
if (p && futures.length === 0) {
|
|
87
|
+
const query = await p.done();
|
|
88
|
+
if (query) {
|
|
89
|
+
const txb = new TransactionBlock();
|
|
90
|
+
const passport = new Passport(txb, query!);
|
|
91
|
+
this.operate(new TransactionBlock(), passport?.get_object())
|
|
92
|
+
passport.destroy();
|
|
93
|
+
|
|
94
|
+
return await Protocol.Client().signAndExecuteTransaction({
|
|
95
|
+
transaction: txb,
|
|
96
|
+
signer: pair!,
|
|
97
|
+
options:{showObjectChanges:true},
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
return p!.future_fills();
|
|
103
|
+
} else { // no passport needed
|
|
104
|
+
return this.exec()
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
protected async exec (account?:string) : Promise<CallResponse> {
|
|
108
|
+
const pair = Account.Instance().get_pair(account, true);
|
|
109
|
+
if (!pair) ERROR(Errors.Fail, 'account invalid')
|
|
110
|
+
|
|
111
|
+
const txb = new TransactionBlock();
|
|
112
|
+
this.operate(txb);
|
|
113
|
+
return await Protocol.Client().signAndExecuteTransaction({
|
|
114
|
+
transaction: txb,
|
|
115
|
+
signer: pair!,
|
|
116
|
+
options:{showObjectChanges:true},
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
static ResponseData(response: CallResponse | undefined ) : ResponseData[] {
|
|
121
|
+
const res : ResponseData[] = [];
|
|
122
|
+
response?.objectChanges?.forEach(v => {
|
|
123
|
+
const type_raw: string | undefined = (v as any)?.objectType;
|
|
124
|
+
const type:string | undefined = type_raw ? Protocol.Instance().object_name_from_type_repr(type_raw) : undefined;
|
|
125
|
+
if (type) {
|
|
126
|
+
res.push({type:type, type_raw:type_raw, object:(v as any)?.objectId, version:(v as any)?.version,
|
|
127
|
+
owner:(v as any)?.owner, change:v.type
|
|
128
|
+
})
|
|
129
|
+
}
|
|
130
|
+
})
|
|
131
|
+
return res;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { TransactionBlock, CallResponse} from 'wowok';
|
|
2
|
+
import { PassportObject, IsValidAddress, Errors, ERROR, Permission, Permission_Entity, Permission_Index, PermissionIndex, UserDefinedIndex,
|
|
3
|
+
PermissionIndexType, Demand, WitnessFill
|
|
4
|
+
} from 'wowok';
|
|
5
|
+
import { OBJECT_QUERY, ObjectDemand } from '../objects';
|
|
6
|
+
import { CallBase } from "./call";
|
|
7
|
+
|
|
8
|
+
export class CallDemand extends CallBase {
|
|
9
|
+
permission_new?: string;
|
|
10
|
+
type_parameter: string;
|
|
11
|
+
guard?: {address:string; service_id_in_guard?:number};
|
|
12
|
+
description?: string;
|
|
13
|
+
time_expire?: {op: 'duration'; minutes:number} | {op:'set'; time:number};
|
|
14
|
+
bounty?: {op:'add'; object?:string; balance:string} | {op:'refund'} | {op:'reward'; service:string};
|
|
15
|
+
present?: {service: string | number; recommend_words:string; service_pay_type:string, guard?:string | 'fetch'}; // guard is the present guard of Demand
|
|
16
|
+
reward?: string; // rerward the service
|
|
17
|
+
refund?: boolean;
|
|
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[] = [];
|
|
26
|
+
|
|
27
|
+
if (this?.permission && IsValidAddress(this.permission)) {
|
|
28
|
+
if (this?.object === 'new') {
|
|
29
|
+
perms.push(PermissionIndex.demand)
|
|
30
|
+
}
|
|
31
|
+
if (this?.permission_new !== undefined) {
|
|
32
|
+
checkOwner = true;
|
|
33
|
+
}
|
|
34
|
+
if (this?.description !== undefined && this.object !== 'new') {
|
|
35
|
+
perms.push(PermissionIndex.demand_description)
|
|
36
|
+
}
|
|
37
|
+
if (this?.time_expire !== undefined && this.object !== 'new') {
|
|
38
|
+
perms.push(PermissionIndex.demand_expand_time)
|
|
39
|
+
}
|
|
40
|
+
if (this?.guard !== undefined) {
|
|
41
|
+
perms.push(PermissionIndex.demand_guard)
|
|
42
|
+
}
|
|
43
|
+
if (this?.reward !== undefined) {
|
|
44
|
+
perms.push(PermissionIndex.demand_yes)
|
|
45
|
+
}
|
|
46
|
+
if (this?.refund) {
|
|
47
|
+
perms.push(PermissionIndex.demand_refund)
|
|
48
|
+
}
|
|
49
|
+
if (this?.present?.guard !== undefined) {
|
|
50
|
+
if (IsValidAddress(this.present.guard)) {
|
|
51
|
+
guards.push(this.present.guard)
|
|
52
|
+
} else if (IsValidAddress(this?.object)) {
|
|
53
|
+
const r = await OBJECT_QUERY.objects({objects:[this.object], showContent:true});
|
|
54
|
+
if (r?.objects && r?.objects[0]?.type === 'Demand') {
|
|
55
|
+
const obj = (r?.objects[0] as ObjectDemand);
|
|
56
|
+
if (obj?.guard) {
|
|
57
|
+
guards.push(obj?.guard.object);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return await this.check_permission_and_call(this.permission, perms, guards, checkOwner, undefined, account)
|
|
63
|
+
}
|
|
64
|
+
return this.exec(account);
|
|
65
|
+
}
|
|
66
|
+
protected async operate(txb:TransactionBlock, passport?:PassportObject) {
|
|
67
|
+
let obj : Demand | undefined ; let permission: any;
|
|
68
|
+
|
|
69
|
+
if (this.object === 'new') {
|
|
70
|
+
if (!this?.permission || !IsValidAddress(this?.permission)) {
|
|
71
|
+
permission = Permission.New(txb, '');
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
if (this.time_expire !== undefined) {
|
|
75
|
+
obj = Demand.New(txb, this.type_parameter, this.time_expire?.op === 'duration' ? true : false,
|
|
76
|
+
this.time_expire?.op === 'duration' ? this.time_expire.minutes : this.time_expire?.time,
|
|
77
|
+
permission ? permission.get_object(): this?.permission, this?.description??'', permission?undefined:passport)
|
|
78
|
+
} else {
|
|
79
|
+
obj = Demand.New(txb, this.type_parameter, true, 30*24*60, // 30days default
|
|
80
|
+
permission ? permission.get_object(): this?.permission, this?.description??'', permission?undefined:passport)
|
|
81
|
+
}
|
|
82
|
+
} else {
|
|
83
|
+
if (IsValidAddress(this.object) && this.type_parameter && this.permission && IsValidAddress(this?.permission)) {
|
|
84
|
+
obj = Demand.From(txb, this.type_parameter, this.permission, this.object)
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
if (obj) {
|
|
89
|
+
if (this?.description !== undefined && this.object !== 'new') {
|
|
90
|
+
obj?.set_description(this.description, passport);
|
|
91
|
+
}
|
|
92
|
+
if (this?.time_expire !== undefined && this.object !== 'new') {
|
|
93
|
+
obj?.expand_time(this.time_expire.op === 'duration' ? true : false,
|
|
94
|
+
this.time_expire.op === 'duration' ? this.time_expire.minutes : this.time_expire.time, passport)
|
|
95
|
+
}
|
|
96
|
+
if (this?.bounty !== undefined) {
|
|
97
|
+
if (this.bounty.op === 'add') {
|
|
98
|
+
let deposit : any | undefined; let b = BigInt(this.bounty.balance);
|
|
99
|
+
if (b > BigInt(0)) {
|
|
100
|
+
if (this.type_parameter === '0x2::sui::SUI' || this.type_parameter === '0x0000000000000000000000000000000000000000000000000000000000000002::sui::SUI') {
|
|
101
|
+
deposit = txb.splitCoins(txb.gas, [b])[0];
|
|
102
|
+
} else if (this?.bounty?.object) {
|
|
103
|
+
deposit = txb.splitCoins(this.bounty.object, [b])[0];
|
|
104
|
+
}
|
|
105
|
+
if (deposit) {
|
|
106
|
+
obj?.deposit(deposit);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
} else if (this.bounty.op === 'refund') {
|
|
110
|
+
obj?.refund(passport);
|
|
111
|
+
} else if (this.bounty.op === 'reward') {
|
|
112
|
+
obj?.yes(this.bounty.service, passport);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
if (this?.present !== undefined) {
|
|
116
|
+
//@ demand guard and its passport, if set
|
|
117
|
+
obj?.present(this.present.service, this.present.service_pay_type, this.present.recommend_words, passport);
|
|
118
|
+
}
|
|
119
|
+
if (this?.guard !== undefined) {
|
|
120
|
+
obj?.set_guard(this.guard.address, this.guard?.service_id_in_guard ?? undefined, passport)
|
|
121
|
+
}
|
|
122
|
+
if (this?.permission_new !== undefined ) {
|
|
123
|
+
obj?.change_permission(this.permission_new);
|
|
124
|
+
}
|
|
125
|
+
if (permission) {
|
|
126
|
+
permission.launch();
|
|
127
|
+
}
|
|
128
|
+
if (this.object === 'new') {
|
|
129
|
+
obj?.launch();
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
import { TransactionBlock, CallResponse} from 'wowok';
|
|
2
|
+
import { PassportObject, IsValidAddress, Errors, ERROR, Permission, PermissionIndex, WitnessFill,
|
|
3
|
+
PermissionIndexType, Machine, Machine_Forward, Machine_Node, Deliverable, ParentProgress, Progress, ProgressNext,
|
|
4
|
+
} from 'wowok';
|
|
5
|
+
import { CallBase } from "./call";
|
|
6
|
+
import { Account } from '../account';
|
|
7
|
+
|
|
8
|
+
export class CallMachine extends CallBase { //@ todo self-owned node operate
|
|
9
|
+
permission_new?: string;
|
|
10
|
+
bPaused?: boolean;
|
|
11
|
+
bPublished?: boolean;
|
|
12
|
+
consensus_repository?: {op:'set' | 'add' | 'remove' ; repositories:string[]} | {op:'removeall'};
|
|
13
|
+
description?: string;
|
|
14
|
+
endpoint?: string;
|
|
15
|
+
clone_new?: boolean;
|
|
16
|
+
nodes?: {op: 'add'; data: Machine_Node[]} | {op: 'remove'; names: string[], bTransferMyself?:boolean}
|
|
17
|
+
| {op:'rename node'; data:{old:string; new:string}[]} | {op:'add from myself'; addresses: string[]}
|
|
18
|
+
| {op:'remove pair'; pairs: {prior_node_name:string; node_name:string}[]}
|
|
19
|
+
| {op:'add forward'; data: {prior_node_name:string; node_name:string; forward:Machine_Forward; threshold?:number; old_need_remove?:string}[]}
|
|
20
|
+
| {op:'remove forward'; data:{prior_node_name:string; node_name:string; forward_name:string}[]}
|
|
21
|
+
progress_new?: {task_address?:string; };
|
|
22
|
+
progress_context_repository?: {progress:string; repository:string};
|
|
23
|
+
progress_parent?: {progress:string, parent?:ParentProgress};
|
|
24
|
+
progress_task?: {progress:string; task:string};
|
|
25
|
+
progress_namedOperator?: {progress:string; data:{name:string, operator:string[]}[]};
|
|
26
|
+
progress_hold?: {progress:string; data:ProgressNext; bHold:boolean; adminUnhold?:boolean};
|
|
27
|
+
progress_next?: {progress:string; data:ProgressNext; deliverable:Deliverable; guard?:string | 'fetch'};
|
|
28
|
+
constructor(object: string | 'new' = 'new') { super(object) }
|
|
29
|
+
async call(account?:string) : Promise<WitnessFill[] | CallResponse | undefined> {
|
|
30
|
+
var checkOwner = false; const guards : string[] = [];
|
|
31
|
+
const perms : PermissionIndexType[] = [];
|
|
32
|
+
|
|
33
|
+
if (this?.permission && IsValidAddress(this.permission)) {
|
|
34
|
+
if (this?.object === 'new') {
|
|
35
|
+
perms.push(PermissionIndex.machine)
|
|
36
|
+
}
|
|
37
|
+
if (this?.permission_new !== undefined) {
|
|
38
|
+
checkOwner = true;
|
|
39
|
+
}
|
|
40
|
+
if (this?.description !== undefined && this.object !== 'new') {
|
|
41
|
+
perms.push(PermissionIndex.machine_description)
|
|
42
|
+
}
|
|
43
|
+
if (this?.bPaused !== undefined) {
|
|
44
|
+
perms.push(PermissionIndex.machine_pause)
|
|
45
|
+
}
|
|
46
|
+
if (this?.bPublished) { // publish is an irreversible one-time operation
|
|
47
|
+
perms.push(PermissionIndex.machine_publish)
|
|
48
|
+
}
|
|
49
|
+
if (this?.endpoint !== undefined && this.object !== 'new') {
|
|
50
|
+
perms.push(PermissionIndex.machine_endpoint)
|
|
51
|
+
}
|
|
52
|
+
if (this?.consensus_repository !== undefined) {
|
|
53
|
+
perms.push(PermissionIndex.machine_repository)
|
|
54
|
+
}
|
|
55
|
+
if (this?.clone_new !== undefined) {
|
|
56
|
+
perms.push(PermissionIndex.machine_clone)
|
|
57
|
+
}
|
|
58
|
+
if (this?.nodes !== undefined) {
|
|
59
|
+
perms.push(PermissionIndex.machine_node)
|
|
60
|
+
}
|
|
61
|
+
if (this?.progress_new !== undefined) {
|
|
62
|
+
perms.push(PermissionIndex.progress)
|
|
63
|
+
}
|
|
64
|
+
if (this?.progress_context_repository !== undefined) {
|
|
65
|
+
perms.push(PermissionIndex.progress_context_repository)
|
|
66
|
+
}
|
|
67
|
+
if (this?.progress_namedOperator !== undefined) {
|
|
68
|
+
perms.push(PermissionIndex.progress_namedOperator)
|
|
69
|
+
}
|
|
70
|
+
if (this?.progress_parent !== undefined) {
|
|
71
|
+
perms.push(PermissionIndex.progress_parent)
|
|
72
|
+
}
|
|
73
|
+
if (this?.progress_task !== undefined) {
|
|
74
|
+
perms.push(PermissionIndex.progress_bind_task)
|
|
75
|
+
}
|
|
76
|
+
if (this?.progress_hold !== undefined) {
|
|
77
|
+
if (this.progress_hold.adminUnhold) {
|
|
78
|
+
perms.push(PermissionIndex.progress_unhold)
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
if (this?.progress_next?.guard !== undefined) {
|
|
82
|
+
if (IsValidAddress(this?.progress_next?.guard)) {
|
|
83
|
+
guards.push(this?.progress_next?.guard)
|
|
84
|
+
} else if (this?.object && IsValidAddress(this.object)) { // fetch guard
|
|
85
|
+
const guard = await Progress.QueryForwardGuard(this?.progress_next.progress, this.object,
|
|
86
|
+
Account.Instance().get_address() ?? '0xe386bb9e01b3528b75f3751ad8a1e418b207ad979fea364087deef5250a73d3f',
|
|
87
|
+
this.progress_next.data.next_node_name, this.progress_next.data.forward);
|
|
88
|
+
if (guard) {
|
|
89
|
+
guards.push(guard)
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
return await this.check_permission_and_call(this.permission, perms, guards, checkOwner, undefined, account)
|
|
95
|
+
}
|
|
96
|
+
return this.exec(account);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
protected async operate(txb:TransactionBlock, passport?:PassportObject) {
|
|
100
|
+
let obj : Machine | undefined ; let permission: any;
|
|
101
|
+
if (this.object === 'new') {
|
|
102
|
+
if (!this?.permission || !IsValidAddress(this?.permission)) {
|
|
103
|
+
permission = Permission.New(txb, '');
|
|
104
|
+
}
|
|
105
|
+
obj = Machine.New(txb, permission ?? this?.permission, this?.description??'', this?.endpoint ?? '', permission?undefined:passport);
|
|
106
|
+
} else {
|
|
107
|
+
if (IsValidAddress(this.object) && this.permission && IsValidAddress(this?.permission)) {
|
|
108
|
+
obj = Machine.From(txb, this.permission, this.object)
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
if (obj) {
|
|
113
|
+
if (this?.description !== undefined && this.object !== 'new') {
|
|
114
|
+
obj?.set_description(this.description, passport);
|
|
115
|
+
}
|
|
116
|
+
if (this?.endpoint !== undefined && this.object !== 'new') {
|
|
117
|
+
obj?.set_endpoint(this.endpoint, passport)
|
|
118
|
+
}
|
|
119
|
+
if (this?.bPaused !== undefined) {
|
|
120
|
+
obj?.pause(this.bPaused, passport)
|
|
121
|
+
}
|
|
122
|
+
if (this?.bPublished ) {
|
|
123
|
+
obj?.publish(passport)
|
|
124
|
+
}
|
|
125
|
+
if (this?.clone_new) {
|
|
126
|
+
obj?.clone(true, passport)
|
|
127
|
+
}
|
|
128
|
+
if (this?.consensus_repository !== undefined) {
|
|
129
|
+
switch (this.consensus_repository.op) {
|
|
130
|
+
case 'add':
|
|
131
|
+
this.consensus_repository.repositories.forEach(v=>obj?.add_repository(v, passport)) ;
|
|
132
|
+
break;
|
|
133
|
+
case 'remove':
|
|
134
|
+
obj?.remove_repository(this.consensus_repository.repositories, false, passport);
|
|
135
|
+
break;
|
|
136
|
+
case 'removeall':
|
|
137
|
+
obj?.remove_repository([], true, passport);
|
|
138
|
+
break;
|
|
139
|
+
case 'set':
|
|
140
|
+
obj?.remove_repository([], true, passport);
|
|
141
|
+
this.consensus_repository.repositories.forEach(v=>obj?.add_repository(v, passport)) ;
|
|
142
|
+
break;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
if (this?.nodes !== undefined) {
|
|
146
|
+
switch (this?.nodes?.op) {
|
|
147
|
+
case 'add':
|
|
148
|
+
obj?.add_node(this.nodes.data, passport)
|
|
149
|
+
break;
|
|
150
|
+
case 'remove':
|
|
151
|
+
obj?.remove_node(this.nodes.names, this.nodes?.bTransferMyself, passport)
|
|
152
|
+
break;
|
|
153
|
+
case 'rename node':
|
|
154
|
+
this.nodes.data.forEach(v => obj?.rename_node(v.old, v.new, passport));
|
|
155
|
+
break;
|
|
156
|
+
case 'add from myself':
|
|
157
|
+
obj?.add_node2(this.nodes.addresses, passport);
|
|
158
|
+
break;
|
|
159
|
+
case 'add forward':
|
|
160
|
+
this.nodes.data.forEach(v => obj?.add_forward(v.prior_node_name, v.node_name, v.forward, v.threshold, v.old_need_remove, passport))
|
|
161
|
+
break;
|
|
162
|
+
case 'remove forward':
|
|
163
|
+
this.nodes.data.forEach(v => obj?.remove_forward(v.prior_node_name, v.node_name, v.forward_name, passport))
|
|
164
|
+
break;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
if (this?.progress_new !== undefined) {
|
|
168
|
+
Progress?.New(txb, this?.object, permission??this?.permission, this?.progress_new.task_address, passport).launch();
|
|
169
|
+
}
|
|
170
|
+
if (this?.progress_context_repository !== undefined) {
|
|
171
|
+
Progress.From(txb, this?.object, permission??this?.permission, this?.progress_context_repository.progress)
|
|
172
|
+
.set_context_repository(this?.progress_context_repository.repository, passport)
|
|
173
|
+
}
|
|
174
|
+
if (this?.progress_namedOperator !== undefined) {
|
|
175
|
+
const p = Progress.From(txb, this?.object, permission??this?.permission, this?.progress_namedOperator.progress);
|
|
176
|
+
this.progress_namedOperator.data.forEach(v => p.set_namedOperator(v.name, v.operator, passport));
|
|
177
|
+
}
|
|
178
|
+
if (this?.progress_parent !== undefined) {
|
|
179
|
+
if (this.progress_parent.parent) {
|
|
180
|
+
Progress.From(txb, this?.object, permission??this?.permission, this?.progress_parent.progress).parent(this.progress_parent.parent);
|
|
181
|
+
} else {
|
|
182
|
+
Progress.From(txb, this?.object, permission??this?.permission, this?.progress_parent.progress).parent_none();
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
if (this?.progress_task !== undefined) {
|
|
186
|
+
Progress.From(txb, this?.object, permission??this?.permission, this?.progress_task.progress).bind_task(this.progress_task.task, passport)
|
|
187
|
+
}
|
|
188
|
+
if (this?.progress_hold !== undefined) {
|
|
189
|
+
if (this?.progress_hold.adminUnhold) {
|
|
190
|
+
Progress.From(txb, this?.object, permission??this?.permission, this?.progress_hold.progress).unhold(this.progress_hold.data, passport)
|
|
191
|
+
} else {
|
|
192
|
+
Progress.From(txb, this?.object, permission??this?.permission, this?.progress_hold.progress).hold(this.progress_hold.data, this.progress_hold.bHold)
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
if (this?.progress_next !== undefined) {
|
|
196
|
+
Progress.From(txb, this?.object, permission??this?.permission, this?.progress_next.progress).next(this.progress_next.data, this.progress_next.deliverable, passport)
|
|
197
|
+
}
|
|
198
|
+
if (this?.permission_new !== undefined ) {
|
|
199
|
+
obj?.change_permission(this.permission_new);
|
|
200
|
+
}
|
|
201
|
+
if (permission) {
|
|
202
|
+
permission.launch();
|
|
203
|
+
}
|
|
204
|
+
if (this.object === 'new') {
|
|
205
|
+
obj?.launch();
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
}
|