smart-lookup/core/tasks.tsx

122 lines
3.3 KiB
TypeScript
Raw Normal View History

2022-06-09 18:10:35 +00:00
import { ethers } from "ethers";
import { VNode } from "vue";
import { gnosisSafeAbi, networkScanBaseUrl } from "./web3";
export type TaskCheckResponse = {
status: "success" | "error" | "warning";
metadata?: object;
};
export type TaskCheckFun = ({
address: string,
provider: any,
}) => PromiseLike<TaskCheckResponse>;
export type Task = {
description: string;
networkResults?: {
[network: string]: TaskCheckResponse;
};
renderMetadata?: ({ network: Network, metadata: object }) => VNode;
renderMetadataValue?: ({ network: Network, key: string, value: any }) => VNode;
statusStrategy?: "some" | "every";
check: TaskCheckFun;
};
export const tasks: Array<Task> = [
{
description: "This is an EOA",
async check({ address, provider }) {
const code = await provider.getCode(address);
if (code != "0x") {
return { status: "error" };
}
return { status: "success" };
},
},
{
2023-03-06 12:02:27 +00:00
description: "Not a smart contract address",
2022-06-09 18:10:35 +00:00
statusStrategy: "some",
async check({ address, provider }) {
const code = await provider.getCode(address);
2023-03-06 12:02:27 +00:00
if (code !== "0x") {
2022-06-09 18:10:35 +00:00
return { status: "error" };
}
return { status: "success" };
},
},
{
2023-03-06 12:02:27 +00:00
description: "Not a gnosis safe address",
2022-06-09 18:10:35 +00:00
statusStrategy: "some",
async check({ address, provider }) {
const contract = new ethers.Contract(address, gnosisSafeAbi, provider);
try {
const [owners, threshold] = await Promise.all([
contract.getOwners(),
contract.getThreshold(),
]);
return {
2023-03-06 12:02:27 +00:00
status: "error",
2022-06-09 18:10:35 +00:00
metadata: { owners, threshold: threshold.toString() },
};
} catch (error) {
2023-03-06 12:02:27 +00:00
return { status: "success" };
2022-06-09 18:10:35 +00:00
}
},
renderMetadataValue({ network, key, value }) {
if (key === "owners") {
return <div class="ml-8 sm:ml-10" >
<ul class="list-decimal">
{
2023-03-06 12:02:27 +00:00
value.map((owner : string) => <li>
2022-06-09 18:10:35 +00:00
<a
class="hover:underline"
target="_blank"
rel="noopener noreferrer"
href={`${networkScanBaseUrl[network]}address/${owner}`}>{owner}
</a>
</li>)
}
</ul>
</div>
}
return <span class='text-sm'> {String(value)} </span>
}
},
{
description: "This address has transactions",
statusStrategy: "some",
async check({ address, provider }) {
const count = await provider.getTransactionCount(address);
if (count === 0) {
return { status: "error" };
}
return { status: "success", metadata: { count } };
},
renderMetadata({ metadata }) {
return <span> {metadata.count} transaction(s) </span>
}
},
];