-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontract.js
104 lines (100 loc) · 7.58 KB
/
contract.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
var Web3a = require("web3");
const AlayaAccounts = require("web3/packages/web3-eth-accounts");
async function main() {
let web3a = new Web3a('http://47.241.91.2:6789');
// // 生成公私秘钥对
// let act = web3a.platon.accounts.create();
// console.log(act.privateKey);
// console.log(act.address);
// !!!!将主网的私钥写到代码上并分享给别人是非常危险的操作!!!!
// address for the privatekey below: atp1sznvsju6gjy3kmgnfgm526jf6e8x83twuctefh
var privateKey="0x5ae02064df442340f861136acbfc4bd62b3d48393903bd6aac77ce0e7aaa9e5e";
var chainId = 201030; //201018 for mainnet
// // privatekey for the address below: 0x0727b77a246f2c01c1b7b068c26bd71b81700f7ea003dd1ba68a38885a69a1a5
// var toAddress = "atp1jqtmpjme9gvg6wnuyphzqzgphslfmjtxcy8mt2";
var act = new AlayaAccounts(web3a.currentProvider, "atp");
act = act.privateKeyToAccount(privateKey);
let from = act.address;
let nonce = web3a.utils.numberToHex(await web3a.platon.getTransactionCount(from));
let gasPrice = await web3a.platon.getGasPrice();
let bytecode = "0x608060405234801561001057600080fd5b5061036b806100206000396000f3fe608060405234801561001057600080fd5b50600436106100365760003560e01c806317d7de7c1461003b578063c47f0027146100b8575b600080fd5b61004361015e565b6040805160208082528351818301528351919283929083019185019080838360005b8381101561007d578181015183820152602001610065565b50505050905090810190601f1680156100aa5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b610043600480360360208110156100ce57600080fd5b8101906020810181356401000000008111156100e957600080fd5b8201836020820111156100fb57600080fd5b8035906020019184600183028401116401000000008311171561011d57600080fd5b91908080601f0160208091040260200160405190810160405280939291908181526020018383808284376000920191909152509295506101f5945050505050565b60008054604080516020601f60026000196101006001881615020190951694909404938401819004810282018101909252828152606093909290918301828280156101ea5780601f106101bf576101008083540402835291602001916101ea565b820191906000526020600020905b8154815290600101906020018083116101cd57829003601f168201915b505050505090505b90565b805160609061020b90600090602085019061029e565b506000805460408051602060026001851615610100026000190190941693909304601f810184900484028201840190925281815292918301828280156102925780601f1061026757610100808354040283529160200191610292565b820191906000526020600020905b81548152906001019060200180831161027557829003601f168201915b50505050509050919050565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106102df57805160ff191683800117855561030c565b8280016001018555821561030c579182015b8281111561030c5782518255916020019190600101906102f1565b5061031892915061031c565b5090565b6101f291905b80821115610318576000815560010161032256fea265627a7a72315820087868e5d428c0270af7145556516942e16a5db5ec467f7dcdca4cfa9ec099bc64736f6c63430005110032";
bytecode = "0x608060405234801561001057600080fd5b50610429806100206000396000f3fe608060405234801561001057600080fd5b50600436106100365760003560e01c806317d7de7c1461003b578063c47f0027146100be575b600080fd5b6100436101f2565b6040518080602001828103825283818151815260200191508051906020019080838360005b83811015610083578082015181840152602081019050610068565b50505050905090810190601f1680156100b05780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b610177600480360360208110156100d457600080fd5b81019080803590602001906401000000008111156100f157600080fd5b82018360208201111561010357600080fd5b8035906020019184600183028401116401000000008311171561012557600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f820116905080830192505050505050509192919290505050610294565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156101b757808201518184015260208101905061019c565b50505050905090810190601f1680156101e45780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b606060008054600181600116156101000203166002900480601f01602080910402602001604051908101604052809291908181526020018280546001816001161561010002031660029004801561028a5780601f1061025f5761010080835404028352916020019161028a565b820191906000526020600020905b81548152906001019060200180831161026d57829003601f168201915b5050505050905090565b606081600090805190602001906102ac92919061034f565b5060008054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156103435780601f1061031857610100808354040283529160200191610343565b820191906000526020600020905b81548152906001019060200180831161032657829003601f168201915b50505050509050919050565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061039057805160ff19168380011785556103be565b828001600101855582156103be579182015b828111156103bd5782518255916020019190600101906103a2565b5b5090506103cb91906103cf565b5090565b6103f191905b808211156103ed5760008160009055506001016103d5565b5090565b9056fea265627a7a72315820a89a95b35ad9e15c39082d4e9ece93b95b29072f95af709314ba017e1fb1487764736f6c63430005110032"
let tx = {
from: from,
chainId: chainId,
gasPrice: gasPrice,
nonce: nonce,
data: bytecode
};
let gas = await web3a.platon.estimateGas(tx);
// console.log(gas);
tx.gas = gas;
// 签名交易
// let signTx = await web3a.platon.accounts.signTransaction(tx, privateKey);
let signTx = await act.signTransaction(tx);
// 发送交易
let receipt = await web3a.platon.sendSignedTransaction(signTx.rawTransaction);
// console.log("The receipt of the transaction is: " + JSON.stringify(receipt));
// console.log("sign tx data:\n", signTx.rawTransaction);
// let contractAddress = "atp1pcvx85klajfw9mvy3tf07acmqxaek5nanuy5t6";
let contractAddress = receipt.contractAddress;
console.log("contract address: " + contractAddress);
let abi = [
{
"constant": false,
"inputs": [
{
"internalType": "string",
"name": "_name",
"type": "string"
}
],
"name": "setName",
"outputs": [
{
"internalType": "string",
"name": "",
"type": "string"
}
],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "getName",
"outputs": [
{
"internalType": "string",
"name": "",
"type": "string"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
}
];
let contract = new web3a.platon.Contract(abi, contractAddress);
let name = await contract.methods.getName().call();
console.log("name before change: " + name);
let trans = contract.methods.setName("rileyge");
let options ={
to : trans._parent._address,
data : trans.encodeABI(),
gas : await trans.estimateGas({from: from}),
gasPrice: gasPrice,
chainId: chainId
}
// let act = web3a.platon.accounts.privateKeyToAccount(privateKey);
// signTx = await act.signTransaction(options);
// signTx = await web3a.platon.accounts.signTransaction(options, privateKey);
signTx = await act.signTransaction(options);
// 发送交易
receipt = await web3a.platon.sendSignedTransaction(signTx.rawTransaction);
name = await contract.methods.getName().call();
console.log("name after change: " + name);
}
main();