{"_id":"@bitfinance-solutions/contract","_rev":"1-a6742020de5272e83b7f455d67bcacc0","name":"@bitfinance-solutions/contract","dist-tags":{"latest":"0.1.56"},"versions":{"0.1.56":{"name":"@bitfinance-solutions/contract","version":"0.1.56","description":"contract libraries for BitFinance","main":"dist/index.js","node":"dist/index.js","browser":"dist/index.js","module":"dist/index.esm.js","jsnext:main":"dist/index.esm.js","typings":"dist/index.d.ts","types":"dist/index.d.ts","scripts":{"test":"echo \"Error: no test specified\" && exit 1"},"author":{"name":"info@bitfinance.solutions"},"publishConfig":{"access":"public"},"license":"MIT","dependencies":{"@bitfinance-js/account":"0.1.56","@bitfinance-js/crypto":"0.1.56","@bitfinance-js/network":"0.1.56","@bitfinance-js/transaction":"0.1.56","@bitfinance-js/utils":"0.1.56"},"directories":{"test":"test"},"repository":{"type":"git","url":"git+https://github.com/bitfinance-ecosystem/npm-packages.git"},"keywords":["bitfinance","sdk","contract"],"bugs":{"url":"https://github.com/bitfinance-ecosystem/npm-packages/issues"},"homepage":"https://github.com/bitfinance-ecosystem/npm-packages#readme","_id":"@bitfinance-solutions/contract@0.1.56","_nodeVersion":"14.8.0","_npmVersion":"6.14.11","dist":{"integrity":"sha512-jqA0tt+5IRRuJ803a34avjAXCgdJuThF8SZtUXoof9epxWBYXlFpwBVjLb7YtlCBh1ihD2BCxGloP604eBjOVA==","shasum":"47aadedd0be76cbc51a36faaec048aece2ea3ede","tarball":"https://registry.npmjs.org/@bitfinance-solutions/contract/-/contract-0.1.56.tgz","fileCount":28,"unpackedSize":15590769,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJgCa0ICRA9TVsSAnZWagAAw/IP/3THYQSQFWkHc1x1duDO\n6ebEuOaFsbf94vFO1GXJ9ttb5iLZkyrMR02JGlOYyg83zEglKkZ30uQHkoZ2\nqFrabY+m3urGhYR95CP7eL4rWqbSDQZbcsuS0D451XOKFOtObuNVcsAYoe7y\nCCQduQbVdVHBf9NhFZed3gqxldmLjLSC3atiLXxFtqzZMiOy+syJObgMGjnL\nc9+j6SjhPj5YUXb8dsI+QBcK+jWCE1qqnrFHQ7WU7+DEOKTDqBIxqKHYmvST\njAueCuQjBKRWsjeAk/n1keH5fKDTNbf7SP2KvZiBvyyHpOlNuby3JxeJSNAO\nntkP4ZdBvtw7VD/j2adbXndGYxx604qaqNto+VqPqA9h/Xqpg5vkItsSvOiI\nApZp6kXyj5jqt2iCRMsbM8AReBHNR6ZL+2Bs4BHbLGasmVIhT4ujqcBd9oUF\n0eq9U0bX/8BK7jS2MlOcZmSFhCo+t9QkJKvqDIsNi22NuwaVqeuZvzf4MVjN\nFngCagZwj+0FxiP0KijaWQjBhq18HQYmXJmOnTgWSUsKPvNVVFQ3AssAb8Is\nPIzGZDpa6XzuxHSvOSB9GbQOS0Ui007YLu2PFIlCR6H4rvNNYwZsJSTI3WYU\nL8GLzOT3tC42mQ2nmjpryw98vUSQnKw/OXmboOIQrdCc14h5kEYPQU3W+4qy\noQDH\r\n=Fg/8\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIGen5D+kRdqNu4gw50AP+FGGKlYLO0xnxs+ZWrmV1xmwAiBWdzc1U23YTvwx8FGyNnZI0csJ7k8aMu505ZZ7mc7cnw=="}]},"_npmUser":{"name":"bitfinance-solutions","email":"info@bitfinance.solutions"},"maintainers":[{"name":"bitfinance-solutions","email":"info@bitfinance.solutions"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/contract_0.1.56_1611246856143_0.08203233801142673"},"_hasShrinkwrap":false}},"time":{"created":"2021-01-21T16:34:16.111Z","0.1.56":"2021-01-21T16:34:16.348Z","modified":"2022-04-04T18:59:57.970Z"},"maintainers":[{"name":"bitfinance-solutions","email":"info@bitfinance.solutions"}],"description":"contract libraries for BitFinance","homepage":"https://github.com/bitfinance-ecosystem/npm-packages#readme","keywords":["bitfinance","sdk","contract"],"repository":{"type":"git","url":"git+https://github.com/bitfinance-ecosystem/npm-packages.git"},"author":{"name":"info@bitfinance.solutions"},"bugs":{"url":"https://github.com/bitfinance-ecosystem/npm-packages/issues"},"license":"MIT","readme":"# @bitfinance-js/contract\n\nThis package provides a collection of apis to create, deploy, and interact with smart contracts. In BitFinance, smart contracts all fully EVM compatible and the formats and terminologies match 1-to-1 with EVM smart contracts.\n\n## Installation\n\n```\nnpm install @bitfinance-js/contract\n```\n\n## Usage\n\nDeploying a contract using `contractConstructor`\n```javascript\nconst { ContractFactory } = require('@bitfinance-js/contract');\nconst { Wallet } = require('@bitfinance-js/account');\nconst { Messenger, HttpProvider } = require('@bitfinance-js/network');\nconst { ChainID, ChainType, hexToNumber } = require('@bitfinance-js/utils');\n\nconst wallet = new Wallet(\n  new Messenger(\n    new HttpProvider('https://api.s0.b.bitfinance.solutions'),\n    ChainType.BitFinance,\n    ChainID.BfnTestnet,\n  ),\n);\nconst factory = new ContractFactory(wallet);\n\nconst contractJson = require(\"./Counter.json\");\nconst contract = factory.createContract(contractJson.abi);\n\nconst options1 = { gasPrice: '0x3B9ACA00' }; // gas price in hex corresponds to 1 Gwei or 1000000000\nlet options2 = { gasPrice: 1000000000, gasLimit: 21000 }; // setting the default gas limit, but changing later based on estimate gas\n\nconst options3 = { data: contractJson.bytecode }; // contractConstructor needs contract bytecode to deploy\n\ncontract.wallet.addByPrivateKey('1f054c21a0f57ebc402c00e14bd1707ddf45542d4ed9989933dbefc4ea96ca68');\n\ncontract.methods.contractConstructor(options3).estimateGas(options1).then(gas => {\n  options2 = {...options2, gasLimit: hexToNumber(gas)};\n  contract.methods.contractConstructor(options3).send(options2).then(response => {\n    console.log('contract deployed at ' + response.transaction.receipt.contractAddress);\n  });\n});\n```\nInstead of `contract.methods.contractConstructor`, `contract.deploy` could be used and it will work.\n\nLoading a contract object using the contract json and contract address for interacting with it\n```javascript\nconst { BitFinance } = require(\"@bitfinance-js/core\");\nconst { ChainID, ChainType } = require(\"@bitfinance-js/utils\");\nconst hmy = new BitFinance(\"https://api.s0.b.bitfinance.solutions\", {\n  chainType: ChainType.BitFinance,\n  chainId: ChainID.BfnTestnet,\n});\n\nconst contractJson = require(\"./Counter.json\");\nconst contractAddr = \"0x19f64050e6b2d376e52AC426E366c49EEb0724B1\";\n\nconst contract = hmy.contracts.createContract(contractJson.abi, contractAddr);\nconsole.log(contract.methods);\n```\n\nDirectly loading contract using `ContractFactory`\n```javascript\nconst { ContractFactory } = require('@bitfinance-js/contract');\nconst { Wallet } = require('@bitfinance-js/account');\nconst { Messenger, HttpProvider } = require('@bitfinance-js/network');\nconst { ChainID, ChainType, hexToNumber } = require('@bitfinance-js/utils');\n\nconst wallet = new Wallet(new Messenger(\n  new HttpProvider('https://api.s0.b.bitfinance.solutions'),\n  ChainType.BitFinance,\n  ChainID.BfnTestnet,\n));\nconst factory = new ContractFactory(wallet);\nconst contract = factory.createContract(contractJson.abi, contractAddr);\n```\n\nEstimate gas for contract methods\n```javascript\nconst options1 = { gasPrice: '0x3B9ACA00' }; // gas price in hex corresponds to 1 Gwei or 1000000000\n\ncontract.methods.getCount().estimateGas(options1).then(gas => {\n  console.log('gas required for getCount is ' + hexToNumber(gas));\n});\n```\n\nCall contract read-only methods. BitFinance uses 1 Gwei gas price and gas limit of 21000 by default. Use the estimate gas api to correctly set the gas limit.\n```javascript\nconst options1 = { gasPrice: '0x3B9ACA00' }; // gas price in hex corresponds to 1 Gwei or 1000000000\nlet options2 = { gasPrice: 1000000000, gasLimit: 21000 }; // setting the default gas limit, but changing later based on estimate gas\n\ncontract.methods.getCount().estimateGas(options1).then(gas => {\n  options2 = {...options2, gasLimit: hexToNumber(gas)};\n  contract.methods.getCount().call(options2).then(count => {\n    console.log('counter value: ' + count);\n  });\n});\n```\n\nInvoking contract modification methods using `send` api. Need to add a signing account to the contract wallet, otherwise `send` api will not work.\n```javascript\nconst options1 = { gasPrice: '0x3B9ACA00' }; // gas price in hex corresponds to 1 Gwei or 1000000000\nlet options2 = { gasPrice: 1000000000, gasLimit: 21000 }; // setting the default gas limit, but changing later based on estimate gas\n\ncontract.wallet.addByPrivateKey('1f054c21a0f57ebc402c00e14bd1707ddf45542d4ed9989933dbefc4ea96ca68');\n\ncontract.methods.incrementCounter().estimateGas(options1).then(gas => {\n  options2 = {...options2, gasLimit: hexToNumber(gas)};\n  contract.methods.incrementCounter().send(options2).then(response => {\n    console.log(response.transaction.receipt);\n  });\n});\n```\n\nAll the above apis can also be asynchronously executed using `async` and `await`.\n\nSubscribing to the contract events requires web socket based messenger.\n```javascript\nconst { ContractFactory } = require('@bitfinance-js/contract');\nconst { Wallet } = require('@bitfinance-js/account');\nconst { Messenger, WSProvider } = require('@bitfinance-js/network');\nconst { ChainID, ChainType, hexToNumber } = require('@bitfinance-js/utils');\nconst ws = new WSProvider('wss://ws.s0.b.bitfinance.solutions');\n\nconst wallet = new Wallet(\n  new Messenger(\n    ws,\n    ChainType.BitFinance,\n    ChainID.BfnTestnet,\n  ),\n);\nconst factory = new ContractFactory(wallet);\n\nconst contractJson = require(\"./Counter.json\");\nconst contractAddr = '0x8ada52172abda19b9838eb00498a40952be6a019';\n\nconst contract = factory.createContract(contractJson.abi, contractAddr);\n\ncontract.events\n  .IncrementedBy()\n  .on('data', (event) => {\n    console.log(event);\n  })\n  .on('error', console.error);\n```","readmeFilename":"README.md"}