diff --git a/.changeset/bright-cameras-carry.md b/.changeset/bright-cameras-carry.md new file mode 100644 index 0000000..5b3c769 --- /dev/null +++ b/.changeset/bright-cameras-carry.md @@ -0,0 +1,5 @@ +--- +"@verify-media/verify-client": patch +--- + +error handling for register org diff --git a/src/graph/protocol/index.ts b/src/graph/protocol/index.ts index b5a9405..66229b1 100644 --- a/src/graph/protocol/index.ts +++ b/src/graph/protocol/index.ts @@ -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' /** @@ -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}`) @@ -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 = ( @@ -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 || '' } } } diff --git a/src/utils/config.ts b/src/utils/config.ts index 61a4c18..789f236 100644 --- a/src/utils/config.ts +++ b/src/utils/config.ts @@ -59,10 +59,10 @@ 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) { @@ -70,7 +70,7 @@ export const { init, getConfig, clearConfig, unset, set } = (() => { '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) { @@ -79,7 +79,7 @@ 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) { @@ -87,25 +87,21 @@ export const { init, getConfig, clearConfig, unset, set } = (() => { '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') diff --git a/src/utils/error/decode-ether-error.ts b/src/utils/error/decode-ether-error.ts index 190a16d..be3e5ec 100644 --- a/src/utils/error/decode-ether-error.ts +++ b/src/utils/error/decode-ether-error.ts @@ -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