Skip to content

Commit 73b38be

Browse files
feat: add safe api headers
1 parent d2f87fe commit 73b38be

File tree

2 files changed

+21
-20
lines changed

2 files changed

+21
-20
lines changed

script/utils/SafeUtils.s.sol

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ contract SafeUtil is ScriptExtended {
8585

8686
function getStatus(address safe) public returns (Status memory) {
8787
string memory endpoint = string.concat(getSafesAPIBaseURL(), vm.toString(safe), "/");
88-
(uint256 status, bytes memory response) = endpoint.get();
88+
(uint256 status, bytes memory response) = endpoint.get(getHeaders());
8989

9090
require(status == 200, "getSafes: Failed to get safes");
9191

@@ -103,7 +103,7 @@ contract SafeUtil is ScriptExtended {
103103
function getNextNonce(address safe) public returns (uint256) {
104104
string memory endpoint =
105105
string.concat(getSafesAPIBaseURL(), vm.toString(safe), "/multisig-transactions/?executed=false&limit=1");
106-
(uint256 status, bytes memory response) = endpoint.get();
106+
(uint256 status, bytes memory response) = endpoint.get(getHeaders());
107107
require(status == 200, "getNextNonce: Failed to get last pending transaction");
108108

109109
uint256 lastPendingNonce = vm.keyExists(string(response), _indexedKey(".results", 0, ".nonce"))
@@ -122,7 +122,7 @@ contract SafeUtil is ScriptExtended {
122122
"/multisig-transactions/?executed=false&nonce=",
123123
vm.toString(nonce)
124124
);
125-
(status, response) = endpoint.get();
125+
(status, response) = endpoint.get(getHeaders());
126126
require(status == 200, "getNextNonce: Failed to get pending transaction");
127127

128128
if (!vm.keyExists(string(response), _indexedKey(".results", 0, ".nonce"))) return nonce;
@@ -133,7 +133,7 @@ contract SafeUtil is ScriptExtended {
133133

134134
function getSafes(address owner) public returns (address[] memory) {
135135
string memory endpoint = string.concat(getOwnersAPIBaseURL(), vm.toString(owner), "/safes/");
136-
(uint256 status, bytes memory response) = endpoint.get();
136+
(uint256 status, bytes memory response) = endpoint.get(getHeaders());
137137

138138
require(status == 200, "getSafes: Failed to get safes");
139139

@@ -142,7 +142,7 @@ contract SafeUtil is ScriptExtended {
142142

143143
function getDelegates(address safe) public returns (address[] memory) {
144144
string memory endpoint = string.concat(getDelegatesAPIBaseURL(), "?safe=", vm.toString(safe));
145-
(uint256 status, bytes memory response) = endpoint.get();
145+
(uint256 status, bytes memory response) = endpoint.get(getHeaders());
146146

147147
require(status == 200, "getDelegates: Failed to get delegates");
148148

@@ -158,8 +158,7 @@ contract SafeUtil is ScriptExtended {
158158

159159
function getPendingTransactions(address safe) public returns (TransactionSimple[] memory) {
160160
string memory endpoint = string.concat(getSafesAPIBaseURL(), vm.toString(safe), "/transactions/queued");
161-
(uint256 status, bytes memory response) = endpoint.get();
162-
161+
(uint256 status, bytes memory response) = endpoint.get(getHeaders());
163162
require(status == 200, "getPendingTransactions: Failed to get pending transactions");
164163

165164
uint256 length = 0;
@@ -191,7 +190,7 @@ contract SafeUtil is ScriptExtended {
191190

192191
function getTransaction(string memory txId) public returns (TransactionSimple memory) {
193192
string memory endpoint = string.concat(getTransactionsAPIBaseURL(), txId);
194-
(uint256 status, bytes memory response) = endpoint.get();
193+
(uint256 status, bytes memory response) = endpoint.get(getHeaders());
195194

196195
require(status == 200, "getTransaction: Failed to get transaction");
197196

@@ -257,20 +256,21 @@ contract SafeUtil is ScriptExtended {
257256
return string.concat(getSafeBaseURL(), version, "/chains/", vm.toString(block.chainid), "/");
258257
}
259258

260-
function getHeaders() internal pure returns (string[] memory) {
261-
string[] memory headers = new string[](2);
262-
headers[0] = "Accept: application/json";
263-
headers[1] = "Content-Type: application/json";
264-
return headers;
265-
}
259+
function getHeaders() internal view returns (string[] memory) {
260+
string[] memory headers;
261+
string memory safeApiKey = vm.envOr("SAFE_API_KEY", string(""));
266262

267-
function getHeadersString() internal pure returns (string memory) {
268-
string[] memory headers = getHeaders();
269-
string memory headersString = " ";
270-
for (uint256 i = 0; i < headers.length; i++) {
271-
headersString = string.concat(headersString, "-H \"", headers[i], "\" ");
263+
if (bytes(safeApiKey).length == 0) {
264+
headers = new string[](2);
265+
} else {
266+
headers = new string[](3);
267+
headers[2] = string.concat("Authorization: Bearer ", safeApiKey);
272268
}
273-
return headersString;
269+
270+
headers[0] = "Accept: application/json";
271+
headers[1] = "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36";
272+
273+
return headers;
274274
}
275275

276276
function parseJsonAddressesFromValueKeys(string memory response, string memory key)

script/utils/determineArgs.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ if [ -z "$DEPLOYMENT_RPC_URL" ]; then
2121
fi
2222

2323
echo "$EXPORT_ENV_VARS"
24+
echo "export SAFE_API_KEY=$SAFE_API_KEY"
2425
echo "export SCRIPT_ARGS='$SCRIPT_ARGS'"
2526

2627
if [ "$DEPLOYMENT_RPC_URL" == "local" ]; then

0 commit comments

Comments
 (0)