Skip to content

Commit

Permalink
read arweave link for proposal description (solana-labs#1797)
Browse files Browse the repository at this point in the history
  • Loading branch information
abrzezinski94 authored Aug 28, 2023
1 parent c1a9602 commit 9cbf5b9
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 8 deletions.
3 changes: 3 additions & 0 deletions actions/dryRunInstruction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
Expand Down
5 changes: 0 additions & 5 deletions components/explorer/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
44 changes: 44 additions & 0 deletions utils/arweave.ts
Original file line number Diff line number Diff line change
@@ -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
}
16 changes: 13 additions & 3 deletions utils/helpers.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
}
Expand Down

0 comments on commit 9cbf5b9

Please sign in to comment.