Skip to content

Commit

Permalink
Merge pull request #5 from dippixyz/develop
Browse files Browse the repository at this point in the history
SDK TBA Update
  • Loading branch information
erazoerazo committed Oct 6, 2023
2 parents f72aadf + 7bb2453 commit d94e5ee
Show file tree
Hide file tree
Showing 4 changed files with 379 additions and 45 deletions.
3 changes: 3 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Rules for Contributing

TBD
147 changes: 136 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Dippi SDK for JS

This is the Dippi SDK for JavaScript. It is a wrapper around the Dippi API.
This is the Dippi SDK for JavaScript, providing a wrapper around the Dippi API.

## Installation

Expand All @@ -10,21 +10,146 @@ npm i @dippixyz/sdk

## Usage

Go to [client.dippi.xyz](https://client.dippi.xyz) to sign up and obtain an `appToken` and `appId`.

### Dippi Client

To use the Dippi Client, follow the steps below:

1. **Initialization**:

```js
const { Dippi, TBA } = require('@dippixyz/sdk');

const dippiClient = new Dippi({
appToken: [appToken],
appId: [appId],
url: 'https://api.dippi.xyz',
});
```

2. **Authentication**:

```js
const { accessToken } = await dippiClient.auth.login();
dippiClient.setAuthToken(accessToken);
```

3. **Fetching User Profile**:

```js
const userProfile = await dippiClient.user.getProfile(userId);
```

4. **Working with Wallets, Applications, and Application Tokens**:

```js
const userWallets = await dippiClient.wallet.list();
const userApplications = await dippiClient.application.list();
const userApplicationToken = await dippiClient.applicationToken.retrieve(applicationId);
```

### Token Bound Account (TBA)

To utilize the Token Bound Account (TBA) Class:

1. **Initialization**:

```js
const tbaConfig = {
appToken: [appToken],
appId: [appId],
url: 'https://api.dippi.xyz',
auth: dippiClient.auth // Passing the auth instance from Dippi client
};

const tba = new TBA(tbaConfig);
```

2. **Initialization and Creation for TBA**:

```js
const initArgs = {
destinationWallet: 'destinationWalletAddress',
nftContract: 'nftContractAddress',
nftId: 'nftId'
};

await tba.init(initArgs);
await tba.create();
```

### Example Application

Below is an example of how you can utilize the Dippi SDK and the TBA feature in a simple Node.js application.

```js
const Dippi = require('@dippixyz/sdk');
const { Dippi, TBA } = require('@dippixyz/sdk');

// Initialize Dippi Client
const dippiClient = new Dippi({
appToken: process.env.APP_TOKEN,
appId: process.env.APP_ID,
url: process.env.CLIENT_URL,
appToken: 'yourAppToken',
appId: 'yourAppId',
url: 'https://api.dippi.xyz',
});

const { accessToken } = await dippiClient.auth.login();
dippiClient.setAuthToken(accessToken);
(async () => {
// Authenticate
const { accessToken } = await dippiClient.auth.login();
dippiClient.setAuthToken(accessToken);

// Fetch User Profile
const userId = 'yourUserId';
const userProfile = await dippiClient.user.getProfile(userId);

// List User Wallets
const userWallets = await dippiClient.wallet.list();

// List User Applications
const userApplications = await dippiClient.application.list();

// Retrieve User Application Token
const tokenId = 'yourTokenId';
const userApplicationToken = await dippiClient.applicationToken.retrieve(tokenId);

// Initialize TBA
const tbaConfig = {
appToken: 'yourAppToken',
appId: 'yourAppId',
url: 'https://api.dippi.xyz',
auth: dippiClient.auth,
_tokenBoundAccount: {}
};

const userProfile = await dippiClient.user.getProfile(id);
const tba = new TBA(tbaConfig);

const userWallets = await dippiClient.wallet.list();
const userApplicaitons = await dippiClient.application.list();
const userApplicationToken = await dippiClient.applicationToken.retrieve(id);
// Initialize and Create TBA
const initArgs = {
destinationWallet: 'destinationWalletAddress',
nftContract: 'nftContractAddress',
nftId: 'nftId'
};

await tba.init(initArgs);
await tba.create();

console.log('TBA created successfully!');
})();
```

Make sure to replace the placeholders in the code with your actual data. Save this code in a file, say `app.js`, and run it using `node app.js`.

## Contributing

Please read [CONTRIBUTING.md](CONTRIBUTING.md) for details on our code of conduct, and the process for submitting pull requests to us.

## License

This project is licensed under the MIT License - see the [LICENSE.md](LICENSE.md) file for details.

## Acknowledgments

* Hat tip to anyone whose code was used
* Inspiration
* etc
```
63 changes: 43 additions & 20 deletions src/dippi.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@ class Dippi {
* Initializes a new instance of the Dippi class.
*
* @param {Object} config - Configuration object.
* @param {String} config.email - User's email.
* @param {String} config.password - User's password.
* @param {String} config.url - API URL.
* @param {String} config.authToken - Authentication token.
* @param {String} config.clientId - Client ID.
* @param {string} config.appToken - Dippi Application token.
* @param {string} config.appId - Dippi Application ID.
* @param {string} config.url - Dippi API URL.
*/
constructor(config) {
this.appToken = config.appToken;
Expand All @@ -39,10 +37,10 @@ class Dippi {
/**
* Updates the authentication token.
*
* @param {String} newAuthToken - New authentication token.
* @param {string} newAuthToken - New authentication token.
*/
async setAuthToken (newAuthToken) {
this.authToken = newAuthToken
setAuthToken(newAuthToken) {
this.authToken = newAuthToken;
}

}
Expand All @@ -55,11 +53,9 @@ class TBA {
* Initializes a new instance of the TBA class.
*
* @param {Object} config - Configuration object.
* @param {String} config.appToken - DippiClient authentication token.
* @param {String} config.appId - DippiClient application ID.
* @param {String} config.url - DippiClient API URL.
* @param {Object} config.auth - DippiClient authentication object.
* @param {Object} config._tokenBoundAccount - DippiClient Token Bound Account configuration.
* @param {string} config.appToken - Dippi authentication token.
* @param {string} config.appId - Dippi application ID.
* @param {string} config.url - Dippi API URL.
*/
constructor(config) {
this.appToken = config.appToken;
Expand All @@ -85,31 +81,58 @@ class TBA {
*
* @typedef InitArgs
* @type {Object}
* @property {String} destinationWallet - Destination wallet address.
* @property {String} nftContract - NFT contract address.
* @property {String} nftId - NFT ID.
*
* @param {InitArgs} args - Initialization arguments.
* @param {string} InitArgs.destinationWallet - Destination wallet address.
* @param {string} InitArgs.chainId - The ID of the blockchain network.
* @param {string} [InitArgs.gasLimit] - The maximum amount of gas that the transaction is allowed to use.
* @param {string} InitArgs.nftContract - NFT contract address.
* @param {string} InitArgs.nftId - NFT ID.
*
* @returns {Promise<Object>} - The result of the initialization.
*/
async init(args) {
const { accessToken } = await this.auth.login();
this.authToken = accessToken
return this._tokenBoundAccount.init(args);
}
}


/**
* Creates a TokenBoundAccount.
* Delivers an NFT with a token-bound account if a wallet address is provided.
* If a wallet address, NFT drop contract address, and NFT token ID are provided,
* a token-bound account is created for the NFT with the provided NFT token ID.
*
* @returns {Promise<Object>} - The result of the creation.
*/
async create() {
return this._tokenBoundAccount.create();
}

/**
* Estimates gas for creating a Token Bound Account.
*
* @returns {Promise<string>} - The result of the creation.
*/
async estimateGas() {
return this._tokenBoundAccount.estimateGas();
}

/**
* Signs the transaction for creating a Token Bound Account.
*
* @returns {Promise<string>} - The signed transaction.
*/
async signCreateTransaction() {
return this._tokenBoundAccount.signCreateAccountTransaction();
}

/**
* Sends the transaction to create a Token Bound Account.
*
* @returns {Promise<string>} - The result of the transaction.
*/
async sendCreateTransaction() {
return this._tokenBoundAccount.sendCreateAccountTransaction();
}
}

module.exports = { Dippi, TBA };
Loading

0 comments on commit d94e5ee

Please sign in to comment.