{"_id":"@dust-defi/evm-provider","name":"@dust-defi/evm-provider","dist-tags":{"latest":"1.0.7"},"versions":{"1.0.7":{"name":"@dust-defi/evm-provider","version":"1.0.7","main":"build/index.js","types":"build/index.d.ts","repository":{"type":"git","url":"git+https://github.com/dustprotocol/evm-provider.js.git"},"author":{"name":"Reef Developers"},"license":"Apache-2.0","scripts":{"build":"polkadot-dev-build-ts","build:release":"polkadot-ci-ghact-build","lint":"polkadot-dev-run-lint","clean":"polkadot-dev-clean-build","postinstall":"polkadot-dev-yarn-only","test":"polkadot-dev-run-test --coverage --runInBand --testPathIgnorePatterns e2e","test:one":"polkadot-dev-run-test","test:watch":"polkadot-dev-run-test --watch"},"resolutions":{"@ethersproject/abstract-signer":"~5.5.0","@ethersproject/abstract-provider":"~5.5.0"},"dependencies":{"@dust-defi/api":"1.0.4","@dust-defi/api-derive":"1.0.4","@dust-defi/type-definitions":"1.0.4","@dust-defi/types":"1.0.4","@open-web3/dev-config":"0.1.13","@open-web3/scanner":"1.0.1","@polkadot/api":"~6.4.2","@polkadot/keyring":"~7.9.2","@polkadot/rpc-provider":"~6.4.2","@polkadot/types":"~6.4.2","@polkadot/util":"~7.9.2","@polkadot/util-crypto":"~7.9.2","@typescript-eslint/eslint-plugin":"4.8.1","blakejs":"~1.1.0","ethers":"~5.5.0"},"devDependencies":{"@babel/core":"^7.12.9","@babel/register":"^7.12.1","@babel/runtime":"^7.12.5","@types/chai":"^4.2.14","@types/jest":"^27.0.1","@types/validator":"^13.1.0","ts-node":"~10.2.0","typescript":"~4.4.3"},"packageManager":"yarn@3.0.1","stableVersion":"1.0.3","gitHead":"cb3520987f2e67e59d31d038b871e5ad268a294a","description":"`evm-provider.js` implements a web3 provider which can interact with the [Dust chain EVM](https://github.com/dust-defi/dust-chain).","bugs":{"url":"https://github.com/dustprotocol/evm-provider.js/issues"},"homepage":"https://github.com/dustprotocol/evm-provider.js#readme","_id":"@dust-defi/evm-provider@1.0.7","_nodeVersion":"16.15.0","_npmVersion":"8.5.5","dist":{"integrity":"sha512-h0oVUwUBB+2Mmwpb/Jtgy2Q15BtVVkpjGif04WWD7BXjhM7EHC/qItKjYf4QjEOec28kDt+/5B/C8RIlLNxn8A==","shasum":"895bef3b8abddbe52c6979a72db46cdb4a869a3f","tarball":"https://registry.npmjs.org/@dust-defi/evm-provider/-/evm-provider-1.0.7.tgz","fileCount":57,"unpackedSize":3910332,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIDH8o+cqG0Mulg28grueLE3nTCxeTFowFzlnkss5cNKqAiEA3r5XkQ+YAMcNQyFbXYu4I7p/OYX+cgYstUh2HRER0kQ="}],"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJikdRiACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2Vmo59g/8Dp8ceCi7HYVgvDQdO33hnwjLWPREFlnxNzFeWAPQvf2TPpyy\r\nXngIY9OYz2Syofhhta3uCd3kfnH57D8z5cM6YsH/4or91f4zSRg5Bgk2/Uf3\r\nf6jwg31Rj65rGVwaCIklCgcdmJTLfekaFFO0IZLjB1uZVQRy8Q/aSxM4hk45\r\n1I+iDfWfjrXko/40kT6ei1jCY5xmB4lSOFUElGgYw1oKBELWqeJ/LLw9t29b\r\nftDCemHegFOIneifyHFulzL2CPsGv252133GP46nWFkqwXWMQa/tDqLPbpNx\r\nQ0EBbhM1e13ASNXqzeFKqSYntQecJj4WyJadQzcyqYac7lXqIwDjUJG7CdwB\r\nqF4Y4HuZtLxBRygQjnrGa+lllbQmr7/3TZZUAbo0R+Mh4jkwQ9NEzciopXqD\r\nQeo9bI72qF/X4n7hUEaMJWABVdnyue2SIQMj7griagGgWqv6E0fnMks5sArp\r\njhTpjDQciQrLivD4ZAkTxe361uMsORDbzpNA3JeLfj5C8J+jFh7ng2iR0hSo\r\nChblaGLye+cbRuElt6rg12pXHZJOrHUWQqPioHLQ5hNSulvCv+BTsxvwZuSj\r\n3XgU/cikJ1G18/zjrgF/l8I5qs1k7g/zy6zdwsqVHiVVZr0KoVSHgeynKUNh\r\nP25tn6ad/V/l1kQW7pZa38ohhyCeSk32Ie4=\r\n=9XOH\r\n-----END PGP SIGNATURE-----\r\n"},"_npmUser":{"name":"marlonhanks","email":"support@kedia.co"},"directories":{},"maintainers":[{"name":"marlonhanks","email":"support@kedia.co"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/evm-provider_1.0.7_1653724258306_0.8996217805443549"},"_hasShrinkwrap":false}},"time":{"created":"2022-05-28T07:50:58.257Z","1.0.7":"2022-05-28T07:50:58.523Z","modified":"2022-05-28T07:50:58.646Z"},"maintainers":[{"name":"marlonhanks","email":"support@kedia.co"}],"description":"`evm-provider.js` implements a web3 provider which can interact with the [Dust chain EVM](https://github.com/dust-defi/dust-chain).","homepage":"https://github.com/dustprotocol/evm-provider.js#readme","repository":{"type":"git","url":"git+https://github.com/dustprotocol/evm-provider.js.git"},"author":{"name":"Reef Developers"},"bugs":{"url":"https://github.com/dustprotocol/evm-provider.js/issues"},"license":"Apache-2.0","readme":"# @dust-defi/evm-provider.js\n\n`evm-provider.js` implements a web3 provider which can interact with the [Dust chain EVM](https://github.com/dust-defi/dust-chain).\n\nIf you only care about developing Solidity contracts on the Dust chain, `@dust-defi/evm-provider.js` is used in our [Hardhat Dust environment](https://github.com/dustprotocol/hardhat-dust). The environment simplifies and abstracts all the low-level intricacies, so you can only focus on the Solidity part. See [hardhat-dust-examples repo](https://github.com/dustprotocol/hardhat-dust-examples/blob/master/scripts/flipper/deploy.js) for more examples.\n\nIf you need more control, then it can also be used as a Substrate provider to query or to interact with the Dust chain using the same calls as in the [Polkadot.js](https://polkadot.js.org/docs/api).\n\n## Installation\n\nInstall dependencies with `yarn` [see issue](https://github.com/dustprotocol/evm-provider.js/issues/5#issuecomment-912389541).\n\n### Yarn\n\n```bash\nyarn add @dust-defi/evm-provider\n```\n\n## Getting started\n\nTo create a `Provider` instance, use the following code:\n\n```javascript\nimport {\n  TestAccountSigningKey,\n  Provider,\n  Signer,\n} from \"@dust-defi/evm-provider\";\nimport { WsProvider, Keyring } from \"@polkadot/api\";\nimport { createTestPairs } from \"@polkadot/keyring/testingPairs\";\nimport { KeyringPair } from \"@polkadot/keyring/types\";\n\nconst WS_URL = process.env.WS_URL || \"ws://127.0.0.1:9944\";\nconst seed = process.env.SEED;\n\nconst setup = async () => {\n  const provider = new Provider({\n    provider: new WsProvider(WS_URL),\n  });\n\n  await provider.api.isReady;\n\n  let pair: KeyringPair;\n  if (seed) {\n    const keyring = new Keyring({ type: \"sr25519\" });\n    pair = keyring.addFromUri(seed);\n  } else {\n    const testPairs = createTestPairs();\n    pair = testPairs.alice;\n  }\n\n  const signingKey = new TestAccountSigningKey(provider.api.registry);\n  signingKey.addKeyringPair(pair);\n\n  const signer = new Signer(provider, pair.address, signingKey);\n\n  // Claim default account\n  if (!(await signer.isClaimed())) {\n    console.log(\n      \"No claimed EVM account found -> claimed default EVM account: \",\n      await signer.getAddress()\n    );\n    await signer.claimDefaultAccount();\n  }\n\n  return {\n    signer,\n    provider,\n  };\n};\n\nexport default setup;\n```\n\nwith this object you can interact with the Substrate chain.\n\nIf you want to interact with injected sources (e.g. from Polkadot{.js}) you can do the following:\n\n```javascript\nimport { Provider, Signer, } from \"@dust-defi/evm-provider\";\nimport { WsProvider } from \"@polkadot/api\";\nimport { web3Accounts, web3Enable } from \"@polkadot/extension-dapp\";\n\nconst WS_URL = process.env.WS_URL || \"ws://127.0.0.1:9944\";\nconst seed = process.env.SEED;\n\nconst setup = async () => {\n  \n  // Return an array of all the injected sources\n  // (this needs to be called first)\n  const allInjected = await web3Enable('your dapp');\n\n  const injected;\n  if (allInjected[0] && allInjected[0].signer) {\n    injected = allInjected[0].signer;\n  }\n\n  // Return an array of { address, meta: { name, source } }\n  // (meta.source contains the name of the extension)\n  const allAccounts = await web3Accounts();\n\n  let account;\n  if (allAccounts[0] && allAccounts[0].address) {\n    account = allAccounts[0].address;\n  }\n\n  const provider = new Provider({\n    provider: new WsProvider(WS_URL)\n  });\n\n  await provider.api.isReady;\n\n  const signer = new Signer(provider, account, injected);\n\n  // Claim default account\n  if (!(await signer.isClaimed())) {\n    console.log(\n      \"No claimed EVM account found -> claimed default EVM account: \",\n      await signer.getAddress()\n    );\n    await signer.claimDefaultAccount();\n  }\n\n  return {\n    signer,\n    provider,\n  };\n};\n\nexport default setup;\n```\n\n## EVM interaction\n\nMost, but not all, of `evm-provider.js` API is compatible with `ethers.js`. If you are not familiar with `ethers.js`, you can start by looking at its [documentation](https://docs.ethers.io/v5/single-page/). See our [Dustswap example](https://github.com/dust-defi/dustswap/blob/653e6f4e77d228bba32fe233bff4a4811eae335e/src/deploy.ts) on how it uses the above `setup` script to deploy and interact with the EVM.\n\n### Get EVM address\n\n```javascript\n// ethers\nlet accounts = await this.provider.listAccounts();\nlet selectedAccount = accounts[0];\n\n// evm-provider\nlet selectedAccount = await this.signer.queryEvmAddress();\n```\n\n### Claim EVM address\n\nIf you would like to inject an evm address that you already own you can do so via the `claimAccount` extrinsic.  The script below illustrates how this can be done.\n\n```javascript\nimport { Keyring, WsProvider } from '@polkadot/api';\nimport { ethers } from 'ethers';\nimport { createClaimEvmSignature } from './utils';\nimport { Provider } from '.';\n\nconst WS_URL = process.env.WS_URL || 'ws://127.0.0.1:9944';\n// dust address - 5H728gLgx4yuCSVEwGCAfLo3RtzTau9F6cTNqNJtrqqjACWq\nconst dustPrivKeyRaw = process.env.DUST_PRIV_KEY || \"0x0000000000000000000000000000000000000000000000000000000000000000\";\nconst ethPrivKey = process.env.ETH_PRIV_KEY || \"0x81376b9868b292a46a1c486d344e427a3088657fda629b5f4a647822d329cd6a\";\n\nconst main = async (): Promise<void> =>  {\n    const provider = new Provider({\n        provider: new WsProvider(WS_URL)\n    });\n    await provider.api.isReady;\n\n    const keyring = new Keyring({ type: 'sr25519' });\n    const dustKey = keyring.addFromUri(dustPrivKeyRaw);\n    const ethKey = new ethers.Wallet(ethPrivKey);\n\n    const msg = createClaimEvmSignature(dustKey.address);\n    let signature = await ethKey.signMessage(msg);\n\n    await provider.api.tx.evmAccounts.claimAccount(\n        ethKey.address,\n        signature\n    ).signAndSend(dustKey);\n\n    process.exit();\n};\n\nmain();\n```\n\n### Provider\n\nThe Provider provides an API for interacting with nodes and is an instance of `ethers.js` [AbstractProvider](https://docs.ethers.io/v5/single-page/#/v5/api/providers/-%23-providers).\n\n### Signer\n\nThe Signer class can sign transactions and messages using a private key. When using the wallet for the first time, make sure to always claim the EVM account for the wallet you are using:\n\n```javascript\nsigner.claimDefaultAccount();\n```\n\nbefore performing any EVM calls otherwise it may lead to `InsufficientBalance` errors.\n\n## Gas limit and storage limit\nIn addition to the gas limit (processing), the Dust chain also charges a [storage fee](https://docs.substrate.io/v3/runtime/smart-contracts/#storage-deposit). When you interact with the EVM, Dust chain will estimate both fees and as such the fees will be invisible to the user. This should work in 99% of cases. It assumes you have at least 60 DUST tokens on the signing account. However, sometimes the heuristics (usually for more complex contracts) are wrong. In this case you can force the values of `gasLimit` and `storageLimit` by adding them to the `options` dictionary at the end of every call, for example:\n\n```\nawait factory.deploy(<contract_args>, {\n  gasLimit: 1000000,\n  customData: { storageLimit: 1000000 }\n});\n```\n\nIf you require maximum flexibility `evm-provider` exports maximum gas and storage limit:\n\n```\nimport { MAX_GAS_LIMIT, MAX_STORAGE_LIMIT } from \"@dust-defi/evm-provider\";\n```\nwhich default to `U64MAX` and `U32MAX` respectively.\n\n## Versions\n- versions 1.\\*.\\* work from Dust v8 chain onwards\n  - no longer requires `resolutions` with `ethers@5.0.9`\n- versions 0.\\*.\\* work from Dust v0 to v7\n\n#### [Changelog](./CHANGELOG.md)\n","readmeFilename":"README.md"}