文章目录
- 一、安装node
- 1.1 在 macOS 上管理 Node版本
- 1.1.1 安装 nvm
- 1.1.2 验证 nvm 是否安装成功
- 1.1.3 使用 nvm 安装/切换 Node.js 版本
- 1.1.4 卸载 Node.js 版本
- 1.2 在 windows 上管理 Node版本
- 1.2.1 安装 nvm-windows
- 1.2.2 安装 Node.js 版本
- 1.2.3 切换 Node.js 版本
- 1.2.4 卸载 Node.js 版本
- 1.2.5 检查当前 Node.js 版本
- 二、安装 aos
- 三、使用AOS
- 3.1 发送第一个命令
- 3.2 发送消息
- 3.3 向 Morpheus 发送消息
- 3.4 收件箱
- 四、操作Arweave的token
- 4.1 发布一个Arweave的token
- 4.2 使用golang调用token
- 4.3 获取消息信息
一、安装node
1.1 在 macOS 上管理 Node版本
在 macOS 上管理 Node.js 版本,通常使用 nvm(Node Version Manager)是最便捷的方式。以下是安装和使用 nvm 来管理 Node.js 版本的步骤:
1.1.1 安装 nvm
打开终端并运行以下命令来安装 nvm:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.4/install.sh | bash
运行上述命令后,按照提示操作,安装过程会将 nvm 的路径添加到 shell 配置文件(如 .bashrc, .bash_profile, .zshrc 等)中。
注意:如果你使用的是 Zsh 作为默认终端,可能需要编辑 .zshrc 文件;如果使用的是 Bash,可能需要编辑 .bash_profile 或 .bashrc。
在安装完成后,执行以下命令来重新加载 shell 配置文件:
source ~/.bashrc # 或者 source ~/.zshrc,视具体情况而定
1.1.2 验证 nvm 是否安装成功
在终端运行以下命令来检查是否成功安装:
nvm --version
如果返回 nvm 版本号,则说明安装成功。
1.1.3 使用 nvm 安装/切换 Node.js 版本
-
查看可用的 Node.js 版本:
nvm ls-remote
-
安装特定版本的 Node.js:
nvm install <version>
例如,安装 Node.js 21.x:
nvm install v21.0.0
-
切换到特定版本的 Node.js:
nvm use <version>
例如,切换到 Node.js 22.x:
nvm use v22.0.0
-
查看当前已安装的 Node.js 版本:
nvm ls
-
设置默认 Node.js 版本:
nvm alias default <version>
例如,设置默认版本为 22.x:
nvm alias default v22.0.0
这样每次打开终端时,系统会自动使用你设定的默认版本。
1.1.4 卸载 Node.js 版本
如果需要卸载某个 Node.js 版本,可以使用以下命令:
nvm uninstall <version>
例如:
nvm uninstall 22
1.2 在 windows 上管理 Node版本
在 Windows 上管理 Node.js 版本,常用的工具是 nvm-windows,这是 Node Version Manager (NVM) 的 Windows 版本。使用它可以轻松安装、切换和管理多个 Node.js 版本。
1.2.1 安装 nvm-windows
-
下载 nvm-windows:前往 nvm-windows GitHub 主页 - 下载最新的 .zip 文件或 .exe 安装文件。
-
安装:运行下载的 .exe 文件,按照安装向导的提示进行安装。建议选择默认的安装路径,以免出现权限问题。
1.2.2 安装 Node.js 版本
- 查看可用的 Node.js 版本
安装好 nvm 后,打开命令行(如 PowerShell 或 CMD),输入以下命令来查看可安装的 Node.js 版本列表:
nvm list available
- 安装指定版本的 Node.js
选择一个版本号,并使用以下命令进行安装,例如安装 Node.js 16.13.0:
nvm install 16.13.0
- 查看已安装的版本
可以查看系统上已经安装的 Node.js 版本:
nvm list
1.2.3 切换 Node.js 版本
使用指定版本的 Node.js
如果你已经安装了多个版本的 Node.js,你可以使用以下命令切换到所需的版本:
nvm use 16.13.0
使用该命令后,你的 Node.js 和 npm 都会切换到该版本。
1.2.4 卸载 Node.js 版本
卸载不需要的版本
如果你不再需要某个版本的 Node.js,可以通过以下命令卸载:
nvm uninstall 16.13.0
1.2.5 检查当前 Node.js 版本
确认当前的 Node.js 版本
-
使用 node -v 来查看当前使用的 Node.js 版本:
node -v
二、安装 aos
-
完成 NodeJS 安装后,只需安装 aos 并运行它:
npm i -g https://preview_ao.g8way.io
-
卸载aos服务
npm uninstall -g @permaweb/aos
-
安装完成后,我们运行命令即可启动一个新的 aos 进程!
aos
aos 命令运行时,其实是你在使用密钥文件向 aos 验证你的身份的。如果没有指定,aos 会默认生成一个新的密钥文件并将其存储在本地 ~/.aos.json。如果你有 Arweave 钱包,可以使用 --wallet [location] 参数使用指定钱包。
刚刚我们启动的程序实例是本地客户端,它已准备好将消息发送到你的新进程(ao 计算机内的进程)。
连接后,我们会看到以下内容:
_____ _______ _____
/\ \ /::\ \ /\ \
/::\ \ /::::\ \ /::\ \
/::::\ \ /::::::\ \ /::::\ \
/::::::\ \ /::::::::\ \ /::::::\ \
/:::/\:::\ \ /:::/~~\:::\ \ /:::/\:::\ \
/:::/__\:::\ \ /:::/ \:::\ \ /:::/__\:::\ \
/::::\ \:::\ \ /:::/ / \:::\ \ \:::\ \:::\ \
/::::::\ \:::\ \ /:::/____/ \:::\____\ ___\:::\ \:::\ \
/:::/\:::\ \:::\ \ |:::| | |:::| | /\ \:::\ \:::\ \
/:::/ \:::\ \:::\____\|:::|____| |:::| |/::\ \:::\ \:::\____\
\::/ \:::\ /:::/ / \:::\ \ /:::/ / \:::\ \:::\ \::/ /
\/____/ \:::\/:::/ / \:::\ \ /:::/ / \:::\ \:::\ \/____/
\::::::/ / \:::\ /:::/ / \:::\ \:::\ \
\::::/ / \:::\__/:::/ / \:::\ \:::\____\
/:::/ / \::::::::/ / \:::\ /:::/ /
/:::/ / \::::::/ / \:::\/:::/ /
/:::/ / \::::/ / \::::::/ /
/:::/ / \::/____/ \::::/ /
\::/ / ~~ \::/ /
\/____/ \/____/
Welcome to AOS: Your operating system for AO, the decentralized open access supercomputer.
Type ".load-blueprint chat" to join the community chat and ask questions!
AOS Client Version: 2.0.0. 2024
Type "Ctrl-C" twice to exit
Your AOS process: 9qyG3YAlPYt9SBns5zwrbIZGvYVdM8s86fIi2qw8jbs
default@aos-2.0.0[Inbox:8]>
三、使用AOS
3.1 发送第一个命令
我们所拥有的 aos 进程,已经驻留在 ao 计算机内部的服务器上,等待接收和执行你的命令。
为了让开发更加的简单,aos 使用 Lua 编程语言撰写命令。 还没学过 Lua? 不要着急! 这是一种超级简单、友好的语言。 看完本手册后你就顺带学会 Lua。
让我们打破僵局并输入:
default@aos-2.0.0[Inbox:8]> "Hello, ao!"
然后按 [Enter] 键。 你会看到 shell 签名并发布消息,请求结果,然后打印结果,如下所示:
Hello, ao!
3.2 发送消息
Send({ Target = "process ID", Data = "Hello World!" })
- Send:Send 是 aos 中全局函数,用于发送消息。
- Target:如果要将消息发送到特定进程,请在消息中包含 Target 字段。
- Data:Data 是你希望目标进程接收的文本消息。 在此示例中,消息是 Hello World!
3.3 向 Morpheus 发送消息
- 存储 Morpheus 的进程 ID
我们将使用下面提供的进程 ID 并将其存储为名为 Morpheus 的变量。
wu_tAUDUveetQZpcN8UxHt51d9dyUkI4Z-MfQV8LnUU
通过复制上面的进程 ID 并在 aos CLI 中运行以下命令以便将其存储为变量:
Morpheus = "wu_tAUDUveetQZpcN8UxHt51d9dyUkI4Z-MfQV8LnUU"
这会将进程 ID 存储为名为 Morpheus 的变量,从而更轻松地与特定进程 ID 进行交互。
创建 Morpheus 变量时,我们应该看到的唯一响应是 undefined。 这是预料之中的。 要检查变量是否已成功创建,请输入 Morpheus 并按 Enter。 我们应该会看到你存储的进程 ID。
检查 Morpheus 变量
-- 通过输入 `Morpheus` 检查 Morpheus 变量
Morpheus
-- 预期结果:
wu_tAUDUveetQZpcN8UxHt51d9dyUkI4Z-MfQV8LnUU
-- 如果 `undefined` 被返回,
-- 那么变量没有创建成功。
- 向 Morpheus 发送消息
获取 Morpheus 的进程 ID 并将其存储在变量中后,我们就可以与它进行通信了。 为此,你可以使用 Send 函数。 Morpheus 本身就是 ao 中运行的一个并行进程。 他使用一系列 handler 接收和发送消息。 让我们向他发送消息,看看会发生什么。
Send({ Target = Morpheus, Data = "Morpheus?" })
-
我们的 Target 是 Morpheus,这是我们之前使用 Morpheus 进程 ID 定义的变量。
-
Data 是我们要发送给 Morpheus 的消息。 在这里,它是 Morpheus?。
预期结果:
-- 我们的消息命令
Send({ Target = Morpheus, Data = "Morpheus?"})
-- 消息已添加到发件箱
message added to outbox
-- 从 `Morpheus` 的进程 ID 收到一条新消息
New Message From BWM...ulw: Data = I am here. You are f
3.4 收件箱
收件箱 是我们从其他进程接收消息的地方。
让我们检查收件箱,看看我们收到了多少条消息。
在 aos CLI 中,输入以下命令:
#Inbox
返回值示范:
-- 你的 `收件箱` 命令
#Inbox
-- 该命令将返回我们收件箱中的消息数量
16
在上面的示例中,返回为 16,表示收件箱中有十六封邮件。
由于我们主要是为了寻找 Morpheus 的回复,因此我们假设他的消息是最后收到的消息。要阅读收件箱中的最后一条消息,请键入以下命令:
Inbox[#Inbox].Data
该命令允许我们将数据与消息分离,并且仅读取特定数据字段的内容。
预期返回:
-- 你的 Inbox[x].Data 命令
Inbox[#Inbox].Data
-- 该命令将返回消息的 `Data` 字段。
-- Data 通常代表基于文本的消息
-- 从一个进程接收到另一进程。
I am here. You are finally awake. Are you ready to see how far the rabbit hole goes?
四、操作Arweave的token
4.1 发布一个Arweave的token
// 引入lua库
// bint用于处理大整数
// json用于处理 JSON 数据
local bint = require('.bint')(256)
local json = require('json')
// 定义工具函数
// add: 将两个数相加,返回字符串形式的结果。
// subtract: 从一个数中减去另一个数,返回字符串形式的结果。
// toBalanceValue: 将一个数字转换为字符串,用于表示余额。
// toNumber: 将字符串形式的数字转换为 Lua 数字。
local utils = {
add = function(a, b)
return tostring(bint(a) + bint(b))
end,
subtract = function(a, b)
return tostring(bint(a) - bint(b))
end,
toBalanceValue = function(a)
return tostring(bint(a))
end,
toNumber = function(a)
return bint.tonumber(a)
end
}
// 全局变量,这部分属于合约本身内部的状态,属于当前process 独立的状态,收到消息并且处理以后呢,将会改变这些状态
// 定义代币的基本信息,版本、精度(小数位数)、初始余额、总供应量、名称、符号和 logo。
// 使用 or 操作符为未定义的变量提供默认值。
Variant = "0.0.3"
Denomination = Denomination or 12
Balances = Balances or { [ao.id] = utils.toBalanceValue(10000 * 10 ^ Denomination) }
TotalSupply = TotalSupply or utils.toBalanceValue(10000 * 10 ^ Denomination)
Name = Name or 'Points Coin'
Ticker = Ticker or 'PNTS'
Logo = Logo or 'SBCCXwwecBlDqRLUjb8dYABExTJXLieawf7m2aBJ-KY'
// info:处理“信息”请求,返回代币的基本信息。
Handlers.add('info', Handlers.utils.hasMatchingTag("Action", "Info"), function(msg)
if msg.reply then
msg.reply({
Name = Name,
Ticker = Ticker,
Logo = Logo,
Denomination = tostring(Denomination)
})
else
Send({Target = msg.From,
Name = Name,
Ticker = Ticker,
Logo = Logo,
Denomination = tostring(Denomination)
})
end
end)
// balance:处理查询余额的请求,根据接收者的不同,返回相应的余额。
Handlers.add('balance', Handlers.utils.hasMatchingTag("Action", "Balance"), function(msg)
local bal = '0'
if (msg.Tags.Recipient) then
if (Balances[msg.Tags.Recipient]) then
bal = Balances[msg.Tags.Recipient]
end
elseif msg.Tags.Target and Balances[msg.Tags.Target] then
bal = Balances[msg.Tags.Target]
elseif Balances[msg.From] then
bal = Balances[msg.From]
end
if msg.reply then
msg.reply({
Balance = bal,
Ticker = Ticker,
Account = msg.Tags.Recipient or msg.From,
Data = bal
})
else
Send({
Target = msg.From,
Balance = bal,
Ticker = Ticker,
Account = msg.Tags.Recipient or msg.From,
Data = bal
})
end
end)
// balances:用于查询所有账户的余额
Handlers.add('balances', Handlers.utils.hasMatchingTag("Action", "Balances"),
function(msg)
if msg.reply then
msg.reply({ Data = json.encode(Balances) })
else
Send({Target = msg.From, Data = json.encode(Balances) })
end
end)
// transfer:处理代币转账请求,检查余额是否足够,如果足够,则进行转账并发送通知。
Handlers.add('transfer', Handlers.utils.hasMatchingTag("Action", "Transfer"), function(msg)
// transfer参数的检查,确保 接收者 和 数量 的类型正确,且数量大于0
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')
// 检查发件人和接收人的余额,如果不存在则初始化为0。
if not Balances[msg.From] then Balances[msg.From] = "0" end
if not Balances[msg.Recipient] then Balances[msg.Recipient] = "0" end
// 检查发送者的余额是否足够进行转账
if bint(msg.Quantity) <= bint(Balances[msg.From]) then
// 从发送者余额中扣除数量,并将该数量加到接收者余额。
Balances[msg.From] = utils.subtract(Balances[msg.From], msg.Quantity)
Balances[msg.Recipient] = utils.add(Balances[msg.Recipient], msg.Quantity)
// 通知发送者及接收者 转账通知
if not msg.Cast then
local debitNotice = {
Action = 'Debit-Notice',
Recipient = msg.Recipient,
Quantity = msg.Quantity,
Data = Colors.gray ..
"You transferred " ..
Colors.blue .. msg.Quantity .. Colors.gray .. " to " .. Colors.green .. msg.Recipient .. Colors.reset
}
local creditNotice = {
Target = msg.Recipient,
Action = 'Credit-Notice',
Sender = msg.From,
Quantity = msg.Quantity,
Data = Colors.gray ..
"You received " ..
Colors.blue .. msg.Quantity .. Colors.gray .. " from " .. Colors.green .. msg.From .. Colors.reset
}
for tagName, tagValue in pairs(msg) do
if string.sub(tagName, 1, 2) == "X-" then
debitNotice[tagName] = tagValue
creditNotice[tagName] = tagValue
end
end
if msg.reply then
msg.reply(debitNotice)
else
Send(debitNotice)
end
Send(creditNotice)
end
else
if msg.reply then
msg.reply({
Action = 'Transfer-Error',
['Message-Id'] = msg.Id,
Error = 'Insufficient Balance!'
})
else
Send({
Target = msg.From,
Action = 'Transfer-Error',
['Message-Id'] = msg.Id,
Error = 'Insufficient Balance!'
})
end
end
end)
// mint:铸造、增发代币
Handlers.add('mint', Handlers.utils.hasMatchingTag("Action","Mint"), function(msg)
assert(type(msg.Quantity) == 'string', 'Quantity is required!')
assert(bint(0) < bint(msg.Quantity), 'Quantity must be greater than zero!')
if not Balances[ao.id] then Balances[ao.id] = "0" end
if msg.From == ao.id then
Balances[msg.From] = utils.add(Balances[msg.From], msg.Quantity)
TotalSupply = utils.add(TotalSupply, msg.Quantity)
if msg.reply then
msg.reply({
Data = Colors.gray .. "Successfully minted " .. Colors.blue .. msg.Quantity .. Colors.reset
})
else
Send({
Target = msg.From,
Data = Colors.gray .. "Successfully minted " .. Colors.blue .. msg.Quantity .. Colors.reset
})
end
else
if msg.reply then
msg.reply({
Action = 'Mint-Error',
['Message-Id'] = msg.Id,
Error = 'Only the Process Id can mint new ' .. Ticker .. ' tokens!'
})
else
Send({
Target = msg.From,
Action = 'Mint-Error',
['Message-Id'] = msg.Id,
Error = 'Only the Process Id can mint new ' .. Ticker .. ' tokens!'
})
end
end
end)
// totalSupply:处理查询总供应量的请求
Handlers.add('totalSupply', Handlers.utils.hasMatchingTag("Action","Total-Supply"), function(msg)
assert(msg.From ~= ao.id, 'Cannot call Total-Supply from the same process!')
if msg.reply then
msg.reply({
Action = 'Total-Supply',
Data = TotalSupply,
Ticker = Ticker
})
else
Send({
Target = msg.From,
Action = 'Total-Supply',
Data = TotalSupply,
Ticker = Ticker
})
end
end)
// burn:销毁代币,
Handlers.add('burn', Handlers.utils.hasMatchingTag("Action",'Burn'), function(msg)
assert(type(msg.Tags.Quantity) == 'string', 'Quantity is required!')
assert(bint(msg.Tags.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.Tags.Quantity)
TotalSupply = utils.subtract(TotalSupply, msg.Tags.Quantity)
if msg.reply then
msg.reply({
Data = Colors.gray .. "Successfully burned " .. Colors.blue .. msg.Tags.Quantity .. Colors.reset
})
else
Send({Target = msg.From, Data = Colors.gray .. "Successfully burned " .. Colors.blue .. msg.Tags.Quantity .. Colors.reset })
end
end)
这段合约实现了一个简单的token系统,支持基本的token操作,如查询余额、转账、铸造和销毁token。通过使用处理器模式,代码结构清晰,易于扩展。若要进一步提高代码的安全性和可读性,可以考虑添加详细的注释和更多的错误处理机制。
4.2 使用golang调用token
package main
import (
"github.com/liteseed/aogo"
"github.com/liteseed/goar/signer"
"github.com/liteseed/goar/tag"
"log"
//aogo 库用于与 Arweave 的智能合约交互
//goar 库用于处理钱包签名和消息标签
)
func main() {
walletPath := "./wallet.json"
// 从钱包路径创建签名器
s, err := signer.FromPath(walletPath)
if err != nil {
log.Fatalf("创建签名器失败:%v", err)
}
// 初始化 AO 对象
ao, err := aogo.New()
if err != nil {
log.Fatalf("初始化 AO 对象失败:%v", err)
}
// 定义目标合约 ID
processPID := "jysQej65l7KHRZi93csg0rvdmciJNL9hteM1N_yakpE" // 合约 ID
// 定义标签,定义操作及操作者
tags := &[]tag.Tag{
{Name: "Action", Value: "Balance"},
{Name: "Target", Value: "Glj6gtx-NJNXTWOF9z9dN2aue3KyU5A_sxR71L1Cak8"},
}
// 向目标合约发送消息
messageID, err := ao.SendMessage(processPID, "", tags, "", s)
if err != nil {
log.Fatalf("发送消息失败:%v", err)
}
log.Println("成功发送消息,消息 ID:", messageID)
res, err := ao.LoadResult(processPID, messageID)
if err != nil {
log.Fatalf("读取消息失败:%v", err)
}
log.Println(res.Messages)
log.Println(res.Error)
}
这段代码展示了如何在 Arweave 网络上发送数据并与智能合约交互。通过创建签名器、定义标签、发送消息和读取结果。
4.3 获取消息信息
可以从API中获得消息信息:
https://cu49.ao-testnet.xyz/result/zpPRT9ASUBrT1-OO2LRIfz3IeALW9HiSlHhk6QOmRP0?process-id=jysQej65l7KHRZi93csg0rvdmciJNL9hteM1N_yakpE
result 后跟:Message Id,process-id则是我们的token合约ID
返回结果为:
{"Messages":[{"Target":"60vmK1FkO0f84yHggO5os6n3e_YnVF6O7V6IeX1vjaU","Data":"0","Tags":[{"name":"Data-Protocol","value":"ao"},{"name":"Variant","value":"ao.TN.1"},{"name":"Type","value":"Message"},{"name":"From-Process","value":"jysQej65l7KHRZi93csg0rvdmciJNL9hteM1N_yakpE"},{"name":"From-Module","value":"5l00H2S0RuPYe-V5GAI-1RgQEHFInSMr20E-3RNXJ_U"},{"name":"Ref_","value":"10"},{"name":"Balance","value":"0"},{"name":"Account","value":"60vmK1FkO0f84yHggO5os6n3e_YnVF6O7V6IeX1vjaU"},{"name":"Ticker","value":"PNTS"}],"Anchor":"00000000000000000000000000000010"}],"Assignments":[],"Spawns":[],"Output":[],"GasUsed":601453529}
注:所有发送的消息及返回的消息都会上链,大概十分钟左右