Skip to content

Commit

Permalink
add retry
Browse files Browse the repository at this point in the history
  • Loading branch information
kollegian committed Nov 12, 2024
1 parent 547ac8f commit ae14a56
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 49 deletions.
79 changes: 33 additions & 46 deletions contracts/test/lib.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { exec } = require("child_process");
const {ethers} = require("hardhat"); // Importing exec from child_process
const {ethers, network} = require("hardhat"); // Importing exec from child_process

const adminKeyName = "admin"

Expand Down Expand Up @@ -213,31 +213,14 @@ async function getPointerForCw721(cw721Address) {
}

async function deployErc20PointerForCw20(provider, cw20Address, attempts=10, from=adminKeyName, evmRpc="") {
let command = `seid tx evm register-evm-pointer CW20 ${cw20Address} --from=${from} -b block`
let command = `seid tx evm register-evm-pointer CW20 ${cw20Address} --from=${from} -b block --gas-prices=0.2usei`;
if (evmRpc) {
command = command + ` --evm-rpc=${evmRpc}`
}
console.log('Deploy command ran');
const output = await execute(command);
const txHash = output.replace(/.*0x/, "0x").trim()
const txHash = output.replace(/.*0x/, "0x").trim();
let attempt = 0;
while(attempt < attempts) {
const receipt = await provider.getTransactionReceipt(txHash);
if(receipt && receipt.status === 1) {
return (await getPointerForCw20(cw20Address)).pointer
} else if(receipt){
const output = await execute(command);
const txHash = output.replace(/.*0x/, "0x").trim()
const receipt = await provider.getTransactionReceipt(txHash);
if (receipt && receipt.status === 1){
return (await getPointerForCw20(cw20Address)).pointer
}
throw new Error("contract deployment failed")
}
await sleep(500)
attempt++
}
throw new Error("contract deployment failed")
return await verifyPointerDeployment(provider, txHash, cw20Address, command, attempts, attempt, 'cw20');
}

async function deployErc20PointerNative(provider, name, from=adminKeyName, evmRpc="") {
Expand All @@ -246,19 +229,10 @@ async function deployErc20PointerNative(provider, name, from=adminKeyName, evmRp
command = command + ` --evm-rpc=${evmRpc}`
}
const output = await execute(command);
console.log(output);
const txHash = output.replace(/.*0x/, "0x").trim()
let attempt = 0;
while(attempt < 15) {
const receipt = await provider.getTransactionReceipt(txHash);
console.log(`Receipt ${attempt}: ${receipt}`)
if(receipt) {
return (await getPointerForNative(name)).pointer
}
await sleep(500)
attempt++
}
throw new Error("contract deployment failed")
let attempts = 10;
return await verifyPointerDeployment(provider, txHash, name, command, attempts, attempt, 'native');
}

