Implemented /api/v1/tx mock

This commit is contained in:
Kirill Fedoseev 2019-11-08 16:02:47 +03:00
parent 64e4d5e9f1
commit e832874891
7 changed files with 95 additions and 21 deletions

View File

@ -10,5 +10,9 @@
},
"engines": {
"node": ">=10.6.0"
},
"dependencies": {
"@binance-chain/javascript-sdk": "^2.16.1",
"@tendermint/amino-js": "^0.4.5"
}
}

View File

@ -16,20 +16,3 @@ RUN ./fullnode/testnet/${BNC_VERSION}/linux/bnbchaind testnet --acc-prefix tbnb
RUN sed -i "s/publishTransfer = false/publishTransfer = true/" ./mytestnet/node0/gaiad/config/app.toml && \
sed -i "s/publishLocal = false/publishLocal = true/" ./mytestnet/node0/gaiad/config/app.toml
#echo 12345678 | docker exec -i 5f6f653730442d81b6e34a82ac3dc8a82f071c1eec130bc09a0dca803ad00bd2 ./tbnbcli send --to tbnb1efjg7xt98t67ql2cmwjc5860lgayet9l8m55ym --amount 100:BNB --from node0 --chain-id Binance-Dev --json
FROM alpine:3.9.4
ARG BNC_VERSION=0.6.2
WORKDIR /bnc
COPY --from=build /binaries/fullnode/testnet/${BNC_VERSION}/linux/bnbchaind ./
COPY --from=build /binaries/cli/testnet/${BNC_VERSION}/linux/tbnbcli ./
COPY --from=build /binaries/mytestnet/node0/gaiacli /root/.bnbcli
COPY --from=build /binaries/mytestnet/node0/gaiad /root/.bnbchaind
EXPOSE 26657
ENTRYPOINT ["./bnbchaind", "start"]

View File

@ -1,7 +1,11 @@
FROM node:10.16.0-alpine
ARG BNC_VERSION=0.6.2
WORKDIR /api-server
COPY --from=testnet-binaries /binaries/cli/testnet/${BNC_VERSION}/linux/tbnbcli ./
COPY ./package.json ./
RUN npm install

View File

@ -1,10 +1,73 @@
const fs = require('fs')
const express = require('express')
const axios = require('axios')
const { execSync } = require('child_process')
const rpcClient = axios.create({
baseURL: process.env.FOREIGN_RPC_URL,
timeout: 10000
})
async function delay(ms) {
await new Promise((res) => setTimeout(res, ms))
}
async function retry(getPromise, n = -1, sleep = 3000) {
while (n) {
try {
return await getPromise()
} catch (e) {
console.debug(`Promise failed, retrying, ${n - 1} attempts left`)
await delay(sleep)
// eslint-disable-next-line no-param-reassign
n -= 1
}
}
return null
}
async function sendRpcRequest(subUrl, method, params) {
console.trace(`Request to ${subUrl}, method ${method}, params `, params)
const response = await retry(() => rpcClient.post(subUrl, {
jsonrpc: '2.0',
method,
params,
id: 1
}))
console.trace('Response, ', response.data)
return response.data
}
const app = express()
app.get('/', (req, res) => {
res.send(fs.readFileSync('./marketdata/marketdata.json').toString())
// GET
// /api/v1/tx/:hash
// /api/v1/time
// /api/v1/transactions
// ?address=a&side=RECEIVE&txAsset=FOREIGN_ASSET&txType=TRANSFER&startTime=111&endTime=222
// /api/v1/account/:account
// /api/v1/account/:account/sequence
// POST
// /api/v1/broadcast?sync=true
app.get('/api/v1/tx/:hash', async (req, res) => {
try {
const {
tx, hash, height, result
} = JSON.parse(
execSync(`./tbnbcli tx ${req.params.hash} --node "http://node:26657" --chain-id Binance-Dev`)
)
res.send({
code: 0,
hash,
height,
log: result.log,
ok: true,
tx
})
} catch (e) {
res.status(404).end()
}
})
app.listen(8000, () => {

View File

@ -1,10 +1,12 @@
version: '3.0'
services:
testnet:
build: ./testnet
node:
build: node
image: bnc-testnet
networks:
- binance_rpc_net
ports:
- '26657:26657'
volumes:
- 'marketdata:/root/.bnbchaind/marketdata'
api-server:

View File

@ -0,0 +1,15 @@
#echo 12345678 | docker exec -i 5f6f653730442d81b6e34a82ac3dc8a82f071c1eec130bc09a0dca803ad00bd2 ./tbnbcli send --to tbnb1efjg7xt98t67ql2cmwjc5860lgayet9l8m55ym --amount 100:BNB --from node0 --chain-id Binance-Dev --json
FROM alpine:3.9.4
ARG BNC_VERSION=0.6.2
WORKDIR /bnc
COPY --from=testnet-binaries /binaries/fullnode/testnet/${BNC_VERSION}/linux/bnbchaind ./
COPY --from=testnet-binaries /binaries/cli/testnet/${BNC_VERSION}/linux/tbnbcli ./
COPY --from=testnet-binaries /binaries/mytestnet/node0/gaiacli /root/.bnbcli
COPY --from=testnet-binaries /binaries/mytestnet/node0/gaiad /root/.bnbchaind
EXPOSE 26657
ENTRYPOINT ["./bnbchaind", "start"]

View File

@ -3,4 +3,7 @@
set -e
docker network create binance_net > /dev/null 2>&1 || true
docker build -t testnet-binaries .
docker-compose up --build