Skip to content

Commit

Permalink
prettier changes - scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
julianmrodri committed Jul 5, 2023
1 parent 451ba68 commit 7e93016
Show file tree
Hide file tree
Showing 10 changed files with 159 additions and 156 deletions.
2 changes: 1 addition & 1 deletion scripts/4bytes-syncced.json
Original file line number Diff line number Diff line change
Expand Up @@ -1123,4 +1123,4 @@
"NoToken(uint8)",
"WrongIndex(uint8)"
]
}
}
167 changes: 87 additions & 80 deletions scripts/4bytes.ts
Original file line number Diff line number Diff line change
@@ -1,98 +1,105 @@
import hre from 'hardhat'
import fs from 'fs'
import fetch from "isomorphic-fetch"
import previousSync from "./4bytes-syncced.json"
import fetch from 'isomorphic-fetch'
import previousSync from './4bytes-syncced.json'
/**
* This script will sync any event and function we have with www.4byte.directory
* The script saves all processed signatures with 4bytes-syncced.json as it succcesses
* this way we avoid syncing the same signature twice.
* */

const sleep = (ms: number) => new Promise(resolve => setTimeout(resolve, ms));
const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms))

async function main() {
const artifacts = await hre.artifacts.getAllFullyQualifiedNames();
const artifactsWithAbi = (await Promise.all(artifacts.map(name => hre.artifacts.readArtifact(name)))).filter(artifact => artifact.abi.length !== 0);
const prevFunctions = new Set<string>(previousSync.functions)
const prevEvents = new Set<string>(previousSync.events)
const newErrorSignatures = new Set<string>()
const newFunctionSignatures = new Set<string>()
const newEventSignatures = new Set<string>()
for (const { abi } of artifactsWithAbi) {
const abiInterface = new hre.ethers.utils.Interface(abi)
// Events and Errors seem to be the same thing for 4bytes
Object.keys(abiInterface.events).filter(e => !prevEvents.has(e)).forEach(e => newEventSignatures.add(e))
Object.keys(abiInterface.errors).filter(e => !prevEvents.has(e)).forEach(e => newEventSignatures.add(e))

Object.keys(abiInterface.functions).filter(e => !prevFunctions.has(e)).forEach(e => newFunctionSignatures.add(e))
}
const total = newErrorSignatures.size + newFunctionSignatures.size + newEventSignatures.size
if (total === 0) {
console.log("All up to date!")
return;
}
const artifacts = await hre.artifacts.getAllFullyQualifiedNames()
const artifactsWithAbi = (
await Promise.all(artifacts.map((name) => hre.artifacts.readArtifact(name)))
).filter((artifact) => artifact.abi.length !== 0)
const prevFunctions = new Set<string>(previousSync.functions)
const prevEvents = new Set<string>(previousSync.events)
const newErrorSignatures = new Set<string>()
const newFunctionSignatures = new Set<string>()
const newEventSignatures = new Set<string>()
for (const { abi } of artifactsWithAbi) {
const abiInterface = new hre.ethers.utils.Interface(abi)
// Events and Errors seem to be the same thing for 4bytes
Object.keys(abiInterface.events)
.filter((e) => !prevEvents.has(e))
.forEach((e) => newEventSignatures.add(e))
Object.keys(abiInterface.errors)
.filter((e) => !prevEvents.has(e))
.forEach((e) => newEventSignatures.add(e))

console.log("Will sync " + total + " signatures with 4bytes...")
Object.keys(abiInterface.functions)
.filter((e) => !prevFunctions.has(e))
.forEach((e) => newFunctionSignatures.add(e))
}
const total = newErrorSignatures.size + newFunctionSignatures.size + newEventSignatures.size
if (total === 0) {
console.log('All up to date!')
return
}

const save = () => {
fs.writeFileSync("./scripts/4bytes-syncced.json", JSON.stringify(previousSync, null, 2));
}
console.log("----- Synccing functions ----- ")
for (const sig of newFunctionSignatures) {
for (let i = 0; i < 3; i++) {
const resp = await fetch("https://www.4byte.directory/api/v1/signatures/", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
"text_signature": sig,
})
})
if (resp.status === 400 || resp.status === 201) {
console.log("function", sig, resp.status, await resp.text())
previousSync.functions.push(sig);
save()
break
}
if (i === 2) {
console.log("Failed to sync function", sig, "after 3 attempts")
} else {
await sleep(1000)
}
}
console.log('Will sync ' + total + ' signatures with 4bytes...')

}
console.log("----- Synccing events ----- ")
for (const sig of newEventSignatures) {
for (let i = 0; i < 3; i++) {
const resp = await fetch("https://www.4byte.directory/api/v1/event-signatures/", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
"text_signature": sig,
})
})
if (resp.status === 400 || resp.status === 201) {
console.log("event", sig, resp.status, await resp.text())
previousSync.events.push(sig);
save()
break
}
const save = () => {
fs.writeFileSync('./scripts/4bytes-syncced.json', JSON.stringify(previousSync, null, 2))
}
console.log('----- Synccing functions ----- ')
for (const sig of newFunctionSignatures) {
for (let i = 0; i < 3; i++) {
const resp = await fetch('https://www.4byte.directory/api/v1/signatures/', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
text_signature: sig,
}),
})
if (resp.status === 400 || resp.status === 201) {
console.log('function', sig, resp.status, await resp.text())
previousSync.functions.push(sig)
save()
break
}
if (i === 2) {
console.log('Failed to sync function', sig, 'after 3 attempts')
} else {
await sleep(1000)
}
}
}
console.log('----- Synccing events ----- ')
for (const sig of newEventSignatures) {
for (let i = 0; i < 3; i++) {
const resp = await fetch('https://www.4byte.directory/api/v1/event-signatures/', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
text_signature: sig,
}),
})
if (resp.status === 400 || resp.status === 201) {
console.log('event', sig, resp.status, await resp.text())
previousSync.events.push(sig)
save()
break
}

