Skip to content

Commit

Permalink
chore: fetch abi
Browse files Browse the repository at this point in the history
  • Loading branch information
mfw78 committed Aug 22, 2023
1 parent ada3598 commit d4e56eb
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
},
"types": "dist/index.d.ts",
"scripts": {
"prebuild": "rm -rf dist && yarn run codegen",
"prebuild": "rm -rf dist && rm -rf abi && yarn ts-node prepublish.ts && yarn run codegen",
"build": "microbundle -f modern,esm,cjs",
"start": "microbundle -f modern,esm,cjs watch",
"postbuild": "cp package.json dist && cp README.md dist",
Expand Down
48 changes: 48 additions & 0 deletions prepublish.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import * as fs from 'fs'
import * as https from 'https'
import * as path from 'path'

const UPSTREAM_COMMIT_HASH = '8c762559c98b707801f52dd070dd39ab9478b876'
const UPSTREAM_REPO = 'https://raw.githubusercontent.com/cowprotocol/composable-cow'
const ABI_TO_FETCH = ['ComposableCoW', 'ExtensibleFallbackHandler', 'TWAP']

const urlsToDownload = ABI_TO_FETCH.map((abi) => {
return `${UPSTREAM_REPO}/${UPSTREAM_COMMIT_HASH}/out/${abi}.sol/${abi}.json`
})

const abiDir = path.join(__dirname, 'abi')

if (!fs.existsSync(abiDir)) {
fs.mkdirSync(abiDir)
}

function downloadFile(url: string, targetPath: string): Promise<void> {
return new Promise<void>((resolve, reject) => {
const fileStream = fs.createWriteStream(targetPath)
const request = https.get(url, (response) => {
response.pipe(fileStream)
fileStream.on('finish', () => {
fileStream.close(() => resolve())
})
})

request.on('error', (error) => {
reject(error)
})
})
}

async function downloadAndMoveFiles(urls: string[]): Promise<void> {
for (const url of urls) {
const filename = path.basename(url)
const targetPath = path.join(abiDir, filename)
await downloadFile(url, targetPath)
console.log(`File ${filename} downloaded and saved successfully.`)
}
}

async function main() {
await downloadAndMoveFiles(urlsToDownload)
}

main()

0 comments on commit d4e56eb

Please sign in to comment.