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 edge case with 1 abi type event #44

Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion src/types/docs-schemas/definitions.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -733,7 +733,7 @@
"title": "AbiType",
"anyOf": [
{ "title": "StringABI", "type": "string" },
{ "title": "ArrayABI", "type": "array" }
{ "title": "ArrayABI", "type": "array", "items": { "type": "string" } }
shahnami marked this conversation as resolved.
Show resolved Hide resolved
]
},
"safe-contracts": {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## items Type

`string`
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
## 1 Type

`array` ([ArrayABI](definitions-definitions-abitype-anyof-arrayabi.md))
`string[]`
2 changes: 1 addition & 1 deletion src/types/schemas/definitions.schema.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/types/types/resources.schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export type Address = string;
export type Network1 = SupportedNetwork | TenantNetwork;
export type AbiType = StringABI | ArrayABI;
export type StringABI = string;
export type ArrayABI = unknown[];
export type ArrayABI = string[];
export type DefenderID5 = string;
export type MonitorOrDefenderID = Monitor | DefenderID6;
export type Monitor = BlockMonitor | FortaMonitor;
Expand Down
16 changes: 13 additions & 3 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import {
} from '../types';
import { sanitise } from './sanitise';
import {
AbiType,
Action,
ActionOrDefenderID,
ActionSecrets,
Expand Down Expand Up @@ -317,6 +318,16 @@ const getDefenderContract = (
return contracts.find((a) => `${a.network}-${a.address}` === `${resource.network}-${resource.address}`);
};

const parseMonitorAbi = (abi: AbiType | undefined) => {
// Because the way AbiType is typed (string | string[]), a list with 1 string item is interpreted as a string rather than string[]
// Therefore, JSON.parse may fail if the string is not a valid JSON
try {
shahnami marked this conversation as resolved.
Show resolved Hide resolved
return abi && JSON.stringify(typeof abi === 'string' ? JSON.parse(abi) : abi);
} catch (e) {
return abi && JSON.stringify([abi]);
}
};

export const constructMonitor = (
context: Serverless,
resources: Resources,
Expand Down Expand Up @@ -353,9 +364,8 @@ export const constructMonitor = (
// otherwise getDefenderContract will return old values
const monitorContracts = monitor.contracts?.map((contract) => getDefenderContract(contract, contracts));
// if monitor.abi is defined, we use that over the first entry from monitorContracts by default
const monitorABI =
(monitor.abi && JSON.stringify(typeof monitor.abi === 'string' ? JSON.parse(monitor.abi) : monitor.abi)) ||
monitorContracts?.[0]?.abi;
const monitorABI = parseMonitorAbi(monitor.abi) || monitorContracts?.[0]?.abi;

// Pull addresses from either monitor.addresses or monitor.contracts
const monitorAddresses =
(monitorContracts &&
Expand Down
Loading