async function deployErc721PointerForCw721(provider, cw721Address, from=adminKeyName, evmRpc="") {
Expand All @@ -269,17 +243,8 @@ async function deployErc721PointerForCw721(provider, cw721Address, from=adminKey
const output = await execute(command);
const txHash = output.replace(/.*0x/, "0x").trim()
let attempt = 0;
while(attempt < 10) {
const receipt = await provider.getTransactionReceipt(txHash);
if(receipt && receipt.status === 1) {
return (await getPointerForCw721(cw721Address)).pointer
} else if(receipt){
throw new Error("contract deployment failed")
}
await sleep(500)
attempt++
}
throw new Error("contract deployment failed")
let attempts = 10;
return await verifyPointerDeployment(provider, txHash, cw721Address, command, attempts, attempt, 'cw721');
}

async function deployWasm(path, adminAddr, label, args = {}, from=adminKeyName) {
Expand Down Expand Up @@ -446,16 +411,13 @@ function execCommand(command) {
return new Promise((resolve, reject) => {
exec(command, (error, stdout, stderr) => {
if (error) {
console.log(error);
reject(error);
return;
}
if (stderr) {
console.log(stderr);
reject(new Error(stderr));
return;
}
console.log('stdout is ', stdout);
resolve(stdout);
});
})
Expand All @@ -470,6 +432,31 @@ async function waitForReceipt(txHash) {
return receipt
}

async function verifyPointerDeployment(provider, txHash, address, command, attempts, attempt, contractType){
while(attempt < attempts) {
const receipt = await provider.getTransactionReceipt(txHash);
if(receipt && receipt.status === 1) {
if (contractType === 'cw20') {
return (await getPointerForCw20(address)).pointer
} else if (contractType === 'cw721'){
return (await getPointerForCw721(address)).pointer
} else {
return (await getPointerForNative(address)).pointer

}
} else if(receipt){
console.log('Deployment failed, retrying one more time');
const output = await execute(command);
const txHash = output.replace(/.*0x/, "0x").trim()
await verifyPointerDeployment(provider, txHash, address, command, attempts, attempt, isNative);
}
await sleep(500);
console.log(attempt)
attempt++
}
throw new Error("contract deployment failed")
}

module.exports = {
fundAddress,
fundSeiAddress,
Expand Down
10 changes: 7 additions & 3 deletions integration_test/dapp_tests/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const {chainIds, rpcUrls, evmRpcUrls} = require("./constants");
const {expect} = require("chai");
const {existsSync, readFileSync, writeFileSync} = require("node:fs");
const {CosmWasmClient} = require("@cosmjs/cosmwasm-stargate");
const {BigNumber} = require("ethers");

async function deployTokenPool(managerContract, firstTokenAddr, secondTokenAddr, swapRatio=1, fee=3000) {
const sqrtPriceX96 = BigInt(Math.sqrt(swapRatio) * (2 ** 96)); // Initial price (1:1)
Expand Down Expand Up @@ -213,7 +214,7 @@ async function deployUniswapContracts(deployer, testChain, accounts){
// Gets the amount of WETH9 required to instantiate pools by depositing Sei to the contract
let gasEstimate = await weth9.estimateGas.deposit({ value: amountETH })
let gasPrice = await deployer.getGasPrice();
const txWrap = await weth9.deposit({ value: amountETH, gasPrice, gasLimit: gasEstimate });
const txWrap = await weth9.deposit({ value: amountETH, gasPrice, gasLimit: BigNumber.from(gasEstimate).add('100000')});
await txWrap.wait();
console.log(`Deposited ${amountETH.toString()} to WETH9`);

Expand Down Expand Up @@ -263,13 +264,15 @@ async function deployCw20WithPointer(deployerSeiAddr, signer, time, evmRpc="") {
}

async function deployCw721WithPointer(deployerSeiAddr, signer, time, evmRpc="") {
console.log('Deploying cw721 pointer');
const CW721_BASE_PATH = (await isDocker()) ? '../integration_test/dapp_tests/nftMarketplace/cw721_base.wasm' : path.resolve(__dirname, '../dapp_tests/nftMarketplace/cw721_base.wasm')
const cw721Address = await deployWasm(CW721_BASE_PATH, deployerSeiAddr, "cw721", {
"name": `testCw721${time}`,
"symbol": "TESTNFT",
"minter": deployerSeiAddr,
"withdraw_address": deployerSeiAddr,
}, deployerSeiAddr);
console.log('Wasm deployed and initialized');
const pointerAddr = await deployErc721PointerForCw721(hre.ethers.provider, cw721Address, deployerSeiAddr, evmRpc);
const pointerContract = new hre.ethers.Contract(pointerAddr, ABI.ERC721, signer);
return {"pointerContract": pointerContract, "cw721Address": cw721Address}
Expand All @@ -280,7 +283,7 @@ async function deployEthersContract(name, abi, bytecode, deployer, deployParams=
const deployTx = contract.getDeployTransaction(...deployParams);
const gasEstimate = await deployer.estimateGas(deployTx);
const gasPrice = await deployer.getGasPrice();
const deployed = await contract.deploy(...deployParams, {gasPrice, gasLimit: gasEstimate});
const deployed = await contract.deploy(...deployParams, {gasPrice, gasLimit: BigNumber.from(gasEstimate).add('100000')});
await deployed.deployed();
console.log(`${name} deployed to:`, deployed.address);
return deployed;
Expand Down Expand Up @@ -332,6 +335,7 @@ async function estimateAndCall(contract, method, args=[], value=0) {
console.error("Error fulfilling order:", error);
}
}
gasLimit = BigNumber.from(gasLimit).add('100000');
const gasPrice = await contract.signer.getGasPrice();
let output;
if (value) {
Expand Down Expand Up @@ -703,7 +707,7 @@ async function deployContractsForNftTests(deployer, testChain, accounts){

const numNftsToMint = 50
await estimateAndCall(erc721token, "batchMint", [deployer.address, numNftsToMint]);

console.log('Batch minted');
// Deploy CW721 token with ERC721 pointer
const time = Date.now().toString();
const deployerSeiAddr = await getSeiAddress(deployer.address);
Expand Down

0 comments on commit ae14a56

Please sign in to comment.