if (i === 2) {
console.log("Failed to sync event", sig, "after 3 attempts")
} else {
await sleep(1000)
}
}
}
console.log("Done!")
if (i === 2) {
console.log('Failed to sync event', sig, 'after 3 attempts')
} else {
await sleep(1000)
}
}
}
console.log('Done!')
}

main().catch((error) => {
console.error(error)
process.exitCode = 1
console.error(error)
process.exitCode = 1
})
32 changes: 14 additions & 18 deletions scripts/ci_backtest_plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ import { ethers } from 'hardhat'
import fs from 'fs'
import { backTestPlugin } from './backtester/backtester'

const htmlReportTemplate = fs.readFileSync("./scripts/backtester/report-template.html", "utf8")
const htmlReportTemplate = fs.readFileSync('./scripts/backtester/report-template.html', 'utf8')

export const main = async () => {
const provider = new providers.JsonRpcProvider(process.env.MAINNET_RPC_URL)

const currentBlock = await provider.getBlockNumber()
const stride = parseInt(process.env.STRIDE ?? '300', 10)
const numberOfSamples = parseInt(process.env.SAMPLES ?? '1000', 10)
const contractToTest = (await ethers.getContractFactory(process.env.CONTRACT_NAME!)).getDeployTransaction(
...JSON.parse(process.env.CONSTRUCTOR_PARAMETERS!)
)
const contractToTest = (
await ethers.getContractFactory(process.env.CONTRACT_NAME!)
).getDeployTransaction(...JSON.parse(process.env.CONSTRUCTOR_PARAMETERS!))

if (process.env.BACKTEST_RESULT_DIR != null) {
console.log('Will save results to ', process.env.BACKTEST_RESULT_DIR)
Expand All @@ -24,20 +24,16 @@ export const main = async () => {

const start = currentBlock - stride * numberOfSamples
const result = {
...(await backTestPlugin(
[
contractToTest.data!
],
{
...(
await backTestPlugin([contractToTest.data!], {
start,
stride,
numberOfSamples,
backtestServiceUrl: process.env.BACKTEST_SERVICE_URL!,
}
))[0],
backtestName: process.env.CONTRACT_NAME!
};

})
)[0],
backtestName: process.env.CONTRACT_NAME!,
}

if (process.env.BACKTEST_RESULT_DIR != null) {
console.log('Backtest done, saving results')
Expand All @@ -47,11 +43,11 @@ export const main = async () => {
JSON.stringify(result, null, 2)
)

const htmlReport = htmlReportTemplate.replace("const data = []", "const data = " + JSON.stringify([result], null, 2))
fs.writeFileSync(
`${process.env.BACKTEST_RESULT_DIR}/report.html`,
htmlReport
const htmlReport = htmlReportTemplate.replace(
'const data = []',
'const data = ' + JSON.stringify([result], null, 2)
)
fs.writeFileSync(`${process.env.BACKTEST_RESULT_DIR}/report.html`, htmlReport)
} else {
console.log(JSON.stringify(result, null, 2))
}
Expand Down
Loading

0 comments on commit 7e93016

Please sign in to comment.