Skip to content

Commit f8e5a7e

Browse files
committed
Add upgradeStashDex script and exchange[to sender] function
1 parent 6205708 commit f8e5a7e

6 files changed

Lines changed: 120 additions & 11 deletions

File tree

contracts/StashDex.sol

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,10 +165,19 @@ contract StashDex is AccessControlUpgradeable {
165165
uint256 amountIn,
166166
uint256 amountOut,
167167
address recipient
168-
) external {
168+
) public {
169169
swap(_indexToAddress(indexIn), _indexToAddress(indexOut), amountIn, amountOut, recipient);
170170
}
171171

172+
function exchange(
173+
uint256 indexIn,
174+
uint256 indexOut,
175+
uint256 amountIn,
176+
uint256 amountOut
177+
) external {
178+
exchange(indexIn, indexOut, amountIn, amountOut, _msgSender());
179+
}
180+
172181
/// @notice Repay the liquidity pool for token if debt is outstanding, using this contract's balance.
173182
function repay(address token) public whenNotPaused() {
174183
TokenConfig storage config = _getStorage().tokenConfig[token];

deployments/deploy-ethereum-stage.log

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,3 +442,21 @@ Using config for: stage, ETHEREUM
442442
Safe 0xA8eeA59b4A17CE2689E57B4dE9e825FD25705414: verified 0xdeC18ca09f163C841Fc3d44237bfD0EBbC8414dE as owner.
443443
Added asset: USDC (0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48) 6 decimals
444444
Added asset: USDT (0xdAC17F958D2ee523a2206206994597C13D831ec7) 6 decimals
445+
446+
Safe 0xA8eeA59b4A17CE2689E57B4dE9e825FD25705414: verified 0xdeC18ca09f163C841Fc3d44237bfD0EBbC8414dE as owner.
447+
Deployment ID: MVP
448+
Upgrade ID: STASH_DEX_INTERFACE
449+
Upgrading StashDex
450+
Using config for: stage, ETHEREUM
451+
Deployer : 0xdeC18ca09f163C841Fc3d44237bfD0EBbC8414dE
452+
DEPLOYER_ADDRESS: 0xdBD91aD22bE5304e385b7b0A2Cfe91164e416e11
453+
StashStablecoinDex proxy: 0x125Bf891F832c3A1422E24f4Ca7ef314c0858740
454+
Oracle: 0x4666013984fD7a624aa8BFADD0E95456870dADEc
455+
Receiver: 0x697ECA1cae710FA0348e2173900e6C09b180C35b
456+
New StashDex implementation deployed to 0x02790659dd9B647ED37DFb4Bbc18CeCCDc5A8068
457+
Sending StashDex upgrade transaction.
458+
Transaction proposed to Safe 0xA8eeA59b4A17CE2689E57B4dE9e825FD25705414. Safe TX hash: 0x573f41a11085b89263fd733b81da52c706ea740b2c2f61420156f1901879e8ab
459+
Threshold met (1/1). Executing on-chain...
460+
Executed. On-chain TX hash: 0xfffdba1d5234084edcb4ae47945f65fa30ab69bfe6b60e41e1bf582ca0ad797b
461+
0xfffdba1d5234084edcb4ae47945f65fa30ab69bfe6b60e41e1bf582ca0ad797b
462+
StashDex upgraded.

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@
129129
"upgrade-usdt-stashdex-processor-ethereum-stage": "PROCESSOR_TOKEN=USDT DEPLOY_TYPE=STAGE hardhat run ./scripts/upgradeStashDexProcessor.ts --network ETHEREUM",
130130
"upgrade-pyusd-stashdex-processor-ethereum": "PROCESSOR_TOKEN=PYUSD hardhat run ./scripts/upgradeStashDexProcessor.ts --network ETHEREUM",
131131
"upgrade-pyusd-stashdex-processor-ethereum-stage": "PROCESSOR_TOKEN=PYUSD DEPLOY_TYPE=STAGE hardhat run ./scripts/upgradeStashDexProcessor.ts --network ETHEREUM",
132+
"upgrade-stashdex-ethereum": "hardhat run ./scripts/upgradeStashDex.ts --network ETHEREUM",
133+
"upgrade-stashdex-ethereum-stage": "DEPLOY_TYPE=STAGE hardhat run ./scripts/upgradeStashDex.ts --network ETHEREUM",
132134
"grant-staging-roles-base": "hardhat run ./scripts/grantStagingRoles.ts --network BASE",
133135
"grant-staging-roles-opmainnet": "hardhat run ./scripts/grantStagingRoles.ts --network OP_MAINNET",
134136
"grant-staging-roles-arbitrumone": "hardhat run ./scripts/grantStagingRoles.ts --network ARBITRUM_ONE",
@@ -293,6 +295,8 @@
293295
"dry:upgrade-usdt-stashdex-processor-ethereum-stage": "DRY_RUN=ETHEREUM DEPLOY_TYPE=STAGE VERIFY=false PROCESSOR_TOKEN=USDT ts-node --files ./scripts/upgradeStashDexProcessor.ts",
294296
"dry:upgrade-pyusd-stashdex-processor-ethereum": "DRY_RUN=ETHEREUM VERIFY=false PROCESSOR_TOKEN=PYUSD ts-node --files ./scripts/upgradeStashDexProcessor.ts",
295297
"dry:upgrade-pyusd-stashdex-processor-ethereum-stage": "DRY_RUN=ETHEREUM DEPLOY_TYPE=STAGE VERIFY=false PROCESSOR_TOKEN=PYUSD ts-node --files ./scripts/upgradeStashDexProcessor.ts",
298+
"dry:upgrade-stashdex-ethereum": "DRY_RUN=ETHEREUM VERIFY=false ts-node --files ./scripts/upgradeStashDex.ts",
299+
"dry:upgrade-stashdex-ethereum-stage": "DRY_RUN=ETHEREUM DEPLOY_TYPE=STAGE VERIFY=false ts-node --files ./scripts/upgradeStashDex.ts",
296300
"lint": "npm run lint:solidity && npm run lint:ts",
297301
"lint:solidity": "solhint 'contracts/**/*.sol'",
298302
"lint:ts": "eslint --ignore-pattern 'coverage/'",

scripts/test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {main as deployPaxosOracle} from "./deployPaxosOracle";
2121
import {main as deployStashDex} from "./deployStashDex";
2222
import {main as deployStashDexProcessor} from "./deployStashDexProcessor";
2323
import {main as upgradeStashDexProcessor} from "./upgradeStashDexProcessor";
24+
import {main as upgradeStashDex} from "./upgradeStashDex";
2425

2526
async function main() {
2627
console.log("Test deploy.");
@@ -60,6 +61,8 @@ async function main() {
6061
await deployStashDex();
6162
console.log("Test upgradeStashDexProcessor USDC.");
6263
await upgradeStashDexProcessor();
64+
console.log("Test upgradeStashDex.");
65+
await upgradeStashDex();
6366
console.log("Test deployStandaloneRepayer.");
6467
process.env.STANDALONE_REPAYER_ENV = "SparkStage";
6568
await deployStandaloneRepayer();

scripts/upgradeStashDex.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import dotenv from "dotenv";
2+
dotenv.config();
3+
import hre from "hardhat";
4+
import {getVerifier, upgradeProxyX, getHardhatNetworkConfig, getNetworkConfig, logDeployers} from "./helpers";
5+
import {createSender} from "./safe";
6+
import {getDeployProxyXAddress, resolveXAddress} from "../test/helpers";
7+
import {isSet, assert} from "./common";
8+
import {StashDex} from "../typechain-types";
9+
import {Network, NetworkConfig} from "../network.config";
10+
11+
export async function main() {
12+
const [deployer] = await hre.ethers.getSigners();
13+
const sender = await createSender(hre, deployer);
14+
15+
assert(isSet(process.env.DEPLOY_ID), "DEPLOY_ID must be set");
16+
assert(isSet(process.env.UPGRADE_ID), "UPGRADE_ID must be set");
17+
const verifier = getVerifier(process.env.UPGRADE_ID);
18+
console.log(`Deployment ID: ${process.env.DEPLOY_ID}`);
19+
console.log(`Upgrade ID: ${process.env.UPGRADE_ID}`);
20+
21+
let network: Network;
22+
let config: NetworkConfig;
23+
console.log("Upgrading StashDex");
24+
({network, config} = await getNetworkConfig());
25+
if (!network) {
26+
({network, config} = await getHardhatNetworkConfig());
27+
}
28+
29+
await logDeployers(false);
30+
31+
assert(config.StashDex, "StashDex must be in config");
32+
33+
const id = "StashStablecoinDex";
34+
const stashDexAddress = await getDeployProxyXAddress(id);
35+
const oracle = await resolveXAddress(config.StashDex.Oracle);
36+
const receiver = await resolveXAddress(config.StashDex.Receiver);
37+
38+
console.log(`${id} proxy: ${stashDexAddress}`);
39+
console.log(`Oracle: ${oracle}`);
40+
console.log(`Receiver: ${receiver}`);
41+
42+
await upgradeProxyX<StashDex>(
43+
verifier.deployX,
44+
stashDexAddress,
45+
"StashDex",
46+
sender,
47+
[oracle, receiver],
48+
id,
49+
);
50+
51+
await verifier.verify(process.env.VERIFY === "true");
52+
}
53+
54+
if (process.env.SCRIPT_ENV !== "CI") {
55+
main();
56+
}

test/StashDex.ts

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -484,24 +484,25 @@ describe("StashDex", function () {
484484
it("reverts InvalidIndex when indexIn has bits above position 159", async function () {
485485
const {stashDex, user, tokenB, USDC} = await loadFixture(deployAll);
486486
const badIndex = (1n << 160n) | BigInt(await tokenB.getAddress());
487-
await expect(
488-
stashDex.connect(user).exchange(badIndex, BigInt(await tokenB.getAddress()), 1n * USDC, 1n * USDC, user)
489-
).to.be.revertedWithCustomError(stashDex, "InvalidIndex");
487+
await expect(stashDex.connect(user)["exchange(uint256,uint256,uint256,uint256,address)"](
488+
badIndex, BigInt(await tokenB.getAddress()), 1n * USDC, 1n * USDC, user
489+
)).to.be.revertedWithCustomError(stashDex, "InvalidIndex");
490490
});
491491

492492
it("reverts InvalidIndex when indexOut has bits above position 159", async function () {
493493
const {stashDex, user, tokenA, USDC} = await loadFixture(deployAll);
494494
const badIndex = (1n << 160n) | BigInt(await tokenA.getAddress());
495-
await expect(
496-
stashDex.connect(user).exchange(BigInt(await tokenA.getAddress()), badIndex, 1n * USDC, 1n * USDC, user)
497-
).to.be.revertedWithCustomError(stashDex, "InvalidIndex");
495+
await expect(stashDex.connect(user)["exchange(uint256,uint256,uint256,uint256,address)"](
496+
BigInt(await tokenA.getAddress()), badIndex, 1n * USDC, 1n * USDC, user
497+
)).to.be.revertedWithCustomError(stashDex, "InvalidIndex");
498498
});
499499

500500
it("accepts type(uint160).max as a valid index for both in and out", async function () {
501501
const {stashDex, user} = await loadFixture(deployAll);
502502
const maxAddr = 2n ** 160n - 1n;
503-
await expect(stashDex.connect(user).exchange(maxAddr, maxAddr, 1n, 1n, user))
504-
.to.be.revertedWithCustomError(stashDex, "RouteNotAllowed");
503+
await expect(stashDex.connect(user)["exchange(uint256,uint256,uint256,uint256,address)"](
504+
maxAddr, maxAddr, 1n, 1n, user
505+
)).to.be.revertedWithCustomError(stashDex, "RouteNotAllowed");
505506
});
506507

507508
it("delegates to swap and transfers tokens correctly", async function () {
@@ -512,7 +513,7 @@ describe("StashDex", function () {
512513
await tokenA.connect(user).approve(stashDex, 10_000n * USDC);
513514
await tokenB.mint(pool, 9_997n * USDC);
514515

515-
const tx = await stashDex.connect(user).exchange(
516+
const tx = await stashDex.connect(user)["exchange(uint256,uint256,uint256,uint256,address)"](
516517
BigInt(await tokenA.getAddress()), BigInt(await tokenB.getAddress()), 10_000n * USDC, 9_997n * USDC, user2
517518
);
518519
await expect(tx).to.emit(stashDex, "Swapped")
@@ -521,6 +522,24 @@ describe("StashDex", function () {
521522
expect(await tokenA.balanceOf(processor)).to.equal(10_000n * USDC);
522523
expect(await tokenB.balanceOf(user2)).to.equal(9_997n * USDC);
523524
});
525+
526+
it("delegates without recipient to swap and transfers tokens correctly", async function () {
527+
const {stashDex, configAdmin, user, tokenA, tokenB, pool, processor, USDC} = await loadFixture(deployAll);
528+
await stashDex.connect(configAdmin).setPool(tokenB, pool);
529+
await stashDex.connect(configAdmin).setRoute({tokenIn: tokenA, tokenOut: tokenB, feeBps: 3, processor});
530+
await tokenA.mint(user, 10_000n * USDC);
531+
await tokenA.connect(user).approve(stashDex, 10_000n * USDC);
532+
await tokenB.mint(pool, 9_997n * USDC);
533+
534+
const tx = await stashDex.connect(user)["exchange(uint256,uint256,uint256,uint256)"](
535+
BigInt(await tokenA.getAddress()), BigInt(await tokenB.getAddress()), 10_000n * USDC, 9_997n * USDC
536+
);
537+
await expect(tx).to.emit(stashDex, "Swapped")
538+
.withArgs(tokenA.target, tokenB.target, 10_000n * USDC, 9_997n * USDC, user.address);
539+
540+
expect(await tokenA.balanceOf(processor)).to.equal(10_000n * USDC);
541+
expect(await tokenB.balanceOf(user)).to.equal(9_997n * USDC);
542+
});
524543
});
525544

526545
describe("repay", function () {
@@ -751,7 +770,7 @@ describe("StashDex", function () {
751770
await tokenA.mint(user, 10_000n * USDC);
752771
await tokenA.connect(user).approve(stashDex, 10_000n * USDC);
753772
await tokenB.mint(pool, 9_997n * USDC);
754-
await stashDex.connect(user).exchange(
773+
await stashDex.connect(user)["exchange(uint256,uint256,uint256,uint256,address)"](
755774
BigInt(await tokenA.getAddress()), BigInt(await tokenB.getAddress()),
756775
10_000n * USDC, 9_997n * USDC, user
757776
);

0 commit comments

Comments
 (0)