Skip to content

Commit

Permalink
refactor: GasPrice logic (#22)
Browse files Browse the repository at this point in the history
* chore: GasPrice values

* chore: Remove gasPrice from logs

* chore: Comments

* chore: Default const ordering

* chore: Update README

* chore: Update README
  • Loading branch information
ChefKai authored Aug 3, 2021
1 parent dc9b646 commit 6abc032
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 52 deletions.
49 changes: 25 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

### Description

The scheduler is composed of multiple scripts used to call `startLottery`, `closeLottery`, and `drawFinalNumberAndMakeLotteryClaimable` functions, and trigger events; as well as monitoring lottery results.
The scheduler is composed of multiple scripts used to call `startLottery`, `closeLottery`, and `drawFinalNumberAndMakeLotteryClaimable` functions, trigger events; and monitor lottery results.

### Configuration

Expand All @@ -12,23 +12,10 @@ The scheduler is composed of multiple scripts used to call `startLottery`, `clos
- `Rewards`: Reward breakdown per bracket (total must be equal to 10,000)
- `Treasury`: Fee (denominated as percentage) to 2 decimals (e.g.: 100 => 1%)

## Crontab

```shell script
# Close lottery
0 6,18 * * * cd /opt/pancake-lottery-scheduler && yarn execute:close:mainnet

# Draw lottery
3 6,18 * * * cd /opt/pancake-lottery-scheduler && yarn execute:draw:mainnet

# Start lottery
5 6,18 * * * cd /opt/pancake-lottery-scheduler && yarn execute:start:mainnet
```
> Configuration can be overwritten by editing [config.ts](config.ts) file.
### Deployment

Configuration can be overwritten by editing [config.ts](config.ts) file.

```shell script
# Export operator private key to be used on Hardhat Network Config.
$ export OPERATOR_PRIVATE_KEY=OPERATOR_PRIVATE_KEY
Expand All @@ -44,24 +31,38 @@ $ yarn execute:[command]:[network]

#### Network(s)

- `56` - Mainnet (`0x38`)
- Mainnet, chainId `56` ([documentation](https://docs.binance.org/smart-chain/developer/rpc.html#mainnetchainid-0x38-56-in-decimal))

- `97` - Testnet (`0x61`)
- Testnet, chainId `97` ([documentation](https://docs.binance.org/smart-chain/developer/rpc.html#testnetchainid-0x61-97-in-decimal))

#### Execution

```shell script
# Close lottery
0 6,18 * * * cd ~/pancake-lottery-scheduler && yarn execute:close:mainnet

# Draw lottery
3 6,18 * * * cd ~/pancake-lottery-scheduler && yarn execute:draw:mainnet

# Start lottery
5 6,18 * * * cd ~/pancake-lottery-scheduler && yarn execute:start:mainnet
```

### Logging

Logs will be generated at `logs/lottery-YYYY-MM-DD.log` and will be archived (.gz), with a daily rotation.
Logs will be generated at `logs/lottery-YYYY-MM-DD.log` and will be archived (.gz) with a daily rotation.

Example of logs for success:
Examples of success logs:

```log
{"message":"[1970-01-01T06:05:00.000Z] network=testnet block=10000000 message='Started lottery' hash=0x... gasPrice=10.0 signer=0x...","level":"info"}
{"message":"[1970-01-01T18:00:00.000Z] network=testnet block=10010010 message='Closed lottery #123' hash=0x... gasPrice=10.0 signer=0x...","level":"info"}
{"message":"[1970-01-01T18:03:00.000Z] network=testnet block=10100100 message='Drawed lottery #123' hash=0x... gasPrice=10.0 signer=0x...","level":"info"}
{"message":"[1970-01-01T18:05:00.000Z] network=testnet block=11001000 message='Started lottery' hash=0x... gasPrice=10.0 signer=0x...","level":"info"}
{"message":"[1970-01-01T06:05:00.000Z] network=testnet block=10000000 message='Started lottery' hash=0x... signer=0x...","level":"info"}
{"message":"[1970-01-01T18:00:00.000Z] network=testnet block=10010010 message='Closed lottery #123' hash=0x... signer=0x...","level":"info"}
{"message":"[1970-01-01T18:03:00.000Z] network=testnet block=10100100 message='Drawed lottery #123' hash=0x... signer=0x...","level":"info"}
{"message":"[1970-01-01T18:05:00.000Z] network=testnet block=11001000 message='Started lottery' hash=0x... signer=0x...","level":"info"}
{"message":"[1970-01-01T18:10:00.000Z] network=testnet block=10010001 message='Injected lottery #124' hash=0x... signer=0x...","level":"info"}
```

Example of logs for error:
Examples of error logs:

```log
{"message":"[1970-01-01T06:00:00.000Z] network=testnet message='Unsupported network'","level":"error"}
Expand Down
15 changes: 5 additions & 10 deletions scripts/close-lottery.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { BigNumber } from "@ethersproject/bignumber";
import { formatUnits } from "@ethersproject/units";
import { ethers, network } from "hardhat";
import lotteryABI from "../abi/PancakeSwapLottery.json";
import randomGeneratorABI from "../abi/RandomNumberGenerator.json";
Expand Down Expand Up @@ -32,9 +30,9 @@ const main = async () => {
const contract = await ethers.getContractAt(lotteryABI, config.Lottery[networkName]);

// Get network data for running script.
const [_gasPrice, _blockNumber, _lotteryId, _randomGenerator] = await Promise.all([
ethers.provider.getGasPrice(),
const [_blockNumber, _gasPrice, _lotteryId, _randomGenerator] = await Promise.all([
ethers.provider.getBlockNumber(),
ethers.provider.getGasPrice(),
contract.currentLotteryId(),
contract.randomGenerator(),
]);
Expand All @@ -43,21 +41,18 @@ const main = async () => {
const randomGeneratorContract = await ethers.getContractAt(randomGeneratorABI, _randomGenerator);
const keyHash = await randomGeneratorContract.keyHash();
if (keyHash !== config.ChainlinkVRF.keyHash[networkName]) {
throw new Error("Invalid keyHash on RandomGenerator contract");
throw new Error("Invalid keyHash on RandomGenerator contract.");
}

// Double the recommended gasPrice from the network for faster validation.
const gasPrice: BigNumber = _gasPrice.mul(BigNumber.from(2));

// Create, sign and broadcast transaction.
const tx = await contract.closeLottery(_lotteryId.toString(), {
gasPrice: gasPrice.toString(),
gasPrice: _gasPrice.mul(2),
from: operator.address,
});

const message = `[${new Date().toISOString()}] network=${networkName} block=${_blockNumber.toString()} message='Closed lottery #${_lotteryId}' hash=${
tx?.hash
} gasPrice=${formatUnits(gasPrice.toString(), "gwei")} signer=${operator.address}`;
} signer=${operator.address}`;
console.log(message);
logger.info({ message });
} catch (error) {
Expand Down
13 changes: 4 additions & 9 deletions scripts/draw-lottery.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { BigNumber } from "@ethersproject/bignumber";
import { formatUnits } from "@ethersproject/units";
import { ethers, network } from "hardhat";
import lotteryABI from "../abi/PancakeSwapLottery.json";
import config from "../config";
Expand Down Expand Up @@ -31,24 +29,21 @@ const main = async () => {
const contract = await ethers.getContractAt(lotteryABI, config.Lottery[networkName]);

// Get network data for running script.
const [_gasPrice, _blockNumber, _lotteryId] = await Promise.all([
ethers.provider.getGasPrice(),
const [_blockNumber, _gasPrice, _lotteryId] = await Promise.all([
ethers.provider.getBlockNumber(),
ethers.provider.getGasPrice(),
contract.currentLotteryId(),
]);

// Double the recommended gasPrice from the network for faster validation.
const gasPrice: BigNumber = _gasPrice.mul(BigNumber.from(2));

// Create, sign and broadcast transaction.
const tx = await contract.drawFinalNumberAndMakeLotteryClaimable(_lotteryId.toString(), true, {
gasPrice: gasPrice.toString(),
gasPrice: _gasPrice.mul(2),
from: operator.address,
});

const message = `[${new Date().toISOString()}] network=${networkName} block=${_blockNumber.toString()} message='Drawed lottery #${_lotteryId}' hash=${
tx?.hash
} gasPrice=${formatUnits(gasPrice.toString(), "gwei")} signer=${operator.address}`;
} signer=${operator.address}`;
console.log(message);
logger.info({ message });
} catch (error) {
Expand Down
14 changes: 5 additions & 9 deletions scripts/start-lottery.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { BigNumber } from "@ethersproject/bignumber";
import { formatUnits, parseUnits } from "@ethersproject/units";
import { parseUnits } from "@ethersproject/units";
import { ethers, network } from "hardhat";
import lotteryABI from "../abi/PancakeSwapLottery.json";
import { getEndTime, getTicketPrice } from "../utils";
Expand Down Expand Up @@ -32,14 +31,11 @@ const main = async () => {
const contract = await ethers.getContractAt(lotteryABI, config.Lottery[networkName]);

// Get network data for running script.
const [_gasPrice, _blockNumber] = await Promise.all([
ethers.provider.getGasPrice(),
const [_blockNumber, _gasPrice] = await Promise.all([
ethers.provider.getBlockNumber(),
ethers.provider.getGasPrice(),
]);

// Double the recommended gasPrice from the network for faster validation.
const gasPrice: BigNumber = _gasPrice.mul(BigNumber.from(2));

// Get ticket price (in Cake equivalent), for a given network.
const ticketPrice: string = await getTicketPrice(
networkName,
Expand All @@ -54,12 +50,12 @@ const main = async () => {
config.Discount[networkName],
config.Rewards[networkName],
config.Treasury[networkName],
{ gasPrice: gasPrice.toString(), from: operator.address }
{ gasPrice: _gasPrice.mul(2), from: operator.address }
);

const message = `[${new Date().toISOString()}] network=${networkName} block=${_blockNumber.toString()} message='Started lottery' hash=${
tx?.hash
} gasPrice=${formatUnits(gasPrice.toString(), "gwei")} signer=${operator.address}`;
} signer=${operator.address}`;
console.log(message);
logger.info({ message });
} catch (error) {
Expand Down

0 comments on commit 6abc032

Please sign in to comment.