1 | const lodash = require('lodash');
|
2 | const callable = require('../util/callable');
|
3 |
|
4 | class ContractMethodOverride {
|
5 | constructor(methods, contract, conflux) {
|
6 | this.signatureToMethod = lodash.keyBy(methods, 'signature');
|
7 | this.contract = contract;
|
8 | this.conflux = conflux;
|
9 |
|
10 | return callable(this, this.call.bind(this));
|
11 | }
|
12 |
|
13 | call(...args) {
|
14 | const acceptArray = [];
|
15 | const rejectArray = [];
|
16 |
|
17 | let transaction;
|
18 | for (const method of Object.values(this.signatureToMethod)) {
|
19 | try {
|
20 | transaction = method(...args);
|
21 | acceptArray.push(method.type);
|
22 | } catch (e) {
|
23 | rejectArray.push(method.type);
|
24 | }
|
25 | }
|
26 |
|
27 | if (!acceptArray.length) {
|
28 | throw new Error(`can not match override "${rejectArray.join('|')}" with args (${args.join(',')})`);
|
29 | }
|
30 | if (acceptArray.length > 1) {
|
31 | throw new Error(`can not determine override "${acceptArray.join('|')}" with args (${args.join(',')})`);
|
32 | }
|
33 |
|
34 | return transaction;
|
35 | }
|
36 |
|
37 | decodeData(hex) {
|
38 | const signature = hex.slice(0, 10);
|
39 | const method = this.signatureToMethod[signature];
|
40 | return method.decodeData(hex);
|
41 | }
|
42 | }
|
43 |
|
44 | module.exports = ContractMethodOverride;
|