Skip to content

Commit

Permalink
Favor nonce from Safe Multisig api (#75)
Browse files Browse the repository at this point in the history
If there is a pending transaction in the Safe, the self-sell script
overwrites it when creating the `settle` transaction.
With these changes, the created transaction will use the nonce that is
recommended by the Safe API. This is the same nonce used by the
interface to determine the nonce for the next transaction.

I also sneaked in a change in the parameters, since the script is not
working unless `--to-token` is explicit.

### Test Plan

This is difficult to test as the script only works on mainnet (see #74).
I suggest to see that the api call returns the expected nonce.
For example,
[0xA03be496e67Ec29bC62F01a428683D7F9c204930](https://app.safe.global/transactions/queue?safe=sep:0xA03be496e67Ec29bC62F01a428683D7F9c204930)
is a Safe that right now has a pending transaction, and you can see that
the API output is the expected one:

```sh
curl -X 'GET' \
  'https://safe-client.safe.global/v1/chains/11155111/safes/0xA03be496e67Ec29bC62F01a428683D7F9c204930/nonces' \
  -H 'accept: application/json'
{"currentNonce":10,"recommendedNonce":11}
```

---------

Co-authored-by: Felix Leupold <[email protected]>
  • Loading branch information
fedgiac and fleupold authored Feb 29, 2024
1 parent 76e311e commit fbb2bb9
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, 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

0 comments on commit fbb2bb9

Please sign in to comment.