npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

dsa-connect

v0.7.16

Published

DSA connect

Downloads

1,530

Readme

DSA Connect Build status

The official DSA SDK for JavaScript, available for browsers and Node.js backends.

Installation

To get started, install the DSA Connect package from npm:

npm install dsa-connect

For browsers, via jsDelivr CDN:

<script src="https://cdn.jsdelivr.net/npm/dsa-connect@latest/dist/index.bundle.min.js"></script>

Usage

To enable web3 calls via SDK, instantiate web3 library

// in browser
if (window.ethereum) {
  window.web3 = new Web3(window.ethereum)
} else if (window.web3) {
  window.web3 = new Web3(window.web3.currentProvider)
} else {
  window.web3 = new Web3(customProvider)
}
// in nodejs
const Web3 = require('web3')
const DSA = require('dsa-connect');
const web3 = new Web3(new Web3.providers.HttpProvider(ETH_NODE_URL))

Now instantiate DSA with web3 instance.

// in browser
const dsa = new DSA(web3);

// in nodejs
const dsa = new DSA({
  web3: web3,
  mode: "node",
  privateKey: PRIVATE_KEY
});

Setting up DSA Accounts

Every user needs to create Smart Account to interact with DeFi Protocols seamlessly; this allows developers to build extensible use-cases with maximum security and composability. You can also create multiple account for a single address.

  • Create Smart Account - build()
  • Fetch Smart Accounts - getAccounts()
  • Set Smart Account - setInstance()

build()

Create a DSA Account. If the account is already created, you can use the setInstance method to activate a paricular DSA account and start casting spells.

// in async functions
await dsa.build()

// or
dsa.build().then(console.log)

The build method also accepts an optional parameters as shown below:

dsa.build({
  gasPrice: gasPrice // estimated gas price
  origin: origin,
  authority: authority,
})

View this Gist for estimation of gas price

| Parameter | Type | Description | |---------------|-----------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | gasPrice | string/number | The gas price in gwei. Mostly used in Node implementation to configure the transaction confirmation speed. | | origin | address | The address to track the origin of transaction. Used for analytics and affiliates. | | authority | address | The DSA authority. The address to be added as authority. | | from | address | The account with which you want to create your DSA. This is helpful to create DSA for other addresses. | | nonce | string/number | Nonce of your sender account. Mostly used in Node implementation to send transaction with a particular nonce either to override unconfirmed transaction or some other purpose. |

The method returns the transaction hash.

This creates a uniquely numbered Smart Account which acts as a proxy to interact with verified DeFi protocols and each DSA has a unique ethereum address.

getAccounts()

Fetch all the accounts owned by an ethereum address by calling getAccounts().

// in async functions
await dsa.getAccounts(address)

// or
dsa.getAccounts(address).then(console.log)

| Parameter | Type | Description | |---------------|-----------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | address | address | An ethereum address. |

The method returns an array of objects with all the DSA accounts where address is authorised:

[
  {
      id: 52, // DSA ID
      address: "0x...", // DSA Address
      version: 1 // DSA version
  },
  ...
]

setInstance()

Be sure to configure global values by calling setInstance(). You can get the id by calling getAccounts(). The configured account will be used for all subsequent calls.

dsa.setInstance(dsaId); // DSA ID

| Parameter | Type | Description | |---------------|-----------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | dsaId | Number | DSA ID to be used for casting. |

The method returns an array of objects with all the DSA accounts where address is authorised:

Casting Spells

Spells denotes a sequence of connector functions that will achieve a given use case. Spells can comprise of any number of functions across any number of connectors.

With this SDK, performing DeFi operations on your dapp consists of creating a spells instance to add transactions. Here is where you can initiate complex transactions amongst different protocols.

Create an instance:

let spells = dsa.Spell()

Add spells that you want to execute. Think of any actions, and by just adding new SPELLS, you can wonderfully CAST transactions across protocols. Let's try to execute the following actions:

  • Deposit 1 ETH to Aave
  • Borrow 100 DAI
  • Deposit borrowed DAI on Compound
spells.add({
  connector: "aave",
  method: "deposit",
  args: [
    "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee",
    "1000000000000000000", // 1 ETH (10^18 wei)
    0,
    0
  ]
})

spells.add({
  connector: "aave",
  method: "borrow",
  args: [
    "0x6B175474E89094C44Da98b954EedeAC495271d0F",
    "100000000000000000000", // 100 * 10^18 wei
    0,
    0
  ]
})

spells.add({
  connector: "compound",
  method: "deposit",
  args: [
    "0x6B175474E89094C44Da98b954EedeAC495271d0F",
    "100000000000000000000", // 100 * 10^18 wei
    0,
    0
  ]
})

Note - Make sure, your smart account have the equivalent ETH balance before executing the above actions.

At last, cast your spell using cast() method.

// in async functions
let transactionHash = await spells.cast();

// or
spells.cast().then(console.log) // returns transaction hash

You can also pass an object to send optional parameters like sending ETH along with the transaction.

spells.cast({
  gasPrice: web3.utils.toWei(gasPrice, 'gwei'), // in gwei, used in node implementation.
  value: "1000000000000000000", // sending 1 Eth along the transaction.
  nonce: nonce
})

| Parameter (optional) | Type | Description | |--------------------------|-----------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | gasPrice | string/number | The gas price in gwei. Mostly used in Node implementation to configure the transaction confirmation speed. | | value | string/number | Amount of ETH which you want to send along with the transaction (in wei). | | nonce | string/number | Nonce of your sender account. Mostly used in Node implementation to send transaction with a particular nonce either to override unconfirmed transaction or some other purpose. |

This will send the transaction to blockchain in node implementation (or ask users to confirm the transaction on web3 wallet like Metamask).

Connectors

You can see the list of connectors here & here