Merge pull request #72 from Instadapp/feat/ERC721-deposit/withdraw

Add ERC721 and ERC1155 connector
This commit is contained in:
Thrilok kumar 2021-09-11 13:42:42 +05:30 committed by GitHub
commit 28a0d67979
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 786 additions and 0 deletions

View File

@ -0,0 +1,20 @@
pragma solidity ^0.7.0;
contract Events {
event LogDepositERC1155(
address indexed erc1155,
address from,
uint256 tokenId,
uint256 amount,
uint256 getId,
uint256 setId
);
event LogWithdrawERC1155(
address indexed erc1155,
uint256 tokenId,
address indexed to,
uint256 amount,
uint256 getId,
uint256 setId
);
}

View File

@ -0,0 +1,93 @@
pragma solidity ^0.7.0;
/**
* @title Basic.
* @dev Deposit & Withdraw from ERC1155 DSA.
*/
import {IERC1155} from "@openzeppelin/contracts/token/ERC1155/IERC1155.sol";
import {DSMath} from "../../common/math.sol";
import {Basic} from "../../common/basic.sol";
import {Events} from "./events.sol";
abstract contract BasicResolver is Events, DSMath, Basic {
/**
* @dev Deposit Assets To Smart Account.
* @notice Deposit a ERC1155 token to DSA
* @param token Address of token.
* @param tokenId ID of token.
* @param amount Amount to deposit.
* @param getId ID to retrieve amount.
* @param setId ID stores the amount.
*/
function depositERC1155(
address token,
uint256 tokenId,
uint256 amount,
uint256 getId,
uint256 setId
)
public
payable
returns (string memory _eventName, bytes memory _eventParam)
{
uint256 _amount = getUint(getId, amount);
IERC1155 tokenContract = IERC1155(token);
tokenContract.safeTransferFrom(
msg.sender,
address(this),
tokenId,
_amount,
""
);
setUint(setId, _amount);
_eventName = "LogDepositERC1155(address,address,uint256,uint256,uint256,uint256)";
_eventParam = abi.encode(
token,
msg.sender,
tokenId,
_amount,
getId,
setId
);
}
/**
* @dev Withdraw Assets To Smart Account.
* @notice Withdraw a ERC1155 token from DSA
* @param token Address of the token.
* @param tokenId ID of token.
* @param to The address to receive the token upon withdrawal
* @param amount Amount to withdraw.
* @param getId ID to retrieve amount.
* @param setId ID stores the amount.
*/
function withdrawERC1155(
address token,
uint256 tokenId,
address payable to,
uint256 amount,
uint256 getId,
uint256 setId
)
public
payable
returns (string memory _eventName, bytes memory _eventParam)
{
uint256 _amount = getUint(getId, amount);
IERC1155 tokenContract = IERC1155(token);
tokenContract.safeTransferFrom(address(this), to, tokenId, _amount, "");
setUint(setId, _amount);
_eventName = "LogWithdrawERC1155(address,uint256,address,uint256,uint256,uint256)";
_eventParam = abi.encode(token, tokenId, to, _amount, getId, setId);
}
}
contract ConnectV2BasicERC1155 is BasicResolver {
string public constant name = "BASIC-ERC1155-v1.0";
}

View File

@ -0,0 +1,18 @@
pragma solidity ^0.7.0;
contract Events {
event LogDepositERC721(
address indexed erc721,
address from,
uint256 tokenId,
uint256 getId,
uint256 setId
);
event LogWithdrawERC721(
address indexed erc721,
uint256 tokenId,
address indexed to,
uint256 getId,
uint256 setId
);
}

View File

@ -0,0 +1,76 @@
pragma solidity ^0.7.0;
/**
* @title Basic.
* @dev Deposit & Withdraw ERC721 from DSA.
*/
import {IERC721} from "@openzeppelin/contracts/token/ERC721/IERC721.sol";
import {DSMath} from "../../common/math.sol";
import {Basic} from "../../common/basic.sol";
import {Events} from "./events.sol";
abstract contract BasicResolver is Events, DSMath, Basic {
/**
* @dev Deposit Assets To Smart Account.
* @notice Deposit a ERC721 token to DSA
* @param token Address of token.
* @param tokenId ID of token.
* @param getId ID to retrieve tokenId.
* @param setId ID stores the tokenId.
*/
function depositERC721(
address token,
uint256 tokenId,
uint256 getId,
uint256 setId
)
public
payable
returns (string memory _eventName, bytes memory _eventParam)
{
uint256 _tokenId = getUint(getId, tokenId);
IERC721 tokenContract = IERC721(token);
tokenContract.safeTransferFrom(msg.sender, address(this), _tokenId);
setUint(setId, _tokenId);
_eventName = "LogDepositERC721(address,address,uint256,uint256,uint256)";
_eventParam = abi.encode(token, msg.sender, _tokenId, getId, setId);
}
/**
* @dev Withdraw Assets To Smart Account.
* @notice Withdraw a ERC721 token from DSA
* @param token Address of the token.
* @param tokenId ID of token.
* @param to The address to receive the token upon withdrawal
* @param getId ID to retrieve tokenId.
* @param setId ID stores the tokenId.
*/
function withdrawERC721(
address token,
uint256 tokenId,
address payable to,
uint256 getId,
uint256 setId
)
public
payable
returns (string memory _eventName, bytes memory _eventParam)
{
uint256 _tokenId = getUint(getId, tokenId);
IERC721 tokenContract = IERC721(token);
tokenContract.safeTransferFrom(address(this), to, _tokenId);
setUint(setId, _tokenId);
_eventName = "LogWithdrawERC721(address,uint256,address,uint256,uint256)";
_eventParam = abi.encode(token, _tokenId, to, getId, setId);
}
}
contract ConnectV2BasicERC721 is BasicResolver {
string public constant name = "BASIC-ERC721-v1.0";
}

View File

@ -0,0 +1,114 @@
pragma solidity ^0.7.0;
pragma experimental ABIEncoderV2;
import { Variables } from "./variables.sol";
interface IndexInterface {
function list() external view returns (address);
}
interface ListInterface {
function addAuth(address user) external;
function removeAuth(address user) external;
}
contract Constants is Variables {
uint256 public constant implementationVersion = 1;
// InstaIndex Address.
address public immutable instaIndex;
// The Account Module Version.
uint256 public constant version = 2;
constructor(address _instaIndex) {
instaIndex = _instaIndex;
}
}
contract Record is Constants {
constructor(address _instaIndex) Constants(_instaIndex) {}
event LogEnableUser(address indexed user);
event LogDisableUser(address indexed user);
/**
* @dev Check for Auth if enabled.
* @param user address/user/owner.
*/
function isAuth(address user) public view returns (bool) {
return _auth[user];
}
/**
* @dev Enable New User.
* @param user Owner address
*/
function enable(address user) public {
require(
msg.sender == address(this) || msg.sender == instaIndex,
"not-self-index"
);
require(user != address(0), "not-valid");
require(!_auth[user], "already-enabled");
_auth[user] = true;
ListInterface(IndexInterface(instaIndex).list()).addAuth(user);
emit LogEnableUser(user);
}
/**
* @dev Disable User.
* @param user Owner address
*/
function disable(address user) public {
require(msg.sender == address(this), "not-self");
require(user != address(0), "not-valid");
require(_auth[user], "already-disabled");
delete _auth[user];
ListInterface(IndexInterface(instaIndex).list()).removeAuth(user);
emit LogDisableUser(user);
}
/**
* @dev ERC721 token receiver
*/
function onERC721Received(
address,
address,
uint256,
bytes calldata
) external returns (bytes4) {
return 0x150b7a02; // bytes4(keccak256("onERC721Received(address,address,uint256,bytes)"))
}
/**
* @dev ERC1155 token receiver
*/
function onERC1155Received(
address,
address,
uint256,
uint256,
bytes memory
) external returns (bytes4) {
return 0xf23a6e61; // bytes4(keccak256("onERC1155Received(address,address,uint256,uint256,bytes)"))
}
/**
* @dev ERC1155 token receiver
*/
function onERC1155BatchReceived(
address,
address,
uint256[] calldata,
uint256[] calldata,
bytes calldata
) external returns (bytes4) {
return 0xbc197c81; // bytes4(keccak256("onERC1155BatchReceived(address,address,uint256[],uint256[],bytes)"))
}
}
contract InstaDefaultImplementation is Record {
constructor(address _instaIndex) public Record(_instaIndex) {}
receive() external payable {}
}

View File

