Skip to content

Commit 579dc9a

Browse files
committed
feat(decode): add feature decode raw transaction
1 parent dd1ab62 commit 579dc9a

File tree

3 files changed

+223
-93
lines changed

3 files changed

+223
-93
lines changed

.eslintrc.yml

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
env:
2+
es2021: true
3+
node: true
4+
extends:
5+
- "eslint:recommended"
6+
- "plugin:@typescript-eslint/recommended"
7+
parser: "@typescript-eslint/parser"
8+
parserOptions:
9+
ecmaVersion: 12
10+
sourceType: module
11+
plugins:
12+
- "@typescript-eslint"
13+
rules:
14+
indent:
15+
- error
16+
- 2
17+
- SwitchCase: 1
18+
linebreak-style:
19+
- error
20+
- unix
21+
quotes:
22+
- error
23+
- double
24+
semi:
25+
- error
26+
- always
27+
no-unused-vars:
28+
- warn
29+
- argsIgnorePattern: "^_"
30+
no-eval:
31+
- error
32+
no-extra-boolean-cast:
33+
- error
34+
no-mixed-spaces-and-tabs:
35+
- error
36+
no-multiple-empty-lines:
37+
- error
38+
- max: 1
39+
maxEOF: 0
40+
no-unreachable:
41+
- error
42+
keyword-spacing:
43+
- error
44+
prefer-const:
45+
- warn
46+
- destructuring: any
47+
key-spacing:
48+
- error
49+
new-cap:
50+
- warn
51+
no-array-constructor:
52+
- error
53+
no-async-promise-executor:
54+
- error
55+
no-compare-neg-zero:
56+
- error
57+
no-const-assign:
58+
- error
59+
no-constant-condition:
60+
- error
61+
no-delete-var:
62+
- error
63+
no-dupe-args:
64+
- error
65+
no-lone-blocks:
66+
- error
67+
no-multi-spaces:
68+
- error
69+
no-case-declarations:
70+
- warn
71+
no-console:
72+
- warn
73+
no-multi-str:
74+
- error
75+
no-var:
76+
- warn
77+
no-this-alias:
78+
- 0
79+
camelcase:
80+
- 1
81+
max-len:
82+
- 1
83+
- code: 100
84+
ignoreUrls: true
85+
ignorePattern: ^import\s.+\sfrom\s.+;
86+
curly:
87+
- error
88+
brace-style:
89+
- error
90+
- 1tbs
91+
no-useless-catch:
92+
- warn
93+
"@typescript-eslint/no-var-requires":
94+
- warn

index.ts

+122-91
Original file line numberDiff line numberDiff line change
@@ -1,176 +1,207 @@
1-
'use strict';
1+
"use strict";
22

33
import Web3 from "web3";
4-
import {default as Tx} from "ethereumjs-tx"
4+
import {default as Tx} from "ethereumjs-tx";
55

