Skip to content

Commit

Permalink
sdk/evm: simplify getting the contract.
Browse files Browse the repository at this point in the history
  • Loading branch information
panoel committed Dec 17, 2024
1 parent 3e105d1 commit 497ba4a
Showing 1 changed file with 38 additions and 86 deletions.
124 changes: 38 additions & 86 deletions evm/sdk/ts/src/platforms/evm/src/admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,84 +9,35 @@ import {
export async function claimAdmin(
contractAddress: string,
signer: ethers.Signer,
withExecutor: boolean = true,
): Promise<void> {
let contract: WormholeGuardiansAdapter | WormholeGuardiansAdapterWithExecutor;
if (withExecutor) {
contract = WormholeGuardiansAdapterWithExecutor__factory.connect(
contractAddress,
signer,
);
} else {
contract = WormholeGuardiansAdapter__factory.connect(
contractAddress,
signer,
);
}
let contract = await getContract(contractAddress, signer);
const tx = await contract.claimAdmin();
await tx.wait();
}

export async function discardAdmin(
contractAddress: string,
signer: ethers.Signer,
withExecutor: boolean = true,
): Promise<void> {
let contract: WormholeGuardiansAdapter | WormholeGuardiansAdapterWithExecutor;
if (withExecutor) {
contract = WormholeGuardiansAdapterWithExecutor__factory.connect(
contractAddress,
signer,
);
} else {
contract = WormholeGuardiansAdapter__factory.connect(
contractAddress,
signer,
);
}
let contract = await getContract(contractAddress, signer);
const tx = await contract.discardAdmin();
await tx.wait();
}

export async function pendingAdmin(
contractAddress: string,
signer: ethers.Signer,
withExecutor: boolean = true,
): Promise<string> {
let contract: WormholeGuardiansAdapter | WormholeGuardiansAdapterWithExecutor;
if (withExecutor) {
contract = WormholeGuardiansAdapterWithExecutor__factory.connect(
contractAddress,
signer,
);
} else {
contract = WormholeGuardiansAdapter__factory.connect(
contractAddress,
signer,
);
}
const result: string = await contract.pendingAdmin();
return result;
let contract = await getContract(contractAddress, signer);
return await contract.pendingAdmin();
}

export async function transferAdmin(
contractAddress: string,
signer: ethers.Signer,
newAdmin: string,
withExecutor: boolean = true,
): Promise<void> {
let contract: WormholeGuardiansAdapter | WormholeGuardiansAdapterWithExecutor;
if (withExecutor) {
contract = WormholeGuardiansAdapterWithExecutor__factory.connect(
contractAddress,
signer,
);
} else {
contract = WormholeGuardiansAdapter__factory.connect(
contractAddress,
signer,
);
}
let contract = await getContract(contractAddress, signer);
const tx = await contract.transferAdmin(newAdmin);
await tx.wait();
}
Expand All @@ -95,53 +46,54 @@ export async function updateAdmin(
contractAddress: string,
signer: ethers.Signer,
newAdmin: string,
withExecutor: boolean = true,
): Promise<void> {
let contract: WormholeGuardiansAdapter | WormholeGuardiansAdapterWithExecutor;
if (withExecutor) {
contract = WormholeGuardiansAdapterWithExecutor__factory.connect(
contractAddress,
signer,
);
} else {
contract = WormholeGuardiansAdapter__factory.connect(
contractAddress,
signer,
);
}
let contract = await getContract(contractAddress, signer);
const tx = await contract.updateAdmin(newAdmin);
await tx.wait();
}

export async function getAdapterType(
contractAddress: string,
signer: ethers.Signer,
withExecutor: boolean = true,
): Promise<string> {
let contract: WormholeGuardiansAdapter | WormholeGuardiansAdapterWithExecutor;
if (withExecutor) {
contract = WormholeGuardiansAdapterWithExecutor__factory.connect(
contractAddress,
signer,
);
} else {
contract = WormholeGuardiansAdapter__factory.connect(
contractAddress,
signer,
);
}
const result: string = await contract.getAdapterType();
return result;
let contract = await getContract(contractAddress, signer);
return await contract.getAdapterType();
}

export async function amIExecutor(
contractAddress: string,
signer: ethers.Signer,
): Promise<boolean> {
const type: string = await getAdapterType(contractAddress, signer);
// TODO: See if this version string is somewhere in a const
if (type === "WormholeGuardiansAdapterWithExecutor-0.0.1") {
return true;
try {
const type: string = await getAdapterType(contractAddress, signer);
// TODO: See if this version string is somewhere in a const
if (type === "WormholeGuardiansAdapterWithExecutor-0.0.1") {
return true;
}
} catch (e) {
console.log("Error getting adapter type", e);
}
return false;
}

async function getContract(
contractAddress: string,
signer: ethers.Signer,
): Promise<WormholeGuardiansAdapter | WormholeGuardiansAdapterWithExecutor> {
try {
return WormholeGuardiansAdapterWithExecutor__factory.connect(
contractAddress,
signer,
);
} catch (e) {
console.log("Error connecting to contract with executor", e);
}
try {
return WormholeGuardiansAdapter__factory.connect(contractAddress, signer);
} catch (e) {
console.log("Error connecting to contract without executor", e);
}
throw new Error(
`Contract address ${contractAddress} is not a WormholeGuardiansAdapter or WormholeGuardiansAdapterWithExecutor contract`,
);
}

0 comments on commit 497ba4a

Please sign in to comment.