@ -0,0 +1,6 @@
pragma solidity ^0.7.0;
contract Variables {
// Auth Module(Address of Auth => bool).
mapping (address => bool) internal _auth;
}

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,127 @@
const { expect } = require("chai");
const hre = require("hardhat");
const { web3, deployments, waffle, ethers } = hre;
const { provider, deployContract } = waffle
const {abi: implementationsABI} = require("../../scripts/constant/abi/core/InstaImplementations.json")
const deployAndEnableConnector = require("../../scripts/deployAndEnableConnector.js")
const buildDSAv2 = require("../../scripts/buildDSAv2")
const encodeSpells = require("../../scripts/encodeSpells.js")
const getMasterSigner = require("../../scripts/getMasterSigner")
const addresses = require("../../scripts/constant/addresses");
const abis = require("../../scripts/constant/abis");
const constants = require("../../scripts/constant/constant");
const tokens = require("../../scripts/constant/tokens");
const connectV2BasicERC1155Artifacts = require("../../artifacts/contracts/mainnet/connectors/basic-ERC1155/main.sol/ConnectV2BasicERC1155.json")
const erc1155Artifacts = require("../../artifacts/@openzeppelin/contracts/token/ERC1155/IERC1155.sol/IERC1155.json")
const TOKEN_CONTRACT_ADDR = "0x1ca3262009b21F944e6b92a2a88D039D06F1acFa";
const TOKEN_OWNER_ADDR = "0x1ca3262009b21F944e6b92a2a88D039D06F1acFa";
const TOKEN_ID = "1";
const implementationsMappingAddr = "0xCBA828153d3a85b30B5b912e1f2daCac5816aE9D"
describe("BASIC-ERC1155", function () {
const connectorName = "BASIC-ERC1155-A"
let dsaWallet0
let masterSigner;
let instaConnectorsV2;
let connector;
let nftContract;
let tokenOwner;
let instaImplementationsMapping;
const wallets = provider.getWallets()
const [wallet0, wallet1, wallet2, wallet3] = wallets
before(async () => {
await hre.network.provider.request({
method: "hardhat_impersonateAccount",
params: [TOKEN_OWNER_ADDR],
});
await network.provider.send("hardhat_setBalance", [
TOKEN_OWNER_ADDR,
"0x1000000000000000",
]);
// get tokenOwner
tokenOwner = await ethers.getSigner(
TOKEN_OWNER_ADDR
);
nftContract = await ethers.getContractAt(erc1155Artifacts.abi, TOKEN_CONTRACT_ADDR)
masterSigner = await getMasterSigner(wallet3)
instaConnectorsV2 = await ethers.getContractAt(abis.core.connectorsV2, addresses.core.connectorsV2);
instaImplementationsMapping = await ethers.getContractAt(implementationsABI, implementationsMappingAddr);
InstaAccountV2DefaultImpl = await ethers.getContractFactory("InstaDefaultImplementation")
instaAccountV2DefaultImpl = await InstaAccountV2DefaultImpl.deploy(addresses.core.instaIndex);
await instaAccountV2DefaultImpl.deployed()
connector = await deployAndEnableConnector({
connectorName,
contractArtifact: connectV2BasicERC1155Artifacts,
signer: masterSigner,
connectors: instaConnectorsV2
})
console.log("Connector address", connector.address)
})
it("Should have contracts deployed.", async function () {
expect(!!instaConnectorsV2.address).to.be.true;
expect(!!connector.address).to.be.true;
expect(!!masterSigner.address).to.be.true;
});
describe("Implementations", function () {
it("Should add default implementation to mapping.", async function () {
const tx = await instaImplementationsMapping.connect(masterSigner).setDefaultImplementation(instaAccountV2DefaultImpl.address);
await tx.wait()
expect(await instaImplementationsMapping.defaultImplementation()).to.be.equal(instaAccountV2DefaultImpl.address);
});
});
describe("DSA wallet setup", function () {
it("Should build DSA v2", async function () {
dsaWallet0 = await buildDSAv2(tokenOwner.address)
expect(!!dsaWallet0.address).to.be.true;
});
it("Deposit ETH into DSA wallet", async function () {
await wallet0.sendTransaction({
to: dsaWallet0.address,
value: ethers.utils.parseEther("10")
});
expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.gte(ethers.utils.parseEther("10"));
});
});
describe("Main", function () {
it("should deposit successfully", async () => {
console.log("DSA wallet address", dsaWallet0.address)
await nftContract.connect(tokenOwner).setApprovalForAll(dsaWallet0.address, true);
const spells = [
{
connector: connectorName,
method: "depositERC1155",
args: [
TOKEN_CONTRACT_ADDR,
TOKEN_ID,
1,
"0",
"0"
]
}
];
const tx = await dsaWallet0
.connect(tokenOwner)
.cast(...encodeSpells(spells), tokenOwner.address);
const receipt = await tx.wait();
});
})
})

View File

