Skip to content

Instantly share code, notes, and snippets.

@nazariyv
Created April 23, 2022 16:39
Show Gist options
  • Save nazariyv/e19571de32a5f2ac6c7b90828fe542b9 to your computer and use it in GitHub Desktop.
Save nazariyv/e19571de32a5f2ac6c7b90828fe542b9 to your computer and use it in GitHub Desktop.
Off-chain refund bids script
from typing import Dict, Tuple
from brownie import accounts, chain
from dataclasses import dataclass
# ! 1. losing threshold to be set manually here
# ! 2. bids to be set manually here
# ! 3. set the account to rkl refunder account in main
# if the bid is below this value, it has lost the auction
# it is possible that there may be two bids for the same
# value and one lost. In this case, timestamp needs to be
# looked at. This will be dealt with, if it occurs
# * ^if equal bids and one lost
LOSING_THRESHOLD = 0.7306900000000001
# * this is where you place the bids json that comes out
# * of the transformer
BIDS = {
"0x0000000000000000000000000000000000004444": 0.25,
"0x0000000000000000000000000000000000003333": 0.418885345123424,
"0x0000000000000000000000000000000000002222": 0.5,
"0x0000000000000000000000000000000000001111": 0.7306900000000001,
"0x000000000000000000000000000000000000dEa1": 0.81,
"0x000000000000000000000000000000000000dEaD": 0.9,
"0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045": 1.42069
}
def bids_ascending() -> Tuple[str, float]:
pairs = zip(BIDS.keys(), BIDS.values())
sorted_pairs = sorted(pairs, key=lambda x: x[1])
bids = [Bid(address, total_amount, 0) for (address, total_amount) in sorted_pairs]
return bids
@dataclass(frozen=True)
class Bid:
address: str
total_amount: float
original_timestamp: int
def is_losing_bid(self) -> bool:
# ! take into account timestamp if required
if self.total_amount < LOSING_THRESHOLD:
return True
return False
def refund_bid(bid: Bid, from_account) -> None:
# two types of bids
# - winning bid
# - losing bid
# We determine which one it is, by comparing the total bid
# to a losing threshold
# If winning bid, then refund the difference between the
# lowest winning bid and this given bid
# If losing bid, refund all
refund_amount = 0.0
# * < LOSING_THRESHOLD
if bid.is_losing_bid():
refund_amount = bid.total_amount
# * == LOSING_THRESHOLD
elif bid.total_amount == LOSING_THRESHOLD:
# the bid that was the lowest winning one
# does not get refunded
return
# * > LOSING_THRESHOLD
else:
refund_amount = bid.total_amount - LOSING_THRESHOLD
# !!!!!!!! NOTICE THE SCALE: ETHER !!!!!!!!!!!!
refund_amount = f'{refund_amount} ether'
# 15% higher than the recommended priority fee
priority_fee = int(1.15 * chain.priority_fee)
print(f'Refunding {refund_amount}. Priority fee: {priority_fee} wei.')
from_account.transfer(bid.address, refund_amount, priority_fee=priority_fee)
def main():
"""
Off-chain script to refund bidders.
"""
refunder = accounts.load("<refunder_account>")
bids = bids_ascending()
num_bids_to_refund = len(bids)
for i, bid in enumerate(bids):
print(f'refunding bid #{i + 1}/{num_bids_to_refund}.')
print(f'bidder: {bid.address}, total_amount: {bid.total_amount} wei.')
refund_bid(bid, refunder)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment