【发布时间】:2018-05-20 20:55:36
【问题描述】:
我目前正在使用 Docker 运行一个节点服务器,它将与已上传的智能合约进行交互。这是我的 Docker 文件:
FROM node:7
WORKDIR /app
COPY package.json /app
RUN npm install
RUN npm install -g npm
RUN ls
COPY . /app
CMD node gameserver.js
EXPOSE 8081
ARG walletPrivate
ENV walletPrivate = $private
ARG walletPublic
ENV walletPublic = $public
我正在定义我将在运行时传递的变量。这是服务器代码:
const express = require('express');
const Web3 = require('web3');
const Tx = require('ethereumjs-tx');
const MissionTrackerJson = require('./contracts/MissionTracker.json');
const app = express();
const port = process.env.PORT || 5001;
const providerUrl = 'https://rinkeby.infura.io/N9Txfkh1TNZhoeKXV6Xm';
const gamePublicKey = process.env.public;
const gamePrivateKey = process.env.private;
const production = true;
let contractAddress = null;
let contract = null;
let web3 = new Web3(new Web3.providers.HttpProvider(providerUrl));
if (typeof web3 === 'undefined') throw 'No web3 detected. Is Metamask/Mist being used?';
console.log("Using web3 version: " + Web3.version);
let contractDataPromise = MissionTrackerJson;
let networkIdPromise = web3.eth.net.getId(); // resolves on the current network id
Promise.all([contractDataPromise, networkIdPromise])
.then(results => {
let contractData = results[0];
let networkId = results[1];
// Make sure the contract is deployed on the connected network
if (!(networkId in contractData.networks)) {
throw new Error("Contract not found in selected Ethereum network on MetaMask.");
}
contractAddress = contractData.networks[networkId].address;
contract = new web3.eth.Contract(contractData.abi, contractAddress);
app.listen(port, () => console.log(`Site server on port ${port}`));
})
.catch(console.error);
if (production) {
app.use('/', express.static(`${__dirname}/client/build`));
}
app.get('/api/complete_checkpoint/:reviewer/:checkpoint', (req, res) => {
let reviewerId = req.params.reviewer;
let checkpointId = req.params.checkpoint;
let encodedABI = contract.methods.setCheckpointComplete(reviewerId, checkpointId).encodeABI();
web3.eth.getTransactionCount(gamePublicKey, 'pending')
.then(nonce => {
let rawTx = {
from: gamePublicKey,
to: contractAddress,
gas: 2000000,
data: encodedABI,
gasPrice: '100',
nonce,
};
let tx = new Tx(rawTx);
tx.sign(gamePrivateKey);
let serializedTx = tx.serialize();
web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex'))
.on('receipt', console.log)
.catch(console.error);
})
});
app.get('/api/add_checkpoint/:checkpoint_name', (req, res) => {
console.log("hello");
let checkpointName = decodeURIComponent(req.params.checkpoint_name);
let encodedABI = contract.methods.addGameCheckpoint(checkpointName).encodeABI();
web3.eth.getTransactionCount(gamePublicKey, 'pending')
.then(nonce => {
let rawTx = {
from: gamePublicKey,
to: contractAddress,
gas: 2000000,
data: encodedABI,
gasPrice: '100',
nonce,
};
let tx = new Tx(rawTx);
tx.sign(gamePrivateKey);
let serializedTx = tx.serialize();
web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex'))
.on('receipt', console.log)
.catch(console.error);
})
});
console.log("end");
要调用合约,我需要使用 HTTP GET 方法 ping 服务器。当我运行以下命令时,我发现我的 Docker 服务器的 IP 地址是 172.17.0.2:8081:
docker run -t -i --env private=0x12345 --env public=0x11111 -p 8081:8081 game_server
我正在制作我的外向端口 8081
如何向我的服务器发送 HTTP GET 方法?我应该寻找其他地址吗?
非常感谢您的帮助!
【问题讨论】:
-
这不是一个问题——你能解释一下你的问题或问什么吗?
-
添加到问题中:如何将 HTTP GET 方法发送到我的服务器?我应该寻找其他地址吗?
标签: node.js docker get smartcontracts web3