diff --git a/CHANGELOG.md b/CHANGELOG.md index 513d6334..76d2de40 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,4 @@ -# [3.17.0](https://github.com/dashevo/DashJS/compare/v3.16.1...v3.17.0) (2020-12-30) +# [3.17.0](https://github.com/dashevo/DashJS/compare/v3.16.2...v3.17.0) (2020-12-30) ### Features @@ -20,6 +20,15 @@ +## [3.16.2](https://github.com/dashevo/DashJS/compare/v3.16.1...v3.16.2) (2020-11-17) + + +### Bug Fixes + +* cannot read property 'getBinaryProperties' of undefined ([#158](https://github.com/dashevo/DashJS/issues/158)) + + + ## [3.16.1](https://github.com/dashevo/DashJS/compare/v3.16.0...v3.16.1) (2020-10-30) diff --git a/docs/README.md b/docs/README.md index 04c8b5f4..b98be296 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,23 +1,23 @@ -## Dash SDK +# Dash SDK -[![Package Version](https://img.shields.io/github/package-json/v/dashevo/dashjs.svg?&style=flat-square)](https://www.npmjs.org/package/dash) -[![Build Status](https://img.shields.io/travis/com/dashevo/dashjs.svg?branch=master&style=flat-square)](https://travis-ci.com/dashevo/dashjs) +[![NPM Version](https://img.shields.io/npm/v/dash)](https://www.npmjs.org/package/dash) +[![Build Status](https://img.shields.io/travis/com/dashevo/js-dash-sdk)](https://travis-ci.com/dashevo/js-dash-sdk) +[![Release Date](https://img.shields.io/github/release-date/dashevo/js-dash-sdk)](https://github.com/dashevo/js-dash-sdk/releases/latest) +[![standard-readme compliant](https://img.shields.io/badge/readme%20style-standard-brightgreen)](https://github.com/RichardLitt/standard-readme) -> Client-side library for wallet payment/signing and application development with Dash. (Wallet, DAPI, Primitives, BLS, ...) +Dash library for JavaScript/TypeScript ecosystem (Wallet, DAPI, Primitives, BLS, ...) ---- - -Dash SDK is intended to provide, in a single entry-point all the different features, classes & utils you might need to interface with the Dash network. +Dash library allows you to connect to DAPI and receive or broadcast payments on the Dash Network, manage identifies, register data contracts, retrieve or submit documents on the Dash Platform, all within a single library. ## Install -## Browser +### Browser ```html ``` -## Node +### Node In order to use this library, you will need to add our [NPM package](https://www.npmjs.com/dash) to your project. @@ -27,57 +27,56 @@ Having [NodeJS](https://nodejs.org/) installed, just type : npm install dash ``` -### Usage +## Usage ```js -const Dash = require("dash"); +const Dash = require('dash'); const client = new Dash.Client({ - network: "testnet", + network: 'evonet', wallet: { - mnemonic: "arena light cheap control apple buffalo indicate rare motor valid accident isolate", + mnemonic: 'arena light cheap control apple buffalo indicate rare motor valid accident isolate', }, }); -// Accessing an account allow you to transact with the Dash Network +// Accessing an account allows you to transact with the Dash Network client.getWalletAccount().then(async (account) => { - console.log("Funding address", account.getUnusedAddress().address); + console.log('Funding address', account.getUnusedAddress().address); const balance = account.getConfirmedBalance(); - console.log("Confirmed Balance", balance); + console.log('Confirmed Balance', balance); - if(balance > 0){ + if (balance > 0) { // Creating an identity is the basis of all interactions with the Dash Platform - const identity = await client.platform.identities.register() - + const identity = await client.platform.identities.register(); + // Prepare a new document containing a simple hello world sent to a hypothetical tutorial contract - const document = await platform.documents.create( + const document = await client.platform.documents.create( 'tutorialContract.note', identity, { message: 'Hello World' }, ); // Broadcast the document into a new state transition - await platform.documents.broadcast({create:[document]}, identity); + await client.platform.documents.broadcast({ create: [document] }, identity); } }); ``` - ### Use-cases examples -- [Generate a mnemonic](/examples/generate-a-new-mnemonic.md) -- [Receive money and display balance](/examples/receive-money-and-check-balance.md) -- [Pay to another address](/examples/pay-to-another-address.md) -- [Use a local evonet](/examples/use-local-evonet.md) -- [Publishing a new contract](/examples/publishing-a-new-contract.md) -- [Use another BIP44 account](/examples/use-different-account.md) - + +- [Generate a mnemonic](/examples/generate-a-new-mnemonic.md) +- [Receive money and display balance](/examples/receive-money-and-check-balance.md) +- [Pay to another address](/examples/pay-to-another-address.md) +- [Use a local evonet](/examples/use-local-evonet.md) +- [Publishing a new contract](/examples/publishing-a-new-contract.md) +- [Use another BIP44 account](/examples/use-different-account.md) + ### Tutorial + - [Register an identity](https://dashplatform.readme.io/docs/tutorial-register-an-identity) - [Register a Name for an Identity](https://dashplatform.readme.io/docs/tutorial-register-a-name-for-an-identity) - ## Licence [MIT](https://github.com/dashevo/dashjs/blob/master/LICENCE.md) © Dash Core Group, Inc. - diff --git a/docs/getting-started/about-schemas.md b/docs/getting-started/about-schemas.md index 05d5f3a6..ab39602f 100644 --- a/docs/getting-started/about-schemas.md +++ b/docs/getting-started/about-schemas.md @@ -1,5 +1,5 @@ ## About Schemas -Schemas represents the application data structure, a JSON Schema language based set of rules that is allow the creation of a Data Contract. +Schemas represents the application data structure, a JSON Schema language based set of rules that allows the creation of a Data Contract. -You can read more on the [Dash Platform Documentation - Data contract section](https://dashplatform.readme.io/docs/explanation-platform-protocol-data-contract). +You can read more in the [Dash Platform Documentation - Data contract section](https://dashplatform.readme.io/docs/explanation-platform-protocol-data-contract). diff --git a/docs/getting-started/core-concepts.md b/docs/getting-started/core-concepts.md index 7f07fd8e..d6bb5e80 100644 --- a/docs/getting-started/core-concepts.md +++ b/docs/getting-started/core-concepts.md @@ -1,31 +1,28 @@ ## Core concepts -The [Dash Core Developer Guide](https://dashcore.readme.io/docs/core-guide-introduction) will answer most of the questions about the fundamentals of Dash. - -However, some elements provided by the SDK need to be grasped, so we will quickly cover some of those. +The [Dash Core Developer Guide](https://dashcore.readme.io/docs/core-guide-introduction) will answer most of questions about the fundamentals of Dash. However, some elements provided by the SDK need to be grasped, so we will quickly cover some of those. ## Wallet -At the core of Dash is the Payment Chain, in order to be able to transact on it, one needs to have a set of [UTXO](https://dashcore.readme.io/docs/core-guide-block-chain-transaction-data) that is controlled by a Wallet instance. +At the core of Dash is the Payment Chain. In order to be able to transact on it, one needs to have a set of [UTXOs](https://dashcore.readme.io/docs/core-guide-block-chain-transaction-data) that are controlled by a Wallet instance. In order to access your UTXO, you will have to provide a valid mnemonic that will unlock the Wallet and automatically fetch the associated UTXOs. -When a SDK instance is created, you can access your wallet via the `client.wallet` variable, with the [wallet-lib Wallet doc](https://dashevo.github.io/wallet-lib/#/usage/wallet) +When an SDK instance is created, you can access your wallet via the `client.wallet` variable, with the [wallet-lib Wallet doc](https://dashevo.github.io/wallet-lib/#/usage/wallet) ## Account -Since the introduction of deterministic wallet ([BIP44](https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki)), a Wallet is a representation of multiple account. +Since the introduction of deterministic wallets ([BIP44](https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki)), a Wallet is a representation of multiple accounts. -It is the instance you will use most of the time for receiving or broadcasting payments. +It is the instance you will use most of the time for receiving or broadcasting payments. -You can access your account with `client.account` and see [how to use a different account](/examples/use-different-account) if you need to get a specific account index. +You can access your account with `client.getWalletAccount()` and see [how to use a different account](/examples/use-different-account) if you need to get a specific account index. ## App Schema and Contracts -The Dash Platform Application Chain, provides to developers the ability to create application. -That application requires a set of rules and conditions describe in a portable document in the form of a JSON names : Application Schema. +The Dash Platform Chain, provides to developers the ability to create applications. Each application requires a set of rules and conditions describe in a portable document in the form of a JSON Schema. -When registered, those app schema are called contracts and contains a contractId (namespace : `client.contracts`). -By default, this library supports DPNS (to attach a name to an identity), under the namespace `client.names` for testnet. +When registered, those applications schemas are called contracts and contains a contractId (namespace : `client.platform.contracts`). +By default, this library supports Dash Platform Name Service (DPNS) (to attach a name to an identity), under the namespace `client.platform.names` for testnet. -You can read more on [how to use DPNS on a local evonet](/examples/use-local-evonet.md) or [how to use multiple apps](/getting-started/multiple-apps.md) +You can read more on [how to use DPNS on a local Evonet devnet](/examples/use-local-evonet.md) or [how to use multiple apps](/getting-started/multiple-apps.md). diff --git a/docs/getting-started/dash-platform-applications.md b/docs/getting-started/dash-platform-applications.md index 6a9fc224..678bba29 100644 --- a/docs/getting-started/dash-platform-applications.md +++ b/docs/getting-started/dash-platform-applications.md @@ -1,12 +1,7 @@ ## DPNS -DPNS is handled in Dash SDK's Client under the namespace `client.names.*'`. [Read more here](/platform/names) +DPNS is handled in the Dash SDK Client under the namespace `client.platform.names.*'`. [Read more here](/platform/names) ## DashPay -Soon. Registration of the contract on public evonet should happen in Q1 2020, we will update the doc shortly after. - - -## Memo.dash - -Soon. Registration of the contract on public evonet should happen in Q1 2020, we will update the doc shortly after. +Registration of the contract on Evonet occurred in Q4 2020. Its functionality is not incorporated with the Dash SDK at this time. diff --git a/docs/getting-started/multiple-apps.md b/docs/getting-started/multiple-apps.md index 29d15f16..15e6b220 100644 --- a/docs/getting-started/multiple-apps.md +++ b/docs/getting-started/multiple-apps.md @@ -1,9 +1,9 @@ # Working with multiple apps -When working with other registered contracts, you will need to know their `contractId` and reference it on the SDK constructor. +When working with other registered contracts, you will need to know their `contractId` and reference it in the SDK constructor. -Assuming a contract DashPay and having a following `contractId: "77w8Xqn25HwJhjodrHW133aXhjuTsTv9ozQaYpSHACE3"`. -You can then pass it as an options. +Assuming a contract DashPay has the following `contractId: "77w8Xqn25HwJhjodrHW133aXhjuTsTv9ozQaYpSHACE3"`. +You can then pass it as an option. ```js const client = new Dash.Client({ @@ -15,9 +15,9 @@ const client = new Dash.Client({ }); ``` -This allow the methods `client.platform.documents.fetch` to provide you field selection. -Therefore, if the dashpay contract have a `profile` field that you wish to access, DashJS will allow you to do dot-syntax access : +This allow the method `client.platform.documents.get` to provide you field selection. +Therefore, if the contract has a `profile` field that you wish to access, the SDK will allow you to use dot-syntax for access : ```js -const bobProfile = await client.platform.documents.fetch('dashpay.profile', {name:'bob'}) +const bobProfile = await client.platform.documents.get('dashpay.profile', { name: 'bob' }); ``` diff --git a/docs/getting-started/quickstart.md b/docs/getting-started/quickstart.md index 19e093ab..d1974ab1 100644 --- a/docs/getting-started/quickstart.md +++ b/docs/getting-started/quickstart.md @@ -7,18 +7,17 @@ Having [NodeJS](https://nodejs.org/) installed, just type : ```bash npm install dash ``` + ## Initialization Let's create a Dash SDK client instance specifying both our mnemonic and the schema we wish to work with. ```js -const Dash = require("../src"); +const Dash = require('dash'); const opts = { - network: 'testnet', apps: { dashpay: { - contractId:1234, - schema: require('schema.json') + contractId: '77w8Xqn25HwJhjodrHW133aXhjuTsTv9ozQaYpSHACE3', }, }, wallet: { @@ -26,39 +25,39 @@ const opts = { }, }; const client = new Dash.Client(opts); -client.isReady().then(()=>{ - const {account} = client; - // Do something - }); +client.getWalletAccount().then(async (account) => { + // Do something +}) ``` Quick note : -- If no mnemonic is provided, the subinstance `client.Wallet` will not be initiated (write function for platforms won't be usable). -If you do not have any mnemonic, you can pass `null` to get one generated or omit that parameter to only use Dash.Client in `read-only`. +- If no mnemonic is provided, the sub-instance `client.Wallet` will not be initialized (writing capabilities of Dash Platform won't be usable). +If you do not have a mnemonic, you can pass `null` to have one generated or omit that parameter to only use Dash.Client for `read-only` operations. ## Make a payment ```js -client.isReady().then(()=>{ - const {account} = client; - - account - .createTransaction({ - recipient:{address:'yLptqWxjgTxtwKJuLHoGY222NnoeqYuN8h', amount:0.12} - }) - .then(account.broadcastTransaction); +client.getWalletAccount().then(async (account) => { + const transaction = account.createTransaction({ + recipient: 'yixnmigzC236WmTXp9SBZ42csyp9By6Hw8', + amount: 0.12, }); + account.broadcastTransaction(transaction); +}); ``` -## Read a document +## Read a document -At time of writing, you will need to have registered dashpay yourself, see on [publishing a new contract](/examples/publishing-a-new-contract.md). +At the time of writing, you will need to have registered a data contract yourself. See [publishing a new contract](/examples/publishing-a-new-contract.md). ```js -client.isReady().then(async ()=>{ - const {account} = client; - const bobProfile = await account.platform.documents.fetch('dashpay.profile', {name:'bob'}) - }); + +client.platform.documents.get( + 'tutorialContract.note', + { limit: 1 }, // Only retrieve 1 document +).then(async (documents) => { + console.log(documents); +}); ``` diff --git a/docs/index.html b/docs/index.html index abf0052c..7f388c62 100644 --- a/docs/index.html +++ b/docs/index.html @@ -14,10 +14,10 @@ diff --git a/package-lock.json b/package-lock.json index e8d17509..3973fc1e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -446,9 +446,9 @@ "dev": true }, "@types/yargs": { - "version": "15.0.11", - "resolved": "https://registry.npmjs.org/@types/yargs/-/yargs-15.0.11.tgz", - "integrity": "sha512-jfcNBxHFYJ4nPIacsi3woz1+kvUO6s1CyeEhtnDHBjHUMNj5UlW2GynmnSgiJJEdNg9yW5C8lfoNRZrHGv5EqA==", + "version": "15.0.10", + "resolved": "https://registry.npmjs.org/@types/yargs/-/yargs-15.0.10.tgz", + "integrity": "sha512-z8PNtlhrj7eJNLmrAivM7rjBESG6JwC5xP3RVk12i/8HVP7Xnx/sEmERnRImyEuUaJfO942X0qMOYsoupaJbZQ==", "dev": true, "requires": { "@types/yargs-parser": "*" @@ -1421,15 +1421,6 @@ "y18n": "^4.0.0" }, "dependencies": { - "lru-cache": { - "version": "5.1.1", - "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-5.1.1.tgz", - "integrity": "sha512-KpNARQA3Iwv+jTA0utUVVbrh+Jlrr1Fv0e56GGzAFOXN7dk/FviaDW8LHmK52DlcH4WP2n6gI8vN1aesBFgo9w==", - "dev": true, - "requires": { - "yallist": "^3.0.2" - } - }, "rimraf": { "version": "2.7.1", "resolved": "https://registry.npmjs.org/rimraf/-/rimraf-2.7.1.tgz", @@ -4232,18 +4223,12 @@ "dev": true }, "lru-cache": { - "version": "6.0.0", - "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-6.0.0.tgz", - "integrity": "sha512-Jo6dJ04CmSjuznwJSS3pUeWmd/H0ffTlkXXgwZi+eq1UCmqQwCh+eLsYOYCwY991i2Fah4h1BEMCx4qThGbsiA==", + "version": "5.1.1", + "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-5.1.1.tgz", + "integrity": "sha512-KpNARQA3Iwv+jTA0utUVVbrh+Jlrr1Fv0e56GGzAFOXN7dk/FviaDW8LHmK52DlcH4WP2n6gI8vN1aesBFgo9w==", + "dev": true, "requires": { - "yallist": "^4.0.0" - }, - "dependencies": { - "yallist": { - "version": "4.0.0", - "resolved": "https://registry.npmjs.org/yallist/-/yallist-4.0.0.tgz", - "integrity": "sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==" - } + "yallist": "^3.0.2" } }, "make-dir": { @@ -5801,6 +5786,21 @@ "integrity": "sha512-tCfb2WLjqFAtXn4KEdxIhalnRtoKFN7nAwj0B3ZXCbQloV2tq5eDbcTmT68JJD3nRJq24/XgxtQKFIpQdtvmVw==", "requires": { "lru-cache": "^6.0.0" + }, + "dependencies": { + "lru-cache": { + "version": "6.0.0", + "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-6.0.0.tgz", + "integrity": "sha512-Jo6dJ04CmSjuznwJSS3pUeWmd/H0ffTlkXXgwZi+eq1UCmqQwCh+eLsYOYCwY991i2Fah4h1BEMCx4qThGbsiA==", + "requires": { + "yallist": "^4.0.0" + } + }, + "yallist": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/yallist/-/yallist-4.0.0.tgz", + "integrity": "sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==" + } } }, "semver-diff": { @@ -7787,9 +7787,9 @@ } }, "ws": { - "version": "7.4.1", - "resolved": "https://registry.npmjs.org/ws/-/ws-7.4.1.tgz", - "integrity": "sha512-pTsP8UAfhy3sk1lSk/O/s4tjD0CRwvMnzvwr4OKGX7ZvqZtUyx4KIJB5JWbkykPoc55tixMGgTNoh3k4FkNGFQ==", + "version": "7.4.0", + "resolved": "https://registry.npmjs.org/ws/-/ws-7.4.0.tgz", + "integrity": "sha512-kyFwXuV/5ymf+IXhS6f0+eAFvydbaBW3zjpT6hUdAh/hbVjTIB5EHBGi0bPoCLSK2wcuz3BrEkB9LrYv1Nm4NQ==", "dev": true }, "xdg-basedir": { diff --git a/src/SDK/Client/ClientApps.spec.ts b/src/SDK/Client/ClientApps.spec.ts new file mode 100644 index 00000000..a361457e --- /dev/null +++ b/src/SDK/Client/ClientApps.spec.ts @@ -0,0 +1,46 @@ +import Identifier from "@dashevo/dpp/lib/Identifier"; +import {expect} from 'chai'; +import {ClientApps} from "./ClientApps"; +import 'mocha'; + +describe('ClientApps', () => { + let apps; + let appsFromProps; + it('constructor', function () { + apps = new ClientApps(); + expect(apps.apps).to.deep.equal({}); + appsFromProps = new ClientApps({ + "dpns": { + contractId: '3VvS19qomuGSbEYWbTsRzeuRgawU3yK4fPMzLrbV62u8', + contract: null, + } + }); + }); + it('.set', function () { + apps.set('dpns', { + contractId: '3VvS19qomuGSbEYWbTsRzeuRgawU3yK4fPMzLrbV62u8', + contract: { someField: true } + }); + apps.set('tutorialContract', { + contractId: '3VvS19qomuGSbEYWbTsRzeuRgawU3yK4fPMzLrbV62u8', + contract: { someField: true } + }); + }); + it('should get', function () { + const getByName = apps.get('dpns'); + expect(getByName).to.deep.equal({ + "contractId": Identifier.from("3VvS19qomuGSbEYWbTsRzeuRgawU3yK4fPMzLrbV62u8"), + "contract": { someField: true } + }) + }); + + it('should .getNames()', function () { + const names = apps.getNames(); + expect(names).to.deep.equal(['dpns', 'tutorialContract']); + }); + it('should .has', function () { + expect(apps.has('dpns')).to.equal(true); + expect(apps.has('tutorialContract')).to.equal(true); + expect(apps.has('tutorialContractt')).to.equal(false); + }); +}); diff --git a/src/SDK/Client/Platform/methods/contracts/get.spec.ts b/src/SDK/Client/Platform/methods/contracts/get.spec.ts index d2984b47..0cc227bc 100644 --- a/src/SDK/Client/Platform/methods/contracts/get.spec.ts +++ b/src/SDK/Client/Platform/methods/contracts/get.spec.ts @@ -15,48 +15,63 @@ const factory = new DataContractFactory( const dpp = { dataContract: factory } -const getDataContract = async (id) => { - const fixtureIdentifier = Identifier.from(contractsFixtures.ratePlatform.$id); - if (id.equals(fixtureIdentifier)) { - const contract = await dpp.dataContract.createFromObject(contractsFixtures.ratePlatform); - return contract.toBuffer() - } +const apps = new ClientApps({ + ratePlatform: { + contractId: contractsFixtures.ratePlatform.$id + }, +}); +let client; +let askedFromDapi; - return null; -}; +describe('Client - Platform - Contracts - .get()', () => { + before(()=>{ + askedFromDapi = 0; + const getDataContract = async (id) => { + const fixtureIdentifier = Identifier.from(contractsFixtures.ratePlatform.$id); + askedFromDapi+=1; -const client = { - getDAPIClient: () => { - return { - platform: { - getDataContract + if (id.equals(fixtureIdentifier)) { + const contract = await dpp.dataContract.createFromObject(contractsFixtures.ratePlatform); + return contract.toBuffer() } + return null; }; - }, - getApps(): ClientApps { - return new ClientApps(); - } -}; - -const apps = {}; -describe('Client - Platform - Contracts - .get()', () => { - it('should get a contract by string', async function () { - // @ts-ignore - const contract = await get.call({apps, dpp, client}, contractsFixtures.ratePlatform.$id); - expect(contract.toJSON()).to.deep.equal(contractsFixtures.ratePlatform); - }); + client = { + getDAPIClient: () => { + return { + platform: { + getDataContract + } + }; + }, + getApps(): ClientApps { + return apps + } + }; + }) + describe('get a contract from string', ()=>{ + it('should get from DAPIClient if there is none locally', async function () { - it('should get a contract by identifier', async function () { - // @ts-ignore - const contract = await get.call({apps, dpp, client}, Identifier.from(contractsFixtures.ratePlatform.$id)); - expect(contract.toJSON()).to.deep.equal(contractsFixtures.ratePlatform); - }); + // @ts-ignore + const contract = await get.call({apps, dpp, client}, contractsFixtures.ratePlatform.$id); + expect(contract.toJSON()).to.deep.equal(contractsFixtures.ratePlatform); + expect(askedFromDapi).to.equal(1); + }); + it('should get from local when already fetched once', async function () { + // @ts-ignore + const contract = await get.call({apps, dpp, client}, contractsFixtures.ratePlatform.$id); + expect(contract.toJSON()).to.deep.equal(contractsFixtures.ratePlatform); + expect(askedFromDapi).to.equal(1); + }); + }) - it('should deal when no contract', async function () { - // @ts-ignore - const contract = await get.call({apps, dpp, client}, identitiesFixtures.bob.id); - expect(contract).to.equal(null); - }); + describe('other conditions', ()=>{ + it('should deal when contract do not exist', async function () { + // @ts-ignore + const contract = await get.call({apps, dpp, client}, identitiesFixtures.bob.id); + expect(contract).to.equal(null); + }); + }) }); diff --git a/src/SDK/Client/Platform/methods/contracts/get.ts b/src/SDK/Client/Platform/methods/contracts/get.ts index 4c874fed..a1224d80 100644 --- a/src/SDK/Client/Platform/methods/contracts/get.ts +++ b/src/SDK/Client/Platform/methods/contracts/get.ts @@ -13,45 +13,37 @@ declare type ContractIdentifier = string | Identifier; * @returns contracts */ export async function get(this: Platform, identifier: ContractIdentifier): Promise { - let localContract; - const contractId : Identifier = Identifier.from(identifier); + // Try to get contract from the cache for (const appName of this.client.getApps().getNames()) { const appDefinition = this.client.getApps().get(appName); - if (appDefinition.contractId.equals(contractId)) { - localContract = appDefinition; - break; + if (appDefinition.contractId.equals(contractId) && appDefinition.contract) { + return appDefinition.contract; } } - if (localContract && localContract.contract) { - return localContract.contract; - } else { - // @ts-ignore - const rawContract = await this.client.getDAPIClient().platform.getDataContract(contractId); + // Fetch contract otherwise - if (!rawContract) { - return null; - } + // @ts-ignore + const rawContract = await this.client.getDAPIClient().platform.getDataContract(contractId); - const contract = await this.dpp.dataContract.createFromBuffer(rawContract); - - if (!localContract) { - // If we do not have even the identifier in this.apps, we add it with timestamp as key - this.client.getApps().set( - Date.now().toString(), - { - contractId: contractId, - contract - } - ); - } else { - localContract.contract = contract; - } + if (!rawContract) { + return null; + } + + const contract = await this.dpp.dataContract.createFromBuffer(rawContract); - return contract; + // Store contract to the cache + + for (const appName of this.client.getApps().getNames()) { + const appDefinition = this.client.getApps().get(appName); + if (appDefinition.contractId.equals(contractId)) { + appDefinition.contract = contract; + } } + + return contract; } export default get;