forked from web3devs/sms-blockchain-protocol
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tweet.js
104 lines (91 loc) · 2.1 KB
/
tweet.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
// get Web3 for rinkeby
const Web3 = require('web3');
const EthereumTx = require('ethereumjs-tx');
const web3 = new Web3(
new Web3.providers.HttpProvider('https://rinkeby.infura.io'),
);
// get abi and address
let abi = [
{
constant: false,
inputs: [
{
name: 'tweetString',
type: 'string',
},
],
name: 'tweet',
outputs: [],
payable: false,
stateMutability: 'nonpayable',
type: 'function',
},
{
constant: true,
inputs: [],
name: 'tweetString',
outputs: [
{
name: '',
type: 'string',
},
],
payable: false,
stateMutability: 'view',
type: 'function',
},
{
inputs: [],
payable: false,
stateMutability: 'nonpayable',
type: 'constructor',
},
{
anonymous: false,
inputs: [
{
indexed: false,
name: 'tweetString',
type: 'string',
},
],
name: 'chirp',
type: 'event',
},
];
let address = '0x1d02a7557aeaf16bbdc4b482930b51d76308a235';
// console.log('WEB3', web3);
let contract = web3.eth.contract(abi).at(address);
const nonce = web3.eth.getTransactionCount(
process.env.ETH_RINKEBY_ADDRESS.toString(),
);
console.log('NONCE', nonce.toString(16));
// call tweet function
function tweet(reqArray, cb) {
let message = reqArray[1];
let tweetData = contract.tweet.getData(message);
const privateKey = Buffer.from(
process.env.ETH_RINKEBY_PRIVATE_KEY.toString(),
'hex',
);
const nonce = web3.eth.getTransactionCount(
process.env.ETH_RINKEBY_ADDRESS.toString(),
);
console.log('NONCE', nonce.toString(16));
const txParams = {
nonce: '0x' + nonce.toString(16),
gasPrice: web3.eth.gasPrice.toNumber() * 1.1,
gasLimit: 3000000,
from: process.env.ETH_RINKEBY_ADDRESS.toString(),
to: address,
value: '0x00',
data: tweetData,
// EIP 155 chainId - mainnet: 1, ropsten: 3
chainId: 4,
};
const tx = new EthereumTx(txParams);
tx.sign(privateKey);
const serializedTx = tx.serialize();
web3.eth.sendRawTransaction('0x' + serializedTx.toString('hex'), cb);
}
module.exports = tweet;