|
| 1 | +local bint = require('.bint')(256) |
| 2 | +--[[ |
| 3 | + This module implements the ao Standard Token Specification. |
| 4 | +
|
| 5 | + Terms: |
| 6 | + Sender: the wallet or Process that sent the Message |
| 7 | +
|
| 8 | + It will first initialize the internal state, and then attach handlers, |
| 9 | + according to the ao Standard Token Spec API: |
| 10 | +
|
| 11 | + - Info(): return the token parameters, like Name, Ticker, Logo, and Denomination |
| 12 | +
|
| 13 | + - Balance(Target?: string): return the token balance of the Target. If Target is not provided, the Sender |
| 14 | + is assumed to be the Target |
| 15 | +
|
| 16 | + - Balances(): return the token balance of all participants |
| 17 | +
|
| 18 | + - Transfer(Target: string, Quantity: number): if the Sender has a sufficient balance, send the specified Quantity |
| 19 | + to the Target. It will also issue a Credit-Notice to the Target and a Debit-Notice to the Sender |
| 20 | +
|
| 21 | + - Mint(Quantity: number): if the Sender matches the Process Owner, then mint the desired Quantity of tokens, adding |
| 22 | + them the Processes' balance |
| 23 | +]] |
| 24 | +-- |
| 25 | +local json = require('json') |
| 26 | + |
| 27 | +--[[ |
| 28 | + utils helper functions to remove the bint complexity. |
| 29 | +]] |
| 30 | +-- |
| 31 | + |
| 32 | + |
| 33 | +local utils = { |
| 34 | + add = function(a, b) |
| 35 | + return tostring(bint(a) + bint(b)) |
| 36 | + end, |
| 37 | + subtract = function(a, b) |
| 38 | + return tostring(bint(a) - bint(b)) |
| 39 | + end, |
| 40 | + toBalanceValue = function(a) |
| 41 | + return tostring(bint(a)) |
| 42 | + end, |
| 43 | + toNumber = function(a) |
| 44 | + return bint.tonumber(a) |
| 45 | + end |
| 46 | +} |
| 47 | + |
| 48 | + |
| 49 | +--[[ |
| 50 | + Initialize State |
| 51 | +
|
| 52 | + ao.id is equal to the Process.Id |
| 53 | + ]] |
| 54 | +-- |
| 55 | +Variant = "0.0.4" |
| 56 | + |
| 57 | +-- token should be idempotent and not change previous state updates |
| 58 | +Denomination = Denomination or 12 |
| 59 | +Balances = Balances or { [id] = utils.toBalanceValue(10000 * 10 ^ Denomination) } |
| 60 | +TotalSupply = TotalSupply or utils.toBalanceValue(10000 * 10 ^ Denomination) |
| 61 | +Name = Name or 'Hyper Test Coin' |
| 62 | +Ticker = Ticker or 'HYPER' |
| 63 | +Logo = Logo or 'eskTHZQzCLOFrpzkr7zeaf4RxmkaNVYvHuPLh4CLpX4' |
| 64 | + |
| 65 | +--[[ |
| 66 | + Add handlers for each incoming Action defined by the ao Standard Token Specification |
| 67 | + ]] |
| 68 | +-- |
| 69 | + |
| 70 | +--[[ |
| 71 | + Info |
| 72 | + ]] |
| 73 | +-- |
| 74 | +Handlers.add('info', function(msg) |
| 75 | + send({ |
| 76 | + Target = msg.from, |
| 77 | + Name = Name, |
| 78 | + Ticker = Ticker, |
| 79 | + Logo = Logo, |
| 80 | + Denomination = tostring(Denomination) |
| 81 | + }) |
| 82 | +end) |
| 83 | + |
| 84 | +--[[ |
| 85 | + Balance |
| 86 | + ]] |
| 87 | +-- |
| 88 | +Handlers.add('balance', { action = "balance"}, function(msg) |
| 89 | + local bal = '0' |
| 90 | + print('balance called') |
| 91 | + print('from ' .. msg.from) |
| 92 | + |
| 93 | + -- If not Recipient is provided, then return the Senders balance |
| 94 | + if (msg.recipient) then |
| 95 | + if (Balances[msg.recipient]) then |
| 96 | + bal = Balances[msg.recipient] |
| 97 | + end |
| 98 | + elseif msg.target and Balances[msg.target] then |
| 99 | + bal = Balances[msg.target] |
| 100 | + elseif Balances[msg.from] then |
| 101 | + bal = Balances[msg.from] |
| 102 | + end |
| 103 | + send({ |
| 104 | + target = msg.from, |
| 105 | + balance = bal, |
| 106 | + ticker = Ticker, |
| 107 | + account = msg.recipient or msg.from, |
| 108 | + data = bal |
| 109 | + }) |
| 110 | +end) |
| 111 | + |
| 112 | +--[[ |
| 113 | + Balances |
| 114 | + ]] |
| 115 | +-- |
| 116 | +Handlers.add('balances', |
| 117 | + function(msg) |
| 118 | + send({target = msg.from, data = json.encode(Balances) }) |
| 119 | + end) |
| 120 | + |
| 121 | +--[[ |
| 122 | + Transfer |
| 123 | + ]] |
| 124 | +-- |
| 125 | +Handlers.add('transfer', function(msg) |
| 126 | + assert(type(msg.recipient) == 'string', 'Recipient is required!') |
| 127 | + assert(type(msg.quantity) == 'string', 'Quantity is required!') |
| 128 | + assert(bint.__lt(0, bint(msg.quantity)), 'Quantity must be greater than 0') |
| 129 | + |
| 130 | + if not Balances[msg.from] then Balances[msg.from] = "0" end |
| 131 | + if not Balances[msg.recipient] then Balances[msg.recipient] = "0" end |
| 132 | + |
| 133 | + if bint(msg.quantity) <= bint(Balances[msg.from]) then |
| 134 | + Balances[msg.from] = utils.subtract(Balances[msg.from], msg.quantity) |
| 135 | + Balances[msg.recipient] = utils.add(Balances[msg.recipient], msg.quantity) |
| 136 | + |
| 137 | + --[[ |
| 138 | + Only send the notifications to the Sender and Recipient |
| 139 | + if the Cast tag is not set on the Transfer message |
| 140 | + ]] |
| 141 | + -- |
| 142 | + if not msg.cast then |
| 143 | + -- Debit-Notice message template, that is sent to the Sender of the transfer |
| 144 | + local debitNotice = { |
| 145 | + action = 'Debit-Notice', |
| 146 | + recipient = msg.recipient, |
| 147 | + quantity = msg.quantity, |
| 148 | + data = colors.gray .. |
| 149 | + "You transferred " .. |
| 150 | + colors.blue .. msg.quantity .. colors.gray .. " to " .. colors.green .. msg.recipient .. colors.reset |
| 151 | + } |
| 152 | + -- Credit-Notice message template, that is sent to the Recipient of the transfer |
| 153 | + local creditNotice = { |
| 154 | + target = msg.recipient, |
| 155 | + action = 'Credit-Notice', |
| 156 | + sender = msg.from, |
| 157 | + quantity = msg.quantity, |
| 158 | + data = colors.gray .. |
| 159 | + "You received " .. |
| 160 | + colors.blue .. msg.quantity .. colors.gray .. " from " .. colors.green .. msg.from .. colors.reset |
| 161 | + } |
| 162 | + |
| 163 | + -- Add forwarded tags to the credit and debit notice messages |
| 164 | + for tagName, tagValue in pairs(msg) do |
| 165 | + -- Tags beginning with "X-" are forwarded |
| 166 | + if string.sub(tagName, 1, 2) == "x-" then |
| 167 | + debitNotice[tagName] = tagValue |
| 168 | + creditNotice[tagName] = tagValue |
| 169 | + end |
| 170 | + end |
| 171 | + |
| 172 | + -- Send Debit-Notice and Credit-Notice |
| 173 | + debitNotice.Target = msg.from |
| 174 | + send(debitNotice) |
| 175 | + send(creditNotice) |
| 176 | + end |
| 177 | + else |
| 178 | + send({ |
| 179 | + target = msg.from, |
| 180 | + action = 'Transfer-Error', |
| 181 | + ['Message-Id'] = msg.id, |
| 182 | + error = 'Insufficient Balance!' |
| 183 | + }) |
| 184 | + end |
| 185 | +end) |
| 186 | + |
| 187 | +--[[ |
| 188 | + Mint |
| 189 | + ]] |
| 190 | +-- |
| 191 | +Handlers.add('mint', function(msg) |
| 192 | + assert(type(msg.quantity) == 'string', 'Quantity is required!') |
| 193 | + assert(bint(0) < bint(msg.quantity), 'Quantity must be greater than zero!') |
| 194 | + |
| 195 | + if not Balances[id] then Balances[id] = "0" end |
| 196 | + |
| 197 | + if msg.from == id then |
| 198 | + -- Add tokens to the token pool, according to Quantity |
| 199 | + Balances[msg.from] = utils.add(Balances[msg.from], msg.quantity) |
| 200 | + TotalSupply = utils.add(TotalSupply, msg.quantity) |
| 201 | + send({ |
| 202 | + target = msg.from, |
| 203 | + data = colors.gray .. "Successfully minted " .. colors.blue .. msg.quantity .. colors.reset |
| 204 | + }) |
| 205 | + else |
| 206 | + send({ |
| 207 | + target = msg.from, |
| 208 | + action = 'Mint-Error', |
| 209 | + ['Message-Id'] = msg.id, |
| 210 | + error = 'Only the Process Id can mint new ' .. Ticker .. ' tokens!' |
| 211 | + }) |
| 212 | + end |
| 213 | +end) |
| 214 | + |
| 215 | +--[[ |
| 216 | + Total Supply |
| 217 | + ]] |
| 218 | +-- |
| 219 | +Handlers.add('totalSupply', function(msg) |
| 220 | + assert(msg.From ~= id, 'Cannot call Total-Supply from the same process!') |
| 221 | + send({ |
| 222 | + Target = msg.From, |
| 223 | + Action = 'Total-Supply', |
| 224 | + Data = TotalSupply, |
| 225 | + Ticker = Ticker |
| 226 | + }) |
| 227 | +end) |
| 228 | + |
| 229 | + |
0 commit comments