Skip to content

Commit

Permalink
surplus keeper update (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
bkellerman authored Apr 28, 2022
1 parent 7dba21b commit f20ef02
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 60 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,14 +202,15 @@ For some known Ubuntu and macOS issues see the [pyflex](https://github.com/refle

### Testing

Note: Tests are currently broken as the testchains need to be updated with the GEB_REDEMPTION_PRICE_SNAP contract.

This project uses [pytest](https://docs.pytest.org/en/latest/) for unit testing. Testing depends on a dockerized local testchain included in `lib\pyflex\tests\config`.

In order to be able to run tests you should execute:

```text
git clone https://github.com/reflexer-labs/auction-keeper.git
cd auction-keeper
git checkout tags/prai-demo
git submodule update --init --recursive
./install.sh
source _virtualenv/bin/activate
Expand Down
2 changes: 1 addition & 1 deletion auction_keeper/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ def __init__(self, args: list, **kwargs):
self.safe_history = SAFEHistory(self.web3, self.geb, self.collateral_type, self.from_block,
self.graph_endpoints, self.arguments.graph_block_threshold)
elif self.surplus_auction_house:
self.strategy = SurplusAuctionStrategy(self.surplus_auction_house, self.prot.address)
self.strategy = SurplusAuctionStrategy(self.surplus_auction_house, self.prot.address, self.geb)
elif self.debt_auction_house:
self.strategy = DebtAuctionStrategy(self.debt_auction_house)
else:
Expand Down
12 changes: 9 additions & 3 deletions auction_keeper/strategy.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,14 +200,16 @@ def bid(self, id: int) -> Tuple[Optional[Wad], Optional[Transact], Optional[Rad]
return our_approximate_price, self.collateral_auction_house.buy_collateral(id, our_bid), Rad(our_bid)

class SurplusAuctionStrategy(Strategy):
def __init__(self, surplus_auction_house: PreSettlementSurplusAuctionHouse, prot: Address):
def __init__(self, surplus_auction_house: PreSettlementSurplusAuctionHouse, prot: Address, geb: GfDeployment):
assert isinstance(surplus_auction_house, PreSettlementSurplusAuctionHouse)
assert isinstance(prot, Address)
assert isinstance(geb, GfDeployment)
super().__init__(surplus_auction_house)

self.surplus_auction_house = surplus_auction_house
self.bid_increase = surplus_auction_house.bid_increase()
self.prot = prot
self.geb = geb

def approve(self, gas_price: GasPrice):
self.surplus_auction_house.approve(self.prot, directly(gas_price=gas_price))
Expand All @@ -221,6 +223,9 @@ def get_input(self, id: int) -> Status:
# Read auction state
bid = self.surplus_auction_house.bids(id)

# get latest redemption price
redemption_price = self.geb.redemption_price_snap.snapped_price()

# Prepare the model input from auction state
return Status(id=id,
collateral_auction_house=None,
Expand All @@ -236,14 +241,15 @@ def get_input(self, id: int) -> Status:
block_time=block_time(self.surplus_auction_house.web3),
bid_expiry=bid.bid_expiry,
auction_deadline=bid.auction_deadline,
price=Wad(bid.amount_to_sell / Rad(bid.bid_amount)) if bid.bid_amount > Wad.from_number(0.000001) else None)
price=Wad(bid.amount_to_sell * Rad(redemption_price) / Rad(bid.bid_amount)) if bid.bid_amount > Wad.from_number(0) else None)

def bid(self, id: int, price: Wad) -> Tuple[Optional[Wad], Optional[Transact], Optional[Rad]]:
assert isinstance(id, int)
assert isinstance(price, Wad)

bid = self.surplus_auction_house.bids(id)
our_bid = bid.amount_to_sell / Rad(price)
redemption_price = self.geb.redemption_price_snap.snapped_price()
our_bid = bid.amount_to_sell * Rad(redemption_price) / Rad(price)

if our_bid >= Rad(bid.bid_amount) * Rad(self.bid_increase) and our_bid > Rad(bid.bid_amount):
return price, self.surplus_auction_house.increase_bid_size(id, bid.amount_to_sell, Wad(our_bid)), Rad(our_bid)
Expand Down
32 changes: 32 additions & 0 deletions models/surplus_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/env python3

import sys
import os
import json

# Example stdin input
"""
{"id": "6", "bid_amount": "0.000000000000000000", "amount_to_sell": "0.000000000000000000000000500000000000000000000", "block_time": 1651155568, "auction_deadline": 1651412740, "price": null, "bid_increase": "1.030000000000000000", "high_bidder": "0x6073E8FE874B53732b5DdD469a2De4047f33C64B", "surplus_auction_house": "0xCdaA2ec0975eD41202E1078b21a4833E414f6379"}
"""

# FLX Price to bid
MAX_BID_PRICE = 60
MIN_BID_PRICE = 0.00000000001

while True:
for auction_state in sys.stdin:
auction_state = json.loads(auction_state)

# If we are already the high bidder, do nothing
if auction_state['high_bidder'] == os.environ['ACCOUNT_ADDRESS']:
continue

# No bids yet
if float(auction_state['bid_amount']) == 0:
bid = {'price': str(MIN_BID_PRICE)}
print(json.dumps(bid), flush=True)
else:
my_bid_price = float(auction_state['price']) * float(auction_state['bid_increase'])
if my_bid_price <= MAX_BID_PRICE:
bid = {'price': str(my_bid_price)}
print(json.dumps(bid), flush=True)
5 changes: 5 additions & 0 deletions models/surplus_model.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env bash
while true; do
echo "{\"price\": \"50.0\"}"
sleep 120
done
24 changes: 24 additions & 0 deletions run_surplus_keeper.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/bash

KEEPER_ADDRESS=<KEEPER ADDRESS>
ETH_RPC_URL=<ETH RPC URL>

KEYSTORE_DIR=<FULL PATH OF KEYSTORE DIR>
KEYSTORE_FILE=<KEYSTORE FILE>
MODEL_DIR=<FULL PATH OF MODEL DIR>
MODEL_FILE=<MODEL FILE>

docker pull reflexer/auction-keeper:latest

docker run -it \
-v ${KEYSTORE_DIR}:/keystore \
-v ${MODEL_DIR}:/models \
--env ACCOUNT_ADDRESS=${KEEPER_ADDRESS} \
reflexer/auction-keeper:latest \
--type surplus \
--model /models/${MODEL_FILE} \
--rpc-uri ${ETH_RPC_URL} \
--eth-from ${KEEPER_ADDRESS} \
--eth-key "key_file=/keystore/${KEYSTORE_FILE}" \
--block-check-interval 30 \
--bid-check-interval 30 \
39 changes: 8 additions & 31 deletions surplus-auction-keeper/running-in-docker.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,43 +8,20 @@ description: Running a surplus auction keeper in a Docker container
In order to participate in surplus auctions you need to bid with protocol tokens
{% endhint %}

## 1\) Create a model file
## 1\) Modify model file as needed

Pick a protocol token/system coin price and paste the following code into `surplus_model.sh`:

```text
#!/usr/bin/env bash
while true; do
echo "{\"price\": \"325.0\"}"
sleep 120
done
```
A basic surplus auction bidding model can be found in `models/surplus_model.py`. It can be modifed to change `MAX_BID_PRICE` or fetch the latest protocol token price from an external source.

### Then:

`chmod +x surplus_model.sh`
`chmod +x surplus_model.py`

For more information about bidding models, see this.
For more information about bidding models, see [Bidding Models](BiddingModels.md)

## 2\) Create the keeper run file
## 2\) Modify keeper run file

Create a file called `run_auction_keeper.sh` and paste the following code in it:

```text
#!/bin/bash
docker run -it \
-v <KEYSTORE_DIR>:/keystore \
-v <MODEL_DIR>:/models \
reflexer/auction-keeper \
--type surplus \
--model /models/surplus_model.sh \
--rpc-uri <ETH_RPC_URL> \
--eth-from <KEEPER_ADDRESS> \
--eth-key key_file=/keystore/<KEYSTORE_FILE>
```
Modify the following variables in `run_surplus_keeper.sh`

### Then, substitute the following variables:

`KEYSTORE_DIR` - the local directory where your keystore file is

Expand All @@ -63,11 +40,11 @@ For more information about this keystore format and how to generate them:

### Then:

`chmod +x run_auction_keeper.sh`
`chmod +x run_surplus_keeper.sh`

## 4\) Start the keeper and enter your keystore file password

`./run_auction_keeper.sh`
`./run_surplus_keeper.sh`

```text
$ ./run_auction_keeper.sh
Expand Down
42 changes: 19 additions & 23 deletions surplus-auction-keeper/running-on-a-host.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,41 +30,34 @@ This creates a virtual environment and installs requirements:

`source _virtualenv/bin/activate`

## 2\) Create a model file
## 2\) Modify model file as needed

Pick a protocol token/system coin price and paste the following code into `surplus_model.sh`:

```text
#!/usr/bin/env bash
while true; do
echo "{\"price\": \"325.0\"}"
sleep 120
done
```
A basic surplus auction bidding model can be found in `models/surplus_model.py`. It can be modifed to change `MAX_BID_PRICE` or fetch the latest protocol token price from an external source.

### Then:

`chmod +x surplus_model.sh`
`chmod +x surplus_model.py`

For more information about bidding models, see [Bidding Models](BiddingModels.md)

## 3\) Create the keeper run file

Create a file called `run_auction_keeper.sh` and paste the following code in it:
Create a file called `run_surplus_keeper.sh` and paste the following code in it:

```text
#!/bin/bash
bin/auction-keeper \
--type surplus \
--model surplus_model.sh \
--model surplus_model.py \
--rpc-uri <ETH_RPC_URL> \
--eth-from <KEEPER_ADDRESS> \
--eth-key key_file=<KEYSTORE_FILE>
```

## Surplus Auctioning Process
--eth-key key_file=<KEYSTORE_FILE> \
--block-check-interval 30 \
--bid-check-interval 30
[Surplus Auctioning Process](surplus-auctions.md)
```

### Then, substitute the following variables:
Modify the following variables in `run_surplus_keeper.sh`

`ETH_RPC_URL` - the URL of your ethereum RPC connection

Expand All @@ -77,16 +70,19 @@ For more information about this keystore format and how to generate them, check:
* [Ethereum UTC / JSON Wallet Encryption](https://wizardforcel.gitbooks.io/practical-cryptography-for-developers-book/content/symmetric-key-ciphers/ethereum-wallet-encryption.html)
* [keythereum](https://github.com/ethereumjs/keythereum)

### Finally:
### Ensure script is executable

`chmod +x run_auction_keeper.sh`
`chmod +x run_surplus_keeper.sh`

## 4\) Start the keeper and enter your keystore file password

`./run_auction_keeper.sh`
`./run_surplus_keeper.sh`

```text
$ ./run_auction_keeper.sh
$ ./run_surplus_keeper.sh
Password for /keystore/key.json:
```

## Surplus Auctioning Process

[Surplus Auctioning Process](surplus-auctions.md)

0 comments on commit f20ef02

Please sign in to comment.