-
Notifications
You must be signed in to change notification settings - Fork 2
/
blockchain.js
100 lines (86 loc) · 2.98 KB
/
blockchain.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
const { Flow } = require('./flow');
const t = require('@onflow/types');
const DappTransactions = require('./dapp-transactions');
const DappScripts = require('./dapp-scripts');
const CONTRACT = 'access(all) contract Noop {}';
module.exports = class Blockchain {
/**
* @dev Calls a read-only smart contract function
*/
static async get(env, tx, args) {
let options = {
}
if (args) {
options.args = [];
for (let arg in args) {
if (typeof args[arg] === 'String') {
options.args.push({
value: args[arg],
type: t.String
});
} else {
options.args.push(args[arg]);
}
}
}
let flow = new Flow(env.config);
options.decode = true;
let response = await flow.executeTransaction(DappScripts[tx](env.imports), options);
return {
callAccount: null,
callData: response
}
}
/**
* @dev Calls a writeable smart contract function
*/
static async post(env, tx, args) {
let proposer = typeof env.roles.proposer === 'string' ? env.roles.proposer : env.config.accounts[0];
let roleInfo = {
[Flow.Roles.PROPOSER]: proposer,
[Flow.Roles.AUTHORIZERS]: env.roles.authorizers && Array.isArray(env.roles.authorizers) && env.roles.authorizers.length > 0 ? env.roles.authorizers : [proposer],
[Flow.Roles.PAYER]: typeof env.roles.payer === 'string' ? env.roles.payer : proposer
};
let options = {
roleInfo,
gasLimit: 300,
decode: false
}
if (args) {
options.args = [];
for (let arg in args) {
if (typeof args[arg] === 'String') {
options.args.push({
value: args[arg],
type: t.String
});
} else {
options.args.push(args[arg]);
}
}
}
let flow = new Flow(env.config);
let result = await flow.executeTransaction(DappTransactions[tx](env.imports), options);
return {
callAccount: proposer,
callData: result.response,
events: result.events
}
}
static async handleEvent(env, event, callback) {
Flow.handleEvent(env, event, callback);
}
static async createAccount(env, keyInfo) {
/* keyInfo : { entropy: byte array, weight: 1 ... 1000 } */
let flow = new Flow(env);
return await flow.createAccount(keyInfo);
}
static async deployContract(env, address, name, contract) {
let flow = new Flow(env);
return await flow.deployContract(address, name, contract);
}
static async getAccount(env, address) {
let flow = new Flow(env);
return await flow.getAccount(address);
}
}