搭建小网站,国内最大的网站建设公司排名,学校网站建设建议,单位邮箱怎么申请163邮箱背景
之前我们已经了解TS的一些语法#xff0c;接下来可以实战训练下#xff0c;这系列的文章就会介绍如何通过Aptos官网提供的TypeScript SDK与Aptos进行交互#xff0c;这篇文章主要讲的就是如何使用提供API在aptos区块链上转帐。
官网示例
官网提供了交互的例子#…背景
之前我们已经了解TS的一些语法接下来可以实战训练下这系列的文章就会介绍如何通过Aptos官网提供的TypeScript SDK与Aptos进行交互这篇文章主要讲的就是如何使用提供API在aptos区块链上转帐。
官网示例
官网提供了交互的例子我们需要先clone下仓库
git clone https://github.com/aptos-labs/aptos-core.git然后进入例子的文件中
cd ~/aptos-core/ecosystem/typescript/sdk/examples/typescript然后安装必要的依赖这里使用的是pnpm如果没有安装pnpm则需要先安装一下然后用一下命令来安装依赖
pnpm install然后通过以下命令来运行例子
pnpm run transfer_coin接着就会看到以下输出 Addresses
Alice: 0x98b90c8febd6a248374f11d409045e9e06a68e3ae8688b00c99cf6c2218cbc18
Bob: 0x5a22c7704392910541ee53960477722c3aec0667b2bcb3da954f8e06490b39d3 Initial Balances
Alice: 100000000
Bob: 0 Intermediate Balances
Alice: 99944800
Bob: 1000 Final Balances
Alice: 99889600
Bob: 2000这期间经过具体的步骤如下
初始化REST和facuet客户端创建两个账户Alice和BobAlice账户从facuet领取代币Alice转账1000代币个Bob并支付gas费Alice再次转帐1000代币给Bob并支付gas费
实现过程
之前我们已经大概了解了这个例子做的事情那么这又是怎么实现的呢接下来我们可以一步一步看代码
初始化客户端
第一步我们就要初始化REST和facuet客户端。
REST客户端是用来和REST API交互的facuet客户端是用来与开发网Faucet服务交互的可以创建账户和获取测试代币
const client new AptosClient(NODE_URL);
const faucetClient new FaucetClient(NODE_URL, FAUCET_URL); 使用API client我们可以创建一个CoinClient使用CoinClient可以进行常规的账户操作如转帐和检查余额。
const coinClient new CoinClient(client);在common.ts中初始化来URL如下
export const NODE_URL process.env.APTOS_NODE_URL || https://fullnode.devnet.aptoslabs.com;
export const FAUCET_URL process.env.APTOS_FAUCET_URL || https://faucet.devnet.aptoslabs.com;❝ 在默认情况下URL都是指向开发网的服务但是我们也可以通过以下两个环境变量配置 ❞ - APTOS_NODE_URL
- APTOS_FAUCET_URL创建本地账户
接下来需要创建两个本地账户账户有链上状态和链下状态链下状态由一个地址和一个公钥/私钥对组成私钥是用来验证所有权的下面代码创建了链下状态
const alice new AptosAccount();
const bob new AptosAccount(); 创建区块链账户
在Aptos中每一个账户都必须要有一个链上代表用于接收代币以及与其他dAPP交互一个账户代表了存储资产的媒介以下代码说明了如何使用Faucet创建账户然后获取代币。
await faucetClient.fundAccount(alice.address(), 100_000_000);
await faucetClient.fundAccount(bob.address(), 0); 读取余额
以下代码说明如何去获取账户余额在这个背景下SDK中的CoinClient函数checkBalance可以查询现在存储的值
console.log(Alice: ${await coinClient.checkBalance(alice)});
console.log(Bob: ${await coinClient.checkBalance(bob)}); async checkBalance(account: AptosAccount | MaybeHexString,extraArgs?: {// The coin type to use, defaults to 0x1::aptos_coin::AptosCoincoinType?: string;},
): Promisebigint {const coinType extraArgs?.coinType ?? APTOS_COIN;const typeTag 0x1::coin::CoinStore${coinType};const address getAddressFromAccountOrAddress(account);const accountResource await this.aptosClient.getAccountResource(address, typeTag);return BigInt((accountResource.data as any).coin.value);
} 转帐
与上一步一样这是另一个帮助步骤它构建了一个将硬币从 Alice 转移到 Bob 的交易。对于正确生成的交易API 将返回交易哈希可在后续步骤中使用该哈希来检查交易状态。 Aptos 区块链确实对提交进行了一些验证检查如果其中任何一个失败用户将收到错误消息。这些验证使用交易签名和未使用的序列号并将交易提交到适当的链。
let txnHash await coinClient.transfer(alice, bob, 1_000, { gasUnitPrice: BigInt(100) }); 在幕后传输函数生成交易负载并让客户端签名、发送并等待它
async transfer(from: AptosAccount,to: AptosAccount | MaybeHexString,amount: number | bigint,extraArgs?: OptionalTransactionArgs {// The coin type to use, defaults to 0x1::aptos_coin::AptosCoincoinType?: string;// If set, create the receiver account if it doesnt exist on-chain.// This is done by calling 0x1::aptos_account::transfer instead, which// will create the account on-chain first if it doesnt exist before// transferring the coins to it.createReceiverIfMissing?: boolean;},
): Promisestring {// If none is explicitly given, use 0x1::aptos_coin::AptosCoin as the coin type.const coinTypeToTransfer extraArgs?.coinType ?? APTOS_COIN;// If we should create the receiver account if it doesnt exist on-chain,// use the 0x1::aptos_account::transfer function.const func extraArgs?.createReceiverIfMissing ? 0x1::aptos_account::transfer : 0x1::coin::transfer;// If were using the 0x1::aptos_account::transfer function, we dont// need type args.const typeArgs extraArgs?.createReceiverIfMissing ? [] : [coinTypeToTransfer];// Get the receiver address from the AptosAccount or MaybeHexString.const toAddress getAddressFromAccountOrAddress(to);const payload this.transactionBuilder.buildTransactionPayload(func, typeArgs, [toAddress, amount]);return this.aptosClient.generateSignSubmitTransaction(from, payload, extraArgs);
} generateSignSubmitTransaction的内容如下
const rawTransaction await this.generateRawTransaction(sender.address(), payload, extraArgs);
const bcsTxn AptosClient.generateBCSTransaction(sender, rawTransaction);
const pendingTransaction await this.submitSignedBCSTransaction(bcsTxn);
return pendingTransaction.hash;等待交易处理
在 TypeScript 中只需调用 coinClient.transfer 就足以等待交易完成。一旦处理成功或不成功该函数将返回 API 返回的事务或者如果处理时间超过超时则抛出错误。如果您希望在事务未成功提交时抛出错误则可以在调用 transfer 时将 checkSuccess 设置为 true
await client.waitForTransaction(txnHash); 最后
这篇文章主要讲述了如何与Aptos区块链进行交互更多内容可以关注公众号QStack。