Skip to content

Commit

Permalink
fix(spec): update handlers to integrate with token spec
Browse files Browse the repository at this point in the history
  • Loading branch information
atticusofsparta committed Jan 15, 2025
1 parent addb3b4 commit 0d96d3a
Show file tree
Hide file tree
Showing 5 changed files with 336 additions and 35 deletions.
114 changes: 87 additions & 27 deletions src/common/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -88,40 +88,92 @@ function ant.init()

createActionHandler(TokenSpecActionMap.Transfer, function(msg)
local recipient = msg.Tags.Recipient
utils.validateOwner(msg.From)
if msg.From ~= Owner then
if msg.reply then
msg.reply({
Action = "Transfer-Error",
["Message-Id"] = msg.Id,
Error = "Insufficient Balance!",
})
else
ao.send({
Target = msg.From,
Action = "Transfer-Error",
["Message-Id"] = msg.Id,
Error = "Insufficient Balance!",
})
end
end
balances.transfer(recipient, msg.Tags["Allow-Unsafe-Addresses"])

if not msg.Cast then
ao.send(notices.debit(msg))
ao.send(notices.credit(msg))
if msg.reply then
msg.reply(notices.debit(msg))
msg.reply(notices.credit(msg))
else
ao.send(notices.debit(msg))
ao.send(notices.credit(msg))
end
end
end)

createActionHandler(TokenSpecActionMap.Balance, function(msg)
local balRes = balances.balance(msg.Tags.Recipient or msg.From, msg.Tags["Allow-Unsafe-Addresses"])

ao.send({
Target = msg.From,
Action = "Balance-Notice",
Balance = tostring(balRes),
Ticker = Ticker,
Address = msg.Tags.Recipient or msg.From,
Data = balRes,
})
local addressToCheck = msg.Tags.Recipient or msg.Tags.Target or msg.From
local balRes = balances.balance(addressToCheck, msg.Tags["Allow-Unsafe-Addresses"])

if msg.reply then
msg.reply({
Action = "Balance-Notice",
Balance = tostring(balRes),
Ticker = Ticker,
Account = addressToCheck,
Address = addressToCheck,
Data = tostring(balRes),
})
else
ao.send({
Target = msg.From,
Action = "Balance-Notice",
Balance = tostring(balRes),
Ticker = Ticker,
Address = msg.Tags.Recipient or msg.From,
Data = tostring(balRes),
})
end
end)

createActionHandler(TokenSpecActionMap.Balances, function()
return balances.balances()
createActionHandler(TokenSpecActionMap.Balances, function(msg)
local bals = {}
for k, v in pairs(balances.balances()) do
bals[k] = tostring(v)
end

if msg.reply then
msg.reply({
Data = json.encode(bals),
})
else
ao.send({ Target = msg.From, Data = json.encode(bals) })
end
end)

createActionHandler(TokenSpecActionMap.TotalSupply, function(msg)
assert(msg.From ~= ao.id, "Cannot call Total-Supply from the same process!")

ao.send({
Target = msg.From,
Action = "Total-Supply-Notice",
Data = TotalSupply,
Ticker = Ticker,
})
if msg.reply then
msg.reply({
Action = "Total-Supply",
Data = tostring(TotalSupply),
Ticker = Ticker,
})
else
ao.send({
Target = msg.From,
Action = "Total-Supply",
Data = tostring(TotalSupply),
Ticker = Ticker,
})
end
end)

createActionHandler(TokenSpecActionMap.Info, function(msg)
Expand All @@ -136,12 +188,20 @@ function ant.init()
Owner = Owner,
Handlers = utils.getHandlerNames(Handlers),
}
ao.send({
Target = msg.From,
Action = "Info-Notice",
Tags = info,
Data = json.encode(info),
})
if msg.reply then
msg.reply({
Action = "Info-Notice",
Tags = info,
Data = json.encode(info),
})
else
ao.send({
Target = msg.From,
Action = "Info-Notice",
Tags = info,
Data = json.encode(info),
})
end
end)

-- ActionMap (ANT Spec)
Expand Down
17 changes: 13 additions & 4 deletions src/common/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -291,20 +291,29 @@ function utils.createHandler(tagName, tagValue, handler, position)
return handler(msg)
end, utils.errorHandler)

local resultNotice = nil
if not handlerStatus then
ao.send(notices.addForwardedTags(msg, {
resultNotice = notices.addForwardedTags(msg, {
Target = msg.From,
Action = "Invalid-" .. tagValue .. "-Notice",
Error = tagValue .. "-Error",
["Message-Id"] = msg.Id,
Data = handlerRes,
}))
})
elseif handlerRes then
ao.send(notices.addForwardedTags(msg, {
resultNotice = notices.addForwardedTags(msg, {
Target = msg.From,
Action = tagValue .. "-Notice",
Data = type(handlerRes) == "string" and handlerRes or json.encode(handlerRes),
}))
})
end

if resultNotice then
if msg.reply then
msg.reply(resultNotice)
else
ao.send(resultNotice)
end
end

local hasNewOwner = Owner ~= prevOwner
Expand Down
8 changes: 4 additions & 4 deletions test/balances.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ describe('aos Balances', async () => {

if (shouldPass === true) {
const targetBalance = result.Messages[0].Data;
assert.equal(typeof targetBalance === 'number', shouldPass);
assert.equal(typeof targetBalance === 'string', shouldPass);
} else {
assert.strictEqual(
result.Messages[0].Tags.find((t) => t.name === 'Error')?.value,
Expand All @@ -97,7 +97,7 @@ describe('aos Balances', async () => {
transferResult.Memory,
);
const balances = JSON.parse(balancesResult.Messages[0].Data);
assert.equal(balances[target_address] === 1, shouldPass);
assert.equal(balances[target_address] === '1', shouldPass);
} else {
assert.strictEqual(
transferResult.Messages[0].Tags.find((t) => t.name === 'Error')
Expand Down Expand Up @@ -157,7 +157,7 @@ describe('aos Balances', async () => {
const [ownerAddress, ownerBalance] = ownerEntry;
assert(Object.entries(balances).length === 1);
assert(ownerAddress === STUB_ADDRESS);
assert(ownerBalance === 1);
assert(ownerBalance === '1');
});

it('should set the logo of the ant', async () => {
Expand All @@ -174,6 +174,6 @@ describe('aos Balances', async () => {
});
it('should get total supply', async () => {
const res = await getTotalSupply();
assert.strictEqual(res, 1, 'total supply should be equal to 1');
assert.strictEqual(res, '1', 'total supply should be equal to 1');
});
});
Loading

0 comments on commit 0d96d3a

Please sign in to comment.