diff --git a/README.md b/README.md index a2ad2b60..60821cee 100644 --- a/README.md +++ b/README.md @@ -4,11 +4,11 @@ logo -Status: Preview rc1.1 -Version: 2.0.0-rc1.1 -Module: `xT0ogTeagEGuySbKuUoo_NaWeeBv1fZ4MqgDdKVKY0U` +Status: Preview rc2 +Version: 2.0.0-rc2 +Module: `PSPMkkFrJzYI2bQbkmeEQ5ONmeR-FJZu0fNQoSCU1-I` -Sqlite-Module: `sFNHeYzhHfP9vV9CPpqZMU-4Zzq_qKGKwlwMZozWi2Y` +Sqlite-Module: `C4bxMlK8d_wQ-QpXIIZLU8UWXu6Sd8PDJw7HN3nNE2I` ## Requirements diff --git a/blueprints/chatroom.lua b/blueprints/chatroom.lua index bffe57e4..12d6bddf 100644 --- a/blueprints/chatroom.lua +++ b/blueprints/chatroom.lua @@ -2,7 +2,7 @@ Members = Members or {} Handlers.add( "register", - Handlers.utils.hasMatchingTag("Action", "Register"), + "Register", function (msg) local found = false for _, member in ipairs(Members) do @@ -23,7 +23,7 @@ Handlers.add( Handlers.add( "unregister", - Handlers.utils.hasMatchingTag("Action", "Unregister"), + "Unregister", function (msg) local found = false for i, v in ipairs(Members) do @@ -42,7 +42,7 @@ Handlers.add( Handlers.add( "broadcast", - Handlers.utils.hasMatchingTag("Action", "Broadcast"), + "Broadcast", function (msg) local haveSentRecords = {} for _, recipient in ipairs(Members) do diff --git a/blueprints/credUtils.lua b/blueprints/credUtils.lua deleted file mode 100644 index 1d8b8044..00000000 --- a/blueprints/credUtils.lua +++ /dev/null @@ -1,112 +0,0 @@ -CRED_PROCESS = "Sa0iBLPNyJQrwpTTG-tWLQU-1QeUAJA73DdxGGiKoJc" - -_CRED = { balance = "Your CRED balance has not been checked yet. Updating now." } - -local credMeta = { - __index = function(t, key) - -- sends CRED balance request - if key == "update" then - Send({ Target = CRED_PROCESS, Action = "Balance", Tags = { Target = ao.id } }) - return "Balance update requested." - -- prints local CRED balance, requests it if not set - elseif key == "balance" then - if _CRED.balance == "Your CRED balance has not been checked yet. Updating now." then - Send({ Target = CRED_PROCESS, Action = "Balance", Tags = { Target = ao.id } }) - end - return _CRED.balance - -- prints CRED process ID - elseif key == "process" then - return CRED_PROCESS - -- tranfers CRED - elseif key == "send" then - return function(target, amount) - -- ensures amount is string - amount = tostring(amount) - print("sending " .. amount .. "CRED to " .. target) - Send({ Target = CRED_PROCESS, Action = "Transfer", Recipient = target, Quantity = amount }) - end - else - return nil - end - end -} - - -CRED = setmetatable({}, credMeta) - --- Function to evaluate if a message is a balance update -local function isCredBalanceMessage(msg) - if msg.From == CRED_PROCESS and msg.Tags.Balance then - return true - else - return false - end -end - --- Function to evaluate if a message is a Debit Notice -local function isDebitNotice(msg) - if msg.From == CRED_PROCESS and msg.Tags.Action == "Debit-Notice" then - return true - else - return false - end -end - --- Function to evaluate if a message is a Credit Notice -local function isCreditNotice(msg) - if msg.From == CRED_PROCESS and msg.Tags.Action == "Credit-Notice" then - return true - else - return false - end -end - -local function formatBalance(balance) - -- Ensure balance is treated as a string - balance = tostring(balance) - -- Check if balance length is more than 3 to avoid unnecessary formatting - if #balance > 3 then - -- Insert dot before the last three digits - balance = balance:sub(1, -4) .. "." .. balance:sub(-3) - end - return balance -end - --- Handles Balance messages -Handlers.add( - "UpdateCredBalance", - isCredBalanceMessage, - function(msg) - local balance = nil - if msg.Tags.Balance then - balance = msg.Tags.Balance - end - -- Format the balance if it's not set - if balance then - -- Format the balance by inserting a dot after the first three digits from the right - local formattedBalance = formatBalance(balance) - _CRED.balance = formattedBalance - print("CRED Balance updated: " .. _CRED.balance) - else - print("An error occurred while updating CRED balance") - end - end -) - --- Handles Debit notices -Handlers.add( - "CRED_Debit", - isDebitNotice, - function(msg) - print(msg.Data) - end -) - --- Handles Credit notices -Handlers.add( - "CRED_Credit", - isCreditNotice, - function(msg) - print(msg.Data) - end -) diff --git a/blueprints/staking.lua b/blueprints/staking.lua index dcfaffa9..ec42c4ce 100644 --- a/blueprints/staking.lua +++ b/blueprints/staking.lua @@ -29,7 +29,7 @@ Handlers.stake = function(msg) Stakers[msg.From].amount = utils.add(Stakers[msg.From].amount, msg.Tags.Quantity) Stakers[msg.From].unstake_at = height + delay print("Successfully Staked " .. msg.Tags.Quantity) - ao.send({Target = msg.From, Data = "Successfully Staked " .. msg.Tags.Quantity}) + msg.reply({ Data = "Successfully Staked " .. msg.Tags.Quantity}) end -- Unstake Action Handler @@ -41,7 +41,7 @@ Handlers.unstake = function(msg) amount = msg.Quantity, release_at = stakerInfo.unstake_at } - ao.send({Target = msg.From, Data = "Successfully unstaked " .. msg.Tags.Quantity}) + msg.reply({ Data = "Successfully unstaked " .. msg.Tags.Quantity}) end -- Finalization Handler @@ -68,8 +68,8 @@ local function continue(fn) end end -Handlers.add('staking.balances', Handlers.utils.hasMatchingTag('Action', 'Stakers'), - function(msg) Send({ Target = msg.From, Data = require('json').encode(Stakers) }) end) +Handlers.add('staking.balances', 'Stakers', + function(msg) msg.reply({ Data = require('json').encode(Stakers) }) end) -- Registering Handlers Handlers.add("staking.stake", continue(Handlers.utils.hasMatchingTag("Action", "Stake")), Handlers.stake) diff --git a/blueprints/token.lua b/blueprints/token.lua index 0e14512d..ce3cda1b 100644 --- a/blueprints/token.lua +++ b/blueprints/token.lua @@ -1,5 +1,4 @@ local bint = require('.bint')(256) -local ao = require('ao') --[[ This module implements the ao Standard Token Specification. @@ -72,9 +71,8 @@ Logo = Logo or 'SBCCXwwecBlDqRLUjb8dYABExTJXLieawf7m2aBJ-KY' Info ]] -- -Handlers.add('info', Handlers.utils.hasMatchingTag('Action', 'Info'), function(msg) - ao.send({ - Target = msg.From, +Handlers.add('info', "Info", function(msg) + msg.reply({ Name = Name, Ticker = Ticker, Logo = Logo, @@ -86,7 +84,7 @@ end) Balance ]] -- -Handlers.add('balance', Handlers.utils.hasMatchingTag('Action', 'Balance'), function(msg) +Handlers.add('balance', "Balance", function(msg) local bal = '0' -- If not Recipient is provided, then return the Senders balance @@ -100,8 +98,7 @@ Handlers.add('balance', Handlers.utils.hasMatchingTag('Action', 'Balance'), func bal = Balances[msg.From] end - ao.send({ - Target = msg.From, + msg.reply({ Balance = bal, Ticker = Ticker, Account = msg.Tags.Recipient or msg.From, @@ -113,14 +110,14 @@ end) Balances ]] -- -Handlers.add('balances', Handlers.utils.hasMatchingTag('Action', 'Balances'), - function(msg) ao.send({ Target = msg.From, Data = json.encode(Balances) }) end) +Handlers.add('balances', "Balances", + function(msg) msg.reply({ Data = json.encode(Balances) }) end) --[[ Transfer ]] -- -Handlers.add('transfer', Handlers.utils.hasMatchingTag('Action', 'Transfer'), function(msg) +Handlers.add('transfer', "Transfer", function(msg) 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') @@ -140,7 +137,6 @@ Handlers.add('transfer', Handlers.utils.hasMatchingTag('Action', 'Transfer'), fu if not msg.Cast then -- Debit-Notice message template, that is sent to the Sender of the transfer local debitNotice = { - Target = msg.From, Action = 'Debit-Notice', Recipient = msg.Recipient, Quantity = msg.Quantity, @@ -169,12 +165,11 @@ Handlers.add('transfer', Handlers.utils.hasMatchingTag('Action', 'Transfer'), fu end -- Send Debit-Notice and Credit-Notice - ao.send(debitNotice) - ao.send(creditNotice) + msg.reply(debitNotice) + Send(creditNotice) end else - ao.send({ - Target = msg.From, + msg.reply({ Action = 'Transfer-Error', ['Message-Id'] = msg.Id, Error = 'Insufficient Balance!' @@ -186,7 +181,7 @@ end) Mint ]] -- -Handlers.add('mint', Handlers.utils.hasMatchingTag('Action', 'Mint'), function(msg) +Handlers.add('mint', "Mint", function(msg) assert(type(msg.Quantity) == 'string', 'Quantity is required!') assert(bint(0) < bint(msg.Quantity), 'Quantity must be greater than zero!') @@ -196,13 +191,11 @@ Handlers.add('mint', Handlers.utils.hasMatchingTag('Action', 'Mint'), function(m -- Add tokens to the token pool, according to Quantity Balances[msg.From] = utils.add(Balances[msg.From], msg.Quantity) TotalSupply = utils.add(TotalSupply, msg.Quantity) - ao.send({ - Target = msg.From, + msg.reply({ Data = Colors.gray .. "Successfully minted " .. Colors.blue .. msg.Quantity .. Colors.reset }) else - ao.send({ - Target = msg.From, + msg.reply({ Action = 'Mint-Error', ['Message-Id'] = msg.Id, Error = 'Only the Process Id can mint new ' .. Ticker .. ' tokens!' @@ -214,11 +207,10 @@ end) Total Supply ]] -- -Handlers.add('totalSupply', Handlers.utils.hasMatchingTag('Action', 'Total-Supply'), function(msg) +Handlers.add('totalSupply', "Total-Supply", function(msg) assert(msg.From ~= ao.id, 'Cannot call Total-Supply from the same process!') - ao.send({ - Target = msg.From, + msg.reply({ Action = 'Total-Supply', Data = TotalSupply, Ticker = Ticker @@ -228,15 +220,14 @@ end) --[[ Burn ]] -- -Handlers.add('burn', Handlers.utils.hasMatchingTag('Action', 'Burn'), function(msg) +Handlers.add('burn', 'Burn', function(msg) assert(type(msg.Quantity) == 'string', 'Quantity is required!') assert(bint(msg.Quantity) <= bint(Balances[msg.From]), 'Quantity must be less than or equal to the current balance!') Balances[msg.From] = utils.subtract(Balances[msg.From], msg.Quantity) TotalSupply = utils.subtract(TotalSupply, msg.Quantity) - ao.send({ - Target = msg.From, + msg.reply({ Data = Colors.gray .. "Successfully burned " .. Colors.blue .. msg.Quantity .. Colors.reset }) end) diff --git a/blueprints/voting.lua b/blueprints/voting.lua index a96fbc1f..d5df05ec 100644 --- a/blueprints/voting.lua +++ b/blueprints/voting.lua @@ -10,6 +10,7 @@ Handlers.vote = function(msg) assert(quantity > 0, "No staked tokens to vote") Votes[target] = Votes[target] or { yay = 0, nay = 0, deadline = deadline } Votes[target][side] = Votes[target][side] + quantity + end -- Finalization Handler @@ -41,4 +42,4 @@ end Handlers.add("vote", continue(Handlers.utils.hasMatchingTag("Action", "Vote")), Handlers.vote) -- Finalization handler should be called for every message -Handlers.add("finalize", function (msg) return -1 end, finalizationHandler) \ No newline at end of file +Handlers.add("finalize", function (msg) return "continue" end, finalizationHandler) \ No newline at end of file diff --git a/package.json b/package.json index 71826fe8..46231b30 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "type": "module", "name": "@permaweb/aos", - "version": "2.0.0-rc1.1", + "version": "2.0.0-rc2", "main": "src/index.js", "bin": "./bin/aos.js", "repository": "https://github.com/permaweb/aos.git", @@ -29,9 +29,9 @@ "yargs": "^17.7.2" }, "aos": { - "module": "xT0ogTeagEGuySbKuUoo_NaWeeBv1fZ4MqgDdKVKY0U", - "sqlite": "sFNHeYzhHfP9vV9CPpqZMU-4Zzq_qKGKwlwMZozWi2Y", - "version": "2.0.0.rc1" + "module": "PSPMkkFrJzYI2bQbkmeEQ5ONmeR-FJZu0fNQoSCU1-I", + "sqlite": "C4bxMlK8d_wQ-QpXIIZLU8UWXu6Sd8PDJw7HN3nNE2I", + "version": "2.0.0.rc2" }, "devDependencies": { "esbuild": "^0.20.1" diff --git a/process/handlers-utils.lua b/process/handlers-utils.lua index 156fbd61..ab6d53ff 100644 --- a/process/handlers-utils.lua +++ b/process/handlers-utils.lua @@ -1,4 +1,4 @@ -local _utils = { _version = "0.0.1" } +local _utils = { _version = "0.0.2" } local _ = require('.utils') local ao = require(".ao") @@ -37,10 +37,10 @@ function _utils.reply(input) assert(type(input) == 'table' or type(input) == 'string', 'invalid arguments: (input : table or string)') return function (msg) if type(input) == 'string' then - ao.send({Target = msg.From, Data = input}) + msg.reply({ Data = input}) return end - ao.send({Target = msg.From, Tags = input }) + msg.reply(input) end end diff --git a/process/package.json b/process/package.json index 8e4266fe..c4e63bbc 100644 --- a/process/package.json +++ b/process/package.json @@ -10,7 +10,7 @@ "scripts": { "build": "ao build", "test": "node --test --experimental-wasm-memory64", - "deploy": "ao publish -w ~/.wallet.json process.wasm -t Memory-Limit -v 1-gb -t Compute-Limit -v 9000000000000 -t Module-Format -v wasm64-unknown-emscripten-draft_2024_02_15 -t AOS-Version -v 2.0.0.rc1", - "deploy-sqlite": "ao publish -w ~/.wallet.json process.wasm -t Memory-Limit -v 1-gb -t Compute-Limit -v 9000000000000 -t Module-Format -v wasm64-unknown-emscripten-draft_2024_02_15 -t AOS-Version -v 2.0.0.rc1 -t AOS-Type -v AOS-SQLITE" + "deploy": "ao publish -w ~/.wallet.json process.wasm -t Memory-Limit -v 1-gb -t Compute-Limit -v 9000000000000 -t Module-Format -v wasm64-unknown-emscripten-draft_2024_02_15 -t AOS-Version -v 2.0.0.rc2 -t Name -v AOS", + "deploy-sqlite": "ao publish -w ~/.wallet.json process.wasm -t Memory-Limit -v 1-gb -t Compute-Limit -v 9000000000000 -t Module-Format -v wasm64-unknown-emscripten-draft_2024_02_15 -t AOS-Version -v 2.0.0.rc2 -t Name -v AOS-SQLITE" } } \ No newline at end of file diff --git a/process/process.lua b/process/process.lua index 3daa9dca..fb61e918 100644 --- a/process/process.lua +++ b/process/process.lua @@ -20,12 +20,16 @@ Utils = require('.utils') Handlers = require('.handlers') local stringify = require(".stringify") local assignment = require('.assignment') -ao = require('.ao') - +ao = nil +if _G.package.loaded['.ao'] then + ao = require('.ao') +elseif _G.package.loaded['ao'] then + ao = require('ao') +end -- Implement assignable polyfills on _ao assignment.init(ao) -local process = { _version = "2.0.0.rc1" } +local process = { _version = "2.0.0.rc2" } local maxInboxCount = 10000 -- wrap ao.send and ao.spawn for magic table @@ -226,7 +230,14 @@ function Version() print("version: " .. process._version) end -function process.handle(msg, env) +function process.handle(msg, _) + env = nil + if _.Process then + env = _ + else + env = _.env + end + ao.init(env) -- relocate custom tags to root message msg = ao.normalize(msg) diff --git a/src/commands/os.js b/src/commands/os.js index 22fdcb83..ae85192b 100644 --- a/src/commands/os.js +++ b/src/commands/os.js @@ -26,7 +26,7 @@ export function dry() { export function update() { // let luaFiles = fs.readdirSync(__dirname + "../../process") // .filter(n => /\.lua$/.test(n)) - let luaFiles = ['utils.lua', 'assignment.lua', 'handlers.lua', 'eval.lua', 'process.lua'] + let luaFiles = ['ao.lua', 'utils.lua', 'assignment.lua', 'handlers-utils.lua', 'handlers.lua', 'eval.lua', 'process.lua'] .map(name => { const code = fs.readFileSync(__dirname + "../../process/" + name, 'utf-8') const mod = name.replace(/\.lua$/, "")