Skip to content

Commit

Permalink
feat: add prepare-upgrade deployment script
Browse files Browse the repository at this point in the history
  • Loading branch information
Luisfc68 committed Jan 9, 2025
1 parent 981ef34 commit 8ecc2cf
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 4 deletions.
56 changes: 56 additions & 0 deletions scripts/deployment-utils/deploy-lbc-implementation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { ethers, upgrades } from "hardhat";
import { DeployedContractInfo, read } from "./deploy";
import { deployUpgradeLibraries } from "./upgrade-proxy";

/**
* This function deploys a LBC implementation contract **without** redirecting the LBC proxy. This function
* should be used when the deployment of the implementation contract is not going to be performed by the
* proxy admin owner. The deployment includes deploying the required libraries and linking them to the contract.
*
* @param network The name of the network to deploy the contract to as it appears in the addresses.json file.
* @param opts Options object, currently only has a verbose flag.
*
* @returns { Promise<DeployedContractInfo> } The information of the deployed contract.
*/
export async function deployLbcImplementation(
network: string,
opts = { verbose: true }
): Promise<Required<DeployedContractInfo>> {
const libs = await deployUpgradeLibraries(network, opts);
const proxyName = "LiquidityBridgeContract";
const upgradeName = "LiquidityBridgeContractV2";
const LiquidityBridgeContractV2 = await ethers.getContractFactory(
upgradeName,
{
libraries: {
QuotesV2: libs.quotesV2,
BtcUtils: libs.btcUtils,
SignatureValidator: libs.signatureValidator,
},
}
);

const proxyAddress = read()[network][proxyName].address;
if (!proxyAddress) {
throw new Error(`Proxy ${proxyName} not deployed on network ${network}`);
}

if (opts.verbose) {
console.info(`Deploying implementation with libs:`, libs);
}
const implementationAddress = (await upgrades.prepareUpgrade(
proxyAddress,
LiquidityBridgeContractV2,
{
unsafeAllow: ["external-library-linking"],
}
)) as string;
if (opts.verbose) {
console.info(`Implementation deployed at ${implementationAddress}`);
}

return {
deployed: true,
address: implementationAddress,
};
}
2 changes: 1 addition & 1 deletion scripts/deployment-utils/upgrade-proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ interface LiquidityBridgeContractLibraries {
signatureValidator: string;
}

async function deployUpgradeLibraries(
export async function deployUpgradeLibraries(
network: string,
opts: { verbose: boolean }
): Promise<LiquidityBridgeContractLibraries> {
Expand Down
13 changes: 13 additions & 0 deletions scripts/deployment/prepare-upgrade.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import hre from "hardhat";
import { deployLbcImplementation } from "../deployment-utils/deploy-lbc-implementation";

async function main() {
const network = hre.network.name;
const deploymentInfo = await deployLbcImplementation(network);
console.info("IMPLEMENTATION ADDRESS: ", deploymentInfo.address);
}

main().catch((error: unknown) => {
console.error(error);
process.exitCode = 1;
});
35 changes: 35 additions & 0 deletions test/deployment.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { deployLbcProxy } from "../scripts/deployment-utils/deploy-proxy";
import { upgradeLbcProxy } from "../scripts/deployment-utils/upgrade-proxy";
import { ZERO_ADDRESS } from "./utils/constants";
import { BRIDGE_ADDRESS } from "../scripts/deployment-utils/constants";
import { deployLbcImplementation } from "../scripts/deployment-utils/deploy-lbc-implementation";

describe("LiquidityBridgeContract deployment process should", function () {
let proxyAddress: string;
Expand Down Expand Up @@ -147,4 +148,38 @@ describe("LiquidityBridgeContract deployment process should", function () {
}
}
});

it("deploy implementation without upgrading proxy", async () => {
const proxy = await deployLbcProxy(hre.network.name, { verbose: false });
proxyAddress = proxy.address;
const lbcProxy = await ethers.getContractAt(
"LiquidityBridgeContractV2",
proxy.address
);
const initializedQuery = await lbcProxy.queryFilter(
lbcProxy.getEvent("Initialized")
);

const implementation = await deployLbcImplementation(hre.network.name, {
verbose: false,
});
const lbcImplementation = await ethers.getContractAt(
"LiquidityBridgeContractV2",
implementation.address
);

expect(proxy.deployed).to.be.eq(true);
expect(implementation.deployed).to.be.eq(true);
expect(initializedQuery.length).length.equal(1);
expect(proxy.address).not.to.eq(implementation.address);

// this is to ensure implementation does not have state
await expect(lbcProxy.getMinCollateral()).to.eventually.eq(
ethers.parseEther("0.03")
);
await expect(lbcImplementation.getMinCollateral()).to.eventually.eq(0n);

await expect(lbcProxy.version()).to.be.reverted;
await expect(lbcImplementation.version()).to.eventually.eq("1.3.0");
});
});
6 changes: 3 additions & 3 deletions test/pegin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,7 @@ describe("LiquidityBridgeContractV2 pegin process should", () => {
productFeeAmount: BigInt("6000000000000000"),
gasFee: BigInt("3000000000000000"),
},
address: "2Mvz5NDrXSSBCXzhrxnuo9TDS8Co4Wk1t8N",
address: "2NAXHKYRnTme4oDCk9mSPfdf4ga2tZ1xM5B",
},
{
quote: {
Expand Down Expand Up @@ -579,7 +579,7 @@ describe("LiquidityBridgeContractV2 pegin process should", () => {
productFeeAmount: BigInt("7000000000000000"),
gasFee: BigInt("4000000000000000"),
},
address: "2Mxb4NdDaDBX4kjxhBrzjMG5WbCjok6LWab",
address: "2NCDJzPze5eosHN5Tx4pf5GF2zXtaKDUHzX",
},
{
quote: {
Expand Down Expand Up @@ -611,7 +611,7 @@ describe("LiquidityBridgeContractV2 pegin process should", () => {
productFeeAmount: BigInt("8000000000000000"),
gasFee: BigInt("5000000000000000"),
},
address: "2NCx9M8j7nZTp3GmP36CFvFgYNLBQwQPwDR",
address: "2N7rxjtHjbxr8W3U3HVncyHJhEhBQ3tBa9w",
},
];

Expand Down

0 comments on commit 8ecc2cf

Please sign in to comment.