Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: error handling for register org and allow multiple init config #46

Merged
merged 2 commits into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/bright-cameras-carry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@verify-media/verify-client": patch
---

error handling for register org
56 changes: 40 additions & 16 deletions src/graph/protocol/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ import { getGraphContractAddress } from '../../constants'
import { fetchFromIPFS } from '../../storage/ipfs'
import { fetchFileFromPinata } from '../../read'
import { PinataConfig } from '../../storage/pinata/types'
import { withErrorHandlingGraph } from '../../utils/error/decode-ether-error'
import {
VerifyError,
withErrorHandlingGraph
} from '../../utils/error/decode-ether-error'
import { debugLogger } from '../../utils/logger'

/**
Expand Down Expand Up @@ -638,10 +641,12 @@ export const registerOrg = withErrorHandlingGraph(
'0x0000000000000000000000000000000000000000000000000000000000000000'
// create org node

let incrBy = 1

debugLogger().debug(`get nodes created for ${rootWalletAddress}`)

let nodesCreated = (
(await getNodesCreated(rootWalletAddress)).toNumber() + 1
(await getNodesCreated(rootWalletAddress)).toNumber() + incrBy
).toString()

debugLogger().debug(`nodesCreated ${nodesCreated}`)
Expand All @@ -653,12 +658,22 @@ export const registerOrg = withErrorHandlingGraph(

debugLogger().debug(`orgId ${orgId}`)

const orgTransaction = await createNode({
id: orgId,
parentId: ZeroHash,
nodeType: NodeType.ORG,
referenceOf: ZeroHash
})
let orgTransaction = null
try {
orgTransaction = await createNode({
id: orgId,
parentId: ZeroHash,
nodeType: NodeType.ORG,
referenceOf: ZeroHash
})
} catch (error) {
if ((error as VerifyError).data === '0xe63231f6') {
incrBy = incrBy + 1
debugLogger().debug('org node already exists')
} else {
throw error
}
}

//create og node
nodesCreated = (
Expand All @@ -674,21 +689,30 @@ export const registerOrg = withErrorHandlingGraph(

debugLogger().debug(`ogId ${ogId}`)

const ogTransaction = await createNode({
id: ogId,
parentId: orgId,
nodeType: NodeType.ORG,
referenceOf: ZeroHash
})
let ogTransaction = null
try {
ogTransaction = await createNode({
id: ogId,
parentId: orgId,
nodeType: NodeType.ORG,
referenceOf: ZeroHash
})
} catch (error) {
if ((error as VerifyError).data === '0xe63231f6') {
debugLogger().debug('og node already exists')
} else {
throw error
}
}

return {
org: {
id: orgId,
txnHash: orgTransaction.transactionHash
txnHash: orgTransaction?.transactionHash || ''
},
originalMaterial: {
id: ogId,
txnHash: ogTransaction.transactionHash
txnHash: ogTransaction?.transactionHash || ''
}
}
}
Expand Down
24 changes: 10 additions & 14 deletions src/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,18 @@ export const { init, getConfig, clearConfig, unset, set } = (() => {
throw new Error('stage can be either sandbox, testnet or mainnet')
}

if (!_config.stage) _config.stage = stage
_config.stage = stage

const pvtKey = config?.pvtKey || process.env.PVT_KEY || ''
if (!_config.pvtKey) _config.pvtKey = pvtKey
_config.pvtKey = pvtKey

const rpcUrl = config?.rpcUrl || process.env.RPC_URL || ''
if (!rpcUrl) {
throw new Error(
'rpcUrl cannot be empty, either set and env var RPC_URL or pass a value to this function'
)
}
if (!_config.rpcUrl) _config.rpcUrl = rpcUrl
_config.rpcUrl = rpcUrl

const chainId = config?.chainId || process.env.CHAIN_ID || 0
if (!chainId) {
Expand All @@ -79,33 +79,29 @@ export const { init, getConfig, clearConfig, unset, set } = (() => {
)
}
//convert chainId to number
if (!_config.chainId) _config.chainId = parseInt(chainId.toString())
_config.chainId = parseInt(chainId.toString())

const chain = config?.chain || process.env.CHAIN || ''
if (!chain) {
throw new Error(
'chain cannot be empty, either set and env var CHAIN or pass a value to this function'
)
}
if (!_config.chain) _config.chain = chain
_config.chain = chain

const walletExpiry =
config?.walletExpiryDays || process.env.WALLET_EXPIRY_DAYS || 1

if (!_config.walletExpiryDays)
_config.walletExpiryDays = Number(walletExpiry)
_config.walletExpiryDays = Number(walletExpiry)

const maxGasPrice = config?.maxGasPrice || process.env.MAX_GAS_PRICE || 0
if (!_config.maxGasPrice) _config.maxGasPrice = Number(maxGasPrice)
_config.maxGasPrice = Number(maxGasPrice)

const rootPvtKey = config?.rootPvtKey || process.env.ROOT_PVT_KEY || ''
if (!_config.rootPvtKey) _config.rootPvtKey = rootPvtKey
_config.rootPvtKey = rootPvtKey

if (!_config.contractAddress)
_config.contractAddress = getGraphContractAddress(stage)

if (!_config.identityContractAddress)
_config.identityContractAddress = getIdentityContractAddress(stage)
_config.contractAddress = getGraphContractAddress(stage)
_config.identityContractAddress = getIdentityContractAddress(stage)

setDebug(config?.debug || process.env.DEBUG === '1')

Expand Down
6 changes: 6 additions & 0 deletions src/utils/error/decode-ether-error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ interface NestedError {
message?: string
}

export type VerifyError = {
type: string
error: string
data: string
}

function extractErrorDataFromErrorObject(error: unknown): string {
let currentError: NestedError | undefined = error as NestedError
let errorData: string | undefined
Expand Down
Loading