Skip to content

feat: add script to un-nest strategies #514

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions scripts/unest_strategy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import 'dotenv/config';
import db from '../src/helpers/mysql';

const STRATEGY_NAME = 'multichain';

// Usage: yarn ts-node scripts/unest_strategy.ts
async function main() {
const spaces = await db.queryAsync(
`SELECT settings->'$.strategies[*].name', id, settings
FROM spaces
WHERE JSON_CONTAINS(settings->'$.strategies[*].name', ?)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if someone is using multichain inside delegation related strategies, you know the query?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we have around 20 spaces using it in innes strategies. This PR will only target top level strategies

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dang, whats the query?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure, they are the leftover after running the script

Copy link
Member

@ChaituVR ChaituVR Mar 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You see any multichain inside multichain? I mean multichain left overs after running the script

Copy link
Contributor Author

@wa0x6e wa0x6e Mar 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it's inside other strategies, like Math

`,
JSON.stringify([STRATEGY_NAME])
);

console.log('Retrieved spaces count:', spaces.length);

for (const space of spaces) {
const strategies = JSON.parse(space.settings).strategies;
let strategiesWithoutNesting = strategies.filter((s: any) => s.name !== STRATEGY_NAME);
const nestedStrategies = strategies.filter((s: any) => s.name === STRATEGY_NAME);

nestedStrategies.forEach((strategy: any) => {
strategiesWithoutNesting = strategiesWithoutNesting.concat(strategy.params?.strategies ?? []);
});

await db.queryAsync(
`UPDATE spaces SET settings = JSON_SET(settings, '$.strategies', ?) WHERE id = ?`,
[JSON.stringify(strategiesWithoutNesting), space.id]
);
}

console.log('Done! ✅');
}

(async () => {
try {
await main();
process.exit(0);
} catch (e) {
console.error(e);
process.exit(1);
}
})();