-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathCFA12.py
More file actions
63 lines (53 loc) · 2.86 KB
/
Copy pathCFA12.py
File metadata and controls
63 lines (53 loc) · 2.86 KB
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
import smartpy as sp
CTErrors = sp.io.import_script_from_url(
"file:contracts/errors/CTokenErrors.py")
EC = CTErrors.ErrorCodes
CToken = sp.io.import_script_from_url("file:contracts/CToken.py")
class CFA12(CToken.CToken):
def __init__(self, comptroller_, interestRateModel_, initialExchangeRateMantissa_, administrator_, metadata_, token_metadata_, fa1_2_TokenAddress_, **extra_storage):
CToken.CToken.__init__(self,
comptroller_,
interestRateModel_,
initialExchangeRateMantissa_,
administrator_,
metadata_,
token_metadata_,
fa1_2_TokenAddress=fa1_2_TokenAddress_,
currentCash=sp.nat(0),
**extra_storage)
def getCashImpl(self):
return self.data.currentCash
def updateCash(self):
cashSum = sp.compute(self.data.currentCash +
self.data.totalSupply + self.data.totalReserves)
# prevent transaction failing if CToken is not listed in underlying fa12 yet
with sp.if_(cashSum > 0):
self.activateOp(CToken.OP.CTokenOperations.GET_CASH)
handle = sp.contract(sp.TPair(sp.TAddress, sp.TContract(
sp.TNat)), self.data.fa1_2_TokenAddress, "getBalance").open_some()
sp.transfer(sp.pair(sp.self_address, sp.self_entry_point(
"setCash")), sp.mutez(0), handle)
@sp.entry_point(lazify=True)
def setCash(self, value):
sp.set_type(value, sp.TNat)
self.verifyAndFinishActiveOp(CToken.OP.CTokenOperations.GET_CASH)
sp.verify(sp.sender == self.data.fa1_2_TokenAddress,
EC.CT_INVALID_CASH_SENDER)
self.data.currentCash = value
def doTransferIn(self, from_, amount):
# Listed FA1.2 underlyings must credit exactly ``amount`` on every
# successful transfer. Fee-on-transfer and transfer-time-rebasing
# tokens are unsupported because currentCash is updated by amount.
self.transferFA12(from_, sp.self_address, amount,
self.data.fa1_2_TokenAddress)
# Keep the accounting cache in lockstep with the scheduled token
# transfer. Subsequent operations in the same operation group use this
# value to price shares and check liquidity.
self.data.currentCash += amount
def doTransferOut(self, to_, amount, isContract=False):
self.data.currentCash = self.sub_nat_nat(self.data.currentCash, amount)
self.transferFA12(sp.self_address, to_, amount,
self.data.fa1_2_TokenAddress)
def verifySweepFA12(self, tokenAddress):
sp.verify(tokenAddress != self.data.fa1_2_TokenAddress,
EC.CT_SWEEP_UNDERLYING)