Skip to content

Commit 523b5e1

Browse files
committed
fix: try to fix loading contract addresses from json, use always fallback for tests until full protocol is deployed
1 parent 7010eed commit 523b5e1

3 files changed

Lines changed: 120 additions & 12 deletions

File tree

‎deployments/protocol/v1.5.json‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"chainlinkFeedRegistry": "0x8D5e90706E52a52853dA9A14fA1c63889a412851",
55
"config": "0xd52a2898d61636bB3eEF0d145f05352FF543bdCC",
66
"configSingleton": "0x1f5febF0efA3aD487508b6Cc7f39a0a54DE9De72",
7-
"crowdsourceLenderVault": "",
7+
"crowdsourceLenderVault": "0x41dCf7E9ECDBd3f0bc69cba50b49f1B37ae65Ca2",
88
"hooks": {
99
"compoundLender": "",
1010
"directLenderRepayment": "",
@@ -13,11 +13,11 @@
1313
"aaveLender": ""
1414
},
1515
"hub": "0x37807A2F031b3B44081F4b21500E5D70EbaDAdd5",
16-
"loan": "",
16+
"loan": "0xc58791ec351349a82036aE712976109C10e34217",
1717
"loanToken": "0x4440C069272cC34b80C7B11bEE657D0349Ba9C23",
1818
"products": {
1919
"stable": "",
20-
"installments": "",
20+
"installments": "0x59Fd11B2518238E363bd4CC2aBb50455d1587966",
2121
"fixed": "",
2222
"uniswapV3Individual": "",
2323
"uniswapV3Set": ""
@@ -51,4 +51,4 @@
5151
"revokedNonce": "0x972204fF33348ee6889B2d0A3967dB67d7b08e4c",
5252
"utilizedCredit": "0x8E6F44DEa3c11d69C63655BDEcbA25Fa986BCE9D"
5353
}
54-
}
54+
}

‎src/Deployments.sol‎

