Skip to content
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
53 changes: 14 additions & 39 deletions packages/ethereum-contracts/ops-scripts/libs/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -364,34 +364,11 @@ async function autodetectAdminType(sf, account) {
throw new Error(`Unknown admin contract type of account ${account}`);
}

// returns the Safe Tx Service URL or throws if none available
// source: https://github.com/safe-global/safe-docs/blob/main/pages/api-supported-networks.md?plain=1
function getSafeTxServiceUrl(chainId) {
const safeChainNames = {
// mainnets
1: "mainnet",
10: "optimism",
56: "bsc",
100: "gnosis-chain",
137: "polygon",
8453: "base",
42161: "arbitrum",
42220: "celo",
43114: "avalanche",
// testnets
11155111: "sepolia"
};
if (safeChainNames[chainId] === undefined) {
throw new Error(`no Safe tx service url known for chainId ${chainId}`);
}
return `https://safe-transaction-${safeChainNames[chainId]}.safe.global`;
}

// safeTxData is the ABI encoded transaction data of the inner call to be made by the Safe
async function executeSafeTransaction(safeAddr, targetContractAddr, safeTxData) {
const Web3Adapter = require('@safe-global/safe-web3-lib').default;
const Safe = require('@safe-global/safe-core-sdk').default;
const SafeServiceClient = require('@safe-global/safe-service-client').default;
const SafeApiKit = require('@safe-global/api-kit').default;

const safeOwner = (await web3.eth.getAccounts())[0]; // tx sender
console.log("Safe signer being used:", safeOwner);
Expand All @@ -403,18 +380,18 @@ async function executeSafeTransaction(safeAddr, targetContractAddr, safeTxData)

const safeSdk = await Safe.create({ ethAdapter: ethAdapterOwner1, safeAddress: safeAddr });

const safeService = new SafeServiceClient({
txServiceUrl: getSafeTxServiceUrl(await web3.eth.getChainId()),
ethAdapter: ethAdapterOwner1
const chainId = await web3.eth.getChainId();
const apiKit = new SafeApiKit({
chainId: BigInt(chainId),
apiKey: process.env.SAFE_API_KEY // Required for auth/default service
});

const data = safeTxData;
const nextNonce = await safeService.getNextNonce(safeAddr);

const safeTransactionData = {
to: targetContractAddr,
value: 0,
data: data,
nonce: process.env.SAFE_REPLACE_LAST_TX ? nextNonce-1 : nextNonce
};
const safeTransaction = await safeSdk.createTransaction({ safeTransactionData });
console.log("Safe tx:", safeTransaction);
Expand All @@ -424,26 +401,24 @@ async function executeSafeTransaction(safeAddr, targetContractAddr, safeTxData)
const signature = await safeSdk.signTransactionHash(safeTxHash);
console.log("Signature:", signature);

const transactionConfig = {
const pendingTxsBefore = await apiKit.getPendingTransactions(safeAddr);

// according to the docs this should return the tx hash, but always returns undefined although succeeding
const ret = await apiKit.proposeTransaction({
safeAddress: safeAddr,
safeTransactionData: safeTransaction.data,
safeTxHash: safeTxHash,
senderAddress: safeOwner,
senderSignature: signature.data,
origin: "ops-scripts"
};

const pendingTxsBefore = await safeService.getPendingTransactions(safeAddr);

// according to the docs this should return the tx hash, but always returns undefined although succeeding
const ret = await safeService.proposeTransaction(transactionConfig);
});
console.log("returned:", ret);

const pendingTxsAfter = await safeService.getPendingTransactions(safeAddr);
console.log(`pending txs before ${pendingTxsBefore.count}, after ${pendingTxsAfter.count}`);
const pendingTxsAfter = await apiKit.getPendingTransactions(safeAddr);
console.log(`pending txs before ${pendingTxsBefore.results.length}, after ${pendingTxsAfter.results.length}`);

// workaround for verifying that the proposal was added
if (!pendingTxsAfter.count > pendingTxsBefore.count) {
if (!pendingTxsAfter.results.length > pendingTxsBefore.results.length) {
throw new Error("Safe pending transactions count didn't increase, propose may have failed!");
}
}
Expand Down
1 change: 1 addition & 0 deletions packages/ethereum-contracts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"dependencies": {
"@decentral.ee/web3-helpers": "0.5.3",
"@nomiclabs/hardhat-ethers": "2.2.3",
"@safe-global/api-kit": "4.0.1",
"@truffle/contract": "4.6.31",
"ethereumjs-tx": "2.1.2",
"ethereumjs-util": "7.1.5",
Expand Down
Loading
Loading