BytesIO
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    1
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    --- 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** 可以安全删除错误或过时的密钥文件。

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully