Skip to content

Instantly share code, notes, and snippets.

@yurenju
Created December 15, 2019 03:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yurenju/00bbf9b7ce7e0127c39ef87252f6335b to your computer and use it in GitHub Desktop.
Save yurenju/00bbf9b7ce7e0127c39ef87252f6335b to your computer and use it in GitHub Desktop.
def transfer(from_agent, to_agent, currency, amount):
from_amount = getattr(from_agent, currency, 0)
to_amount = getattr(to_agent, currency, 0)
setattr(from_agent, currency, from_amount - amount)
setattr(to_agent, currency, to_amount + amount)
class Uniswap(Agent):
def __init__(self, unique_id, model, dai, eth):
super().__init__(unique_id, model)
self.dai = dai
self.eth = eth
self.base_price = self.get_price()
self.external_price = self.base_price
def step(self):
self.external_price = self.base_price * (1 + math.sin(0.1 * self.model.schedule.steps) / 10)
def get_price(self):
return self.dai / self.eth
def trade(self, trader, currency, amount):
if currency == "eth":
eth_amount = self.eth - (self.eth * self.dai) / (self.dai + amount)
transfer(trader, self, "dai", amount)
transfer(self, trader, "eth", eth_amount)
else:
dai_amount = self.dai - (self.eth * self.dai) / (self.eth + amount)
transfer(trader, self, "eth", amount)
transfer(self, trader, "dai", dai_amount)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment