Skip to content

Commit

Permalink
chore: Browser file management (#451)
Browse files Browse the repository at this point in the history
  • Loading branch information
rrr523 authored Dec 22, 2023
1 parent 17a0ef2 commit 1216a7d
Show file tree
Hide file tree
Showing 20 changed files with 2,152 additions and 44 deletions.
8 changes: 8 additions & 0 deletions examples/browser-file-management/.env.simple
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
NEXT_PUBLIC_GRPC_URL=https://gnfd-testnet-fullnode-tendermint-ap.bnbchain.org

NEXT_PUBLIC_GREENFIELD_RPC_URL=https://gnfd-testnet-fullnode-tendermint-ap.bnbchain.org
NEXT_PUBLIC_GREEN_CHAIN_ID=5600

# BSC End POINT
NEXT_PUBLIC_BSC_RPC_URL=https://data-seed-prebsc-1-s1.binance.org:8545
NEXT_PUBLIC_BSC_CHAIN_ID=97
35 changes: 35 additions & 0 deletions examples/browser-file-management/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# next.js
/.next/
/out/

# production
/build

# misc
.DS_Store
*.pem

# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# local env files
.env*.local

# vercel
.vercel

# typescript
*.tsbuildinfo
next-env.d.ts
7 changes: 7 additions & 0 deletions examples/browser-file-management/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Browser File Management

```bash
> cp .env.simple .env
```

And then fill your account info in `.env`.
37 changes: 37 additions & 0 deletions examples/browser-file-management/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const _getPublicEnv = (prefix) => {
const envs = process.env;
const res = {};

Object.keys(envs).forEach((k) => {
if (k.startsWith(prefix)) {
res[k] = envs[k];
}
});

return res;
};

/** @type {import('next').NextConfig} */
const nextConfig = {
reactStrictMode: false,
trailingSlash: true,
publicRuntimeConfig: {
..._getPublicEnv('NEXT_PUBLIC_'),
},
eslint: {
// Warning: This allows production builds to successfully complete even if
// your project has ESLint errors.
ignoreDuringBuilds: true,
},
webpack: (config) => {
config.resolve.fallback = {
fs: false,
net: false,
tls: false,
};

return config;
},
};

module.exports = nextConfig;
25 changes: 25 additions & 0 deletions examples/browser-file-management/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "browser-file-management",
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint"
},
"dependencies": {
"@bnb-chain/greenfield-js-sdk": "latest",
"@bnb-chain/reed-solomon": "latest",
"@rainbow-me/rainbowkit": "^1.3.0",
"@types/node": "20.6.0",
"@types/react": "18.2.21",
"@types/react-dom": "18.2.7",
"next": "13.4.19",
"react": "18.2.0",
"react-dom": "18.2.0",
"typescript": "5.2.2",
"viem": "^1.19.11",
"wagmi": "^1.4.8"
}
}
45 changes: 45 additions & 0 deletions examples/browser-file-management/src/client/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { GREEN_CHAIN_ID, GRPC_URL } from '@/config/env';
import { Client } from '@bnb-chain/greenfield-js-sdk';

export const client = Client.create(GRPC_URL, String(GREEN_CHAIN_ID), {
zkCryptoUrl: 'https://unpkg.com/@bnb-chain/[email protected]/dist/node/zk-crypto.wasm',
});

export const getSps = async () => {
const sps = await client.sp.getStorageProviders();
const finalSps = (sps ?? []).filter((v: any) => v.endpoint.includes('nodereal'));

return finalSps;
};

export const getAllSps = async () => {
const sps = await getSps();

return sps.map((sp) => {
return {
address: sp.operatorAddress,
endpoint: sp.endpoint,
name: sp.description?.moniker,
};
});
};

export const selectSp = async () => {
const finalSps = await getSps();

const selectIndex = Math.floor(Math.random() * finalSps.length);

const secondarySpAddresses = [
...finalSps.slice(0, selectIndex),
...finalSps.slice(selectIndex + 1),
].map((item) => item.operatorAddress);
const selectSpInfo = {
id: finalSps[selectIndex].id,
endpoint: finalSps[selectIndex].endpoint,
primarySpAddress: finalSps[selectIndex]?.operatorAddress,
sealAddress: finalSps[selectIndex].sealAddress,
secondarySpAddresses,
};

return selectSpInfo;
};
Loading

0 comments on commit 1216a7d

Please sign in to comment.