-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathblockchain.provider.web3.js
128 lines (114 loc) Β· 5.25 KB
/
blockchain.provider.web3.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
var configuration = require('./configuration.json');
module.exports = function Web3BlockchainProvider(engine) {
var context = this;
context.attach = function attach(dfoHub) {
engine.eth.dfoHub = dfoHub;
};
context.getNetworkId = function getNetworkId() {
return engine.eth.net.getId();
};
context.sha3 = function sha3(data) {
return engine.utils.sha3(data);
};
context.newContract = function newContract(abi, address) {
var contract = new engine.eth.Contract(abi, address);
contract.transactionBlockTimeout = 999999999;
contract.transactionPollingTimeout = new Date().getTime();
return contract;
};
context.getPastLogs = function getPastLogs(params) {
return engine.eth.getPastLogs(params);
};
context.encodeAbi = function encodeAbi(type, value) {
return engine.eth.abi["encodeParameter" + ((typeof type).toLowerCase() === 'string' ? '' : 's')](type, value);
};
context.decodeAbi = function decodeAbi(type, value) {
return engine.eth.abi["decodeParameter" + ((typeof type).toLowerCase() === 'string' ? '' : 's')](type, value);
};
context.callContract = function callContract(contract, methodName) {
var value = undefined;
var start = 2;
if(contract === undefined || contract === null || (typeof contract).toLowerCase() === 'number' || (typeof contract).toLowerCase() === 'string') {
start = 3;
value = contract;
contract = methodName;
methodName = arguments[2];
}
var args = [value, contract.methods[methodName]];
for(var i = start; i < arguments.length; i++) {
args.push(arguments[i]);
}
return blockchainCall.apply(context, args);
};
context.simpleCall = function simpleCall(contract, methodName) {
var args = [];
for(var i = 2; i < arguments.length; i++) {
args.push(arguments[i]);
}
return engine.eth.call({
to: contract.options.address,
data: contract.methods[methodName].apply(contract, args).encodeABI()
});
}
var blockchainCall = async function blockchainCall(value, call) {
var args = [];
for (var i = 2; i < arguments.length; i++) {
args.push(arguments[i]);
}
var method = (call.implementation ? call.get : call.new ? call.new : call).apply(call, args);
return await (method._method.stateMutability === 'view' || method._method.stateMutability === 'pure' || method._method.constant ? method.call(await getSendingOptions()) : sendBlockchainTransaction(value, method));
};
var sendBlockchainTransaction = function sendBlockchainTransaction(value, transaction) {
return new Promise(async function(ok, ko) {
var handleTransactionError = function handleTransactionError(e) {
e !== undefined && e !== null && (e.message || e).indexOf('not mined within') === -1 && ko(e);
}
try {
(transaction = transaction.send ? transaction.send(await getSendingOptions(transaction, value), handleTransactionError) : transaction).on('transactionHash', transactionHash => {
var timeout = async function() {
var receipt = await engine.eth.getTransactionReceipt(transactionHash);
if (!receipt || !receipt.blockNumber || parseInt(await engine.eth.getBlockNumber()) < (parseInt(receipt.blockNumber) + (configuration.transactionConfirmations || 0))) {
return global.setTimeout(timeout, configuration.transactionConfirmationsTimeoutMillis);
}
return transaction.then(ok);
};
global.setTimeout(timeout);
}).catch(handleTransactionError);
} catch (e) {
return handleTransactionError(e);
}
});
};
var getAddress = async function getAddress() {
global.ethereum && global.ethereum.enable && await global.ethereum.enable();
return (context.walletAddress = (await engine.eth.getAccounts())[0]);
};
var getSendingOptions = function getSendingOptions(transaction, value) {
return new Promise(async function(ok, ko) {
if (transaction) {
var address = await getAddress();
var txnData = {
from: address,
gasPrice: engine.utils.toWei("13", "gwei")
};
value && (txnData.value = value);
return transaction.estimateGas(txnData,
function(error, gas) {
if (error) {
return ko(error.message || error);
}
var data = {
from: address,
gas: gas || configuration.gasLimit || '7900000'
};
value && (data.value = value);
return ok(data);
});
}
return ok({
from: context.walletAddress || null,
gas: configuration.gasLimit || '7900000'
});
});
};
};