owned this note
owned this note
Published
Linked with GitHub
---
id: truffle
title: Using Truffle with the EVM
sidebar_label: Truffle
---
[Truffle](https://trufflesuite.com/truffle) 是用于基于EVM的智能合约开发和测试的流行工具。典型的使用Truffle开发的dApp在项目根目录中将有一个名为“ truffle-config.js”的文件,用于指定有关连接,编译,网络等的详细信息。
NEAR有一个名为[`near-web3-provider`](https://github.com/near/near-web3-provider)的自定义提供程序,可以在Truffle配置中指定。它以[NPM软件包](https://npmjs.com/package/near-web3-provider)的形式发布,可以添加以下内容:
npm install near-web3-provider --save-dev
**注意**: 您也可以在前端使用'near-web3-provider'作为提供者,在这种情况下,正确的标志是' --save'而不是'--save-dev'。
## NEAR的`truffle-config.js`示例
```js
const { NearProvider } = require('near-web3-provider');
module.exports = {
networks: {
near_local: {
network_id: "*",
skipDryRun: true,
provider: () => new NearProvider({
networkId: 'local',
masterAccountId: 'test.near',
}),
},
near_betanet: {
network_id: "*",
skipDryRun: true,
provider: () => new NearProvider({
networkId: 'betanet',
masterAccountId: process.env.NEAR_MASTER_ACCOUNT
}),
},
near_testnet: {
network_id: "*",
skipDryRun: true,
provider: () => new NearProvider({
networkId: 'testnet',
masterAccountId: process.env.NEAR_MASTER_ACCOUNT
}),
},
near_mainnet: {
network_id: 1313161554, // See https://chainid.network
skipDryRun: true,
provider: () => new NearProvider({
networkId: 'mainnet',
masterAccountId: process.env.NEAR_MASTER_ACCOUNT
}),
},
develop: {
host: "127.0.0.1",
network_id: "*",
port: 8545
}
}
};
```
要在项目中查看此示例,请参阅 [NEAR Pet Shop example](https://github.com/near-examples/near-pet-shop/blob/master/truffle-config.js).
## Testing
目前,测试是使用网络上的现有帐户运行的。例如,如果用户要在betanet上进行测试,则他们将首先在以下位置创建一个betanet帐户:
https://wallet.betanet.near.org
然后使用[NEAR CLI](/docs/tools/near-cli)登录到该帐户,该帐户将在其主目录中创建完全访问权限的密钥文件。 (特别是`〜/ .near-credentials`)现在,诸如NEAR CLI和[`near-api-js`](https://github.com/near/near-api-js) 之类的工具和库都可以利用这些密钥来执行(包括部署合同,转移Ⓝ,创建子帐户等)的操作。(请参阅所有可用的[操作](https://nomicon.io/RuntimeSpec/Actions.html))为测试而创建的帐户是子帐户(例如从`yourname.betanet`创建的`subaccount.yourname.betanet`)
将帐户存储在本地文件中后,我们将首先创建必要的测试帐户,然后运行测试。
创建测试账号(以下示例在betanet上):
NEAR_ENV=betanet near evm-dev-init yourname.betanet
这些将会创建(默认为5个) 位于 `~/.near-credentials/betanet`位置下的测试账号.
**注意**:在撰写本文时,创建的测试帐户的数量将足够多,如果少于5个,则总共会创建5个帐号。
然后运行测试:
env NEAR_MASTER_ACCOUNT=yourname.betanet npx truffle test --network near_betanet
## 构建和部署
Truffle的 [`migrate`](https://www.trufflesuite.com/docs/truffle/getting-started/running-migrations#command)指令会构建合约并且把它们部署到指定的网络上。迁移文件一般会保存在项目根目录的 `migrations` 文件夹中。在项目根目录中,如果需要构建和部署项目到betanet上,这个指令应该为:
env NEAR_MASTER_ACCOUNT=yourname.betanet npx truffle migrate --network near_betanet
这将输出包括如下表的日志:
```shell script
Deploying 'Adoption'
--------------------
> transaction hash: FHo73dk5n1LujaGwHchrXMHzRBHz88aCNWex1kiLrC57:mike.betanet
> Blocks: 0 Seconds: 0
> contract address: 0xAdf11a39283CEB00DEB90a5cE9220F89c6C27E67
> block number: 3689031
> block timestamp: 1607882742969
> account: 0xb948c53cBA274D77e54109061068512e92d1249d
> balance: 0
> gas used: 2293234665220 (0x215ef700704)
> gas price: 0.1 gwei
> value sent: 0 ETH
> total cost: 229.323466522 ETH
```
需要注意以下三点:
1. **合约地址** 已经部署合约的以太坊地址.
2. **总花费** 在当前不是准确的.
3. **账号** 是一个基于NEAR账户的以太坊地址。在这种情况下,这是`near-web3-provider`的实用函数`nearAccountToEvmAddress()`的结果。
使用 [REPL of NodeJS](https://nodejs.dev/learn/how-to-use-the-nodejs-repl), 我们可以看到如下:
```shell script
mike@near ~/near-web3-provider/src $ node
Welcome to Node.js v12.18.4.
Type ".help" for more information.
> const utils = require('./utils');
> utils.nearAccountToEvmAddress('mike.betanet');
'0xb948c53cba274d77e54109061068512e92d1249d'
```
## 故障排除
如果NEAR CLI命令给出如下消息:
`Found account <yourname>.<network> is not available on the network`
Please check that all the files located in `~/.near-credentials/<network>` are associated with working accounts. 请检查位于`~/.near-credentials/<network>` 中的所有文件是否与工作帐户相关联。
要检查网络上是否存在帐户,请在NEAR CLI上使用`state`命令。
near state mightexist.betanet
如果不存在(不再存在),则将输出:
> **yourname.network** is not found in **network**
可以安全删除错误或过时的密钥文件。