-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
59 lines (47 loc) · 1.36 KB
/
index.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
const isHex = require('isstrhex');
const printf = require('util').format;
const str_pad = require('locutus/php/strings/str_pad');
class TokenData {
constructor(token) {
this.token = {decimals: token};
}
values() {
console.log(this);
}
prepare() {
this.to = this.sanitizeHex(to);
}
sanitizeHex(hex) {
if(hex.startsWith("0x")) {
hex = hex.replace("0x", "");
}
if(hex.length == 0 ||!isHex(hex) ) {
throw Error('Invalid hex fomart');
}
return hex;
}
generate(toAddress, Amount) {
if(typeof toAddress !== 'string') {
process.exitCode = 1;
throw Error('String expected on address.');
}
if(typeof Amount !=='number') {
process.exitCode = 1;
throw Error('Number expected on amount.');
}
// convert from ether to wei
Amount = (Amount)*(Math.pow(10,this.token.decimals));
// "a9059cbb" stands for Transfer method on Contracts
// transfer(address,uint256)
toAddress = this.sanitizeHex(toAddress);
if (toAddress.length !== 40) {
throw Error('Invalid format, must be 40 chars');
}
var data = printf('0x%s%s%s',
str_pad('a9059cbb', 32, '0', 'STR_PAD_RIGHT'),
str_pad(toAddress, 32, '0', 'STR_PAD_LEFT'),
str_pad(this.sanitizeHex(Amount.toString(16)), 64, '0', 'STR_PAD_LEFT'));
return data;
}
}
module.exports = TokenData;