-
Notifications
You must be signed in to change notification settings - Fork 0
/
modputcontract.py
94 lines (63 loc) · 3.2 KB
/
modputcontract.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import smartpy as sp
class PutContract(sp.Contract):
def __init__(self,admin,endCycle,endWithdraw):
self.init(contractBuyer=sp.map(),liquidityPool=sp.big_map(),poolMap = sp.big_map(),
administrator = admin,poolCounter=sp.int(0),
totalLiquidity=0,xtzPrice=300,adminAccount=100000,tempcal=0,
cycleEnd=sp.timestamp(endCycle),withdrawTime=sp.timestamp(endWithdraw))
@sp.entry_point
def putBuyer(self,params):
sp.verify(sp.now < self.data.cycleEnd)
sp.verify(~self.data.contractBuyer.contains(sp.sender))
totalAmount = params.strikePrice*params.options*100
sp.verify(self.data.totalLiquidity > totalAmount)
self.data.contractBuyer[sp.sender] = sp.record(strikePrice = params.strikePrice, pool = sp.map(),adminpayment =0,options=params.options)
# iteration
temp = 2
premiumCal = 0
CollateralCal = 0
tempTotalPremium = 0
tempTotalCollateral = 0
sp.for i in range(temp):
premiumCal = self.data.liquidityPool[i].amount*params.fee
premiumCal = premiumCal/self.data.totalLiquidity
tempTotalPremium += premiumCal
self.data.liquidityPool[i].premium += premiumCal
CollateralCal = self.data.liquidityPool[i].amount*params.strikePrice*params.options*100
CollateralCal = CollateralCal/self.data.totalLiquidity
self.data.contractBuyer[sp.sender].pool[self.data.liquidityPool[i].address] = CollateralCal
tempTotalCollateral += CollateralCal
#self.data.liquidityPool[i].amount -= sp.as_nat(CollateralCal)
#self.data.totalLiquidity -= tempTotalCollateral
@sp.entry_point
def putSeller(self,params):
sp.verify(sp.now < self.data.cycleEnd)
# intercontract call to reduce amount
sp.if self.data.poolMap.contains(sp.sender):
self.data.liquidityPool[self.data.poolMap[sp.sender]].amount += params.amount
sp.else:
self.data.poolMap[sp.sender] = self.data.poolCounter
self.data.liquidityPool[self.data.poolCounter] = sp.record(amount=0,premium=0,address=sp.sender)
self.data.liquidityPool[self.data.poolCounter].amount += params.amount
self.data.poolCounter += 1
self.data.totalLiquidity += params.amount
@sp.entry_point
def modifyPrice(self,params):
sp.verify(sp.sender == self.data.administrator)
self.data.xtzPrice = params.price
@sp.add_test(name = "Put Contract Testing")
def test():
admin = sp.address("tz123")
# Put Buyers
bob = sp.address("tz1678")
# Put Sellers
alice = sp.address("tz1456")
alex = sp.address("tz1910")
scenario = sp.test_scenario()
c1 = PutContract(admin,100,120)
scenario += c1
scenario += c1.putSeller(amount=50000).run(now=45,sender=alex)
scenario += c1.putSeller(amount=10000).run(now=46,sender=alex)
scenario += c1.putSeller(amount=40000).run(now=47,sender=alice)
scenario += c1.putBuyer(strikePrice=100,options=5,fee=200).run(now=50,sender=bob)
#scenario += c1.putBuyer().run(now=50)