From 9cbf5b9063481512aa8dc6302bd1fbcbd595f9b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrian=20Brzezi=C5=84ski?= Date: Mon, 28 Aug 2023 19:03:19 +0200 Subject: [PATCH] read arweave link for proposal description (#1797) --- actions/dryRunInstruction.ts | 3 +++ components/explorer/tools.ts | 5 ---- utils/arweave.ts | 44 ++++++++++++++++++++++++++++++++++++ utils/helpers.ts | 16 ++++++++++--- 4 files changed, 60 insertions(+), 8 deletions(-) create mode 100644 utils/arweave.ts diff --git a/actions/dryRunInstruction.ts b/actions/dryRunInstruction.ts index 0edc09edb3..d19e84b8ff 100644 --- a/actions/dryRunInstruction.ts +++ b/actions/dryRunInstruction.ts @@ -15,7 +15,10 @@ export async function dryRunInstruction( prerequisiteInstructionsToRun?: TransactionInstruction[] | undefined, additionalInstructions?: InstructionData[] ) { + const recentBlockHash = await connection.getLatestBlockhash() const transaction = new Transaction({ feePayer: wallet.publicKey }) + transaction.lastValidBlockHeight = recentBlockHash.lastValidBlockHeight + transaction.recentBlockhash = recentBlockHash.blockhash transaction.add( ComputeBudgetProgram.setComputeUnitLimit({ units: 1_000_000 }) diff --git a/components/explorer/tools.ts b/components/explorer/tools.ts index 042ed2d4ea..d8e6c33690 100644 --- a/components/explorer/tools.ts +++ b/components/explorer/tools.ts @@ -45,11 +45,6 @@ export async function getExplorerInspectorUrl( base58.encode(s.signature ?? Buffer.alloc(SIGNATURE_LENGTH)) ) explorerUrl.searchParams.append('signatures', JSON.stringify(signatures)) - const recentBlockHash = await connection.current.getLatestBlockhash() - if (transaction instanceof Transaction) { - transaction.lastValidBlockHeight = recentBlockHash.lastValidBlockHeight - transaction.recentBlockhash = recentBlockHash.blockhash - } const message = transaction instanceof Transaction diff --git a/utils/arweave.ts b/utils/arweave.ts new file mode 100644 index 0000000000..43c0cd65be --- /dev/null +++ b/utils/arweave.ts @@ -0,0 +1,44 @@ +import axios from 'axios' + +const urlRegex = + // eslint-disable-next-line + /(https:\/\/)(arweave\.net\/)([\w-]{43})/ + +export const arweaveDescriptionApi = { + fetchArweaveFile: fetchArweaveFile, + cancel: function () { + if (this?.abortController) { + this.abortController.abort() + this.abortController = null + } + }, + abortController: null, +} +async function fetchArweaveFile(url: string) { + const controller = new AbortController() + if (typeof this !== 'undefined') { + this.abortController = controller + } + const pieces = url.match(urlRegex) + console.log(pieces) + if (pieces) { + console.log(pieces) + const idPiece = pieces[3] + if (idPiece) { + const apiUrl = 'https://arweave.net/' + idPiece + const apiResponse = await axios.get(apiUrl, { + signal: controller.signal, + }) + if (apiResponse.status === 200) { + return apiResponse.data.description + } else { + console.warn('could not arweave file', { + url, + apiResponse: apiResponse.data, + }) + } + } + } + + return undefined +} diff --git a/utils/helpers.ts b/utils/helpers.ts index e23514e350..29b35ae4be 100644 --- a/utils/helpers.ts +++ b/utils/helpers.ts @@ -1,5 +1,6 @@ import { AccountInfo, Connection, PublicKey } from '@solana/web3.js' import { gistApi } from './github' +import { arweaveDescriptionApi } from './arweave' export function capitalize(str?: string) { return str ? str?.charAt(0).toUpperCase() + str?.slice(1) : str @@ -24,10 +25,19 @@ export class SanitizedObject { export async function resolveProposalDescription(descriptionLink: string) { try { gistApi.cancel() + arweaveDescriptionApi.cancel() + let desc = '' const url = new URL(descriptionLink) - const desc = - (await gistApi.fetchGistFile(url.toString())) ?? descriptionLink - return desc + + if (url.toString().includes('gist')) { + desc = await gistApi.fetchGistFile(url.toString()) + } + + if (url.toString().includes('arweave')) { + desc = await arweaveDescriptionApi.fetchArweaveFile(url.toString()) + } + + return desc ? desc : descriptionLink } catch { return descriptionLink }