-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
32 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import express from 'express'; | ||
import Web3 from 'web3'; | ||
import { abi, bytecode } from '../contracts/MyContract'; | ||
|
||
const router = express.Router(); | ||
const web3 = new Web3(new Web3.providers.HttpProvider('https://mainnet.infura.io/v3/YOUR_PROJECT_ID')); | ||
|
||
router.post('/deploy-contract', async (req, res) => { | ||
const { from, gas, gasPrice } = req.body; | ||
const contract = new web3.eth.Contract(abi); | ||
const deployTx = contract.deploy({ data: bytecode, arguments: [] }); | ||
const txCount = await web3.eth.getTransactionCount(from); | ||
const tx = { | ||
from, | ||
gas, | ||
gasPrice, | ||
data: deployTx.encodeABI(), | ||
nonce: web3.utils.toHex(txCount), | ||
}; | ||
const signedTx = await web3.eth.accounts.signTransaction(tx, '0xYOUR_PRIVATE_KEY'); | ||
const receipt = await web3.eth.sendSignedTransaction(signedTx.rawTransaction); | ||
res.json({ contractAddress: receipt.contractAddress }); | ||
}); | ||
|
||
router.post('/interact-contract', async (req, res) => { | ||
const { contractAddress, functionName, args } = req.body; | ||
const contract = new web3.eth.Contract(abi, contractAddress); | ||
const result = await contract.methods[functionName](...args).call(); | ||
res.json({ result }); | ||
}); | ||
|
||
export default router; |