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 __tests__/token.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,7 @@ describe("Token standard functionalities", () => {
const res = await handle(
generateOracleResponse({ BTC: 85325.425 }, oracleRes)
);

expect(res.Messages).toEqual(
expect.arrayContaining([
expect.objectContaining({
Expand Down
3 changes: 1 addition & 2 deletions src/borrow/borrow.lua
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,7 @@ function mod.borrow(msg, _, oracle)
" " ..
CollateralTicker
)
ao.send({
device = "patch@1.0",
patch({
["pool-state"] = {
cash = precision.formatInternalAsNative(Cash, "rounddown"),
["total-borrows"] = precision.formatInternalAsNative(TotalBorrows, "roundup"),
Expand Down
6 changes: 2 additions & 4 deletions src/borrow/interest.lua
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,7 @@ function mod.accrueInterest(msg)
ReservesRemainder = tostring(remainder)

-- patch the updated globals
ao.send({
device = "patch@1.0",
patch({
["pool-state"] = {
["total-borrows"] = precision.formatInternalAsNative(TotalBorrows, "roundup"),
["total-reserves"] = precision.formatInternalAsNative(Reserves, "roundup"),
Expand Down Expand Up @@ -212,8 +211,7 @@ function mod.accrueInterestForUser(address)
InterestIndices[address] = tostring(interestIndex)

-- patch updated global values
ao.send({
device = "patch@1.0",
patch({
loans = { [address] = Loans[address] },
["interest-indices"] = { [address] = InterestIndices[address] },
-- !!do not update positions to avoid an infinite recursive loop!!
Expand Down
3 changes: 1 addition & 2 deletions src/borrow/repay.lua
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,7 @@ function repay.repayToPool(target, quantity)
TotalBorrows = tostring(bint(TotalBorrows) - precision.toInternalPrecision(actualRepaidQty))

-- patch the updated globals
ao.send({
device = "patch@1.0",
patch({
["pool-state"] = {
cash = precision.formatInternalAsNative(Cash, "rounddown"),
["total-borrows"] = precision.formatInternalAsNative(TotalBorrows, "roundup"),
Expand Down
6 changes: 2 additions & 4 deletions src/controller/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,7 @@ function mod.update(msg)
if newAOToken then AOToken = newAOToken end

-- patch
ao.send({
device = "patch@1.0",
patch({
environment = {
["risk-parameters"] = {
["collateral-factor"] = CollateralFactor,
Expand Down Expand Up @@ -173,8 +172,7 @@ function mod.toggleInteractions(msg)
-- patch
local enabledInteractions, disabledInteractions = patching.utils.interactionsState()

ao.send({
device = "patch@1.0",
patch({
environment = {
limits = {
["enabled-interactions"] = enabledInteractions,
Expand Down
3 changes: 1 addition & 2 deletions src/controller/cooldown.lua
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ function mod.gate(msg)
Cooldowns[sender] = msg["Block-Height"] + CooldownPeriod

-- patch
ao.send({
device = "patch@1.0",
patch({
cooldowns = { [sender] = Cooldowns[sender] }
})
end
Expand Down
7 changes: 3 additions & 4 deletions src/controller/friend.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local assertions = require ".utils.assertions"
local patching = require ".token.patching"
local utils = require ".utils.utils"
local json = require "json"

Expand Down Expand Up @@ -52,8 +53,7 @@ function friend.add(msg)
})

-- patch
ao.send({
device = "patch@1.0",
patch({
environment = { friends = Friends }
})

Expand Down Expand Up @@ -89,8 +89,7 @@ function friend.remove(msg)
assert(#removed > 0, "Friend " .. target .. " not yet added")

-- patch
ao.send({
device = "patch@1.0",
patch({
environment = { friends = Friends }
})

Expand Down
4 changes: 2 additions & 2 deletions src/controller/reserves.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local assertions = require ".utils.assertions"
local precision = require ".utils.precision"
local patching = require ".token.patching"
local bint = require ".utils.bint"(1024)

local mod = {}
Expand Down Expand Up @@ -66,8 +67,7 @@ function mod.deploy(msg)
Cash = tostring(bint(Cash) + quantity)

-- patch
ao.send({
device = "patch@1.0",
patch({
["pool-state"] = {
cash = precision.formatInternalAsNative(Cash, "rounddown"),
["total-reserves"] = precision.formatInternalAsNative(Reserves, "roundup")
Expand Down
3 changes: 1 addition & 2 deletions src/liquidations/liquidate.lua
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,7 @@ function mod.liquidatePosition(msg)
})

-- patch
ao.send({
device = "patch@1.0",
patch({
["token-info"] = { supply = TotalSupply },
["pool-state"] = {
cash = precision.formatInternalAsNative(Cash, "rounddown"),
Expand Down
6 changes: 3 additions & 3 deletions src/process.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ require(".utils.assignment").init(ao)
local process = { _version = "0.0.1" }

local coroutine = require "coroutine"
local utils = require ".utils.utils"

patch = function (data) utils.patch_table(PatchData or {}, data) end

local friend = require ".controller.friend"
local config = require ".controller.config"
Expand Down Expand Up @@ -34,7 +37,6 @@ local rate = require ".supply.rate"
local redeem = require ".supply.redeem"
local delegation = require ".supply.delegation"

local utils = require ".utils.utils"
local precision = require ".utils.precision"

HandlersAdded = HandlersAdded or false
Expand Down Expand Up @@ -380,8 +382,6 @@ function process.handle(msg, env)
if not status then
-- call default error handler
Handlers.defaultErrorHandler(msg, env, result)

return ao.result()
end

return ao.result()
Expand Down
3 changes: 1 addition & 2 deletions src/supply/mint.lua
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ function mint.handler(msg)

-- patch
patching.patchOrder(msg.Tags["Pushed-For"] or msg.Id, true)
ao.send({
device = "patch@1.0",
patch({
["pool-state"] = {
cash = precision.formatInternalAsNative(Cash, "rounddown"),
["total-borrows"] = precision.formatInternalAsNative(TotalBorrows, "roundup"),
Expand Down
3 changes: 1 addition & 2 deletions src/supply/redeem.lua
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,7 @@ function mod.redeem(msg, _, oracle)
Recipient = sender
})
patching.patchOrder(msg.Id, true)
ao.send({
device = "patch@1.0",
patch({
["pool-state"] = {
cash = precision.formatInternalAsNative(Cash, "rounddown"),
["total-borrows"] = precision.formatInternalAsNative(TotalBorrows, "roundup"),
Expand Down
8 changes: 6 additions & 2 deletions src/token/patching.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ local mod = {

---@type HandlerFunction
function mod.setup()
PatchingSetUp = PatchingSetUp or false
if PatchingSetUp then return end

-- enabled and disabled interactions
local enabledInteractions, disabledInteractions = mod.utils.interactionsState()

Expand Down Expand Up @@ -87,15 +90,16 @@ function mod.setup()
["interest-indices"] = interestIndices,
orders = { device = "trie@1.0" }
})

PatchingSetUp = true
end

-- Patch the result of a borrow/mint/redeem/repay
---@param id string The ID of the message that initiated the order (pushed for or message id)
---@param success boolean The result of the order
---@param message string? Optional result message
function mod.patchOrder(id, success, message)
ao.send({
device = "patch@1.0",
patch({
orders = {
[id] = {
success = success,
Expand Down
3 changes: 1 addition & 2 deletions src/token/transfer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ local function transfer(msg, _, oracle)
Balances[sender] = tostring(walletBalance - quantity)

-- patch transfer
ao.send({
device = "patch@1.0",
patch({
balances = {
[sender] = Balances[sender],
[target] = Balances[target]
Expand Down
12 changes: 11 additions & 1 deletion src/utils/handlers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,9 @@ function handlers.evaluate(msg, env)
assert(type(msg) == 'table', 'msg is not valid')
assert(type(env) == 'table', 'env is not valid')

-- reset current patch
PatchData = {}

for _, o in ipairs(handlers.list) do
if o.name ~= "_default" and not o.inactive then
local match = utils.matchesSpec(msg, o.pattern)
Expand Down Expand Up @@ -556,7 +559,7 @@ function handlers.evaluate(msg, env)
end
end
if match < 0 then
return handled
break
end
end
end
Expand All @@ -569,6 +572,13 @@ function handlers.evaluate(msg, env)
handlers.remove(name)
end

-- send summarized patch
if next(PatchData) ~= nil then
PatchData.device = "patch@1.0"
ao.send(PatchData)
PatchData = {}
end

-- make sure the request was handled
assert(
handled or msg.Id == ao.id or string.match(msg.Action or "", "Error$"),
Expand Down
13 changes: 13 additions & 0 deletions src/utils/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -402,4 +402,17 @@ function utils.udiv_roundup(x, y)
)
end

-- Patches a table with another table, without deleting any keys (recursively merges nested tables)
---@param target table The table to be patched (modified in-place)
---@param patch table The table providing new or updated values
function utils.patch_table(target, patch)
for k, v in pairs(patch) do
if type(v) == "table" and type(target[k]) == "table" then
utils.patch_table(target[k], v)
else
target[k] = v
end
end
end

return utils