Skip to content

Commit

Permalink
Merge pull request rhinestonewtf#91 from rhinestonewtf/feature/update…
Browse files Browse the repository at this point in the history
…-indexing

feat: update indexing logic
  • Loading branch information
kopy-kat authored Oct 24, 2024
2 parents 392044b + 38fbfaa commit a223beb
Show file tree
Hide file tree
Showing 13 changed files with 201 additions and 51 deletions.
49 changes: 43 additions & 6 deletions src/account/erc7579-implementation/api/getInstalledModules.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Address, PublicClient } from 'viem'
import { Address, parseAbi, PublicClient, zeroAddress } from 'viem'
import { Account } from '../../types'

import { getInstalledModules as getInstalledModulesQuery } from '../../../common/queries/account'
import { accountAbi } from '../constants/abis'
import { SENTINEL_ADDRESS } from '../../../common/constants'

export const getInstalledModules = async ({
account,
Expand All @@ -10,8 +11,44 @@ export const getInstalledModules = async ({
account: Account
client: PublicClient
}): Promise<Address[]> => {
return getInstalledModulesQuery({
account,
client,
})
let installedModules: Address[] = []
try {
installedModules = await getInstalledModulesQuery({
account,
client,
})
} catch (e) {}
if (installedModules.length === 0) {
const installedValidators = (await client.readContract({
address: account.address,
abi: parseAbi(accountAbi),
functionName: 'getValidatorsPaginated',
args: [SENTINEL_ADDRESS, 100],
})) as Address[]
for (const validator of installedValidators) {
installedModules.push(validator)
}

const installedExecutors = (await client.readContract({
address: account.address,
abi: parseAbi(accountAbi),
functionName: 'getExecutorsPaginated',
args: [SENTINEL_ADDRESS, 100],
})) as Address[]

for (const executor of installedExecutors) {
installedModules.push(executor)
}

const installedHook = (await client.readContract({
address: account.address,
abi: parseAbi(accountAbi),
functionName: 'getActiveHook',
})) as Address

if (installedHook != zeroAddress) {
installedModules.push(installedHook)
}
}
return installedModules
}
2 changes: 1 addition & 1 deletion src/account/erc7579-implementation/constants/abis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const bootstrapAbi = [

export const accountAbi = [
'function getActiveHook() external view returns (address hook)',
'function getValidatorPaginated(address cursor,uint256 size) returns (address[] memory, address)',
'function getValidatorsPaginated(address cursor,uint256 size) returns (address[] memory, address)',
'function getExecutorsPaginated(address cursor,uint256 size) returns (address[] memory, address)',
'function installModule(uint256 moduleTypeId,address module,bytes calldata initData)',
'function uninstallModule(uint256 moduleTypeId,address module,bytes calldata deInitData)',
Expand Down
49 changes: 43 additions & 6 deletions src/account/nexus/api/getInstalledModules.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Address, PublicClient } from 'viem'
import { Address, parseAbi, PublicClient, zeroAddress } from 'viem'
import { Account } from '../../types'

import { getInstalledModules as getInstalledModulesQuery } from '../../../common/queries/account'
import { accountAbi } from '../constants/abis'
import { SENTINEL_ADDRESS } from '../../../common/constants'

export const getInstalledModules = async ({
account,
Expand All @@ -10,8 +11,44 @@ export const getInstalledModules = async ({
account: Account
client: PublicClient
}): Promise<Address[]> => {
return getInstalledModulesQuery({
account,
client,
})
let installedModules: Address[] = []
try {
installedModules = await getInstalledModulesQuery({
account,
client,
})
} catch (e) {}
if (installedModules.length === 0) {
const installedValidators = (await client.readContract({
address: account.address,
abi: parseAbi(accountAbi),
functionName: 'getValidatorsPaginated',
args: [SENTINEL_ADDRESS, 100],
})) as Address[]
for (const validator of installedValidators) {
installedModules.push(validator)
}

const installedExecutors = (await client.readContract({
address: account.address,
abi: parseAbi(accountAbi),
functionName: 'getExecutorsPaginated',
args: [SENTINEL_ADDRESS, 100],
})) as Address[]

for (const executor of installedExecutors) {
installedModules.push(executor)
}

const installedHook = (await client.readContract({
address: account.address,
abi: parseAbi(accountAbi),
functionName: 'getActiveHook',
})) as Address

if (installedHook != zeroAddress) {
installedModules.push(installedHook)
}
}
return installedModules
}
45 changes: 43 additions & 2 deletions src/account/safe/api/getInstalledModules.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Address, PublicClient } from 'viem'
import { Address, parseAbi, PublicClient, zeroAddress } from 'viem'
import { Account } from '../../types'
import { getInstalledModules as getInstalledModulesQuery } from '../../../common/queries'
import { accountAbi } from '../constants/abis'
import { SENTINEL_ADDRESS } from '../../../common/constants'

export const getInstalledModules = async ({
account,
Expand All @@ -9,5 +11,44 @@ export const getInstalledModules = async ({
account: Account
client: PublicClient
}): Promise<Address[]> => {
return getInstalledModulesQuery({ account, client })
let installedModules: Address[] = []
try {
installedModules = await getInstalledModulesQuery({
account,
client,
})
} catch (e) {}
if (installedModules.length === 0) {
const installedValidators = (await client.readContract({
address: account.address,
abi: parseAbi(accountAbi),
functionName: 'getValidatorsPaginated',
args: [SENTINEL_ADDRESS, 100],
})) as Address[]
for (const validator of installedValidators) {
installedModules.push(validator)
}

const installedExecutors = (await client.readContract({
address: account.address,
abi: parseAbi(accountAbi),
functionName: 'getExecutorsPaginated',
args: [SENTINEL_ADDRESS, 100],
})) as Address[]

for (const executor of installedExecutors) {
installedModules.push(executor)
}

const installedHook = (await client.readContract({
address: account.address,
abi: parseAbi(accountAbi),
functionName: 'getActiveHook',
})) as Address

if (installedHook != zeroAddress) {
installedModules.push(installedHook)
}
}
return installedModules
}
2 changes: 1 addition & 1 deletion src/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ import { Address } from 'viem'
export const SENTINEL_ADDRESS: Address =
'0x0000000000000000000000000000000000000001'

export const GRAPHQL_API_URL =
export const INDEXER_URL =
'https://indexer.bigdevenergy.link/c03b38d/v1/graphql'
4 changes: 2 additions & 2 deletions src/common/queries/account.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Address, PublicClient } from 'viem'
import { GRAPHQL_API_URL } from '../constants'
import { INDEXER_URL } from '../constants'
import { Account } from '../../account'

const query = `
Expand All @@ -23,7 +23,7 @@ export const getInstalledModules = async ({
chainId: await client.getChainId(),
}

const response = await fetch(GRAPHQL_API_URL, {
const response = await fetch(INDEXER_URL, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Expand Down
4 changes: 2 additions & 2 deletions src/common/queries/registry.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { PublicClient } from 'viem'
import { GRAPHQL_API_URL } from '../constants'
import { INDEXER_URL } from '../constants'

const query = `
query ($chainId: Int) {
Expand All @@ -19,7 +19,7 @@ export const getRegistryModules = async ({
chainId: await client.getChainId(),
}

const response = await fetch(GRAPHQL_API_URL, {
const response = await fetch(INDEXER_URL, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Expand Down
6 changes: 3 additions & 3 deletions src/common/queries/scheduledOrders.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Address, PublicClient } from 'viem'
import { GRAPHQL_API_URL } from '../constants'
import { INDEXER_URL } from '../constants'
import { Account } from '../../account'

const query = `
Expand Down Expand Up @@ -29,7 +29,7 @@ export const getScheduledOrders = async ({
chainId: client.chain?.id,
}

const response = await fetch(GRAPHQL_API_URL, {
const response = await fetch(INDEXER_URL, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Expand Down Expand Up @@ -81,7 +81,7 @@ export const getScheduledOrderByJobId = async ({
chainId: await client.getChainId(),
}

const response = await fetch(GRAPHQL_API_URL, {
const response = await fetch(INDEXER_URL, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Expand Down
6 changes: 3 additions & 3 deletions src/common/queries/scheduledTransfers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Address, PublicClient } from 'viem'
import { GRAPHQL_API_URL } from '../constants'
import { INDEXER_URL } from '../constants'
import { Account } from '../../account'

const query = `
Expand Down Expand Up @@ -29,7 +29,7 @@ export const getScheduledTransfers = async ({
chainId: await client.getChainId(),
}

const response = await fetch(GRAPHQL_API_URL, {
const response = await fetch(INDEXER_URL, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Expand Down Expand Up @@ -81,7 +81,7 @@ export const getScheduledTransferByJobId = async ({
chainId: await client.getChainId(),
}

const response = await fetch(GRAPHQL_API_URL, {
const response = await fetch(INDEXER_URL, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Expand Down
42 changes: 19 additions & 23 deletions src/module/auto-savings/usage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,36 +97,32 @@ export const getDeleteAutoSavingConfigAction = async ({
}
}

export const getAutoSaveAction = async ({
export const getAutoSaveAction = ({
token,
amountReceived,
}: {
token: Address
amountReceived: number
}): Promise<Execution> => {
}): Execution => {
const swapDetails = getSwapDetails()
try {
const data = encodeFunctionData({
functionName: 'autoSave',
abi,
args: [
token,
BigInt(amountReceived),
swapDetails.sqrtPriceLimitX96,
swapDetails.amountOutMin,
swapDetails.fee,
],
})
const data = encodeFunctionData({
functionName: 'autoSave',
abi,
args: [
token,
BigInt(amountReceived),
swapDetails.sqrtPriceLimitX96,
swapDetails.amountOutMin,
swapDetails.fee,
],
})

return {
to: AUTO_SAVINGS_ADDRESS,
target: AUTO_SAVINGS_ADDRESS,
value: BigInt(0),
callData: data,
data,
}
} catch {
throw new Error(`Failed to create autosave action for token ${token}`)
return {
to: AUTO_SAVINGS_ADDRESS,
target: AUTO_SAVINGS_ADDRESS,
value: BigInt(0),
callData: data,
data,
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { getAccount, getInstalledModules } from 'src'
import { getClient } from 'src'
import { MockAccountDeployed } from 'test/utils/mocks/account'
import { MockClient } from 'test/utils/mocks/client'

describe('Get installed modules', () => {
// Setup
const client = getClient(MockClient)
const account = getAccount(MockAccountDeployed)

it('Should return the installed modules', async () => {
const installedModules = await getInstalledModules({
client,
account,
})

expect(installedModules.length).toBeGreaterThan(0)
})
})
19 changes: 19 additions & 0 deletions test/unit/account/account-types/safe/getInstalledModules.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { getAccount, getInstalledModules } from 'src'
import { getClient } from 'src'
import { MockSafeAccountDeployed } from 'test/utils/mocks/account'
import { MockClient } from 'test/utils/mocks/client'

describe('Get installed modules', () => {
// Setup
const client = getClient(MockClient)
const account = getAccount(MockSafeAccountDeployed)

it('Should return the installed modules', async () => {
const installedModules = await getInstalledModules({
client,
account,
})

expect(installedModules.length).toBeGreaterThan(0)
})
})
5 changes: 3 additions & 2 deletions test/utils/mocks/account.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Account } from 'src/account/types'

export const MockAccountDeployed: Account = {
address: '0x7227dcfb0c5ec7a5f539f97b18be261c49687ed6',
// address: '0x7227dcfb0c5ec7a5f539f97b18be261c49687ed6',
address: '0x0579bCB9b3B9678A991F553531c27f4ea4863bE4',
deployedOnChains: [11155111],
type: 'erc7579-implementation',
}
Expand All @@ -13,7 +14,7 @@ export const MockKernelAccountDeployed: Account = {
}

export const MockSafeAccountDeployed: Account = {
address: '0xc2b17e73603dccc195118a36f3203134fd7985f5',
address: '0x2b22106d8Aade2C3E546749bC027D5a21195FfED',
deployedOnChains: [11155111],
type: 'safe',
}
Expand Down

0 comments on commit a223beb

Please sign in to comment.