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__/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export function createMessage(message: Partial<Omit<Message, "Tags">> & { [tagNa
Target: env.Process.Id,
Owner: env.Process.Owner,
From: env.Process.Owner,
["Block-Height"]: "1",
["Block-Height"]: 1,
Timestamp: defaultTimestamp,
Module: "examplemodule",
Cron: false,
Expand Down
22 changes: 5 additions & 17 deletions controller/controller.lua
Original file line number Diff line number Diff line change
Expand Up @@ -224,18 +224,12 @@ Handlers.add(
-- TODO: timeout here? (what if this doesn't return in time, the liquidation remains in a pending state)

-- liquidate the loan
ao.send({
local loanLiquidationRes = ao.send({
Target = msg.From,
Action = "Transfer",
Quantity = msg.Tags.Quantity,
Recipient = Tokens[msg.From]
})

-- get result of liquidation
local loanLiquidationRes = Handlers.receive({
From = Tokens[msg.From],
["X-Reference"] = tostring(ao.reference)
})
}).receive(Tokens[msg.From])

-- check loan liquidation result
if loanLiquidationRes.Tags.Error then
Expand Down Expand Up @@ -408,19 +402,13 @@ function tokens.spawnProtocolLogo(collateralLogo)

-- message that spawns the logo
-- we're sending this to ourselves
ao.send({
---@type Message
local spawnedImage = ao.send({
Target = ao.id,
Action = "Spawn-Logo",
["Content-Type"] = "image/svg+xml",
Data = logoPart1 .. "/" .. collateralLogo .. logoPart2
})

-- now receive the message we are sending ourselves
---@type Message
local spawnedImage = Handlers.receive({
From = ao.id,
Reference = tostring(ao.reference)
})
}).receive(ao.id)

return spawnedImage.Id
end
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
"typescript": "^5.5.4"
},
"dependencies": {
"@permaweb/ao-loader": "^0.0.42"
"@permaweb/ao-loader": "^0.0.43"
}
}
6 changes: 4 additions & 2 deletions src/borrow/pool.lua
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,16 @@ function mod.setup(msg)
---@type string[]
Friends = Friends or json.decode(ao.env.Process.Tags.Friends or "[]")

-- global current timestamp for the oracle
-- global current timestamp and block for the oracle
Timestamp = msg.Timestamp
Block = msg["Block-Height"]
end

-- This syncs the global timestamp using the current message
-- This syncs the global timestamp anc block using the current message
---@type HandlerFunction
function mod.syncTimestamp(msg)
Timestamp = msg.Timestamp
Block = msg["Block-Height"]
end

return mod
4 changes: 2 additions & 2 deletions src/liquidations/oracle.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function mod.setup()
-- oracle process id
Oracle = Oracle or ao.env.Process.Tags.Oracle

-- oracle delay tolerance in miliseconds
-- oracle delay tolerance in milliseconds
---@type number
MaxOracleDelay = MaxOracleDelay or tonumber(ao.env.Process.Tags["Oracle-Delay-Tolerance"]) or 0

Expand Down Expand Up @@ -64,7 +64,7 @@ function mod.getPrice(...)
Target = Oracle,
Action = "v2.Request-Latest-Data",
Tickers = json.encode(pricesToSync)
}).receive().Data
}).receive(nil, Block + 1).Data

-- check if there was any data returned
assert(rawData ~= nil and rawData ~= "", "No data returned from the oracle")
Expand Down
59 changes: 37 additions & 22 deletions src/process.lua
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,17 @@ local function setup_handlers()
Handlers.add(
"borrow-loan-interest-sync-dynamic",
Handlers.utils.continue(Handlers.utils.hasMatchingTagOf("Action", {
"Borrow", "Repay", "Borrow-Balance", "Borrow-Capacity", "Position", "Global-Position", "Positions", "Redeem", "Transfer", "Liquidate-Borrow"
"Borrow",
"Repay",
"Borrow-Balance",
"Borrow-Capacity",
"Position",
"Global-Position",
"Positions",
"Redeem",
"Transfer",
"Liquidate-Borrow",
"Mint"
})),
interest.syncInterests
)
Expand Down Expand Up @@ -115,18 +125,17 @@ local function setup_handlers()
config.setLiquidationThreshold
)

Handlers.add(
"liquidate-borrow",
{
Handlers.advanced({
name = "liquidate-borrow",
pattern = {
From = CollateralID,
Action = "Credit-Notice",
Sender = ao.env.Process.Owner,
["X-Action"] = "Liquidate-Borrow"
},
liquidate.liquidateBorrow,
nil,
liquidate.refund
)
handle = liquidate.liquidateBorrow,
errorHandler = liquidate.refund
})
Handlers.add(
"liquidate-position",
{ From = ao.env.Process.Owner, Action = "Liquidate-Position" },
Expand All @@ -148,13 +157,16 @@ local function setup_handlers()
Handlers.utils.hasMatchingTag("Action", "Borrow"),
borrow
)
Handlers.add(
"borrow-repay",
{ From = CollateralID, Action = "Credit-Notice", ["X-Action"] = "Repay" },
repay.handler,
nil,
repay.error
)
Handlers.advanced({
name = "borrow-repay",
pattern = {
From = CollateralID,
Action = "Credit-Notice",
["X-Action"] = "Repay"
},
handle = repay.handler,
errorHandler = repay.error
})
Handlers.add(
"borrow-position-balance",
Handlers.utils.hasMatchingTag("Action", "Borrow-Balance"),
Expand All @@ -181,13 +193,16 @@ local function setup_handlers()
position.allPositions
)

Handlers.add(
"supply-mint",
{ From = CollateralID, Action = "Credit-Notice", ["X-Action"] = "Mint" },
mint.handler,
nil,
mint.error
)
Handlers.advanced({
name = "supply-mint",
pattern = {
From = CollateralID,
Action = "Credit-Notice",
["X-Action"] = "Mint"
},
handle = mint.handler,
errorHandler = mint.error
})
Handlers.add(
"supply-price",
Handlers.utils.hasMatchingTag("Action", "Get-Price"),
Expand Down
23 changes: 15 additions & 8 deletions src/utils/ao.lua
Original file line number Diff line number Diff line change
Expand Up @@ -197,11 +197,16 @@ function ao.send(msg)
resolver)
end

message.receive = function(...)
local from = message.Target
if select("#", ...) == 1 then from = select(1, ...) end
return
Handlers.receive({From = from, ["X-Reference"] = referenceString})
message.receive = function(from, timeout)
if from == nil then from = message.Target end

local result, expired = Handlers.receive({
From = from,
["X-Reference"] = referenceString
}, timeout)
assert(not expired, "Response expired")

return result
end

return message
Expand Down Expand Up @@ -267,13 +272,15 @@ function ao.spawn(module, msg)
}, callback)
end

spawn.receive = function()
return Handlers.receive({
spawn.receive = function(timeout)
local result, expired = Handlers.receive({
Action = "Spawned",
From = ao.id,
["Reference"] = spawnRef
})
}, timeout)
assert(not expired, "Response expired")

return result
end

return spawn
Expand Down
Loading