Skip to content

Commit

Permalink
Fix hotspot onboard and update failing to parse when linking from oth…
Browse files Browse the repository at this point in the history
…er applications.
  • Loading branch information
matthewcarlreetz committed Aug 14, 2024
1 parent 4c90e38 commit f7add3e
Showing 1 changed file with 66 additions and 40 deletions.
106 changes: 66 additions & 40 deletions src/features/txnDelegation/useSolTxns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ import bs58 from 'bs58'
import { get, last } from 'lodash'
import { useCallback, useMemo, useRef, useState } from 'react'
import { useAsync } from 'react-async-hook'
import { IdlInstruction } from '@coral-xyz/anchor/dist/cjs/idl'
import { useSolana } from '../../solana/SolanaProvider'
import { getKeypair, getSolanaKeypair } from '../../storage/secureStorage'
import { submitSolana } from '../../utils/solanaUtils'
import { prettyPrintToConsole } from '../../utils/logger'

const ValidTxnKeys = [
'onboardIotHotspotV0',
Expand Down Expand Up @@ -92,19 +94,35 @@ const useSolTxns = ({
async ({
decodedInstruction,
instruction,
idlInstruction,
coder,
connection,
}: {
decodedInstruction: Instruction
instruction: web3.TransactionInstruction
coder: BorshInstructionCoder
connection: Connection
idlInstruction: IdlInstruction | undefined
}) => {
const formatted = coder.format(decodedInstruction, instruction.keys)
const keyToAssetAccount = formatted?.accounts.find(
({ name }) => name === 'Key To Asset',
)
if (!keyToAssetAccount) return {}
const rootInfo = get(decodedInstruction, 'data.args') as {
location: BN
elevation: number
gain: number
index: number
}

const wifiInfo = get(
decodedInstruction,
'data.args.deploymentInfo.wifiInfoV0',
) as { azimuth: number; elevation: number } | undefined

const accounts = idlInstruction?.accounts || []
const keyToAssetIndex = accounts.findIndex((a) => a.name === 'keyToAsset')
const keyToAssetAccount = instruction.keys[keyToAssetIndex]

if (!keyToAssetAccount) {
throw new Error('Failed to format instruction')
}

const sigs = await connection.getSignaturesForAddress(
new PublicKey(keyToAssetAccount.pubkey),
Expand Down Expand Up @@ -143,22 +161,15 @@ const useSolTxns = ({
}
})

const { location, elevation, gain } = get(
decodedInstruction,
'data.args',
) as {
location: BN
elevation: number
gain: number
index: number
}
prettyPrintToConsole(decodedInstruction)

return {
name: decodedInstruction.name || '',
location: rootInfo.location?.toString('hex'),
elevation: wifiInfo?.elevation || rootInfo.elevation,
gain: rootInfo.gain,
name: decodedInstruction.name,
gatewayAddress,
location: location?.toString('hex'),
elevation,
gain,
azimuth: wifiInfo?.azimuth,
}
},
[],
Expand All @@ -185,47 +196,48 @@ const useSolTxns = ({
async ({
decodedInstruction,
instruction,
idlInstruction,
connection,
coder,
}: {
coder: BorshInstructionCoder
decodedInstruction: Instruction
instruction: web3.TransactionInstruction
idlInstruction: IdlInstruction | undefined
connection: Connection
}) => {
const formatted = coder.format(decodedInstruction, instruction.keys)

const merkleTreeAccount = formatted?.accounts.find(
({ name }) => name === 'Merkle Tree',
)
if (!merkleTreeAccount) {
throw new Error('Failed to format instruction')
}

const { location, elevation, gain, index } = get(
decodedInstruction,
'data.args',
) as {
const rootInfo = get(decodedInstruction, 'data.args') as {
location: BN
elevation: number
gain: number
index: number
}

const wifiInfo = get(
decodedInstruction,
'data.args.deploymentInfo.wifiInfoV0',
) as { azimuth: number; elevation: number } | undefined

const accounts = idlInstruction?.accounts || []
const merkleIndex = accounts.findIndex((a) => a.name === 'merkleTree')
const merkleTreeAccount = instruction.keys[merkleIndex]

if (!merkleTreeAccount) {
throw new Error('Failed to format instruction')
}
const pubKey = await getLeafAssetId(
merkleTreeAccount.pubkey,
new BN(index),
new BN(rootInfo.index),
)
const asset = await getAsset(connection.rpcEndpoint, pubKey)

const gatewayAddress = await assetToAddress(asset)

return {
location: location?.toString('hex'),
elevation,
gain,
location: rootInfo.location?.toString('hex'),
elevation: wifiInfo?.elevation || rootInfo.elevation,
gain: rootInfo.gain,
name: decodedInstruction.name,
gatewayAddress,
azimuth: wifiInfo?.azimuth,
}
},
[assetToAddress],
Expand Down Expand Up @@ -324,17 +336,29 @@ const useSolTxns = ({

try {
const idl = await fetchIdl(instruction.programId)

const coder = new BorshInstructionCoder(idl)
const decodedInstruction = coder.decode(instruction.data)

if (!decodedInstruction) return {}

switch (decodedInstruction.name as ValidTxn) {
const name = decodedInstruction.name as ValidTxn
let idlInstruction: IdlInstruction | undefined
idl.instructions.every((i) => {
const found = i.name === name
if (found) {
idlInstruction = i
}
return !found
})

switch (name) {
case 'onboardIotHotspotV0':
case 'onboardMobileHotspotV0':
return await handleOnboard({
decodedInstruction,
instruction,
idlInstruction,
coder,
connection,
})
Expand All @@ -344,8 +368,8 @@ const useSolTxns = ({
return await handleUpdateMeta({
decodedInstruction,
instruction,
idlInstruction,
connection,
coder,
})
case 'transfer':
return await handleTransfer({
Expand All @@ -364,6 +388,7 @@ const useSolTxns = ({
})
}
} catch (e) {
console.error(e)
return {}
}
},
Expand Down Expand Up @@ -523,7 +548,8 @@ const useSolTxns = ({
return { newOwner: txn?.newOwner || '' }
}, [transactions])

const hasTransactions = useMemo(() => !!gatewayAddress, [gatewayAddress])
// const hasTransactions = useMemo(() => !!gatewayAddress, [gatewayAddress])
const hasTransactions = true

const submit = useCallback(async () => {
if (!anchorProvider) return
Expand Down

0 comments on commit f7add3e

Please sign in to comment.