Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Examples in this guide are simplified Hic Et Nunc demo.
Create a new YAML file and adapt the following example to your needs:

```yaml
spec_version: 0.0.1
spec_version: 0.1
package: demo_hic_et_nunc

database:
Expand Down
33 changes: 23 additions & 10 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ services:
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD:-changeme}
- ADMIN_SECRET=${ADMIN_SECRET:-changeme}

# quipuswap:
# build: .
# depends_on:
# - db
# volumes:
# - ./src/demo_quipuswap/dipdup-docker.yml:/home/dipdup/dipdup.yml
# restart: always
# environment:
# - POSTGRES_PASSWORD=${POSTGRES_PASSWORD:-changeme}
# - ADMIN_SECRET=${ADMIN_SECRET:-changeme}
quipuswap:
build: .
depends_on:
- db
volumes:
- ./src/demo_quipuswap/dipdup-docker.yml:/home/dipdup/dipdup.yml
restart: always
environment:
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD:-changeme}
- ADMIN_SECRET=${ADMIN_SECRET:-changeme}

tzcolors:
build: .
Expand All @@ -34,8 +34,21 @@ services:
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD:-changeme}
- ADMIN_SECRET=${ADMIN_SECRET:-changeme}

domains:
build: .
depends_on:
- db
volumes:
- ./src/demo_tezos_domains/dipdup-docker.yml:/home/dipdup/dipdup.yml
restart: always
environment:
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD:-changeme}
- ADMIN_SECRET=${ADMIN_SECRET:-changeme}

db:
image: postgres:13
ports:
- 127.0.0.1:6423:5432
restart: always
volumes:
- db:/var/lib/postgres/data
Expand Down
41 changes: 7 additions & 34 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ python = "^3.8"
aiohttp = "^3.7.4"
aiomysql = "^0.0.21"
asyncpg = "^0.22.0"
datamodel-code-generator = "^0.10.0"
datamodel-code-generator = "^0.11.1"
"ruamel.yaml" = "^0.17.2"
tortoise-orm = "^0.17.1"
pydantic = "^1.8.1"
Expand Down
2 changes: 1 addition & 1 deletion src/demo_hic_et_nunc/dipdup.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
spec_version: 0.0.1
spec_version: 0.1
package: demo_hic_et_nunc

database:
Expand Down
8 changes: 8 additions & 0 deletions src/demo_quipuswap/dipdup-docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ templates:
entrypoint: divestLiquidity
- destination: <token_contract>
entrypoint: transfer
- callback: on_fa12_withdraw_profit
pattern:
- destination: <dex_contract>
entrypoint: withdrawProfit

quipuswap_fa2:
kind: operation
Expand Down Expand Up @@ -99,6 +103,10 @@ templates:
entrypoint: divestLiquidity
- destination: <token_contract>
entrypoint: transfer
- callback: on_fa20_withdraw_profit
pattern:
- destination: <dex_contract>
entrypoint: withdrawProfit

indexes:
kusd_mainnet:
Expand Down
10 changes: 9 additions & 1 deletion src/demo_quipuswap/dipdup.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
spec_version: 0.0.1
spec_version: 0.1
package: demo_quipuswap

database:
Expand Down Expand Up @@ -60,6 +60,10 @@ templates:
entrypoint: divestLiquidity
- destination: <token_contract>
entrypoint: transfer
- callback: on_fa12_withdraw_profit
pattern:
- destination: <dex_contract>
entrypoint: withdrawProfit

quipuswap_fa2:
kind: operation
Expand Down Expand Up @@ -90,6 +94,10 @@ templates:
entrypoint: divestLiquidity
- destination: <token_contract>
entrypoint: transfer
- callback: on_fa20_withdraw_profit
pattern:
- destination: <dex_contract>
entrypoint: withdrawProfit

indexes:
kusd_mainnet:
Expand Down
24 changes: 19 additions & 5 deletions src/demo_quipuswap/handlers/on_fa12_divest_liquidity.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from decimal import Decimal
from typing import cast

import demo_quipuswap.models as models
from demo_quipuswap.types.fa12_token.parameter.transfer import Transfer
from demo_quipuswap.types.quipu_fa12.parameter.divest_liquidity import DivestLiquidity
from demo_quipuswap.types.quipu_fa12.storage import Storage as QuipuFA12Storage
from dipdup.models import HandlerContext, OperationContext


Expand All @@ -15,12 +17,24 @@ async def on_fa12_divest_liquidity(
if ctx.template_values is None:
raise Exception('This index must be templated')

storage = cast(QuipuFA12Storage, divest_liquidity.storage) # FIXME: remove

decimals = int(ctx.template_values['decimals'])
trader, _ = await models.Trader.get_or_create(address=divest_liquidity.data.sender_address)
instrument, _ = await models.Instrument.get_or_create(symbol=ctx.template_values['symbol'])
position, _ = await models.Position.get_or_create(trader=trader, instrument=instrument)
symbol = ctx.template_values['symbol']
trader = divest_liquidity.data.sender_address

position, _ = await models.Position.get_or_create(trader=trader, symbol=symbol)
transaction = next(op for op in ctx.operations if op.amount)
position.tez_qty -= Decimal(transaction.amount) / (10 ** 6) # type: ignore
position.token_qty -= Decimal(transfer.parameter.value) / (10 ** decimals)

tez_qty = Decimal(transaction.amount) / (10 ** 6)
token_qty = Decimal(transfer.parameter.value) / (10 ** decimals)
shares_qty = int(divest_liquidity.parameter.shares)

price = (Decimal(storage.storage.tez_pool) / (10 ** 6)) / (Decimal(storage.storage.token_pool) / (10 ** decimals))
share_px = (tez_qty + price * token_qty) / shares_qty

position.realized_pl += shares_qty * (share_px - position.avg_share_px)
position.shares_qty -= shares_qty # type: ignore
assert position.shares_qty >= 0, divest_liquidity.data.hash

await position.save()
25 changes: 20 additions & 5 deletions src/demo_quipuswap/handlers/on_fa12_invest_liquidity.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from decimal import Decimal
from typing import cast

import demo_quipuswap.models as models
from demo_quipuswap.types.fa12_token.parameter.transfer import Transfer
from demo_quipuswap.types.quipu_fa12.parameter.invest_liquidity import InvestLiquidity
from demo_quipuswap.types.quipu_fa12.storage import Storage as QuipuFA12Storage
from dipdup.models import HandlerContext, OperationContext


Expand All @@ -15,11 +17,24 @@ async def on_fa12_invest_liquidity(
if ctx.template_values is None:
raise Exception('This index must be templated')

storage = cast(QuipuFA12Storage, invest_liquidity.storage) # FIXME: remove

decimals = int(ctx.template_values['decimals'])
trader, _ = await models.Trader.get_or_create(address=invest_liquidity.data.sender_address)
instrument, _ = await models.Instrument.get_or_create(symbol=ctx.template_values['symbol'])
position, _ = await models.Position.get_or_create(trader=trader, instrument=instrument)
symbol = ctx.template_values['symbol']
trader = invest_liquidity.data.sender_address

position, _ = await models.Position.get_or_create(trader=trader, symbol=symbol)

tez_qty = Decimal(invest_liquidity.data.amount) / (10 ** 6)
token_qty = Decimal(transfer.parameter.value) / (10 ** decimals)
new_shares_qty = int(storage.storage.ledger[trader].balance) + int(storage.storage.ledger[trader].frozen_balance)

price = (Decimal(storage.storage.tez_pool) / (10 ** 6)) / (Decimal(storage.storage.token_pool) / (10 ** decimals))
value = tez_qty + price * token_qty
share_px = value / (new_shares_qty - position.shares_qty)
assert share_px > 0, invest_liquidity.data.hash

position.avg_share_px = (position.shares_qty * position.avg_share_px + value) / new_shares_qty
position.shares_qty = new_shares_qty # type: ignore

position.tez_qty += Decimal(invest_liquidity.data.amount) / (10 ** 6) # type: ignore
position.token_qty += Decimal(transfer.parameter.value) / (10 ** decimals)
await position.save()
10 changes: 6 additions & 4 deletions src/demo_quipuswap/handlers/on_fa12_tez_to_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,21 @@ async def on_fa12_tez_to_token(
raise Exception('This index must be templated')

decimals = int(ctx.template_values['decimals'])
trader, _ = await models.Trader.get_or_create(address=transfer.parameter.to)
instrument, _ = await models.Instrument.get_or_create(symbol=ctx.template_values['symbol'])
symbol = ctx.template_values['symbol']
trader = tez_to_token_payment.data.sender_address

min_token_quantity = Decimal(tez_to_token_payment.parameter.min_out) / (10 ** decimals)
token_quantity = Decimal(transfer.parameter.value) / (10 ** decimals)
tez_quantity = Decimal(tez_to_token_payment.data.amount) / (10 ** 6)
assert min_token_quantity <= token_quantity, tez_to_token_payment.data.hash

trade = models.Trade(
instrument=instrument,
symbol=symbol,
trader=trader,
side=models.TradeSide.BUY,
quantity=token_quantity,
price=token_quantity / tez_quantity,
slippage=((min_token_quantity / token_quantity) - 1) * 100,
slippage=(1 - (min_token_quantity / token_quantity)).quantize(Decimal('0.000001')),
level=transfer.data.level,
timestamp=transfer.data.timestamp,
)
Expand Down
Loading