Identify Safe addresses

This commit is contained in:
Alexis Sellier
2021-06-03 15:42:28 +02:00
parent 8d91e480c5
commit ef1c285712
4 changed files with 48 additions and 0 deletions
+35
View File
@@ -7,9 +7,16 @@ import { assert } from '@app/error';
export enum AddressType {
Contract,
Org,
Safe,
EOA,
}
export interface Safe {
address: string
owners: string[]
threshold: number
}
export function formatBalance(n: BigNumber) {
return ethers.utils.commify(parseFloat(ethers.utils.formatUnits(n)).toFixed(2));
}
@@ -127,6 +134,11 @@ export function formatProjectHash(hash: Uint8Array, format: number): string {
// Identify an address by checking whether it's a contract or an externally-owned address.
export async function identifyAddress(address: string, config: Config): Promise<AddressType> {
let safe = await getSafe(address, config);
if (safe) {
return AddressType.Safe;
}
let code = await config.provider.getCode(address);
let bytes = ethers.utils.arrayify(code);
@@ -143,3 +155,26 @@ export async function identifyAddress(address: string, config: Config): Promise<
export async function resolveLabel(label: string, config: Config): Promise<string | null> {
return config.provider.resolveName(`${label}.${config.registrar.domain}`);
}
// Get a Gnosis Safe at an address.
export async function getSafe(address: string, config: Config): Promise<Safe | null> {
if (! config.safe.api) return null;
const response = await fetch(`${config.safe.api}/safes/${address}`, {
method: 'GET',
headers: {
'Accept': 'application/json',
}
});
if (! response.ok) {
return null;
}
const json = await response.json();
return {
address: json.address,
owners: json.owners,
threshold: json.threshold
};
}