Skip to content

Commit

Permalink
Merge pull request #12 from lidofinance/develop
Browse files Browse the repository at this point in the history
develop
  • Loading branch information
avsetsin authored Oct 4, 2023
2 parents 3b9c5d6 + 9a62689 commit d0173e0
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 40 deletions.
3 changes: 1 addition & 2 deletions programs/common/logs.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { getLatestBlock } from '@utils';
import { Command } from 'commander';
import { Contract, EventLog } from 'ethers';
import { Filter } from 'ethers/types/providers';
import { Contract, EventLog, Filter } from 'ethers';

export const addLogsCommands = (command: Command, contract: Contract) => {
command
Expand Down
9 changes: 9 additions & 0 deletions utils/authorized-call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,37 @@ import { encodeCallScript } from './scripts';
import { forwardVoteFromTm } from './voting';
import { contractCallTxWithConfirm } from './call-tx';
import { agentForward } from 'scripts/agent';
import { printTx } from './print-tx';

export const authorizedCall = async (contract: Contract, method: string, args: unknown[] = []) => {
printTx(contract, method, args);
const errors = [];

try {
const passed = await authorizedCallEOA(contract, method, args);
if (passed) return;
} catch (error) {
console.warn('direct call failed, trying to forward to the voting');
errors.push(error);
}

try {
const passed = await authorizedCallVoting(contract, method, args);
if (passed) return;
} catch (error) {
console.warn('call from voting failed');
errors.push(error);
}

try {
const passed = await authorizedCallAgent(contract, method, args);
if (passed) return;
} catch (error) {
console.warn('call from agent failed');
errors.push(error);
}

console.log(errors);
};

export const authorizedCallEOA = async (contract: Contract, method: string, args: unknown[] = []) => {
Expand Down
29 changes: 2 additions & 27 deletions utils/call-tx.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,14 @@
import chalk from 'chalk';
import { AbstractSigner, Contract, ContractTransaction, ContractTransactionResponse } from 'ethers';
import { Contract, ContractTransaction, ContractTransactionResponse } from 'ethers';
import { confirmTx } from './confirm-tx';
import { stringify } from './stringify';

export const contractCallTxWithConfirm = async (contract: Contract, method: string, args: unknown[]) => {
const confirmed = await contractCallConfirm(contract, method, args);
const confirmed = await confirmTx();
if (!confirmed) return null;

return await contractCallTx(contract, method, args);
};

export const contractCallConfirm = async (contract: Contract, method: string, args: unknown[]) => {
const provider = contract.runner?.provider;

if (!provider) {
throw new Error('Provider is not set');
}

if (!(contract.runner instanceof AbstractSigner)) {
throw new Error('Runner is not a signer');
}

const signer = contract.runner;
const from = await signer.getAddress();

const network = await provider.getNetwork();
const to = await contract.getAddress();

const parsedArgs = args.map((arg) => stringify(arg));
const call = `${method}(${parsedArgs})`;
const data = contract.interface.encodeFunctionData(method, args);

return confirmTx(network.name, from, to, call, data);
};

export const populateGasLimit = async (contract: Contract, method: string, argsWithOverrides: unknown[]) => {
const fragment = contract.interface.getFunction(method, argsWithOverrides);

Expand Down
12 changes: 1 addition & 11 deletions utils/confirm-tx.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
import chalk from 'chalk';
import prompts from 'prompts';

const title = chalk.gray;
const value = chalk.blue.bold;

export const confirmTx = async (network: string, from: string, to: string, call: string, data: string) => {
console.log(title('chain:'), value(network));
console.log(title(' from:'), value(from));
console.log(title(' to:'), value(to));
console.log(title(' call:'), value(call));
console.log(title(' data:'), value(data));

export const confirmTx = async () => {
const { confirm } = await prompts({
type: 'confirm',
name: 'confirm',
Expand Down
34 changes: 34 additions & 0 deletions utils/print-tx.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import chalk from 'chalk';
import { AbstractSigner, Contract } from 'ethers';
import { stringify } from './stringify';

const title = chalk.gray;
const value = chalk.blue.bold;

export const printTx = async (contract: Contract, method: string, args: unknown[] = []) => {
const provider = contract.runner?.provider;

if (!provider) {
throw new Error('Provider is not set');
}

if (!(contract.runner instanceof AbstractSigner)) {
throw new Error('Runner is not a signer');
}

const signer = contract.runner;
const from = await signer.getAddress();

const network = await provider.getNetwork();
const to = await contract.getAddress();

const parsedArgs = args.map((arg) => stringify(arg));
const call = `${method}(${parsedArgs})`;
const data = contract.interface.encodeFunctionData(method, args);

console.log(title('chain:'), value(network.name));
console.log(title(' from:'), value(from));
console.log(title(' to:'), value(to));
console.log(title(' call:'), value(call));
console.log(title(' data:'), value(data));
};

0 comments on commit d0173e0

Please sign in to comment.