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

Favor nonce from Safe Multisig api #75

Merged
merged 2 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 1 addition & 4 deletions src/tasks/selfSell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -899,10 +899,7 @@ const setupSelfSellTask: () => void = () =>
"blocknativeGasPrice",
"Use BlockNative gas price estimates for transactions.",
)
.addOptionalParam(
"toToken",
"All input tokens will be dumped to this token. If not specified, it defaults to the network's native token (e.g., ETH)",
)
.addParam("toToken", "All input tokens will be dumped to this token.")
.addFlag(
"safe",
"Whether the solver is a Safe and the script should propose the transaction to the Safe UI instead of sending a transaction directly",
Expand Down
25 changes: 24 additions & 1 deletion src/tasks/withdraw/safe.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import SafeApiKit from "@safe-global/api-kit";
import Safe, { EthersAdapter } from "@safe-global/protocol-kit";
import { SafeTransactionDataPartial } from "@safe-global/safe-core-sdk-types";
import { HardhatRuntimeEnvironment } from "hardhat/types";

interface Transaction {
Expand All @@ -18,22 +19,44 @@ function serviceUrlForNetwork(network: string): string {
}
}

interface SafesAddressNoncesOutput {
recommendedNonce: number;
}
// Once `@safe-global/api-kit` has been migrated from v1 to v2, this can be replaced with `getnextnonce`.
// <https://docs.safe.global/sdk-api-kit/reference#getnextnonce>
async function recommendedNonce(chainId: number, safeAddress: string) {
// <https://safe-client.safe.global/index.html#/safes/SafesController_getNonces>
const url = `https://safe-client.safe.global/v1/chains/${chainId.toString()}/safes/${safeAddress}/nonces`;
const response = await fetch(url);
const output: SafesAddressNoncesOutput = await response.json();
return output.recommendedNonce;
}

// Creates and proposes a transaction to the Safe Multisig, which can then be confirmed by other signers in the Web UI. Returns the link to the transaction in the Web UI.
export async function proposeTransaction(
{ ethers }: HardhatRuntimeEnvironment,
network: string,
{ authoringSafe, to, data }: Transaction,
): Promise<string> {
const { chainId } = await ethers.provider.getNetwork();
const [proposer] = await ethers.getSigners();
const ethAdapter = new EthersAdapter({
ethers,
signerOrProvider: proposer,
});
let nonce;
try {
nonce = await recommendedNonce(chainId, authoringSafe);
} catch {
console.log("Unable to determine recommended nonce, using current one");
nonce = undefined;
}

const safeTransactionData = {
const safeTransactionData: SafeTransactionDataPartial = {
to,
data,
value: "0",
nonce,
};

const safeSdk = await Safe.create({ ethAdapter, safeAddress: authoringSafe });
Expand Down
Loading