@ -0,0 +1,126 @@
const { expect } = require("chai");
const hre = require("hardhat");
const { web3, deployments, waffle, ethers } = hre;
const { provider, deployContract } = waffle
const {abi: implementationsABI} = require("../../scripts/constant/abi/core/InstaImplementations.json")
const deployAndEnableConnector = require("../../scripts/deployAndEnableConnector.js")
const buildDSAv2 = require("../../scripts/buildDSAv2")
const encodeSpells = require("../../scripts/encodeSpells.js")
const getMasterSigner = require("../../scripts/getMasterSigner")
const addresses = require("../../scripts/constant/addresses");
const abis = require("../../scripts/constant/abis");
const constants = require("../../scripts/constant/constant");
const tokens = require("../../scripts/constant/tokens");
const connectV2BasicERC721Artifacts = require("../../artifacts/contracts/mainnet/connectors/basic-ERC721/main.sol/ConnectV2BasicERC721.json")
const erc721Artifacts = require("../../artifacts/@openzeppelin/contracts/token/ERC721/IERC721.sol/IERC721.json")
const TOKEN_CONTRACT_ADDR = "0x4d695c615a7aacf2d7b9c481b66045bb2457dfde";
const TOKEN_OWNER_ADDR = "0x8c6b10d42ff08e56133fca0dac75e1931b1fcc23";
const TOKEN_ID = "38";
const implementationsMappingAddr = "0xCBA828153d3a85b30B5b912e1f2daCac5816aE9D"
describe("BASIC-ERC721", function () {
const connectorName = "BASIC-ERC721-A"
let dsaWallet0
let masterSigner;
let instaConnectorsV2;
let connector;
let nftContract;
let tokenOwner;
let instaImplementationsMapping;
const wallets = provider.getWallets()
const [wallet0, wallet1, wallet2, wallet3] = wallets
before(async () => {
await hre.network.provider.request({
method: "hardhat_impersonateAccount",
params: [TOKEN_OWNER_ADDR],
});
await network.provider.send("hardhat_setBalance", [
TOKEN_OWNER_ADDR,
"0x1000000000000000",
]);
// get tokenOwner
tokenOwner = await ethers.getSigner(
TOKEN_OWNER_ADDR
);
nftContract = await ethers.getContractAt(erc721Artifacts.abi, TOKEN_CONTRACT_ADDR)
masterSigner = await getMasterSigner(wallet3)
instaConnectorsV2 = await ethers.getContractAt(abis.core.connectorsV2, addresses.core.connectorsV2);
instaImplementationsMapping = await ethers.getContractAt(implementationsABI, implementationsMappingAddr);
InstaAccountV2DefaultImpl = await ethers.getContractFactory("InstaDefaultImplementation")
instaAccountV2DefaultImpl = await InstaAccountV2DefaultImpl.deploy(addresses.core.instaIndex);
await instaAccountV2DefaultImpl.deployed()
connector = await deployAndEnableConnector({
connectorName,
contractArtifact: connectV2BasicERC721Artifacts,
signer: masterSigner,
connectors: instaConnectorsV2
})
console.log("Connector address", connector.address)
})
it("Should have contracts deployed.", async function () {
expect(!!instaConnectorsV2.address).to.be.true;
expect(!!connector.address).to.be.true;
expect(!!masterSigner.address).to.be.true;
});
describe("Implementations", function () {
it("Should add default implementation to mapping.", async function () {
const tx = await instaImplementationsMapping.connect(masterSigner).setDefaultImplementation(instaAccountV2DefaultImpl.address);
await tx.wait()
expect(await instaImplementationsMapping.defaultImplementation()).to.be.equal(instaAccountV2DefaultImpl.address);
});
});
describe("DSA wallet setup", function () {
it("Should build DSA v2", async function () {
dsaWallet0 = await buildDSAv2(tokenOwner.address)
expect(!!dsaWallet0.address).to.be.true;
});
it("Deposit ETH into DSA wallet", async function () {
await wallet0.sendTransaction({
to: dsaWallet0.address,
value: ethers.utils.parseEther("10")
});
expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.gte(ethers.utils.parseEther("10"));
});
});
describe("Main", function () {
it("should deposit successfully", async () => {
console.log("DSA wallet address", dsaWallet0.address)
await nftContract.connect(tokenOwner).setApprovalForAll(dsaWallet0.address, true);
const spells = [
{
connector: connectorName,
method: "depositERC721",
args: [
TOKEN_CONTRACT_ADDR,
TOKEN_ID,
"0",
"0"
]
}
];
const tx = await dsaWallet0
.connect(tokenOwner)
.cast(...encodeSpells(spells), tokenOwner.address);
const receipt = await tx.wait();
});
})
})