Lines changed: 108 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,27 +116,128 @@ abstract contract Deployments is CommonBase {
116116

117117
function _loadDeployedAddresses() internal {
118118
string memory root = vm.projectRoot();
119+
string memory chainIdKey = block.chainid.toString();
119120

121+
// Load creation code
122+
_loadCreationCode(root);
123+
124+
// Load external addresses
125+
_loadExternalAddresses(root, chainIdKey);
126+
127+
// Load deployment addresses
128+
_loadDeploymentAddresses(root, chainIdKey);
129+
}
130+
131+
function _loadCreationCode(string memory root) internal {
120132
string memory creationJson = vm.readFile(string.concat(root, deploymentsSubpath, "/deployments/creation/creationCode.json"));
121-
bytes memory rawCreation = creationJson.parseRaw(".");
122-
__cc = abi.decode(rawCreation, (CreationCode));
133+
__cc.categoryRegistry = creationJson.readBytes(".categoryRegistry");
134+
__cc.chainlinkFeedRegistry = creationJson.readBytes(".chainlinkFeedRegistry");
135+
__cc.config = creationJson.readBytes(".config");
136+
__cc.configSingleton_v1_2 = creationJson.readBytes(".configSingleton_v1_2");
137+
__cc.hub = creationJson.readBytes(".hub");
138+
__cc.loanToken = creationJson.readBytes(".loanToken");
139+
__cc.revokedNonce = creationJson.readBytes(".revokedNonce");
140+
__cc.utilizedCredit = creationJson.readBytes(".utilizedCredit");
141+
}
123142

143+
function _loadExternalAddresses(string memory root, string memory chainIdKey) internal {
124144
string memory externalJson = vm.readFile(string.concat(root, deploymentsSubpath, "/deployments/external/external.json"));
125-
bytes memory rawExternal = externalJson.parseRaw(string.concat(".", block.chainid.toString()));
126-
__e = abi.decode(rawExternal, (External));
145+
string memory externalKey = string.concat(".", chainIdKey);
146+
__e.aave = IAaveLike(externalJson.readAddress(string.concat(externalKey, ".aave")));
147+
__e.adminTimelock = externalJson.readAddress(string.concat(externalKey, ".adminTimelock"));
148+
__e.chainlinkL2SequencerUptimeFeed = externalJson.readAddress(string.concat(externalKey, ".chainlinkL2SequencerUptimeFeed"));
149+
__e.dao = externalJson.readAddress(string.concat(externalKey, ".dao"));
150+
__e.daoSafe = externalJson.readAddress(string.concat(externalKey, ".daoSafe"));
151+
__e.deployer = IPWNDeployer(externalJson.readAddress(string.concat(externalKey, ".deployer")));
152+
__e.deployerSafe = externalJson.readAddress(string.concat(externalKey, ".deployerSafe"));
153+
__e.isL2 = externalJson.readBool(string.concat(externalKey, ".isL2"));
154+
__e.protocolTimelock = externalJson.readAddress(string.concat(externalKey, ".protocolTimelock"));
155+
__e.uniswapV3Factory = externalJson.readAddress(string.concat(externalKey, ".uniswapV3Factory"));
156+
__e.uniswapV3NFTPositionManager = externalJson.readAddress(string.concat(externalKey, ".uniswapV3NFTPositionManager"));
157+
__e.weth = externalJson.readAddress(string.concat(externalKey, ".weth"));
158+
}
127159

160+
function _loadDeploymentAddresses(string memory root, string memory chainIdKey) private {
128161
string memory deploymentsJson = vm.readFile(string.concat(root, deploymentsSubpath, "/deployments/protocol/v1.5.json"));
129-
bytes memory rawDeployment = deploymentsJson.parseRaw(string.concat(".", block.chainid.toString()));
130-
162+
string memory deploymentKey = string.concat(".", chainIdKey);
163+
164+
// Check if deployment exists for this chain by checking if raw bytes exist
165+
bytes memory rawDeployment = deploymentsJson.parseRaw(deploymentKey);
166+
131167
if (rawDeployment.length > 0) {
132168
wasPredeployedOnFork = true;
133-
__d = abi.decode(rawDeployment, (Deployment));
169+
_loadDeploymentTopLevel(deploymentsJson, deploymentKey);
170+
_loadDeploymentProducts(deploymentsJson, deploymentKey);
171+
_loadDeploymentHooks(deploymentsJson, deploymentKey);
134172
} else {
135173
wasPredeployedOnFork = false;
136174
_protocolNotDeployedOnSelectedChain();
137175
}
138176
}
139177

178+
function _safeReadAddress(string memory json, string memory key) private pure returns (address) {
179+
string memory addrStr = json.readString(key);
180+
if (bytes(addrStr).length == 0) {
181+
return address(0);
182+
}
183+
return json.readAddress(key);
184+
}
185+
186+
function _loadDeploymentTopLevel(string memory deploymentsJson, string memory deploymentKey) private {
187+
__d.categoryRegistry = MultiTokenCategoryRegistry(_safeReadAddress(deploymentsJson, string.concat(deploymentKey, ".categoryRegistry")));
188+
__d.chainlinkFeedRegistry = IChainlinkFeedRegistryLike(_safeReadAddress(deploymentsJson, string.concat(deploymentKey, ".chainlinkFeedRegistry")));
189+
__d.config = PWNConfig(_safeReadAddress(deploymentsJson, string.concat(deploymentKey, ".config")));
190+
__d.configSingleton = PWNConfig(_safeReadAddress(deploymentsJson, string.concat(deploymentKey, ".configSingleton")));
191+
__d.hub = PWNHub(_safeReadAddress(deploymentsJson, string.concat(deploymentKey, ".hub")));
192+
__d.loanToken = PWNLOAN(_safeReadAddress(deploymentsJson, string.concat(deploymentKey, ".loanToken")));
193+
194+
address addr = _safeReadAddress(deploymentsJson, string.concat(deploymentKey, ".crowdsourceLenderVault"));
195+
if (addr != address(0)) __d.crowdsourceLenderVault = PWNCrowdsourceLenderVault(addr);
196+
197+
addr = _safeReadAddress(deploymentsJson, string.concat(deploymentKey, ".loan"));
198+
if (addr != address(0)) __d.loan = PWNLoan(addr);
199+
200+
addr = _safeReadAddress(deploymentsJson, string.concat(deploymentKey, ".revokedNonce"));
201+
if (addr != address(0)) __d.revokedNonce = PWNRevokedNonce(addr);
202+
203+
addr = _safeReadAddress(deploymentsJson, string.concat(deploymentKey, ".utilizedCredit"));
204+
if (addr != address(0)) __d.utilizedCredit = PWNUtilizedCredit(addr);
205+
}
206+
207+
function _loadDeploymentProducts(string memory deploymentsJson, string memory deploymentKey) private {
208+
address addr = _safeReadAddress(deploymentsJson, string.concat(deploymentKey, ".products.stable"));
209+
if (addr != address(0)) __d.products.stable = PWNStableProduct(addr);
210+
211+
addr = _safeReadAddress(deploymentsJson, string.concat(deploymentKey, ".products.installments"));
212+
if (addr != address(0)) __d.products.installments = PWNInstallmentsProduct(addr);
213+
214+
addr = _safeReadAddress(deploymentsJson, string.concat(deploymentKey, ".products.fixed"));
215+
if (addr != address(0)) __d.products._fixed = PWNFixedProduct(addr);
216+
217+
addr = _safeReadAddress(deploymentsJson, string.concat(deploymentKey, ".products.uniswapV3Individual"));
218+
if (addr != address(0)) __d.products.uniswapV3Individual = PWNUniswapV3IndividualProduct(addr);
219+
220+
addr = _safeReadAddress(deploymentsJson, string.concat(deploymentKey, ".products.uniswapV3Set"));
221+
if (addr != address(0)) __d.products.uniswapV3Set = PWNUniswapV3SetProduct(addr);
222+
}
223+
224+
function _loadDeploymentHooks(string memory deploymentsJson, string memory deploymentKey) private {
225+
address addr = _safeReadAddress(deploymentsJson, string.concat(deploymentKey, ".hooks.aaveLender"));
226+
if (addr != address(0)) __d.hooks.aaveLender = PWNAaveLenderHook(addr);
227+
228+
addr = _safeReadAddress(deploymentsJson, string.concat(deploymentKey, ".hooks.compoundLender"));
229+
if (addr != address(0)) __d.hooks.compoundLender = PWNCompoundLenderHook(addr);
230+
231+
addr = _safeReadAddress(deploymentsJson, string.concat(deploymentKey, ".hooks.directLenderRepayment"));
232+
if (addr != address(0)) __d.hooks.directLenderRepayment = PWNDirectLenderRepaymentHook(addr);
233+
234+
addr = _safeReadAddress(deploymentsJson, string.concat(deploymentKey, ".hooks.refinanceBorrowerCreate"));
235+
if (addr != address(0)) __d.hooks.refinanceBorrowerCreate = PWNRefinanceBorrowerCreateHook(addr);
236+
237+
addr = _safeReadAddress(deploymentsJson, string.concat(deploymentKey, ".hooks.vaultLender"));
238+
if (addr != address(0)) __d.hooks.vaultLender = PWN4626VaultLenderHook(addr);
239+
}
240+
140241
function _protocolNotDeployedOnSelectedChain() internal virtual {
141242
// Override
142243
}

‎test/DeploymentTest.t.sol‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,14 @@ abstract contract DeploymentTest is Deployments, Test {
4141
address borrower;
4242

4343
function setUp() public virtual {
44-
_loadDeployedAddresses();
44+
_loadCreationCode(vm.projectRoot());
45+
_loadExternalAddresses(vm.projectRoot(), "1");
46+
47+
// note: there is issue that tests are failing if only some of the contracts/products
48+
// are deployed (e.g. only Installments product is deployed, while Stable product not yet)
49+
// and because of this we are calling the _protocolNotDeployedOnSelectedChain() fallback
50+
// _loadDeployedAddresses();
51+
_protocolNotDeployedOnSelectedChain();
4552

4653
(lender, lenderPK) = makeAddrAndKey("lender");
4754
(borrower, borrowerPK) = makeAddrAndKey("borrower");

0 commit comments

Comments
 (0)