forked from svanas/delphereum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb3.eth.defi.pas
179 lines (153 loc) · 4.78 KB
/
web3.eth.defi.pas
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
{******************************************************************************}
{ }
{ Delphereum }
{ }
{ Copyright(c) 2020 Stefan van As <[email protected]> }
{ Github Repository <https://github.com/svanas/delphereum> }
{ }
{ Distributed under Creative Commons NonCommercial (aka CC BY-NC) license. }
{ }
{******************************************************************************}
unit web3.eth.defi;
{$I web3.inc}
interface
uses
// Velthuis' BigNumbers
Velthuis.BigIntegers,
// web3
web3,
web3.eth.erc20,
web3.eth.types;
type
TReserve = (DAI, USDC, USDT);
TReserveHelper = record helper for TReserve
function Symbol : string;
function Decimals: Extended;
function Address : TAddress;
function Scale(amount: Extended): BigInteger;
function Unscale(amount: BigInteger): Extended;
procedure BalanceOf(client: TWeb3; owner: TAddress; callback: TAsyncQuantity);
end;
TPeriod = (oneDay, threeDays, oneWeek, oneMonth);
TPeriodHelper = record helper for TPeriod
function Days : Extended;
function Hours : Extended;
function Minutes: Integer;
function Seconds: Integer;
function ToYear(value: Extended): Extended;
end;
TLendingProtocol = class abstract
public
class function Name: string; virtual; abstract;
class function Supports(
chain : TChain;
reserve: TReserve): Boolean; virtual; abstract;
// Returns the annual yield as a percentage with 4 decimals.
class procedure APY(
client : TWeb3;
reserve : TReserve;
period : TPeriod;
callback: TAsyncFloat); virtual; abstract;
// Deposits an underlying asset into the lending pool.
class procedure Deposit(
client : TWeb3;
from : TPrivateKey;
reserve : TReserve;
amount : BigInteger;
callback: TAsyncReceipt); virtual; abstract;
// Returns how much underlying assets you are entitled to.
class procedure Balance(
client : TWeb3;
owner : TAddress;
reserve : TReserve;
callback: TAsyncQuantity); virtual; abstract;
// Withdraws your underlying asset from the lending pool.
class procedure Withdraw(
client : TWeb3;
from : TPrivateKey;
reserve : TReserve;
callback: TAsyncReceiptEx); virtual; abstract;
class procedure WithdrawEx(
client : TWeb3;
from : TPrivateKey;
reserve : TReserve;
amount : BigInteger;
callback: TAsyncReceiptEx); virtual; abstract;
end;
TLendingProtocolClass = class of TLendingProtocol;
TAsyncLendingProtocol = reference to procedure(proto: TLendingProtocolClass; err: IError);
implementation
uses
// Delphi
System.TypInfo;
{ TPeriodHelper }
function TPeriodHelper.Days: Extended;
begin
Result := 1;
case Self of
threeDays: Result := 3;
oneWeek : Result := 7;
oneMonth : Result := 365.25 / 12;
end;
end;
function TPeriodHelper.Hours: Extended;
begin
Result := Self.Days * 24;
end;
function TPeriodHelper.Minutes: Integer;
begin
Result := Round(Self.Hours * 60);
end;
function TPeriodHelper.Seconds: Integer;
begin
Result := Self.Minutes * 60;
end;
function TPeriodHelper.ToYear(value: Extended): Extended;
begin
Result := value * (365.25 / Self.Days);
end;
{ TReserveHelper }
function TReserveHelper.Symbol: string;
begin
Result := GetEnumName(TypeInfo(TReserve), Ord(Self));
end;
function TReserveHelper.Decimals: Extended;
begin
case Self of
DAI : Result := 1e18;
USDC: Result := 1e6;
USDT: Result := 1e6;
else
raise EWeb3.CreateFmt('%s not implemented', [Self.Symbol]);
end;
end;
function TReserveHelper.Address: TAddress;
begin
case Self of
DAI : Result := '0x6b175474e89094c44da98b954eedeac495271d0f';
USDC: Result := '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48';
USDT: Result := '0xdac17f958d2ee523a2206206994597c13d831ec7';
else
raise EWeb3.CreateFmt('%s not implemented', [Self.Symbol]);
end;
end;
function TReserveHelper.Scale(amount: Extended): BigInteger;
begin
Result := BigInteger.Create(amount * Self.Decimals);
end;
function TReserveHelper.Unscale(amount: BigInteger): Extended;
begin
Result := amount.AsExtended / Self.Decimals;
end;
procedure TReserveHelper.BalanceOf(client: TWeb3; owner: TAddress; callback: TAsyncQuantity);
var
erc20: TERC20;
begin
erc20 := TERC20.Create(client, Self.Address);
try
erc20.BalanceOf(owner, callback);
finally
erc20.Free;
end;
end;
end.