Skip to content

Commit

Permalink
Merge pull request #122 from mayurmarvel/fix-token-blueprint
Browse files Browse the repository at this point in the history
fix: Token Blueprint code in AO Cookbook
  • Loading branch information
PSkinnerTech committed Mar 4, 2024
2 parents 57d06f3 + af3fa6c commit 4567e6b
Showing 1 changed file with 57 additions and 35 deletions.
92 changes: 57 additions & 35 deletions src/guides/aos/blueprints/token.md
Expand Up @@ -30,6 +30,8 @@ Type in `Handlers.list` to see the newly loaded handlers.
## What's in the Token Blueprint:

```lua
local bint = require('.bint')(256)
local ao = require('ao')
--[[
This module implements the ao Standard Token Specification.
Expand Down Expand Up @@ -61,13 +63,13 @@ local json = require('json')
ao.id is equal to the Process.Id
]]
--
if not Balances then Balances = { [ao.id] = 100000000000000 } end
if not Balances then Balances = { [ao.id] = tostring(bint(10000 * 1e12)) } end

if Name ~= 'Points Coin' then Name = 'Points Coin' end

if Ticker ~= 'Points' then Ticker = 'PNTS' end

if Denomination ~= 10 then Denomination = 10 end
if Denomination ~= 12 then Denomination = 12 end

if not Logo then Logo = 'SBCCXwwecBlDqRLUjb8dYABExTJXLieawf7m2aBJ-KY' end

Expand All @@ -81,8 +83,13 @@ if not Logo then Logo = 'SBCCXwwecBlDqRLUjb8dYABExTJXLieawf7m2aBJ-KY' 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) } })
ao.send({
Target = msg.From,
Name = Name,
Ticker = Ticker,
Logo = Logo,
Denomination = tostring(Denomination)
})
end)

--[[
Expand All @@ -94,14 +101,17 @@ Handlers.add('balance', Handlers.utils.hasMatchingTag('Action', 'Balance'), func

-- 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])
bal = Balances[msg.Tags.Target]
elseif Balances[msg.From] then
bal = tostring(Balances[msg.From])
bal = Balances[msg.From]
end

ao.send({
Target = msg.From,
Tags = { Target = msg.From, Balance = bal, Ticker = Ticker, Data = json.encode(tonumber(bal)) }
Balance = bal,
Ticker = Ticker,
Account = msg.Tags.Target or msg.From,
Data = bal
})
end)

Expand All @@ -117,41 +127,48 @@ Handlers.add('balances', Handlers.utils.hasMatchingTag('Action', 'Balances'),
]]
--
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
assert(type(msg.Recipient) == 'string', 'Recipient is required!')
assert(type(msg.Quantity) == 'string', 'Quantity is required!')
assert(bint.__lt(0, bint(msg.Quantity)), 'Quantity must be greater than 0')

local qty = tonumber(msg.Tags.Quantity)
assert(type(qty) == 'number', 'qty must be number')
if not Balances[msg.From] then Balances[msg.From] = "0" end
if not Balances[msg.Recipient] then Balances[msg.Recipient] = "0" end

if Balances[msg.From] >= qty then
Balances[msg.From] = Balances[msg.From] - qty
Balances[msg.Tags.Recipient] = Balances[msg.Tags.Recipient] + qty
local qty = bint(msg.Quantity)
local balance = bint(Balances[msg.From])
if bint.__le(qty, balance) then
Balances[msg.From] = tostring(bint.__sub(balance, qty))
Balances[msg.Recipient] = tostring(bint.__add(Balances[msg.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
if not msg.Cast then
-- Send Debit-Notice to the Sender
ao.send({
Target = msg.From,
Tags = { Action = 'Debit-Notice', Recipient = msg.Tags.Recipient, Quantity = tostring(qty) }
Action = 'Debit-Notice',
Recipient = msg.Recipient,
Quantity = tostring(qty),
Data = Colors.gray .. "You transferred " .. Colors.blue .. msg.Quantity .. Colors.gray .. " to " .. Colors.green .. msg.Recipient .. Colors.reset
})
-- Send Credit-Notice to the Recipient
ao.send({
Target = msg.Tags.Recipient,
Tags = { Action = 'Credit-Notice', Sender = msg.From, Quantity = tostring(qty) }
Target = msg.Recipient,
Action = 'Credit-Notice',
Sender = msg.From,
Quantity = tostring(qty),
Data = Colors.gray .. "You received " .. Colors.blue .. msg.Quantity .. Colors.gray .. " from " .. Colors.green .. msg.Recipient .. Colors.reset
})
end
else
ao.send({
Target = msg.Tags.From,
Tags = { Action = 'Transfer-Error', ['Message-Id'] = msg.Id, Error = 'Insufficient Balance!' }
Target = msg.From,
Action = 'Transfer-Error',
['Message-Id'] = msg.Id,
Error = 'Insufficient Balance!'
})
end
end)
Expand All @@ -160,22 +177,27 @@ end)
Mint
]]
--
Handlers.add('mint', Handlers.utils.hasMatchingTag('Action', 'Mint'), function(msg, env)
assert(type(msg.Tags.Quantity) == 'string', 'Quantity is required!')
Handlers.add('mint', Handlers.utils.hasMatchingTag('Action', 'Mint'), function (msg)
assert(type(msg.Quantity) == 'string', 'Quantity is required!')
assert(bint.__lt(0, msg.Quantity), 'Quantity must be greater than zero!')

if msg.From == env.Process.Id then
if not Balances[ao.id] then Balances[ao.id] = "0" end

if msg.From == ao.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
Balances[msg.From] = tostring(bint.__add(Balances[Owner], msg.Quantity))
ao.send({
Target = msg.From,
Data = Colors.gray .. "Successfully minted " .. Colors.blue .. msg.Quantity .. Colors.reset
})
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!'
}
Target = msg.From,
Action = 'Mint-Error',
['Message-Id'] = msg.Id,
Error = 'Only the Process Owner can mint new ' .. Ticker .. ' tokens!'
})
end
end)

```

0 comments on commit 4567e6b

Please sign in to comment.