Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement builder pattern #15

Merged

Conversation

michaeldev5
Copy link
Collaborator

@michaeldev5 michaeldev5 commented Jan 7, 2023

To make SDK easier to use builder pattern was implemented in the file Builder.ts. From an implementation point of view, the "Builder with a twist" design pattern also known as "Step builder" was specifically chosen because of the large number of mandatory fields. In short this pattern uses the common builder pattern, but adds a twist by using interface to force which value to set next and thereby solving the problem with many mandatory values.

Builder pattern supports following use cases:

  • Transfer tokens from parachain to parachain
  • Transfer tokens from relaychain to parachain
  • Transfer tokens from parachain to relaychain
  • Open a channel
  • Close a channel

To use Builder API, developer first imports a Builder function as follows:

import { Builder } from '@paraspell/sdk'

Builder function accepts one parameter of type ApiPromise. Then, other functions can be chained depending on the use case. In the case of initiating a token transfer the Builder class decides which scenario to use depending on the from() and to() methods. Depending on the scenario different mandatory fields are required. Every step of the build process is properly typed and therefore invalid object cannot be created. After providing all required parameters the build() method executes the transaction or open/closes a channel. Mandatory fields must be specified in the same order as they are shown in the example otherwise typescript error will be generated.

Developer experience

When developing, the developer is guided by the typescript and thus knows which parameter can be added as next. This increases the developer experience and makes SDK easier to use.
Screenshot 2023-01-08 at 14 27 53

Other changes:

  • Unit tests for each use case are written in the file Builder.test.ts. unit tests are designed in such a way that they check the parameters of the target function, which each case of using the builder pattern calls when build() method is executed. This is achieved by the spy function from vitest.

Examples of all use cases

Transfer tokens from parachain to parachain

Both from and to parameters are provided, thus the parachain to parachain scenario will be used.

  Builder(api)
      .from('Acala')
      .to('Quartz')
      .currency('ACA')
      .currencyId(currencyId)
      .amount(amount)
      .address(address)
      .build()

Transfer tokens from relaychain to parachain

Only from parameter is provided, thus the relaychain to parachain scenario will be used.

  Builder(api)
      .to('Acala')
      .amount(amount)
      .address(address)
      .build()

Transfer tokens from parachain to relaychain

Only to parameter is provided, thus the parachain to relaychain scenario will be used.

    Builder(api)
      .from('Acala')
      .currency('KSM')
      .currencyId(currencyId)
      .amount(amount)
      .address(address)
      .build()

Open a channel

When opening a new channel the operation has to be specified by calling openChannel() method and then providing maxSize and maxMessageSize parameters.

    Builder(api)
      .from('Acala')
      .to('Quartz')
      .openChannel()
      .maxSize(maxSize)
      .maxMessageSize(maxMsgSize)
      .build()

Close a channel

When closing channels the operation has to be specified by calling closeChannel() method and then providing inbound and outbound parameters.

    Builder(api)
      .from('Acala')
      .closeChannel()
      .inbound(inbound)
      .outbound(outbound)
      .build()

@michaeldev5 michaeldev5 marked this pull request as ready for review January 8, 2023 12:16
@dudo50
Copy link
Contributor

dudo50 commented Jan 8, 2023

PR will close issue #2

@dudo50 dudo50 merged commit 74fba22 into paraspell:beta-pre-release Jan 8, 2023
@vikiival
Copy link
Collaborator

vikiival commented Jan 8, 2023

Nice job!

Wondering why you need both currency and currencyId?

@dudo50
Copy link
Contributor

dudo50 commented Jan 9, 2023

@vikiival some nodes require asset symbols and some asset ids. This way user can enter ones that their node supports. However this is good point and I am going to create new issue covering this.

@vikiival vikiival linked an issue Jan 11, 2023 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Rewrite SDK to builder pattern
3 participants