66
export default class BlockchainService {
77

8-
protected WEB3
9-
protected SCA
10-
protected gasPrice
11-
protected ABI
8+
protected WEB3;
9+
protected SCA;
10+
protected gasPrice;
11+
protected ABI;
1212

13-
/**
13+
/**
1414
* The constructor function is used to initialize the web3 object, gas price, smart contract
1515
* address, and smart contract ABI
1616
* @param {string} RPC - The RPC address of the Ethereum node you want to connect to.
1717
* @param {number} gasPrice - The gas price you want to use for your transactions.
1818
* @param {string} SCA - The address of the smart contract you want to interact with.
1919
* @param {any} ABI - The ABI of the contract you want to interact with.
2020
*/
21-
constructor(RPC:string,gasPrice:number,SCA:string,ABI:any) {
22-
this.WEB3 = new Web3(
23-
new Web3.providers.HttpProvider(RPC)
24-
);
21+
constructor(RPC:string,gasPrice:number,SCA:string,ABI:any) {
22+
this.WEB3 = new Web3(
23+
new Web3.providers.HttpProvider(RPC)
24+
);
2525

26-
this.gasPrice = gasPrice;
27-
this.SCA = SCA;
28-
this.ABI = ABI;
29-
}
26+
this.gasPrice = gasPrice;
27+
this.SCA = SCA;
28+
this.ABI = ABI;
29+
}
3030

31-
/**
31+
/**
3232
* It takes a function name, parameters, and a from address, and returns a raw transaction object
3333
* @param [funcName] - The name of the function you want to call
3434
* @param params - The parameters of the function you want to call.
3535
* @param [from] - The address of the account that will be sending the transaction.
3636
* @returns A raw transaction object.
3737
*/
38-
public async createRaw(funcName="",params,from="",value=0) {
38+
public async createRaw(funcName="",params,from="",value=0) {
3939

40-
let ABI = JSON.parse(JSON.stringify(this.ABI))
40+
const ABI = JSON.parse(JSON.stringify(this.ABI));
4141

42-
const contractDeployed = new this.WEB3.eth.Contract(
43-
ABI,
44-
this.SCA
45-
);
42+
const contractDeployed = new this.WEB3.eth.Contract(
43+
ABI,
44+
this.SCA
45+
);
4646

47-
const dataFunc = await contractDeployed.methods[funcName](
48-
...params
49-
).encodeABI();
47+
const dataFunc = await contractDeployed.methods[funcName](
48+
...params
49+
).encodeABI();
5050

51-
const gasLimit = await contractDeployed.methods[funcName](
52-
...params
53-
).estimateGas({ from })
51+
const gasLimit = await contractDeployed.methods[funcName](
52+
...params
53+
).estimateGas({ from });
5454

55-
const nonce = await this.WEB3.eth.getTransactionCount(from)
55+
const nonce = await this.WEB3.eth.getTransactionCount(from);
5656

57-
const rawTx = {
58-
from: from,
59-
to: this.SCA,
60-
gasLimit,
61-
gasPrice: this.gasPrice,
62-
nonce: nonce,
63-
data: dataFunc,
64-
value: value
65-
};
57+
const rawTx = {
58+
from: from,
59+
to: this.SCA,
60+
gasLimit,
61+
gasPrice: this.gasPrice,
62+
nonce: nonce,
63+
data: dataFunc,
64+
value: value
65+
};
6666

67-
return rawTx;
68-
}
67+
return rawTx;
68+
}
6969

70-
/**
70+
/**
7171
* It takes a raw transaction object, a private key, and a chain ID, and returns a transaction hash
7272
* and the transaction object
7373
* @param rawTx - The raw transaction object.
7474
* @param privateKey - The private key of the account you want to send the transaction from.
7575
* @param [chainId=97] - The chain ID of the network you're sending to.
7676
* @returns The transaction hash and the transaction object.
7777
*/
78-
public async signRaw(rawTx = {}, privateKey, chainId=97) {
79-
80-
privateKey = Buffer.from(privateKey, 'hex')
81-
82-
var transaction = new Tx(rawTx, {chainId: chainId})
83-
84-
await transaction.sign(privateKey)
85-
86-
let signedTx = '0x' + transaction.serialize().toString('hex')
87-
88-
return signedTx;
89-
}
78+
public async signRaw(rawTx = {}, privateKey, chainId=97) {
79+
80+
privateKey = Buffer.from(privateKey, "hex");
81+
82+
const transaction = new Tx(rawTx, {chainId: chainId});
83+
84+
await transaction.sign(privateKey);
85+
86+
const signedTx = "0x" + transaction.serialize().toString("hex");
87+
88+
return signedTx;
89+
}
90+
91+
/**
92+
* It takes a raw transaction string, and returns an object with the transaction's details
93+
* @param {string} raw - The raw transaction string
94+
* @param [chainId=97] - The chainId of the network you're on.
95+
* @returns The rawTx object is being returned.
96+
*/
97+
public async decodeRaw(raw: string) {
98+
const tx = new Tx(raw);
99+
100+
const [
101+
nonce, gasPrice,
102+
gasLimit, to,
103+
value, data,
104+
v, r,
105+
s
106+
] = tx.toJSON();
107+
108+
const rawTx = {
109+
nonce, gasPrice,
110+
gasLimit, to,
111+
value, data,
112+
v, r,
113+
s,
114+
chainId: tx._chainId,
115+
from: "0x" + tx.getSenderAddress().toString("hex"),
116+
txHash: "0x" + tx.hash().toString("hex")
117+
};
118+
119+
return rawTx;
120+
}
90121

91-
/**
122+
/**
92123
* It sends a signed transaction to the blockchain.
93124
* @param {string} signedTx - The signed transaction in hex format.
94125
* @returns The txHash and the tx object.
95126
*/
96-
public async sendSignedRaw(signedTx: string) {
127+
public async sendSignedRaw(signedTx: string) {
97128

98-
let txHash = this.WEB3.utils.keccak256(signedTx) // ALIAS
129+
const txHash = this.WEB3.utils.keccak256(signedTx); // ALIAS
99130

100-
const tx = await this.WEB3.eth.sendSignedTransaction(signedTx);
131+
const tx = await this.WEB3.eth.sendSignedTransaction(signedTx);
101132

102-
return {txHash, tx};
103-
}
133+
return {txHash, tx};
134+
}
104135

105-
/**
136+
/**
106137
* It takes in a function name, parameters, and a from address, and returns the data from the
107138
* function
108139
* @param [funcName] - The name of the function you want to call.
109140
* @param params - an array of parameters that the function takes.
110141
* @param from - The address of the account that will be used to call the function.
111142
* @returns The return value of the function.
112143
*/
113-
public async readFunc(funcName="",params, from) {
144+
public async readFunc(funcName="",params, from) {
114145

115-
let ABI = JSON.parse(JSON.stringify(this.ABI))
146+
const ABI = JSON.parse(JSON.stringify(this.ABI));
116147

117-
const contractDeployed = new this.WEB3.eth.Contract(
118-
ABI,
119-
this.SCA
120-
);
148+
const contractDeployed = new this.WEB3.eth.Contract(
149+
ABI,
150+
this.SCA
151+
);
121152

122-
const dataFunc = await contractDeployed.methods[funcName](
123-
...params
124-
).call({ from });
153+
const dataFunc = await contractDeployed.methods[funcName](
154+
...params
155+
).call({ from });
125156

126-
return dataFunc;
157+
return dataFunc;
127158

128-
}
159+
}
129160

130-
/**
161+
/**
131162
* This function returns the receipt of a transaction
132163
* @param {string} txHash - The transaction hash of the transaction you want to get the receipt
133164
* for.
134165
* @returns The receipt of the transaction.
135166
*/
136-
public async getReceipt(txHash: string) {
137-
const receipt = this.WEB3.eth.getTransactionReceipt(txHash);
167+
public async getReceipt(txHash: string) {
168+
const receipt = this.WEB3.eth.getTransactionReceipt(txHash);
138169

139-
return receipt;
140-
}
170+
return receipt;
171+
}
141172

142-
/**
173+
/**
143174
* This function returns event logs is published from a smart contract
144175
* @param {array} topics - The topics of log
145176
* topics[0]: signature event
146177
* topics[1-3]: indexed params
147178
* @returns The event log.
148179
*/
149-
public async getEvent(topics, fromBlock=0, toBlock=499) {
150-
const event = await this.WEB3.eth.getPastLogs({
151-
address: this.SCA, //smart contract address published event
152-
topics,
153-
fromBlock,
154-
toBlock
155-
})
180+
public async getEvent(topics, fromBlock=0, toBlock=499) {
181+
const event = await this.WEB3.eth.getPastLogs({
182+
address: this.SCA, //smart contract address published event
183+
topics,
184+
fromBlock,
185+
toBlock
186+
});
156187

157188
return event;
158-
}
189+
}
159190

160-
/**
191+
/**
161192
* It takes the ABI of the contract, the data from the log, and the topics from the log, and
162193
* returns the decoded log
163194
* @param [data] - The data parameter is the data that was emitted from the contract.
164195
* @param topics - An array of values generated by the: event.raw.topics
165196
* @returns The decoded log.
166197
*/
167-
public async decodeLog(data="",topics) {
198+
public async decodeLog(data="",topics) {
168199

169-
let ABI = JSON.parse(JSON.stringify(this.ABI))
200+
const ABI = JSON.parse(JSON.stringify(this.ABI));
170201

171-
const result = await this.WEB3.eth.abi.decodeLog(ABI, data, topics)
202+
const result = await this.WEB3.eth.abi.decodeLog(ABI, data, topics);
172203

173-
return result;
174-
}
204+
return result;
205+
}
175206

176207
}

0 commit comments

Comments
 (0)