dsa-developers/README.md

238 lines
6.0 KiB
Markdown
Raw Normal View History

2020-04-06 17:11:49 +00:00
# Developing on DSA
2020-04-08 14:51:27 +00:00
We empower third-party developers to build DeFi dapps, use-cases, and other integrations on DSAs platform. That way, users can get curated experience as per their needs, and developers can build their own businesses supporting those users. This virtuous circle creates new opportunities and benefits users, developers, and protocols.
2020-04-06 17:11:49 +00:00
2020-04-08 17:18:52 +00:00
For an overview, refer [here](overview.md)
2020-04-08 14:51:27 +00:00
Our team is super active to assist with your queries at our [TG developer group](https://t.me/instadevelopers) or [discord channel](https://discord.gg/83vvrnY).
2020-04-07 10:55:48 +00:00
2020-04-06 17:57:37 +00:00
## Get Started
2020-04-07 17:22:40 +00:00
**Node**
2020-04-06 17:11:49 +00:00
To get started, install the DSA SDK package from NPM:
```bash
npm install dsa-sdk
```
2020-04-07 17:22:40 +00:00
**For Browser**
2020-04-06 17:11:49 +00:00
2020-04-07 17:22:40 +00:00
via jsDelivr CDN:
```html
<script src="https://cdn.jsdelivr.net/npm/dsa-sdk@1.2.3/build/dsa.js"></script>
2020-04-06 17:11:49 +00:00
```
2020-04-06 17:21:08 +00:00
For production, we recommend linking to a specific version number ([jsdeliver](https://www.jsdelivr.com/package/npm/dsa-sdk)).
2020-04-06 17:11:49 +00:00
2020-04-07 17:22:40 +00:00
## Usage
2020-04-07 10:55:48 +00:00
Now instantiate DSA.
2020-04-06 17:11:49 +00:00
```js
2020-04-07 17:22:40 +00:00
// in node.js
2020-04-08 11:32:26 +00:00
const dsa = require('dsa-sdk');
2020-04-07 17:22:40 +00:00
const dsa = new DSA();
2020-04-07 10:55:48 +00:00
```
2020-04-07 17:22:40 +00:00
DSA only works with web3 library so you also have to instantiate [Web3](https://github.com/ethereum/web3.js/#installation).
2020-04-07 10:55:48 +00:00
```js
2020-04-06 17:11:49 +00:00
if (window.ethereum) {
window.web3 = new Web3(window.ethereum)
} else if (window.web3) {
window.web3 = new Web3(window.web3.currentProvider)
} else {
2020-04-07 10:55:48 +00:00
window.web3 = new Web3(customProvider)
2020-04-06 17:11:49 +00:00
}
```
2020-04-06 18:20:12 +00:00
## Get Accounts
2020-04-06 17:57:37 +00:00
2020-04-06 19:56:09 +00:00
Once connected to a web3 client, get all the DSA where a specific address is authorised.
2020-04-06 17:11:49 +00:00
```js
2020-04-08 11:32:26 +00:00
let address = "0x...";
dsa.getAccounts(address)
2020-04-06 17:11:49 +00:00
.then(data => {
return data
})
.catch(error => {
return error
})
```
### Parameters
`address` - An ethereum address.
### Returns
2020-04-08 13:47:51 +00:00
`Array` of `Object` with all the DSA where `address` parameter is authorised.
2020-04-06 17:11:49 +00:00
2020-04-06 17:13:30 +00:00
```js
2020-04-06 17:21:08 +00:00
[
{
id: 52, // DSA Number
2020-04-07 12:48:19 +00:00
address: "0x...", // DSA Address
2020-04-06 17:21:08 +00:00
version: 1 // DSA version
},
...
]
2020-04-06 17:11:49 +00:00
```
2020-04-06 19:56:09 +00:00
Web3 equivalent of the above (return value might have different format):
2020-04-06 17:11:49 +00:00
```js
2020-04-07 17:22:40 +00:00
let ABI = []; // => https://github.com/InstaDApp/dsa-sdk/blob/master/src/abi/resolvers/core.json
let contract = "0xD6fB4fd8b595d0A1dE727C35fe6F1D4aE5B60F51";
let account = "0x...";
2020-04-06 19:56:09 +00:00
new web3.eth.Contract(ABI, contract);
2020-04-07 17:22:40 +00:00
.getOwnerDetails(account)
2020-04-06 17:11:49 +00:00
.call({ from: "0x..." })
.then((data) => {
return data
})
.catch((error) => {
return error
});
2020-04-06 17:21:08 +00:00
```
2020-04-06 19:56:09 +00:00
## Set Instance
2020-04-06 21:07:55 +00:00
Once you get the DSA(s), set some common values so you don't have to pass similar arguments in further calls.
2020-04-06 19:56:09 +00:00
```js
2020-04-08 11:32:26 +00:00
let address = "0x...";
let dsaAccount = dsa.getAccounts(address);
2020-04-06 19:56:09 +00:00
dsa.setInstance({
id: dsaAccount[0].id,
2020-04-07 12:48:19 +00:00
address: dsaAccount[0].account,
2020-04-06 19:56:09 +00:00
origin: "0x..." // optional
})
```
### Parameters
1. `Object`
* `id` - `Number`: The number of DSA.
2020-04-07 12:48:19 +00:00
* `address` - `String`: The address of DSA.
2020-04-06 19:56:09 +00:00
* `origin` - `String`: The address to track the transaction origination (affiliates).
## Build DSA
Build a new DSA.
```js
dsa.build()
.then(data => {
2020-04-07 10:55:48 +00:00
return data // transaction hash
2020-04-06 19:56:09 +00:00
})
.catch(error => {
return error
2020-04-07 17:22:40 +00:00
});
2020-04-06 19:56:09 +00:00
```
### Parameters
1. `Object` (ALL optional)
* `owner`: The authorised address which will be authorised on DSA (defaulted to selected address).
* `origin` - `String`: The address to track the transaction origination (affiliates).
* `from` - `String`: The address transactions should be made from (defaulted to selected address).
* `gasPrice` - `String`: The gas price in wei to use for transactions.
* `gas` - `Number`: The maximum gas provided for a transaction (gas limit).
### Returns
`String`: Transaction hash `0x.....`.
2020-04-07 08:45:14 +00:00
Web3 equivalent of the above:
2020-04-06 19:56:09 +00:00
```js
2020-04-06 21:07:55 +00:00
let ABI = [] // => https://github.com/InstaDApp/dsa-sdk/blob/master/src/abi/core/index.json
2020-04-07 17:22:40 +00:00
let contract = "0xD6fB4fd8b595d0A1dE727C35fe6F1D4aE5B60F51";
2020-04-08 11:32:26 +00:00
let address = await web3.eth.getAccounts();
2020-04-07 17:22:40 +00:00
let owner = "0x...";
2020-04-06 19:56:09 +00:00
let version = 1;
2020-04-07 17:22:40 +00:00
let origin = "0x...";
2020-04-06 19:56:09 +00:00
new web3.eth.Contract(ABI, contract).methods
.build(owner, version, origin)
.send({
2020-04-08 11:32:26 +00:00
from: address[0]
2020-04-06 19:56:09 +00:00
})
.on("transactionHash", (txHash) => {
2020-04-06 21:07:55 +00:00
return txHash;
2020-04-06 19:56:09 +00:00
})
2020-04-06 21:07:55 +00:00
.on("error", (error) => {
return error;
});
```
## Interact with DSA
2020-04-08 11:32:26 +00:00
Once the DSA is build, use the following function to make calls to your DSA. This is where you'll interact with other smart contracts like DeFi protocols.
2020-04-06 21:07:55 +00:00
Create a new instance.
```js
let spells = dsa.Spell();
```
Add the series of transactions details in the instance.
```js
spells.add({
2020-04-07 12:36:54 +00:00
connector: "basic", // name
method: "deposit", // method
args: [dsa.token.usdc.address, 1000000, 0, 1] // method arguments
2020-04-07 17:22:40 +00:00
});
2020-04-07 08:45:14 +00:00
2020-04-06 21:07:55 +00:00
spells.add({
connector: "basic",
method: "withdraw",
args: [dsa.token.usdc.address, 0, "0x03d70891b8994feB6ccA7022B25c32be92ee3725", 1, 0]
2020-04-07 17:22:40 +00:00
});
2020-04-06 21:07:55 +00:00
```
Note: You can get the specific input interface by calling `dsa.getInterface(connector, method)`
Send the transaction to blockchain. CAST YOUR SPELL.
```js
2020-04-07 10:55:48 +00:00
dsa.cast(spells) // or dsa.cast({spells:spells})
2020-04-06 21:07:55 +00:00
.then(data => {
2020-04-07 10:55:48 +00:00
return data // transaction hash
2020-04-06 21:07:55 +00:00
})
.catch(error => {
return error
2020-04-07 17:22:40 +00:00
});
2020-04-06 21:07:55 +00:00
```
### Parameters
1. `Instance` - The spell instance.
OR
1. `Object`
2020-04-08 14:21:56 +00:00
* `spells` - The spell instance.
2020-04-06 21:07:55 +00:00
* `from` - `String` (optional): The address transactions should be made from (defaulted to selected address).
* `gasPrice` - `String` (optional): The gas price in wei to use for transactions.
* `gas` - `Number` (optional): The maximum gas provided for a transaction (gas limit).
### Returns
`String`: Transaction hash `0x.....`.
2020-04-07 08:45:14 +00:00
Web3 equivalent of the above:
2020-04-06 21:07:55 +00:00
```js
2020-04-07 17:22:40 +00:00
let ABI = []; // => https://github.com/InstaDApp/dsa-sdk/blob/master/src/abi/core/account.json
let contract = "0x..."; // DSA address
let origin = "0x...";
2020-04-08 11:32:26 +00:00
let address = await web3.eth.getAccounts();
2020-04-06 21:07:55 +00:00
new web3.eth.Contract(ABI, contract).methods
.cast(
["0x...", "0x..."], // Array of target addresses
["0x......", "0x......"], // Array of encoded function call, check encodeFunctionCall
origin
)
.send({
2020-04-08 11:32:26 +00:00
from: address[0]
2020-04-06 21:07:55 +00:00
})
.on("transactionHash", (txHash) => {
return txHash;
})
.on("error", (error) => {
return error;
2020-04-06 19:56:09 +00:00
});
```
2020-04-08 17:18:52 +00:00
Our team is super active to assist with your queries at our [TG developer group](https://t.me/instadevelopers) or [discord channel](https://discord.gg/83vvrnY).