Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

secrets loader integration #2

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
## NODE_ENV = development || production || qa || etc.
NODE_ENV=development

## APP CAN BE USED TO DECLARE THE RESPONSIBILITY OF THIS DEPLOYMENT
APP=dec-market-maker

## Set USE_AWS_SECRETS to true if you want secrets to load from Secrets Manager

# USE_AWS_SECRETS=true

## AWS_REGION is required when USE_AWS_SECRETS=true,
## but will default to us-east-1

# AWS_REGION=us-east-1

## You may set SECRETS_KEY_PREFIX when USE_AWS_SECRETS=true
## This will result in fetching keys with the specified prefix
## e.g. if SECRETS_KEY_PREFIX=production the key to retrieve the db connection
## would be production/database/steemmonsters

#SECRETS_KEY_PREFIX=production


## SECRETS_VALIDATION_MODE WILL BE INJECTED INTO THE SECRETS CONFIG
## AND CAN BE USED DURING SCHEMA VALIDATION
SECRETS_VALIDATION_MODE=development

## AWS ENV variables for assuming a role with access to restricted secrets
# AWS_ACCESS_KEY_ID=
# AWS_SECRET_ACCESS_KEY=
# AWS_SESSION_TOKEN=


## IF USE_AWS_SECRETS=false
## These env variables will be used to populate the config
SM_ACTIVE_KEY=
## OPTIONAL
## END OPTIONAL
## END IF USE_AWS_SECRETS=false

## Overrides for default AWS Secrets Keys
## These variables will only be used when USE_AWS_SECRETS=true and,
## they only need to be set if you are overriding the default secrets
## locations for one or more of the secrets.

#AWS_SM_ACTIVEKEY_SECRET=

## END OVERRIDES FOR DEFAULT AWS SECRET LOCATIONS

# BEGIN ENV CONFIG

# END ENV CONFIG
4 changes: 1 addition & 3 deletions config-example.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

"prefix": "dev-sm_",
"account": "account_name",
"posting_key": "",

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just guessing posting_key wasn't used anywhere?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct

"active_key": "",

"fee_pct": 500,
"sm_api_url": "https://steemmonsters.com",
Expand All @@ -18,4 +16,4 @@
"chain_id": "ssc-mainnet-hive",
"deposit_account": "honey-swap"
}
}
}
29 changes: 20 additions & 9 deletions dec-market-maker.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,23 @@ var express = require('express');
var app = express();
const interface = require('@splinterlands/hive-interface');
const hive = new interface.Hive({ rpc_nodes: config.rpc_nodes });
const { secrets, secretsConfigSchema } = require('./secrets-config');
const { SecretsLoader, secretsConfig } = require('@steem-monsters/sl-secrets-loader/secrets-loader');

let last_block = 0;

start();

async function start() {
try{
// Need to ensure that async config is loaded before anything else initializes
const sl = new SecretsLoader(secretsConfigSchema);
await sl.loadConfig(secrets);
}catch(err){
utils.log(`Error loading secrets: ${err}`);
process.exit(1);
}

// Check if state has been saved to disk, in which case load it
if (fs.existsSync('state.json')) {
var state = JSON.parse(fs.readFileSync("state.json"));
Expand Down Expand Up @@ -144,7 +155,7 @@ async function processOp(op, block_num, block_id, prev_block_id, trx_id, block_t

if(conversion.hive < amount) {
// Not enough DEC available for sale, refund remaining HIVE
await hive.transfer(config.account, to, `${(amount - conversion.hive).toFixed(3)} HIVE`, `Not enough DEC available for purchase. Refunding remaining HIVE.`, config.active_key);
await hive.transfer(config.account, to, `${(amount - conversion.hive).toFixed(3)} HIVE`, `Not enough DEC available for purchase. Refunding remaining HIVE.`, secretsConfig.active_key);
}

let dec_amount_net_fee = +(conversion.DEC * (1 - config.fee_pct / 10000)).toFixed(3);
Expand All @@ -156,12 +167,12 @@ async function processOp(op, block_num, block_id, prev_block_id, trx_id, block_t
if(dec_balance < dec_amount_net_fee) {
// Insufficient balance, refund payment
utils.log(`Insufficient DEC balance [${dec_balance}]!`, 1, 'Red');
await hive.transfer(config.account, to, op[1].amount, `Insufficient DEC balance. Refunding payment.`, config.active_key);
await hive.transfer(config.account, to, op[1].amount, `Insufficient DEC balance. Refunding payment.`, secretsConfig.active_key);
return;
}

// Transfer the DEC minus the conversion fee
hive.custom_json(`${config.prefix}token_transfer`, { to: to, qty: dec_amount_net_fee, token: 'DEC' }, config.account, config.active_key, true);
hive.custom_json(`${config.prefix}token_transfer`, { to: to, qty: dec_amount_net_fee, token: 'DEC' }, config.account, secretsConfig.active_key, true);

// Deposit HIVE to Hive Engine, buy DEC, and withdraw it back to the game
poolBuy(amount);
Expand All @@ -179,7 +190,7 @@ async function poolBuy(amount) {
config.ssc.deposit_account,
`${amount.toFixed(3)} HIVE`,
`{"id":"${config.ssc.chain_id}","json":{"contractName":"hivepegged","contractAction":"buy","contractPayload":{}}}`,
config.active_key
secretsConfig.active_key
);

if(!deposit || !deposit.id) {
Expand Down Expand Up @@ -212,7 +223,7 @@ async function poolBuy(amount) {
"tradeType": "exactInput",
"maxSlippage": "10"
}
}, config.account, config.active_key, true);
}, config.account, secretsConfig.active_key, true);

if(!pool_swap || !pool_swap.id) {
utils.log(`Diesel pool swap of [${amount} HIVE] failed!`, 1, 'Red');
Expand Down Expand Up @@ -240,7 +251,7 @@ async function poolBuy(amount) {
"quantity": dec.toFixed(3),
"to": config.sm_account
}
}, config.account, config.active_key, true);
}, config.account, secretsConfig.active_key, true);

if(!dec_transfer || !dec_transfer.id) {
utils.log(`Transfer of [${dec} DEC] to @${config.sm_account} failed!`, 1, 'Red');
Expand All @@ -267,7 +278,7 @@ async function marketBuy(amount, dec_amount) {
config.ssc.deposit_account,
`${amount.toFixed(3)} HIVE`,
`{"id":"${config.ssc.chain_id}","json":{"contractName":"hivepegged","contractAction":"buy","contractPayload":{}}}`,
config.active_key
secretsConfig.active_key
);

if(!deposit || !deposit.id) {
Expand Down Expand Up @@ -297,7 +308,7 @@ async function marketBuy(amount, dec_amount) {
"quantity": dec_amount.toFixed(3),
"price": purchase_price
}
}, config.account, config.active_key, true);
}, config.account, secretsConfig.active_key, true);

if(!market_buy || !market_buy.id) {
utils.log(`Market buy of [${dec_amount} DEC] failed!`, 1, 'Red');
Expand Down Expand Up @@ -325,7 +336,7 @@ async function marketBuy(amount, dec_amount) {
"quantity": dec.toFixed(3),
"to": config.sm_account
}
}, config.account, config.active_key, true);
}, config.account, secretsConfig.active_key, true);

if(!dec_transfer || !dec_transfer.id) {
utils.log(`Transfer of [${dec} DEC] to @${config.sm_account} failed!`, 1, 'Red');
Expand Down
Loading