mm_contract 1.0.1 → 1.0.2
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/README.md +11 -2
- package/index.js +4 -22
- package/lib/mm_contract.js +24 -0
- package/lib/mm_eth_ext.js +290 -0
- package/mm_eth.js +436 -720
- package/package.json +10 -3
- package/tool/contract/index.js +132 -0
- package/tool/contract/tool.json +111 -0
- package/.gitattributes +0 -2
- package/app/ap.sol +0 -26
- package/app/test.sol +0 -463
- package/app/token.sol +0 -260
- package/demo/abi/auction.json +0 -1168
- package/demo/abi/bep20.json +0 -533
- package/demo/abi/blindbox.json +0 -767
- package/demo/abi/erc1155.json +0 -833
- package/demo/abi/erc721s.json +0 -1393
- package/demo/abi/exchange.json +0 -408
- package/demo/abi/invite.json +0 -605
- package/demo/abi/itemsell.json +0 -420
- package/demo/abi/nftmarket.json +0 -747
- package/demo/abi/plant.json +0 -1093
- package/demo/abi/sharepool.json +0 -773
- package/demo/abi/tokenmarket.json +0 -174
- package/demo/abi/tokensell.json +0 -465
- package/demo/abi/vipcard.json +0 -86
- package/demo/abi/vipshop.json +0 -303
- package/demo/index.js +0 -63
- package/demo/index_2.js +0 -31
- package/demo/test.js +0 -65
- package/demo/test_abi.js +0 -448
- package/sol/assets.sol +0 -2
- package/sol/erc20.sol +0 -23
- package/sol/erc721.sol +0 -40
- package/sol/ownable.sol +0 -42
- package/sol/safeMath.sol +0 -118
- package/sol//345/220/210/347/272/246/345/234/260/345/235/200.txt +0 -2
- package/test.js +0 -13
package/package.json
CHANGED
|
@@ -1,8 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mm_contract",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "这是超级美眉智能合约调用辅助模块,用于快速发布合约和调用区块链ABI方法,适用于eth和tron。",
|
|
5
5
|
"main": "index.js",
|
|
6
|
+
"files": [
|
|
7
|
+
"index.js",
|
|
8
|
+
"lib",
|
|
9
|
+
"mm_eth.js",
|
|
10
|
+
"mm_tron.js",
|
|
11
|
+
"tool"
|
|
12
|
+
],
|
|
6
13
|
"scripts": {
|
|
7
14
|
"test": "node test.js",
|
|
8
15
|
"dev": "nodemon ./demo/index.js"
|
|
@@ -28,8 +35,8 @@
|
|
|
28
35
|
"调用ABI",
|
|
29
36
|
"Contract"
|
|
30
37
|
],
|
|
31
|
-
"layer": "
|
|
32
|
-
"group": "
|
|
38
|
+
"layer": "util",
|
|
39
|
+
"group": "io",
|
|
33
40
|
"author": "邱文武(qiuwenwu)",
|
|
34
41
|
"license": "ISC",
|
|
35
42
|
"bugs": {
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
/**
|
|
3
|
+
* contract —— 链上合约账户与调用工具
|
|
4
|
+
* 复用模块合约类(内部按 chain 分派到 eth/tron 实现),能力层不接触 web3 细节。
|
|
5
|
+
* 说明:模块的 web3 连接由 init()/login() 建立,所有动作都先完成连接再取数。
|
|
6
|
+
*/
|
|
7
|
+
const fs = require('fs');
|
|
8
|
+
let { mm_contract: Contract } = require('../../index.js');
|
|
9
|
+
|
|
10
|
+
/** 合约分组名:call 按模块约定以「分组 → 别名 → { address, contract }」注册合约 */
|
|
11
|
+
const GROUP = 'tool_contract';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* 构建模块配置
|
|
15
|
+
* @param {object} p 参数
|
|
16
|
+
* @returns {object} 模块配置
|
|
17
|
+
*/
|
|
18
|
+
function _config(p) {
|
|
19
|
+
let chainId = Number(p.chain_id || 97);
|
|
20
|
+
let cfg = { chainId: chainId, chain_accuracy: 18 };
|
|
21
|
+
if (p.rpc) {
|
|
22
|
+
let map = {};
|
|
23
|
+
map[chainId] = p.rpc;
|
|
24
|
+
cfg.rpc = map;
|
|
25
|
+
}
|
|
26
|
+
if (p.default_account) {
|
|
27
|
+
cfg.defaultAccount = p.default_account;
|
|
28
|
+
}
|
|
29
|
+
return cfg;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* 新建合约实例
|
|
34
|
+
* @param {object} p 参数
|
|
35
|
+
* @returns {object} 合约实例
|
|
36
|
+
*/
|
|
37
|
+
function _instance(p) {
|
|
38
|
+
return new Contract(_config(p), p.chain === 'tron' ? 'tron' : 'eth');
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* 建立连接(默认账户由此就绪)
|
|
43
|
+
* @param {object} inst 合约实例
|
|
44
|
+
* @returns {Promise<object>} 合约实例
|
|
45
|
+
*/
|
|
46
|
+
async function _connect(inst) {
|
|
47
|
+
if (typeof inst.init === 'function') {
|
|
48
|
+
await inst.init();
|
|
49
|
+
} else if (typeof inst.login === 'function') {
|
|
50
|
+
await inst.login();
|
|
51
|
+
}
|
|
52
|
+
return inst;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* 读取 ABI
|
|
57
|
+
* @param {string} abi ABI JSON 字符串或文件路径
|
|
58
|
+
* @returns {Array} ABI 数组
|
|
59
|
+
*/
|
|
60
|
+
function _abi(abi) {
|
|
61
|
+
if (!abi) throw new TypeError('abi 不能为空');
|
|
62
|
+
let text = abi.trim()[0] === '[' ? abi : fs.readFileSync(abi, 'utf8');
|
|
63
|
+
return JSON.parse(text);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* 执行合约工具动作
|
|
68
|
+
* @param {object} params 工具参数
|
|
69
|
+
* @param {object} ctx 上下文
|
|
70
|
+
* @param {object} options 选项
|
|
71
|
+
* @returns {Promise<object>} 执行结果
|
|
72
|
+
*/
|
|
73
|
+
async function main(params, ctx, options) {
|
|
74
|
+
void options;
|
|
75
|
+
let p = params || {};
|
|
76
|
+
let action = p.action || 'account';
|
|
77
|
+
/**
|
|
78
|
+
* 写入上下文输出并返回结果
|
|
79
|
+
* @param {object} data 输出数据
|
|
80
|
+
* @returns {object} 输出数据
|
|
81
|
+
*/
|
|
82
|
+
const done = (data) => { ctx.output = data; return data; };
|
|
83
|
+
|
|
84
|
+
try {
|
|
85
|
+
if (action === 'account') {
|
|
86
|
+
let inst = await _connect(_instance(p));
|
|
87
|
+
let address = await inst.getAccount();
|
|
88
|
+
if (!address) return done({ code: 10002, msg: '取默认账户失败:节点未返回账户且未配置 default_account' });
|
|
89
|
+
return done({ code: 0, msg: '成功', address: String(address) });
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
if (action === 'balance') {
|
|
93
|
+
let inst = await _connect(_instance(p));
|
|
94
|
+
let balance = await inst.getBalance(p.address || undefined);
|
|
95
|
+
if (typeof balance !== 'number' || Number.isNaN(balance)) {
|
|
96
|
+
return done({ code: 10002, msg: '余额查询无有效返回:请确认 rpc 可达、address 合法' });
|
|
97
|
+
}
|
|
98
|
+
return done({ code: 0, msg: '成功', address: p.address || '', balance: String(balance) });
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (action === 'sign') {
|
|
102
|
+
if (!p.message) throw new TypeError('message 不能为空');
|
|
103
|
+
let inst = await _connect(_instance(p));
|
|
104
|
+
let signature = await inst.sign(p.message, p.address || inst.address);
|
|
105
|
+
if (!signature) return done({ code: 10002, msg: '签名无返回:需节点托管并解锁该账户私钥' });
|
|
106
|
+
return done({ code: 0, msg: '成功', message: p.message, signature: signature });
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
if (action === 'call') {
|
|
110
|
+
if (!p.contract) throw new TypeError('contract 不能为空');
|
|
111
|
+
if (!p.method) throw new TypeError('method 不能为空');
|
|
112
|
+
let abi = _abi(p.abi);
|
|
113
|
+
let inst = await _connect(_instance(p));
|
|
114
|
+
let alias = p.name || 'main';
|
|
115
|
+
let contract = inst.new(alias, abi, p.contract);
|
|
116
|
+
inst[GROUP] = {};
|
|
117
|
+
inst[GROUP][alias] = { address: p.contract, contract: contract };
|
|
118
|
+
let args = Array.isArray(p.args) ? p.args : [];
|
|
119
|
+
let result = await inst.call(GROUP, alias, p.method, 0, ...args);
|
|
120
|
+
if (result === undefined || result === null) {
|
|
121
|
+
return done({ code: 10002, msg: '合约调用无返回:请确认 rpc 可达、方法名与 abi 匹配、参数类型正确' });
|
|
122
|
+
}
|
|
123
|
+
return done({ code: 0, msg: '调用完成', contract: p.contract, method: p.method, result: result });
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
return done({ code: 10001, msg: '不支持的操作: ' + action });
|
|
127
|
+
} catch (error) {
|
|
128
|
+
return done({ code: 10000, msg: '执行失败:' + error.message, error: { name: error.name, message: error.message } });
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
module.exports = { main };
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "contract",
|
|
3
|
+
"title": "链上合约账户与调用",
|
|
4
|
+
"description": "基于 mm_contract 的链上合约辅助能力:连接节点后取默认账户、按地址查余额、对消息签名、调用合约只读方法。适用场景:查询钱包余额、确认默认账户、签名后转发、读合约视图方法。工作流提示:chain 选 eth/tron;balance/call 需传 rpc;只读调用用 call,写操作请在业务侧另走交易流程。",
|
|
5
|
+
"category": "区块链",
|
|
6
|
+
"group": "blockchain",
|
|
7
|
+
"type": "function",
|
|
8
|
+
"main": "./index.js",
|
|
9
|
+
"params": {
|
|
10
|
+
"type": "object",
|
|
11
|
+
"properties": {
|
|
12
|
+
"action": {
|
|
13
|
+
"type": "string",
|
|
14
|
+
"description": "操作类型: account(默认账户)/balance(查余额)/sign(消息签名)/call(合约只读调用)",
|
|
15
|
+
"enum": [
|
|
16
|
+
"account",
|
|
17
|
+
"balance",
|
|
18
|
+
"sign",
|
|
19
|
+
"call"
|
|
20
|
+
],
|
|
21
|
+
"default": "account"
|
|
22
|
+
},
|
|
23
|
+
"chain": {
|
|
24
|
+
"type": "string",
|
|
25
|
+
"description": "链类型: eth/tron,缺省 eth",
|
|
26
|
+
"enum": [
|
|
27
|
+
"eth",
|
|
28
|
+
"tron"
|
|
29
|
+
],
|
|
30
|
+
"default": "eth"
|
|
31
|
+
},
|
|
32
|
+
"chain_id": {
|
|
33
|
+
"type": "number",
|
|
34
|
+
"description": "链 ID,缺省 97(BSC 测试网)",
|
|
35
|
+
"default": 97
|
|
36
|
+
},
|
|
37
|
+
"rpc": {
|
|
38
|
+
"type": "string",
|
|
39
|
+
"description": "节点 RPC 地址,缺省用模块内置链表"
|
|
40
|
+
},
|
|
41
|
+
"default_account": {
|
|
42
|
+
"type": "string",
|
|
43
|
+
"description": "默认账户地址,缺省用模块内置地址"
|
|
44
|
+
},
|
|
45
|
+
"address": {
|
|
46
|
+
"type": "string",
|
|
47
|
+
"description": "查询/签名地址(balance 可选,缺省取默认账户)"
|
|
48
|
+
},
|
|
49
|
+
"message": {
|
|
50
|
+
"type": "string",
|
|
51
|
+
"description": "待签消息(sign 必填)"
|
|
52
|
+
},
|
|
53
|
+
"contract": {
|
|
54
|
+
"type": "string",
|
|
55
|
+
"description": "合约地址(call 必填)"
|
|
56
|
+
},
|
|
57
|
+
"name": {
|
|
58
|
+
"type": "string",
|
|
59
|
+
"description": "合约别名(call 可选),缺省 main"
|
|
60
|
+
},
|
|
61
|
+
"abi": {
|
|
62
|
+
"type": "string",
|
|
63
|
+
"description": "合约 ABI(JSON 字符串或 ABI 文件路径,call 必填)"
|
|
64
|
+
},
|
|
65
|
+
"method": {
|
|
66
|
+
"type": "string",
|
|
67
|
+
"description": "方法名(call 必填)"
|
|
68
|
+
},
|
|
69
|
+
"args": {
|
|
70
|
+
"type": "array",
|
|
71
|
+
"description": "方法参数(call 可选)"
|
|
72
|
+
}
|
|
73
|
+
},
|
|
74
|
+
"required": [
|
|
75
|
+
"action"
|
|
76
|
+
]
|
|
77
|
+
},
|
|
78
|
+
"returns": {
|
|
79
|
+
"type": "object",
|
|
80
|
+
"properties": {
|
|
81
|
+
"code": {
|
|
82
|
+
"type": "number",
|
|
83
|
+
"description": "0 表示成功,1xxxx 表示业务/执行异常"
|
|
84
|
+
},
|
|
85
|
+
"msg": {
|
|
86
|
+
"type": "string",
|
|
87
|
+
"description": "结果描述"
|
|
88
|
+
},
|
|
89
|
+
"address": {
|
|
90
|
+
"type": "string",
|
|
91
|
+
"description": "账户地址"
|
|
92
|
+
},
|
|
93
|
+
"balance": {
|
|
94
|
+
"type": "string",
|
|
95
|
+
"description": "余额"
|
|
96
|
+
},
|
|
97
|
+
"signature": {
|
|
98
|
+
"type": "string",
|
|
99
|
+
"description": "签名结果(sign)"
|
|
100
|
+
},
|
|
101
|
+
"result": {
|
|
102
|
+
"description": "调用返回值(call)"
|
|
103
|
+
},
|
|
104
|
+
"error": {
|
|
105
|
+
"type": "object",
|
|
106
|
+
"description": "异常对象 { name, message }"
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
},
|
|
110
|
+
"handler": "manager"
|
|
111
|
+
}
|
package/.gitattributes
DELETED
package/app/ap.sol
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
pragma solidity ^0.4.0;
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* @dev ERC20 Token abstract constract.
|
|
5
|
-
*/
|
|
6
|
-
contract ERC20Token {
|
|
7
|
-
function transferFrom(address, address, uint256) public returns (bool);
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
contract Blank {
|
|
11
|
-
/**
|
|
12
|
-
* @dev calls the ERC20 token's transferFrom function
|
|
13
|
-
* @param _token address The address of ERC20 token.
|
|
14
|
-
* @param _dsts address The addresses which be air dropped.
|
|
15
|
-
* @param _values uint256 The token values that each address will receive.
|
|
16
|
-
*/
|
|
17
|
-
function transfer(address _token, address[] _dsts, uint256[] _values)
|
|
18
|
-
public
|
|
19
|
-
payable
|
|
20
|
-
{
|
|
21
|
-
ERC20Token token = ERC20Token(_token);
|
|
22
|
-
for (uint256 i = 0; i < _dsts.length; i++) {
|
|
23
|
-
token.transferFrom(msg.sender, _dsts[i], _values[i]);
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
}
|