Skip to content

Commit

Permalink
Port dev image faucet from python to node #957
Browse files Browse the repository at this point in the history
  • Loading branch information
levackt committed Jul 12, 2022
1 parent a6071fa commit e17104d
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 101 deletions.
155 changes: 155 additions & 0 deletions deployment/docker/devimage/faucet/faucet_server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
const http = require("http");
const querystring = require("querystring");
const exec = require("child_process").exec;

const FAUCET_WALLET_NAME = process.env.FAUCET_WALLET_NAME || "a";
const FAUCET_AMOUNT = process.env.FAUCET_AMOUNT || "1000000000";
const DENOM = process.env.DENOM || "uscrt";

let faucet_address;

/**
* Execute a shell command and return it as a Promise.
* @param cmd {string}
* @return {Promise<string>}
*/
function execShellCommand(cmd) {
return new Promise((resolve, reject) => {
exec(cmd, (error, stdout, stderr) => {
if (error) {
console.error("error in execShellCommand", error);
reject(error);
} else if (stderr) {
console.error("stderr in execShellCommand", stderr);
reject(stderr);
} else {
resolve(JSON.parse(stdout));
}
});
});
}

/**
* Command to send coins.
* @param src_key_name source account key name, default 'a'
* @param src_address source account's secret address
* @param dest_address destination address
* @param amount amount to send
* @returns result of executing the command.
*/
async function send_command(src_key_name, src_address, dest_address, amount) {
const send_message = `secretd tx bank send ${src_address} ${dest_address} ${amount}${DENOM} --from ${src_key_name} --gas-prices 0.25uscrt -y`;
console.log(`send_message: \n ${send_message}`);

const result = await execShellCommand(send_message);
console.log(`Sent tokens with txhash: ${result.txhash}`);
return result.txhash;
}

/**
* Returns the address for the requested account key.
* @param key_name faucet account key to use, default 'a'
* @returns address
*/
async function get_address(key_name) {
// Already looked up, won't change while running
if (faucet_address !== undefined) {
return faucet_address;
}

const list_keys = "secretd keys list";
const result = await execShellCommand(list_keys);

for (index in result) {
const key = result[index];
if (key["name"] == key_name) {
console.log(`Found key with address: ${key["address"]}`);
faucet_address = key["address"];
break;
}
}

return faucet_address;
}

// Start the http server
const server = http.createServer();
server.on("request", async (req, res) => {
try {
// for root or status, return the configured faucet address and amount sent
if (req.url === "/" || req.url === "/status") {
const faucet_address = await get_address(FAUCET_WALLET_NAME);

if (faucet_address === undefined) {
console.error(
`No key account with required name: ${FAUCET_WALLET_NAME}`
);

res.writeHead(500, { "Content-Type": "application/json" });
res.write(
JSON.stringify({
error: `No key account with required name: ${FAUCET_WALLET_NAME}`,
})
);
res.end();
return;
} else {
res.writeHead(200, { "Content-Type": "application/json" });
res.write(
JSON.stringify({
faucet_address: faucet_address,
amount: FAUCET_AMOUNT,
})
);
res.end();
}
} else if (req.url.startsWith("/faucet")) {
// ensure address is present, not necessarily valid checksum
if (!req.url.startsWith("/faucet?address=")) {
res.writeHead(400, { "Content-Type": "application/json" });
res.write(JSON.stringify({ error: "address is required" }));
res.end();
return;
}

const address = querystring.parse(req.url)["/faucet?address"];
const faucet_address = await get_address(FAUCET_WALLET_NAME);

if (faucet_address === undefined) {
console.error(
`No key account with required name: ${FAUCET_WALLET_NAME}`
);

res.writeHead(500, { "Content-Type": "application/json" });
res.write(
JSON.stringify({
error: `No key account with required name: ${FAUCET_WALLET_NAME}`,
})
);
res.end();
return;
} else {
const txhash = await send_command(
FAUCET_WALLET_NAME,
faucet_address,
address,
FAUCET_AMOUNT
);

res.writeHead(200, { "Content-Type": "application/json" });
res.write(JSON.stringify({ txhash: txhash }));
res.end();
}
} else {
res.end("Invalid Request!");
}
} catch (err) {
res.writeHead(500, { "Content-Type": "application/json" });
res.write(JSON.stringify({ error: `${err.message}` }));
res.end();
}
});

server.listen(5000);

console.log("Secret Faucet is running on port 5000 ...");
4 changes: 0 additions & 4 deletions deployment/docker/devimage/faucet/requirements.txt

This file was deleted.

86 changes: 0 additions & 86 deletions deployment/docker/devimage/faucet/svc.py

This file was deleted.

12 changes: 1 addition & 11 deletions deployment/dockerfiles/dev-image.Dockerfile
Original file line number Diff line number Diff line change
@@ -1,17 +1,7 @@
# Final image
FROM build-release

RUN apt-get update && \
apt-get install -y --no-install-recommends \
#### Base utilities ####
python3.8 \
python3-pip

COPY deployment/docker/devimage/faucet/requirements.txt .

RUN pip install -r requirements.txt

COPY deployment/docker/devimage/bootstrap_init_no_stop.sh bootstrap_init.sh
COPY deployment/docker/devimage/faucet/svc.py .
COPY deployment/docker/devimage/faucet/faucet_server.js .

ENTRYPOINT ["./bootstrap_init.sh"]

0 comments on commit e17104d

Please sign in to comment.