Skip to content

Commit

Permalink
update api calls to read from new API
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianscatularo committed Mar 20, 2024
1 parent 4ecc1de commit 01257a3
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 23 deletions.
11 changes: 8 additions & 3 deletions frontend/claim_sdk/claim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,20 @@ export type Ecosystem =
| 'evm'
| 'sui'
| 'aptos'
| 'cosmwasm'
| 'terra'
| 'osmosis'
| 'injective'
| 'algorand'
export const Ecosystems: Ecosystem[] = [
'discord',
'solana',
'evm',
'sui',
'aptos',
'cosmwasm',
'terra',
'osmosis',
'injective',
'algorand'
]

export class ClaimInfo {
Expand Down Expand Up @@ -53,7 +57,8 @@ export class ClaimInfo {
}
break
}
case 'cosmwasm': {
case 'osmosis':
case 'terra': {
identityStruct = {
cosmwasm: { address: this.identity },
}
Expand Down
1 change: 1 addition & 0 deletions frontend/claim_sdk/merkleTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const LEAF_PREFIX = Buffer.from('00', 'hex')
const NODE_PREFIX = Buffer.from('01', 'hex')
const NULL_PREFIX = Buffer.from('02', 'hex')

// The size of the hash output in bytes
export const HASH_SIZE = 20

export class MerkleTree {
Expand Down
66 changes: 46 additions & 20 deletions frontend/utils/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@ import { ClaimInfo, Ecosystem } from '../claim_sdk/claim'
import { HASH_SIZE } from '../claim_sdk/merkleTree'
import { PublicKey, VersionedTransaction } from '@solana/web3.js'
import { SignedMessage } from '../claim_sdk/ecosystems/signatures'
import { ECOSYSTEM_IDS } from './constants'

const MERKLE_PROOFS = process.env.NEXT_PUBLIC_MERKLE_PROOFS

function parseProof(proof: string) {
// TODO remove it, we should not have empty proofs and will fail if tahat happens
if (!proof || proof === '') return []
const buffer = Buffer.from(proof, 'hex')
const chunks = []

Expand All @@ -19,47 +24,67 @@ function parseProof(proof: string) {
return chunks
}

export function getAmountAndProofRoute(
ecosystem: Ecosystem,
identity: string
): string {
return `/api/grant/v1/amount_and_proof?ecosystem=${ecosystem}&identity=${identity}`
const getAmountAndProofRoute = (ecosystem: Ecosystem, identity: string): string[] => {
if (ecosystem === 'evm') {
return [
`${MERKLE_PROOFS}/${identity.toLowerCase()}_${ECOSYSTEM_IDS[ecosystem]}.json`,
`${MERKLE_PROOFS}/${identity.toUpperCase()}_${ECOSYSTEM_IDS[ecosystem]}.json`,
`${MERKLE_PROOFS}/${identity}_${ECOSYSTEM_IDS[ecosystem]}.json`
]
} else {
return [`${MERKLE_PROOFS}/${identity}_${ECOSYSTEM_IDS[ecosystem]}.json`]
}
}

// TODO refactor/remove
export function handleAmountAndProofResponse(
ecosystem: Ecosystem,
identity: string,
status: number,
data: any
{
address,
amount,
hashes,
}: any = {}
): { claimInfo: ClaimInfo; proofOfInclusion: Uint8Array[] } | undefined {
if (status == 404) return undefined
if (status == 200) {
return {
claimInfo: new ClaimInfo(ecosystem, identity, new BN(data.amount)),
proofOfInclusion: parseProof(data.proof),
if (identity === address) {
return {
claimInfo: new ClaimInfo(ecosystem, identity, new BN(amount)),
proofOfInclusion: parseProof(hashes),
}
}
}
}

// If the given identity is not eligible the value will be undefined
// Else the value contains the eligibility information
export type Eligibility =
| { claimInfo: ClaimInfo; proofOfInclusion: Uint8Array[] }
| undefined
export type Eligibility = { claimInfo: ClaimInfo; proofOfInclusion: Uint8Array[] }

export async function fetchAmountAndProof(
ecosystem: Ecosystem,
identity: string
): Promise<Eligibility> {
const response = await fetch(getAmountAndProofRoute(ecosystem, identity))
return handleAmountAndProofResponse(
ecosystem,
identity,
response.status,
await response.json()
)
): Promise<Eligibility | undefined> {
const files = getAmountAndProofRoute(ecosystem, identity)
// Iterate over each posible file name and return the first valid response
// The best case will be to have only one file per identity
for (const file of files) {
const response = await fetch(file)
if (response.headers.get('content-type') === 'application/json') {
const data = await response.json()
if (response.status === 200 && data.address === identity) {
return {
claimInfo: new ClaimInfo(ecosystem, identity, new BN(data.amount)),
proofOfInclusion: parseProof(data.hashes),
}
}
}
}
}

export function getDiscordSignedMessageRoute(claimant: PublicKey) {
// TODO update it with lambda route
return `/api/grant/v1/discord_signed_message?publicKey=${claimant.toBase58()}`
}

Expand Down Expand Up @@ -88,6 +113,7 @@ export async function fetchDiscordSignedMessage(
}

export function getFundTransactionRoute(): string {
// TODO update it with lambda route
return `/api/grant/v1/fund_transaction`
}

Expand Down
14 changes: 14 additions & 0 deletions frontend/utils/constants.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
import { Ecosystem } from "claim_sdk/claim"

// TODO remove it
export type SOLANA_SOURCES = 'nft' | 'defi'

export const ECOSYSTEM_IDS: Record<Ecosystem, number> = {
'solana': 1,
'evm': 2,
'terra': 3,
'algorand': 8,
'injective': 19,
'osmosis': 20,
'sui': 21,
'aptos': 22,
'discord': 14443,
} as const

export const EVM_CHAINS = [
'optimism-mainnet',
'arbitrum-mainnet',
Expand Down

0 comments on commit 01257a3

Please sign in to comment.