Skip to content

Commit

Permalink
Favor nonce from Safe Multisig api
Browse files Browse the repository at this point in the history
  • Loading branch information
fedgiac committed Feb 29, 2024
1 parent 76e311e commit 01440b1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
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");
nonce = undefined;
}

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

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

0 comments on commit 01440b1

Please sign in to comment.