Initial commit with basic InstaIndex support

This commit is contained in:
Juan Manuel Rodriguez Defago 2020-05-20 17:31:12 -03:00
commit c7a72ab648
14 changed files with 3605 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
node_modules/
/generated
/build

1
README.md Normal file
View File

@ -0,0 +1 @@
## InstaDapp DeFi Smart Accounts

380
abis/InstaIndex.json Normal file
View File

@ -0,0 +1,380 @@
[
{
"anonymous": false,
"inputs": [
{
"indexed": false,
"internalType": "address",
"name": "sender",
"type": "address"
},
{
"indexed": true,
"internalType": "address",
"name": "owner",
"type": "address"
},
{
"indexed": true,
"internalType": "address",
"name": "account",
"type": "address"
},
{
"indexed": true,
"internalType": "address",
"name": "origin",
"type": "address"
}
],
"name": "LogAccountCreated",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "address",
"name": "_newAccount",
"type": "address"
},
{
"indexed": true,
"internalType": "address",
"name": "_connectors",
"type": "address"
},
{
"indexed": true,
"internalType": "address",
"name": "_check",
"type": "address"
}
],
"name": "LogNewAccount",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "uint256",
"name": "accountVersion",
"type": "uint256"
},
{
"indexed": true,
"internalType": "address",
"name": "check",
"type": "address"
}
],
"name": "LogNewCheck",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "address",
"name": "master",
"type": "address"
}
],
"name": "LogNewMaster",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "address",
"name": "master",
"type": "address"
}
],
"name": "LogUpdateMaster",
"type": "event"
},
{
"inputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"name": "account",
"outputs": [
{
"internalType": "address",
"name": "",
"type": "address"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "_newAccount",
"type": "address"
},
{
"internalType": "address",
"name": "_connectors",
"type": "address"
},
{
"internalType": "address",
"name": "_check",
"type": "address"
}
],
"name": "addNewAccount",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "_owner",
"type": "address"
},
{
"internalType": "uint256",
"name": "accountVersion",
"type": "uint256"
},
{
"internalType": "address",
"name": "_origin",
"type": "address"
}
],
"name": "build",
"outputs": [
{
"internalType": "address",
"name": "_account",
"type": "address"
}
],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "_owner",
"type": "address"
},
{
"internalType": "uint256",
"name": "accountVersion",
"type": "uint256"
},
{
"internalType": "address[]",
"name": "_targets",
"type": "address[]"
},
{
"internalType": "bytes[]",
"name": "_datas",
"type": "bytes[]"
},
{
"internalType": "address",
"name": "_origin",
"type": "address"
}
],
"name": "buildWithCast",
"outputs": [
{
"internalType": "address",
"name": "_account",
"type": "address"
}
],
"stateMutability": "payable",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint256",
"name": "accountVersion",
"type": "uint256"
},
{
"internalType": "address",
"name": "_newCheck",
"type": "address"
}
],
"name": "changeCheck",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "_newMaster",
"type": "address"
}
],
"name": "changeMaster",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"name": "check",
"outputs": [
{
"internalType": "address",
"name": "",
"type": "address"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"name": "connectors",
"outputs": [
{
"internalType": "address",
"name": "",
"type": "address"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint256",
"name": "version",
"type": "uint256"
},
{
"internalType": "address",
"name": "query",
"type": "address"
}
],
"name": "isClone",
"outputs": [
{
"internalType": "bool",
"name": "result",
"type": "bool"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "list",
"outputs": [
{
"internalType": "address",
"name": "",
"type": "address"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "master",
"outputs": [
{
"internalType": "address",
"name": "",
"type": "address"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "_master",
"type": "address"
},
{
"internalType": "address",
"name": "_list",
"type": "address"
},
{
"internalType": "address",
"name": "_account",
"type": "address"
},
{
"internalType": "address",
"name": "_connectors",
"type": "address"
}
],
"name": "setBasics",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [],
"name": "updateMaster",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [],
"name": "versionCount",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
}
]

18
package.json Normal file
View File

@ -0,0 +1,18 @@
{
"name": "instadapp-dsa",
"version": "0.1.0",
"private": true,
"scripts": {
"codegen": "graph codegen",
"build": "graph build",
"deploy": "graph deploy --node https://api.thegraph.com/deploy/ --ipfs https://api.thegraph.com/ipfs/ juanmardefago/dsa",
"deploy-dev": "graph deploy --node https://api.thegraph.com/deploy/ --ipfs https://api.thegraph.com/ipfs/ protofire/augur-v2-kovan",
"create-local": "graph create --node http://localhost:8020/ protofire/augur-v2-kovan",
"remove-local": "graph remove --node http://localhost:8020/ protofire/augur-v2-kovan",
"deploy-local": "graph deploy --node http://localhost:8020/ --ipfs http://localhost:5001 protofire/augur-v2-kovan"
},
"dependencies": {
"@graphprotocol/graph-cli": "0.18.0",
"@graphprotocol/graph-ts": "0.18.0"
}
}

38
schema.graphql Normal file
View File

@ -0,0 +1,38 @@
type User @entity {
id: ID!
smartAccountsOwned: [SmartAccount!]! @derivedFrom(field:"owner")
smartAccountsCreated: [SmartAccount!]! @derivedFrom(field:"creator")
}
type SmartAccount @entity {
id: ID!
owner: User!
creator: User!
origin: String!
}
type AccountModule @entity {
"ID is the account module version"
id: ID!
version: Int
connectors: String!
check: String!
instaIndex: InstaIndex!
}
type InstaIndex @entity {
id: ID!
master: String
accountModules: [AccountModule!]! @derivedFrom(field: "instaIndex")
}

7
src/index.ts Normal file
View File

@ -0,0 +1,7 @@
export {
handleLogAccountCreated,
handleLogNewAccount,
handleLogNewCheck,
handleLogNewMaster,
handleLogUpdateMaster
} from "./mappings/instaIndex";

View File

@ -0,0 +1,81 @@
import { SmartAccount, User } from "../../generated/schema";
import {
LogAccountCreated,
LogNewAccount,
LogNewCheck,
LogNewMaster,
LogUpdateMaster,
InstaIndex
} from "../../generated/InstaIndex/InstaIndex";
import {
getOrCreateAccountModule,
getOrCreateUser,
getOrCreateSmartAccount
} from '../utils/helpers'
// - event: LogAccountCreated(address,indexed address,indexed address,indexed address)
// handler: handleLogAccountCreated
// Creation of new smart account for user
// event LogAccountCreated(address sender, address indexed owner, address indexed account, address indexed origin);
export function handleLogAccountCreated(event: LogAccountCreated): void {
let smartAccount = getOrCreateSmartAccount(event.params.account.toHexString());
let owner = getOrCreateUser(event.params.owner.toHexString())
let sender = getOrCreateUser(event.params.sender.toHexString())
smartAccount.owner = owner.id;
smartAccount.sender = sender.id;
smartAccount.origin = event.params.origin.toHexString();
smartAccount.save();
}
// - event: LogNewAccount(indexed address,indexed address,indexed address)
// handler: handleLogNewAccount
// Creation of new "Account Module"
// emit LogNewAccount(_newAccount, _connectors, _check);
export function handleLogNewAccount(event: LogNewAccount): void {
// current account version has to be retrieved from the contract
let accountVersion = InstaIndex.bind(event.address).versionCount();
let accountModule = getOrCreateAccountModule(accountVersion.toString())
accountModule.address = event.params._newAccount.toHexString();
accountModule.connectors = event.params._connectors.toHexString();
accountModule.check = event.params._check.toHexString();
accountModule.save();
}
// - event: LogNewCheck(indexed uint256,indexed address)
// handler: handleLogNewCheck
export function handleLogNewCheck(event: LogNewCheck): void {
let accountModule = getOrCreateAccountModule(event.params.accountVersion.toString());
accountModule.check = event.params.check.toHexString();
accountModule.save();
}
// - event: LogNewMaster(indexed address)
// handler: handleLogNewMaster
export function handleLogNewMaster(event: LogNewMaster): void {
let index = getOrCreateInstaIndex();
index.master = event.params.master.toHexString();
index.save();
}
// - event: LogUpdateMaster(indexed address)
// handler: handleLogUpdateMaster
export function handleLogUpdateMaster(event: LogUpdateMaster): void {
let index = getOrCreateInstaIndex();
index.master = event.params.master.toHexString();
index.save();
}

6
src/utils/constants.ts Normal file
View File

@ -0,0 +1,6 @@
import { BigDecimal, BigInt } from "@graphprotocol/graph-ts";
export const ZERO_ADDRESS = "0x0000000000000000000000000000000000000000";
export let BIGINT_ZERO = BigInt.fromI32(0);
export let BIGINT_ONE = BigInt.fromI32(1);
export let BIGDECIMAL_ZERO = new BigDecimal(BIGINT_ZERO);

25
src/utils/decimals.ts Normal file
View File

@ -0,0 +1,25 @@
import { BigDecimal, BigInt } from '@graphprotocol/graph-ts'
export const DEFAULT_DECIMALS = 18
export function pow(base: BigDecimal, exponent: number): BigDecimal {
let result = base
if (exponent == 0) {
return BigDecimal.fromString('1')
}
for (let i = 2; i <= exponent; i++) {
result = result.times(base)
}
return result
}
export function toDecimal(value: BigInt, decimals: number = DEFAULT_DECIMALS): BigDecimal {
let precision = BigInt.fromI32(10)
.pow(<u8>decimals)
.toBigDecimal()
return value.divDecimal(precision)
}

View File

@ -0,0 +1,6 @@
export {
getOrCreateUser,
getOrCreateSmartAccount,
getOrCreateAccountModule,
getOrCreateInstaIndex
} from "./instaIndex";

View File

@ -0,0 +1,59 @@
import { User, SmartAccount, AccountModule, InstaIndex } from "../../../generated/schema";
export function getOrCreateUser(
id: String,
createIfNotFound: boolean = true,
save: boolean = true
): User {
let user = User.load(id);
if (user == null && createIfNotFound) {
user = new User(id);
if (save) {
user.save();
}
}
return user as User;
}
export function getOrCreateSmartAccount(
id: String,
createIfNotFound: boolean = true
): SmartAccount {
let smartAccount = SmartAccount.load(id);
if (smartAccount == null && createIfNotFound) {
smartAccount = new SmartAccount(id);
}
return smartAccount as SmartAccount;
}
export function getOrCreateAccountModule(
id: String,
createIfNotFound: boolean = true
): AccountModule {
let accountModule = AccountModule.load(id);
if (accountModule == null && createIfNotFound) {
accountModule = new AccountModule(id);
let instaIndex = getOrCreateInstaIndex();
accountModule.instaIndex = instaIndex.id;
}
return accountModule as AccountModule;
}
export function getOrCreateInstaIndex(): InstaIndex {
let index = InstaIndex.load("INDEX");
if (index == null && createIfNotFound) {
index = new InstaIndex("INDEX");
index.save();
}
return index as InstaIndex;
}

35
subgraph.yaml Normal file
View File

@ -0,0 +1,35 @@
specVersion: 0.0.2
description: Augur v2 subgraph
repository: https://github.com/protofire/augur-v2-subgraph
schema:
file: ./schema.graphql
dataSources:
- kind: ethereum/contract
name: InstaIndex
network: mainnet
source:
address: "0x2971adfa57b20e5a416ae5a708a8655a9c74f723"
abi: InstaIndex
startBlock: 9747241
mapping:
kind: ethereum/events
apiVersion: 0.0.4
language: wasm/assemblyscript
file: ./src/index.ts
entities:
- User
- SmartAccount
abis:
- name: InstaIndex
file: ./abis/InstaIndex.json
eventHandlers:
- event: LogAccountCreated(address,indexed address,indexed address,indexed address)
handler: handleLogAccountCreated
- event: LogNewAccount(indexed address,indexed address,indexed address)
handler: handleLogNewAccount
- event: LogNewCheck(indexed uint256,indexed address)
handler: handleLogNewCheck
- event: LogNewMaster(indexed address)
handler: handleLogNewMaster
- event: LogUpdateMaster(indexed address)
handler: handleLogUpdateMaster

3
tsconfig.json Normal file
View File

@ -0,0 +1,3 @@
{
"extends": "./node_modules/assemblyscript/src/tsconfig.json"
}

2943
yarn.lock Normal file

File diff suppressed because it is too large Load Diff