-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmithril.stake.mint.lua
More file actions
583 lines (490 loc) · 17.1 KB
/
mithril.stake.mint.lua
File metadata and controls
583 lines (490 loc) · 17.1 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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
Variant = '0.0.1'
local bint = require('.bint')(256)
local json = require('json')
local function countTableElements(t)
local count = 0
for _ in pairs(t) do
count = count + 1
end
return count
end
-- Constants
CRON_CALLER = 'h7nm30_3nDfMrN5TRdEC80ZIUzQl-fIWWxobwews4WE'
TOKEN_OWNER = 'OsK9Vgjxo0ypX_HLz2iJJuh4hp3I80yA9KArsJjIloU'
TOTAL_SUPPLY = 21000000 * 10 ^ 8 -- 21M tokens with 8 decimal places
EMISSION_RATE_PER_MONTH = 0.007125 -- was 0.01425 (1.425%) but fair launch is no doing 50% of it -- 1.425% monthly rate
PERIODS_PER_MONTH = 8760 -- number of 5-minute periods in a month (43800/5)
PRECISION_FACTOR = bint(10 ^ 16) -- calculating emissions
TOKEN_CONFIG_PROCESS = 'G3biaSUvclo3cd_1ErpPYt-VoSSazWrKcuBlzeLkTnU' -- token config process
MINT_TOKEN_PROCESS = 'SWQx44W-1iMwGFBSHlC3lStCq3Z7O2WZrx9quLeZOu0' -- mint token process
local PRE_MINT = 5050
-- State variables
CurrentSupply = CurrentSupply or PRE_MINT
LastMintTimestamp = LastMintTimestamp or 0
-- Token configuration state
AllowedTokens = AllowedTokens or {}
AllowedTokensNames = AllowedTokensNames or {}
TokenWeights = TokenWeights or {}
--[[
Initialize the staker table. stakers[token][user] = balance
]]
---@return table<string, table>
function UpdateAllowedTokens()
local stakers = Stakers
for address, _ in pairs(AllowedTokens) do
if not stakers[address] then stakers[address] = {} end
end
return stakers
end
Stakers = Stakers or UpdateAllowedTokens()
StakersBackup = StakersBackup or Stakers --used before calling UpdateAllowedTokens just for safety
--[[
utils helper functions to remove the bint complexity.
]]
--
local utils = {
add = function(a, b)
return tostring(bint(a) + bint(b))
end,
subtract = function(a, b)
return tostring(bint(a) - bint(b))
end,
multiply = function(a, b)
return tostring(bint(a) * bint(b))
end,
divide = function(a, b)
return tostring(bint.__idiv(bint(a), bint(b)))
end,
toBalanceValue = function(a)
return tostring(bint(a))
end,
toNumber = function(a)
return tonumber(a)
end
}
--[[
Handler to update allowed tokens.
Update AllowedTokens arrays then call this handler
]]
--
Handlers.add('update-allowed-tokens', Handlers.utils.hasMatchingTag('Action', 'Update-Allowed-Tokens'),
function(msg)
assert(msg.From == ao.id, 'Caller is not authorized!')
Stakers = UpdateAllowedTokens()
Send({
Target = msg.From,
Data = 'Allowed tokens: ' .. json.encode(AllowedTokens)
})
end)
--[[
Handler for staking. To stake, simply send tokens to this address.
]]
--
Handlers.add('stake', Handlers.utils.hasMatchingTag('Action', 'Credit-Notice'),
function(msg)
-- credit notice is sent by the token process to the staking contract
local token = msg.From
local quantity = msg.Quantity
local stakeable = AllowedTokens[token] ~= nil
local sender = msg.Sender
local tokenName = AllowedTokensNames[token]
--don't bother to refund unstakeable tokens - to prevent being drained through spurious fees
assert(stakeable, 'Token: ' .. token .. ' is not stakable and was ignored!')
assert(bint(0) < bint(quantity), 'Quantity must be greater than zero!')
if not Stakers[token][sender] then Stakers[token][sender] = '0' end
Stakers[token][sender] = utils.add(Stakers[token][sender], quantity)
Send({
Target = sender,
Data = Colors.gray ..
'You have staked a total of ' ..
Colors.blue .. Stakers[token][sender] .. Colors.reset .. ' ' .. tokenName
})
end)
--[[
Handler to unstake
]]
--
Handlers.add('unstake', Handlers.utils.hasMatchingTag('Action', 'Unstake'),
function(msg)
local from = msg.From
local token = msg.Tags['Token']
local quantity = Stakers[token][from] or '0'
local tokenName = AllowedTokensNames[token]
assert(token ~= MINT_TOKEN_PROCESS, 'MINT token cannot be unstaked')
assert(bint(0) < bint(quantity), 'You need to have more than zero staked tokens!')
Stakers[token][from] = nil
--send the staked tokens back to the user
Send({
Target = token,
Action = 'Transfer',
Recipient = from,
Quantity = quantity,
['X-Message'] = 'Mithril Unstake',
['X-Staked-Balance-Remaining-' .. tokenName] = '0'
})
Send({
Target = from,
Data = Colors.gray ..
'Successfully unstaked ' ..
Colors.blue .. quantity .. Colors.reset .. ' ' .. tokenName
})
end)
--[[
Handler to get staked balances for a specific staker
]]
--
Handlers.add('get-staked-balances', Handlers.utils.hasMatchingTag('Action', 'Get-Staked-Balances'),
function(msg)
local staker = msg.Tags['Staker']
assert(type(staker) == 'string', 'Staker address is required!')
-- Initialize result array to store balances
local balances = {}
-- Loop through AllowedTokens to maintain consistent order
for address, _ in pairs(AllowedTokens) do
local amount = '0' -- Default to '0' for unstaked tokens
-- If there's a staked balance for this token, use it
if Stakers[address] and Stakers[address][staker] then
amount = Stakers[address][staker]
end
-- Add entry to balances array
table.insert(balances, {
name = AllowedTokensNames[address],
address = address,
amount = amount
})
end
-- Send response with balances array
Send({
Target = msg.From,
Action = 'Staked-Balances',
Staker = staker,
Data = json.encode(balances),
Weights = json.encode(TokenWeights)
})
end)
--[[
Handler to get list of allowed tokens
]]
--
Handlers.add('get-allowed-tokens', Handlers.utils.hasMatchingTag('Action', 'Get-Allowed-Tokens'),
function(msg)
Send({
Target = msg.From,
Action = 'Allowed-Tokens',
Data = json.encode({ AllowedTokens, AllowedTokensNames })
})
end)
--[[
Calculate emission for a 5-minute period
Returns the number of tokens to mint in this period
]]
local function calculateEmission()
-- Convert all values to bint early to avoid overflow
local totalSupplyBint = bint(TOTAL_SUPPLY)
local currentSupplyBint = bint(CurrentSupply)
local remainingSupply = totalSupplyBint - currentSupplyBint
-- If no supply remaining, return 0
if remainingSupply <= bint.zero() then
return '0'
end
-- Calculate the emission rate for a 5-minute period
-- Convert rate to a fixed-point number with 8 decimal places for precision
local periodRateFixed = math.floor((EMISSION_RATE_PER_MONTH / PERIODS_PER_MONTH) * 10 ^ 8)
local periodRateBint = bint(periodRateFixed)
-- Calculate tokens to emit this period
-- First multiply by rate, then divide by 10^8 to get back to normal scale
local emission = bint.__idiv(remainingSupply * periodRateBint, bint(10 ^ 8))
-- Double check we don't exceed remaining supply
if emission > remainingSupply then
emission = remainingSupply
end
return utils.toBalanceValue(emission)
end
--[[
Calculate individual staker allocations based on their stake weight
]]
local function calculateStakerAllocations(totalEmission)
local allocations = {}
local totalStakeWeight = bint.zero()
local emissionBint = bint(totalEmission)
-- First pass: calculate total weighted stake
for token, stakersMap in pairs(Stakers) do
if TokenWeights[token] then
local tokenWeight = bint(TokenWeights[token])
for _, amount in pairs(stakersMap) do
totalStakeWeight = totalStakeWeight + (bint(amount) * tokenWeight)
end
end
end
-- If no stakes, return empty allocations
if totalStakeWeight == bint.zero() then
return allocations
end
-- Track rounding errors to distribute later
local totalAllocated = bint.zero()
-- Second pass: calculate individual allocations with higher precision
for token, stakersMap in pairs(Stakers) do
if TokenWeights[token] then
local tokenWeight = bint(TokenWeights[token])
for staker, amount in pairs(stakersMap) do
local stakerWeight = bint(amount) * tokenWeight
-- Calculate allocation with higher precision
-- First multiply by emission and precision factor before division
local allocation = bint.__idiv(
(emissionBint * stakerWeight * PRECISION_FACTOR),
(totalStakeWeight)
)
-- Remove precision factor
allocation = bint.__idiv(allocation, PRECISION_FACTOR)
-- Track total allocated
totalAllocated = totalAllocated + allocation
-- Store allocation
if allocation > bint.zero() then
if not allocations[staker] then
allocations[staker] = '0'
end
allocations[staker] = utils.toBalanceValue(bint(allocations[staker]) + allocation)
end
end
end
end
-- Distribute any remaining dust from rounding (if any)
local remainingEmission = emissionBint - totalAllocated
if remainingEmission > bint.zero() then
-- Find the staker with the highest stake to give them the dust
local highestStaker = nil
local highestStake = bint.zero()
for token, stakersMap in pairs(Stakers) do
if TokenWeights[token] then
local tokenWeight = bint(TokenWeights[token])
for staker, amount in pairs(stakersMap) do
local stakerWeight = bint(amount) * tokenWeight
if stakerWeight > highestStake then
highestStake = stakerWeight
highestStaker = staker
end
end
end
end
-- Add remaining dust to highest staker's allocation
if highestStaker then
if not allocations[highestStaker] then
allocations[highestStaker] = '0'
end
allocations[highestStaker] = utils.toBalanceValue(
bint(allocations[highestStaker]) + remainingEmission
)
end
end
return allocations
end
--[[
Handler to request token mints
Called every 5 minutes by a cron job oracle
]]
Handlers.add('request-token-mints', Handlers.utils.hasMatchingTag('Action', 'Request-Token-Mints'),
function(msg)
assert(CRON_CALLER == msg.From, 'Request is not from the trusted Cron!')
-- Ensure sufficient time has passed since last mint
local currentTime = os.time()
assert(currentTime >= LastMintTimestamp + 300, 'Too soon for next mint') -- 300 seconds = 5 minutes
-- Calculate new tokens to mint this period
local newTokens = calculateEmission()
-- If no tokens to mint, exit early
if newTokens == '0' then
return
end
-- Calculate allocation for each staker
local allocations = calculateStakerAllocations(newTokens)
-- Prepare mint requests
local mints = {}
for staker, amount in pairs(allocations) do
-- Only include non-zero allocations
if bint(amount) > bint.zero() then
table.insert(mints, {
address = staker,
amount = amount
})
end
end
-- Update state
CurrentSupply = utils.add(CurrentSupply, newTokens)
LastMintTimestamp = currentTime
-- Send mint requests to token contract only if there are valid mints
if #mints > 0 then
Send({
Target = TOKEN_OWNER,
Action = 'Mint-From-Stake',
Data = json.encode(mints)
})
end
end)
--[[
Handler to get stake ownership percentage for a specific address
This considers the weighted value of all staked tokens
]]
--
Handlers.add('get-stake-ownership', Handlers.utils.hasMatchingTag('Action', 'Get-Stake-Ownership'),
function(msg)
-- Input validation with clear error message
local staker = msg.Tags['Staker']
assert(type(staker) == 'string', 'Staker address is required!')
-- Initialize weights with bint
local totalStakeWeight = bint.zero()
local stakerWeight = bint.zero()
-- Calculate total weighted stake across all tokens
for token, stakersMap in pairs(Stakers) do
if TokenWeights[token] then
local tokenWeight = bint(TokenWeights[token])
-- Calculate weights for all stakers
for addr, amount in pairs(stakersMap) do
-- Convert amount to bint and calculate weight
local weight = bint(amount) * tokenWeight
totalStakeWeight = totalStakeWeight + weight
-- Calculate this staker's weight if it matches
if addr == staker then
stakerWeight = stakerWeight + weight
end
end
end
end
-- Handle case where there are no stakes
if totalStakeWeight == bint.zero() then
Send({
Target = msg.From,
Action = 'Stake-Ownership',
Staker = staker,
['Ownership-Percentage'] = '0.000000',
Data = json.encode({
percentage = '0.000000',
stakerWeight = '0',
totalWeight = '0'
})
})
return
end
-- Calculate ownership percentage with 6 decimal places of precision
-- Multiply by 10^8 before division to maintain precision, then format result
local scaledStakerWeight = bint(utils.toBalanceValue(stakerWeight)) * bint(100 * PRECISION_FACTOR)
local ownershipPercentageBint = bint.__idiv(scaledStakerWeight, bint(utils.toBalanceValue(totalStakeWeight)))
-- Convert to string with proper decimal places
local ownershipPercentageStr = string.format('%.6f',
tonumber(utils.toBalanceValue(ownershipPercentageBint)) / PRECISION_FACTOR)
-- Send response with ownership details
Send({
Target = msg.From,
Action = 'Stake-Ownership',
Staker = staker,
['Ownership-Percentage'] = ownershipPercentageStr,
Data = json.encode({
percentage = ownershipPercentageStr,
stakerWeight = utils.toBalanceValue(stakerWeight),
totalWeight = utils.toBalanceValue(totalStakeWeight)
})
})
end
)
-- Get number of unique stakers across all tokens
Handlers.add('get-unique-stakers',
Handlers.utils.hasMatchingTag('Action', 'Get-Unique-Stakers'),
function(msg)
local uniqueStakers = {}
for token, stakersMap in pairs(Stakers) do
for staker, balance in pairs(stakersMap) do
-- Only count stakers with positive balance
if bint(balance) > bint.zero() then
uniqueStakers[staker] = true
end
end
end
msg.reply({
Action = 'Unique-Stakers',
Data = tostring(countTableElements(uniqueStakers))
})
end
)
-- Get current minting rate in tokens per day
Handlers.add('get-minting-rate',
Handlers.utils.hasMatchingTag('Action', 'Get-Minting-Rate'),
function(msg)
-- Get remaining supply
local totalSupplyBint = bint(TOTAL_SUPPLY)
local currentSupplyBint = bint(CurrentSupply)
local remainingSupply = totalSupplyBint - currentSupplyBint
-- If no supply remaining, return 0
if remainingSupply <= bint.zero() then
msg.reply({
Action = 'Minting-Rate',
Data = '0'
})
return
end
-- Calculate the emission rate for one day
-- EMISSION_RATE_PER_MONTH is 0.01425 (1.425%)
-- There are 288 5-minute periods in a day (24 * 60 / 5)
local periodRateFixed = math.floor((EMISSION_RATE_PER_MONTH / PERIODS_PER_MONTH) * 10 ^ 8)
local periodRateBint = bint(periodRateFixed)
-- Calculate daily emission: remainingSupply * (periodRate * 288)
local dailyRate = bint.__idiv(remainingSupply * periodRateBint * bint(288), bint(10 ^ 8))
msg.reply({
Action = 'Minting-Rate',
Data = utils.toBalanceValue(dailyRate)
})
end
)
-- Get token staking breakdown
Handlers.add('get-token-stakes',
Handlers.utils.hasMatchingTag('Action', 'Get-Token-Stakes'),
function(msg)
local tokenStats = {}
for token, stakersMap in pairs(Stakers) do
local totalStaked = '0'
for _, amount in pairs(stakersMap) do
totalStaked = utils.add(totalStaked, amount)
end
table.insert(tokenStats, {
address = token,
name = AllowedTokensNames[token],
total_staked = totalStaked,
num_stakers = countTableElements(stakersMap)
})
end
msg.reply({
Action = 'Token-Stakes',
Data = json.encode(tokenStats)
})
end
)
-- Refresh token configs (called by cron)
Handlers.add('refresh-token-configs',
Handlers.utils.hasMatchingTag('Action', 'Refresh-Token-Configs'),
function(msg)
assert(msg.From == ao.id or msg.From == TOKEN_CONFIG_PROCESS, 'Caller is not authorized!')
Send({
Target = TOKEN_CONFIG_PROCESS,
Action = 'Get-Token-Configs'
}).onReply(function(reply)
local configs = json.decode(reply.Data)
AllowedTokens = configs.allowedTokens
AllowedTokensNames = configs.allowedTokensNames
TokenWeights = configs.tokenWeights
-- Initialize staker entries for any new tokens
Stakers = UpdateAllowedTokens()
msg.reply({
Action = 'Token-Configs-Refreshed',
Data = 'Token configurations have been refreshed and new tokens initialized'
})
end)
end
)
--claim AO tokens from permaswap
--TODO: call this one by cron every month
Handlers.add('claim-permaswap-ao', Handlers.utils.hasMatchingTag('Action', 'Claim-Permaswap-AO'),
function(msg)
local permaswapProcess = 'LzT6n3Ey6qGLm5TEX25-fB15dlnjBEJe8ti2QyQJt1A'
Send({
Target = permaswapProcess,
Action = 'Claim'
})
end
)