以太坊 erc20_如何编写ERC-20以太坊智能合约的单元测试

以太坊 erc20

by Il Kadyrov

由Il Kadyrov

如何编写ERC-20以太坊智能合约的单元测试 (How to write Unit tests for ERC-20 Ethereum Smart Contracts)

It’s very important to write unit tests for your smart contracts, same as for any development project. However, unit testing in blockchain-based solutions is often underestimated and overlooked.

与任何开发项目一样,为智能合约编写单元测试非常重要。 但是,基于区块链的解决方案中的单元测试经常被低估和忽视。

Last year I performed more than 200 audits of smart contracts written mostly for Ethereum, and also for Neo, Eos, Tron and Bitcoin blockchains.

去年,我对主要为以太坊以及Neo,Eos,Tron和比特币区块链编写的智能合约进行了200多次审核。

From what I observed, nearly half of these projects didn’t write unit tests. Such oversight often resulted in poor contract performance and various security issues identified during the audit.

从我观察到的结果来看,这些项目中有将近一半没有编写单元测试。 这种监督通常会导致合同绩效不佳以及审核期间发现的各种安全问题。

必备测试 (Must-have tests)

Each smart contract has common parts such as

每个智能合约都有一些共同的部分,例如

  • a constructor

    构造函数
  • total supply

    总供应
  • functions for transfer to and from

    往来传递功能
  • functions for approval

    批准功能
  • and sometimes function for burning extra tokens

    有时用于刻录额外的令牌

So it’s important to check your smart contract for correct initialization of all the parameters, and for reverts when you overflow or underflow total supply or other uint values. Also, you need to check modifiers and proper rights usage.

因此,重要的是要检查智能合约中所有参数的正确初始化,以及在上溢或下溢总供应量或其他uint值时进行还原。 另外,您需要检查修饰符和正确的权限使用情况。

In this article, I’ll focus specifically on Ethereum smart contracts, but this also applies to other platforms as contracts have the same structure across other crypto currencies. First, let’s test the proper initialization of token and its correct transfer to some address.

在本文中,我将重点关注以太坊智能合约,但这也适用于其他平台,因为合约在其他加密货币中具有相同的结构。 首先,让我们测试令牌的正确初始化及其向某个地址的正确传输。

检查初始化 (Checking Initialization)

Tests for correct initialization are simple. You just need to create a sample contract and check for correctness of all values that must be initialized.

正确初始化的测试很简单。 您只需要创建一个样本合同并检查所有必须初始化的值的正确性。

检查转账 (Checking Transfers)

Checking the transfer function is very important, because there may be issues that would cause incorrect transfers. You must make sure that recipient’s and sender’s balances change upon transfer, try to get reverts in case the function gets wrong parameters.

检查转移 功能非常重要,因为可能存在会导致错误传输的问题。 您必须确保接收方和发送方的余额在转移时会发生变化,并在功能参数错误的情况下尝试还原。

For example,

例如,

  • when the amount being sent exceeds the sender’s balance

    当发送的金额超过发送者的余额时
  • when the contract address or invalid address is sent instead of recipient address, etc.

    当发送合同地址或无效地址而不是收件人地址等时

And finally, you must check that the transfer event is logged.

最后,您必须检查是否已记录 传输事件。

The transferFrom function is very similar to transfer, but here you also need to test that the spender has enough approved balance for sending.

transferFrom函数与transfer非常相似,但是在这里,您还需要测试支出者是否有足够的批准余额来进行发送。

Here are the tests when the spender has insufficient funds required for a transfer.

当支出者的资金不足以进行转帐时,这是测试。

检查批准 (Checking Approvals)

The approve function is the simplest function from the ERC20 standard. There is no need to check for zero address. It’s enough to check that the allowance array is correctly filled. Also if you don’t have increaseApproval or decreaseApproval functions, approve will overwrite all previous values.

批准功能是ERC20标准中最简单的功能。 无需检查零地址。 检查配额阵列是否正确填充就足够了。 另外,如果您没有增加或减少功能,批准将覆盖所有以前的值。

So it is recommend to use these functions as protection from unnecessary overwrites. And of course, it’s important to check that you get correct logs from the Approval event.

因此,建议使用这些功能来防止不必要的覆盖。 当然,重要的是要检查从批准事件中获取的日志是否正确。

燃烧未售出的代币 (Burning Unsold Tokens)

Most smart contracts include a function for burning tokens left after the main sale. Lots of them have a special token holder account, sometimes it’s the owner account. So the best solution for burning unsold tokens is the following:

大多数智能合约都包含一个用于燃烧主销售后剩余的代币的功能。 他们中的许多人都有一个特殊的令牌持有者帐户,有时是所有者帐户。 因此,刻录未售出令牌的最佳解决方案如下:

  • get amount of tokens on holder’s address

    在持有人的地址上获取代币数量
  • then subtract this amount from total supply

    然后从总供应量中减去此金额
  • and set amount of tokens on holder’s address to zero.

    并将持有人地址上的令牌数量设置为零。

This will ensure that you don’t burn all the tokens, so it’s important to lay out your token burn strategy in your white paper.

这将确保您不会刻录所有令牌,因此在白皮书中列出令牌刻录策略很重要。

结论 (Conclusion)

It’s very important to test your smart contract before deploying it on the main network in order to prevent issues in the future. When you have written unit tests, they will guarantee that there won’t be any discrepancy between your white paper and smart contract, and your smart contract will not be hacked by calling functions which should have the correct rights but don’t.

为了避免将来出现问题,在将智能合约部署到主网络之前对其进行测试非常重要。 当您编写了单元测试时,它们将保证您的白皮书和智能合约之间不会有任何差异,并且不会通过调用具有正确权限但具有此功能的函数来破坏您的智能合约。

It’s not about just Smart Contracts: you need Unit tests for all your apps and code because it shows you all the ways your app could have failed.

这不仅仅是智能合约:您需要对所有应用程序和代码进行单元测试,因为它向您显示了应用程序可能失败的所有方式。

翻译自: https://www.freecodecamp.org/news/how-to-write-unit-tests-for-erc-20-ethereum-smart-contracts-abfa2c482aea/

以太坊 erc20

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值