-
Notifications
You must be signed in to change notification settings - Fork 2
/
tasks.js
185 lines (176 loc) · 4.5 KB
/
tasks.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#!/usr/bin/env -S yarn node
const { calculateFee, GasPrice } = require("@cosmjs/stargate");
const { fromBase64, toHex, toUtf8 } = require("@cosmjs/encoding");
const { toBinary } = require("@cosmjs/cosmwasm-stargate");
const utils = require('./utils');
async function main() {
const config = await utils.getChainConfig()
const coinConfig = await utils.getChainCoinConfig(config)
const userWallet = await utils.getLabelledWallet(config, utils.agentnyms[0])
const user = await utils.getAgentClient(config, utils.agentnyms[0])
const managerWallet = await utils.getLabelledWallet(config, utils.catnyms[0])
const managerContract = `${managerWallet.contractAddress}`
const iftttSimpleWallet = await utils.getLabelledWallet(config, utils.catnyms[1])
const iftttSimpleContract = `${iftttSimpleWallet.contractAddress}`
const gasPrice = GasPrice.fromString(`0.025${coinConfig.gas}`)
const fee = calculateFee(450_000, gasPrice)
const memo = `tasks MEOW!`;
const userAddress = `${userWallet.accounts[0].address}`
const sampleActions = [
{
msg: {
wasm: {
execute: {
contract_addr: iftttSimpleContract,
funds: [],
/// msg is the json-encoded ExecuteMsg struct (as raw Binary)
msg: toBinary({ increment: {} }),
}
}
},
gas_limit: 280000,
},
{
msg: {
staking: {
delegate: {
// See template/.wasmd/config/genesis.json
validator: 'wasmvaloper1tjgue6r5kqj5dets24pwaa9u7wuzucpwfsgndk',
amount: {
amount: '100',
denom: coinConfig.gas
},
}
}
},
gas_limit: 280000,
},
{
msg: {
distribution: {
withdraw_delegator_reward: {
validator: 'wasmvaloper1tjgue6r5kqj5dets24pwaa9u7wuzucpwfsgndk',
}
}
},
gas_limit: 280000,
},
]
// TaskRequest {
// pub interval: Interval,
// pub boundary: Boundary,
// pub stop_on_fail: bool,
// pub actions: Vec<Action>,
// pub rules: Option<Vec<Rule>>,
// }
const tasks = [
{
// interval: 'Once',
// interval: 'Immediate',
interval: {
Block: 15
},
// interval: {
// Cron: '*/5 * * * * *'
// },
boundary: {
start: null,
end: null,
},
stop_on_fail: false,
actions: [sampleActions[1]],
// TODO: setup a rules example too
rules: [],
},
{
interval: {
Cron: '*/5 * * * * *'
},
boundary: { start: null, end: null, },
stop_on_fail: false,
actions: [sampleActions[2]],
rules: [],
},
// IFTTT Simple (1 rule)
{
interval: {
Block: 1
},
boundary: { start: null, end: null, },
stop_on_fail: false,
actions: [sampleActions[0]],
// rules: [
// {
// contract_addr: iftttSimpleContract,
// msg: toBinary({ check_modulo: {} }),
// }
// ],
},
// // IFTTT Simple (2 rules)
// {
// interval: {
// Block: 1
// },
// boundary: { start: null, end: null, },
// stop_on_fail: true,
// actions: [sampleActions[0]],
// rules: [
// {
// contract_addr: iftttSimpleContract,
// // msg: Binary,
// msg: {
// CheckModulo: {}
// },
// },
// {
// contract_addr: iftttSimpleContract,
// // msg: Binary,
// msg: {
// CheckInputModulo: {}
// },
// },
// ],
// },
]
// 1. Create several tasks
// CreateTask { task: TaskRequest }
for await (const task of tasks) {
const funds = [{ amount: '450000', denom: coinConfig.gas }]
try {
const r_tx = await user.execute(
userAddress,
managerContract,
{ create_task: { task } },
fee,
memo,
funds,
);
console.log('task create tx hash', r_tx.transactionHash, JSON.stringify(r_tx));
} catch (e) {
console.log('TASK CREATE FAILED', e);
return;
}
}
// 2. Get list of tasks
// GetTasks { }
try {
const q_tx = await user.queryContractSmart(
managerContract,
{ get_tasks: {} },
);
console.log('get tasks', q_tx);
} catch (e) {
console.log('GET FAILED', e);
return;
}
}
main().then(
() => {
console.info("All meows complete.");
process.exit(0);
},
(error) => {
console.error(error);
process.exit(1);
},
);