Created
February 22, 2024 16:38
-
-
Save 0xcyp/df62a3e0b323c8948cbd68680bb787ef to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local json = require('json') | |
if not Balances then Balances = { [ao.id] = 690000000000000 } end | |
if Name ~= 'bitcaos' then Name = 'bitcaos' end | |
if Ticker ~= 'CAOS' then Ticker = 'CAOS' end | |
if Denomination ~= 100 then Denomination = 100 end | |
if not Logo then Logo = 'QkbZIyUCQtgfALlpdaZn4-1zaQbxXvEAyqGdZTCj1tc' end | |
Handlers.add('info', Handlers.utils.hasMatchingTag('Action', 'Info'), function(msg) | |
ao.send( | |
{ Target = msg.From, Tags = { Name = Name, Ticker = Ticker, Logo = Logo, Denomination = tostring(Denomination) } }) | |
end) | |
Handlers.add('balance', Handlers.utils.hasMatchingTag('Action', 'Balance'), function(msg) | |
local bal = '0' | |
-- If not Target is provided, then return the Senders balance | |
if (msg.Tags.Target and Balances[msg.Tags.Target]) then | |
bal = tostring(Balances[msg.Tags.Target]) | |
elseif Balances[msg.From] then | |
bal = tostring(Balances[msg.From]) | |
end | |
ao.send({ | |
Target = msg.From, | |
Tags = { Target = msg.From, Balance = bal, Ticker = Ticker, Data = json.encode(tonumber(bal)) } | |
}) | |
end) | |
Handlers.add('balances', Handlers.utils.hasMatchingTag('Action', 'Balances'), | |
function(msg) ao.send({ Target = msg.From, Data = json.encode(Balances) }) | |
end) | |
Handlers.add('transfer', Handlers.utils.hasMatchingTag('Action', 'Transfer'), function(msg) | |
assert(type(msg.Tags.Recipient) == 'string', 'Recipient is required!') | |
assert(type(msg.Tags.Quantity) == 'string', 'Quantity is required!') | |
if not Balances[msg.From] then Balances[msg.From] = 0 end | |
if not Balances[msg.Tags.Recipient] then Balances[msg.Tags.Recipient] = 0 end | |
local qty = tonumber(msg.Tags.Quantity) | |
assert(type(qty) == 'number', 'qty must be number') | |
if Balances[msg.From] >= qty then | |
Balances[msg.From] = Balances[msg.From] - qty | |
Balances[msg.Tags.Recipient] = Balances[msg.Tags.Recipient] + qty | |
--[[ | |
Only Send the notifications to the Sender and Recipient | |
if the Cast tag is not set on the Transfer message | |
]] -- | |
if not msg.Tags.Cast then | |
-- Send Debit-Notice to the Sender | |
ao.send({ | |
Target = msg.From, | |
Tags = { Action = 'Debit-Notice', Recipient = msg.Tags.Recipient, Quantity = tostring(qty) } | |
}) | |
-- Send Credit-Notice to the Recipient | |
ao.send({ | |
Target = msg.Tags.Recipient, | |
Tags = { Action = 'Credit-Notice', Sender = msg.From, Quantity = tostring(qty) } | |
}) | |
end | |
else | |
ao.send({ | |
Target = msg.Tags.From, | |
Tags = { Action = 'Transfer-Error', ['Message-Id'] = msg.Id, Error = 'Insufficient Balance!' } | |
}) | |
end | |
end) | |
-- Send Debit-Notice to the Sender | |
ao.send({ | |
Target = msg.From, | |
Tags = { Action = 'Debit-Notice', Recipient = msg.Tags.Recipient, Quantity = tostring(qty) } | |
}) | |
-- Send Credit-Notice to the Recipient | |
ao.send({ | |
Target = msg.Tags.Recipient, | |
Tags = { Action = 'Credit-Notice', Sender = msg.From, Quantity = tostring(qty) } | |
}) | |
ao.send({ | |
Target = msg.Tags.From, | |
Tags = { Action = 'Transfer-Error', ['Message-Id'] = msg.Id, Error = 'Insufficient Balance!' } | |
}) | |
Handlers.add('mint', Handlers.utils.hasMatchingTag('Action', 'Mint'), function(msg, env) | |
assert(type(msg.Tags.Quantity) == 'string', 'Quantity is required!') | |
if msg.From == env.Process.Id then | |
-- Add tokens to the token pool, according to Quantity | |
local qty = tonumber(msg.Tags.Quantity) | |
Balances[env.Process.Id] = Balances[env.Process.Id] + qty | |
else | |
ao.send({ | |
Target = msg.Tags.From, | |
Tags = { | |
Action = 'Mint-Error', | |
['Message-Id'] = msg.Id, | |
Error = 'Only the Process Owner can mint new ' .. Ticker .. ' tokens!' | |
} | |
}) | |
end | |
end) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment