Skip to content

Commit c1f61d9

Browse files
committed
Added deploy contract endpoint. Refactoring
1 parent 06d2fa1 commit c1f61d9

File tree

6 files changed

+64
-27
lines changed

6 files changed

+64
-27
lines changed
Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
export interface Transaction {
1+
import { TransactionBase } from "./TransactionBase";
2+
3+
export interface Transaction extends TransactionBase {
24
blockHash: string;
35
blockNumber: number;
4-
from: string;
5-
gas: number;
6-
gasPrice: number;
76
hash: string;
7+
to: string,
88
input: string;
99
nonce: number;
10-
to: string;
1110
transactionIndex: number;
12-
value: number;
1311
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export interface TransactionBase {
2+
from?: string;
3+
gas?: number;
4+
gasPrice?: number;
5+
value?: number;
6+
}

src/api/service/controller/ContractController.ts

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { IWeb3 } from '../../blockchain/IWeb3';
88
import { Web3Instance } from '../../blockchain/Web3Instance';
99
import { Web3Configuration } from '../../blockchain/Web3Configuration';
1010
import { DeployContractRequest } from '../request/DeployContractRequest';
11+
import { TransactionRequest } from '../request/TransactionRequest';
1112

1213
@Route('contract')
1314
@provideSingleton(ContractController)
@@ -41,7 +42,13 @@ export class ContractController extends Controller {
4142
async deploy(
4243
@Body() deployRequest: DeployContractRequest,
4344
) {
44-
45+
const config = {
46+
blockchainHost: deployRequest.blockchainHost,
47+
blockchainProtocol: deployRequest.blockchainProtocol,
48+
blockchainBasicAuthUsername: deployRequest.blockchainBasicAuthUsername,
49+
blockchainBasicAuthPassword: deployRequest.blockchainBasicAuthPassword
50+
} as Web3Configuration
51+
return this.contractService.deployContractSource(config, deployRequest.name, deployRequest.source, deployRequest.path, deployRequest)
4552
}
4653

4754
@Post('run/{contractAddress}')
@@ -56,18 +63,13 @@ export class ContractController extends Controller {
5663
blockchainBasicAuthUsername: runFunction.blockchainBasicAuthUsername,
5764
blockchainBasicAuthPassword: runFunction.blockchainBasicAuthPassword
5865
} as Web3Configuration
59-
const iWeb3: IWeb3 = new Web3Instance(config)
60-
const web3 = iWeb3.getInstance()
61-
const functionCallEncoded = web3.eth.abi.encodeFunctionCall(runFunction.abi, runFunction.params)
62-
const accounts = await web3.eth.getAccounts()
63-
const receipt = await web3.eth.sendTransaction({
64-
to: contractAddress,
65-
from: runFunction.from || accounts[0],
66-
gas: runFunction.gas,
67-
gasPrice: runFunction.gasPrice,
68-
value: runFunction.value,
69-
input: functionCallEncoded
70-
})
71-
return receipt
66+
67+
return this.contractService.runFunction(
68+
config,
69+
runFunction.abi,
70+
runFunction.params,
71+
contractAddress,
72+
runFunction
73+
)
7274
}
7375
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
import { TransactionRequest } from "./TransactionRequest";
22

33
export interface DeployContractRequest extends TransactionRequest {
4+
name: string,
5+
source: string,
6+
path: string
47
}

src/api/service/request/TransactionRequest.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
export interface TransactionRequest {
2-
from?: string,
3-
gas?: number,
4-
gasPrice?: number,
5-
value?: number,
1+
import { TransactionBase } from "../bean/TransactionBase";
2+
3+
export interface TransactionRequest extends TransactionBase {
64
blockchainHost?: string,
75
blockchainProtocol?: string,
86
blockchainBasicAuthUsername?: string,

src/api/service/service/ContractService.ts

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
import { injectable } from "inversify";
2+
import { Web3Instance } from "../../blockchain/Web3Instance";
3+
import { IWeb3 } from "../../blockchain/IWeb3";
4+
import { TransactionBase } from "../bean/TransactionBase";
25
let solc = require('solc')
36
let fs = require('fs')
47
let nodePath = require('path')
58

69
@injectable()
710
export class ContractService {
811

9-
getAbi(contractName: string, source: string, path: string) {
12+
getAbi(contractName: string, source: string, path: string): any {
1013
const contract = this.compileContract(contractName, source, path)
1114
return contract.abi
1215
}
1316

14-
compileContract(contractName: string, source: string, path: string) {
17+
compileContract(contractName: string, source: string, path: string): any {
1518
const compileJson = this.generateCompileObject(contractName, source, path)
1619
const compiledContract = JSON.parse(solc.compileStandardWrapper(JSON.stringify(compileJson)))
1720
const contractWithExt = `${contractName}.sol`
@@ -22,6 +25,33 @@ export class ContractService {
2225
return contract
2326
}
2427

28+
async deployContractSource <T extends TransactionBase> (web3config: any, contractName: string, source: string, path: string, txBase: T): Promise<any> {
29+
const iWeb3: IWeb3 = new Web3Instance(web3config)
30+
const web3 = iWeb3.getInstance()
31+
const compiledContract = this.compileContract(contractName, source, path)
32+
const bytecode = compiledContract.evm.bytecode.object
33+
return this.sendTx(web3, null, bytecode, txBase.from, txBase.gas, txBase.gasPrice, txBase.value)
34+
}
35+
36+
async runFunction <T extends TransactionBase> (web3config: any, abi: any, params: string[], to: string, txBase: T): Promise<any> {
37+
const iWeb3: IWeb3 = new Web3Instance(web3config)
38+
const web3 = iWeb3.getInstance()
39+
const functionCallEncoded: string = web3.eth.abi.encodeFunctionCall(abi, params)
40+
return this.sendTx(web3, to, functionCallEncoded, txBase.from, txBase.gas, txBase.gasPrice, txBase.value)
41+
}
42+
43+
private async sendTx(web3: any, to: string, input: string, from?: string, gas?: number, gasPrice?: number, value?: number): Promise<any> {
44+
const accounts = await web3.eth.getAccounts()
45+
return web3.eth.sendTransaction({
46+
to,
47+
from: from || accounts[0],
48+
gas,
49+
gasPrice,
50+
value,
51+
input
52+
});
53+
}
54+
2555
private generateCompileObject(contractName: string, content: string, path: string) {
2656
const sources = {}
2757
sources[`${contractName}.sol`] = {

0 commit comments

Comments
 (0)