Skip to content

Commit

Permalink
feat: test implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Majorfi committed Mar 28, 2024
1 parent 4d85d90 commit 5471e3d
Show file tree
Hide file tree
Showing 6 changed files with 2,263 additions and 212 deletions.
66 changes: 66 additions & 0 deletions .github/scripts/push-to-ipfs.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import {FleekSdk, PersonalAccessTokenService} from '@fleekxyz/sdk';
import path from 'path';
import fs from 'fs';

const CHAIN_RECORD_NAME = 'k51qzi5uqu5dljyjy7wm6qdvoqrscpg7t4kjabdm3y8nawvjucpefnoxi25ko0'

const newAccessTokenService = new PersonalAccessTokenService({
personalAccessToken: process.env.PERSONAL_ACCESS_TOKEN,
projectId: process.env.PROJECT_ID,
})
const fleekSdk = new FleekSdk({accessTokenService: newAccessTokenService});

const uploadDirectoryToIPFS = async (path) => {
try {
const result = await fleekSdk.ipfs().addFromPath(path, {wrapWithDirectory: true});
return result
} catch (error) {
console.error(error)
}
}


const setIPNSRecord = async (cid, retry) => {
try {
console.log(`Setting IPNS record for ${cid}`)
const {id} = await fleekSdk.ipns().getRecord({name: CHAIN_RECORD_NAME});
await new Promise(resolve => setTimeout(resolve, 10000));
const record = await fleekSdk.ipns().publishRecord({id: id, hash: cid});
console.log(record)
return record
} catch (error) {
console.error(`Error setting IPNS record: ${error}`)
if (retry < 10) {
await new Promise(resolve => setTimeout(resolve, 10000));
retry += 1
await setIPNSRecord(cid, retry)
}
}
}

let cid;
const pathToData = path.join(process.cwd(), 'chains');
async function execute() {
const timestamp = new Date().getTime().toString()
const file = path.join(pathToData, '_info.json')
const infoJSON = {
timestamp: timestamp,
record: CHAIN_RECORD_NAME
}
fs.writeFileSync(file, JSON.stringify(infoJSON, null, 2))

const results = await uploadDirectoryToIPFS(pathToData)
if (results) {
for (const item of results) {
if (item.path === '') {
cid = item.cid
break;
}
}
}

await setIPNSRecord(cid.toString(), 0)
process.exit(0)
}

execute()
3 changes: 3 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ jobs:
- name: Build token lists
run: node ./.github/scripts/generate-lists.mjs

- name: Publish to IPFS
run: PERSONAL_ACCESS_TOKEN=${{ secrets.FLEEK_PERSONAL_ACCESS_TOKEN }} PROJECT_ID=${{ secrets.FLEEK_PROJECT_ID }} node ./.github/scripts/push-to-ipfs.mjs

- name: Commit files
run: |
git config --local user.email "41898282+github-actions[bot]@users.noreply.github.com"
Expand Down
4 changes: 4 additions & 0 deletions chains/_info.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"timestamp": "1711646116796",
"record": "k51qzi5uqu5dljyjy7wm6qdvoqrscpg7t4kjabdm3y8nawvjucpefnoxi25ko0"
}
Loading

0 comments on commit 5471e3d

Please sign in to comment.