Instadapp / docs

Instadapp documentation
docs-nuxt-instadapp-eng.vercel.app
6 stars 11 forks source link

Update DOCS: Add Interaction with DSA contract guide. #86

Closed pradyuman-verma closed 2 years ago

pradyuman-verma commented 2 years ago

Interact with DSA on smart contract level:

This guide will show you how to interact with DSA on the smart contract level.

Quick setup:

pragma solidity ^0.8.3;

import "hardhat/console.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
interface IInstaIndex {
    function build(
        address _owner,
        uint256 _accountVersion,
        address _origin
    ) external returns (address _account);
}

interface IDSA {
    function cast(
        string[] calldata _targetNames,
        bytes[] calldata _datas,
        address _origin
    ) external payable returns (bytes32);
}

contract InteractingDSA {

    IInstaIndex instaIndex = IInstaIndex(0x2971adfa57b20e5a416ae5a708a8655a9c74f723); // this address is only of mainnet.

    function buildAndCast(address _owner) external {
        // creating an account
        address _account = instaIndex.build(_owner, 2, address(0)); // 2 is the most recent DSA version

        // encoding data to run multiple things through cast on account
        // Depositing in DSA and then deposit in Compound through DSA.
        string[] memory _targets = new string[](2);
        bytes[] memory _data = new bytes[](2);

        _targets[0] = "BASIC-A";
        _targets[1] = "COMPOUND-A";

        bytes4 private constant basicDeposit = bytes4(keccak256("deposit(address,uint256,uint256,uint256)"));
        bytes4 private constant compoundDeposit = bytes4(keccak256("deposit(string,uint256,uint256,uint256)"));

        address dai = 0x6B175474E89094C44Da98b954EedeAC495271d0F;
        uint amtToDeposit = 1e18; // 1 DAI

        _data[0] = abi.encodeWithSelector(basicDeposit, dai, amtToDeposit, 0, 0);
        _data[1] = abi.encodeWithSelector(compoundDeposit, dai, amtToDeposit, 0, 0);

        IDSA(_account).cast(_targets, _data, address(0)); // Magic!!
    }

}

Now we can start interacting with the DSA contract.

Go to Networks to find each chain-related address.

Usage

* InstaIndex:

This is the Main Contract for all the Defi Smart Accounts. Used to create a new Defi Smart Account for a user and run a cast function in the new smart account.

* InstaImplementationM1:

This contract contains most core functions of smart account name cast(). It is only called by owners of smart accounts and has full-fledge access over the smart account. Used also to access all the DSA.

* build(owner, accountVersion, _origin):

Create a DSA Account using this function. It returns the address of the DSA account created.

 owner: Owner of the Smart Account
 accountVersion: Account Module version
_origin: Where Smart Account is created.

* cast(_targets, _datas, _origin):

Using cast() user can access the connectors, which allows the smart account to interact with protocols or set up any settings on the smart account.

_target: string array mentioning connectors, encoded data. 
_datas: encoded data containing function abi and params.

DSA Setup

Inside the interactingDSA contract, we will create a DSA account and use that to cast a spell.

Creating InstaIndex instance

IInstaIndex instaIndex = IInstaIndex(0x2971adfa57b20e5a416ae5a708a8655a9c74f723);

Creating DSA account

address _account = instaIndex.build(_owner, 2, address(0));

Casting Spell:

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

 IDSA(_account).cast(_targets, _data, address(0));

We create an InstaImplementationM1 contract instance using our DSA account and then cast function to cast the spell.

pradyuman-verma commented 2 years ago

Resolved in #88 !! Now closing this