Skip to content

Commit

Permalink
feature: added force transactions config value that will force tx eve…
Browse files Browse the repository at this point in the history
…n when the gas estimate failed

feature: added a skip for eip1559 support on a defined network
feature: added unified estimate gas function that will add slippage if needed
  • Loading branch information
H34D committed Nov 21, 2023
1 parent 9e0b913 commit db2c329
Show file tree
Hide file tree
Showing 22 changed files with 315 additions and 299 deletions.
10 changes: 5 additions & 5 deletions package.json
Expand Up @@ -41,13 +41,13 @@
"axios": "^1.3.2",
"ethers": "~5.7.2",
"grapheme-splitter": "^1.0.4",
"typescript": "^5.0.2"
"typescript": "^5.3.2"
},
"devDependencies": {
"@types/chai": "^4.3.6",
"@types/mocha": "^10.0.4",
"@typescript-eslint/eslint-plugin": "^6.11.0",
"@typescript-eslint/parser": "^6.9.1",
"@types/chai": "^4.3.11",
"@types/mocha": "^10.0.5",
"@typescript-eslint/eslint-plugin": "^6.12.0",
"@typescript-eslint/parser": "^6.12.0",
"chai": "^4.3.7",
"circular-dependency-plugin": "^5.2.2",
"cross-env": "^7.0.3",
Expand Down
1 change: 1 addition & 0 deletions src/collections/networks.ts
Expand Up @@ -180,6 +180,7 @@ const polygon: Network = {
blockExplorerUrls: ["https://polygonscan.com"],
blockExplorerApiUrls: ["https://api.polygonscan.com/api"],
addresses: addresses["polygon"] as Addresses,
skipEip1559: true,
};
const mumbai: Network = {
networkName: "mumbai",
Expand Down
38 changes: 14 additions & 24 deletions src/contracts/contract-modules/credit-score.ts
Expand Up @@ -103,7 +103,7 @@ export class CreditScore extends MasaSBTModuleBase {
isNativeCurrency(paymentMethod) ? price : undefined,
);

const creditScoreMintParametersIdentity: [
const creditScoreMintParameters: [
string,
BigNumber,
string,
Expand All @@ -126,33 +126,27 @@ export class CreditScore extends MasaSBTModuleBase {
} = this.instances.SoulboundCreditScoreContract;

// estimate gas
let gasLimit: BigNumber = await estimateGas(
...creditScoreMintParametersIdentity,
const gasLimit = await this.estimateGasWithSlippage(
estimateGas,
creditScoreMintParameters,
creditScoreMintOverrides,
);

if (this.masa.config.network?.gasSlippagePercentage) {
gasLimit = CreditScore.addSlippage(
gasLimit,
this.masa.config.network.gasSlippagePercentage,
);
}

const creditScoreMintOverridesWithGasLimit = {
...creditScoreMintOverrides,
gasLimit,
};

if (this.masa.config.verbose) {
console.info({
creditScoreMintParametersIdentity,
creditScoreMintParameters,
creditScoreMintOverridesWithGasLimit,
});
}

// execute
return mint(
...creditScoreMintParametersIdentity,
...creditScoreMintParameters,
creditScoreMintOverridesWithGasLimit,
);
};
Expand Down Expand Up @@ -212,19 +206,15 @@ export class CreditScore extends MasaSBTModuleBase {
public burn = async (creditScoreId: BigNumber): Promise<boolean> => {
console.log(`Burning Credit Score with ID '${creditScoreId}'!`);

const {
estimateGas: { burn: estimateGas },
burn,
} = this.masa.contracts.instances.SoulboundCreditScoreContract;

try {
const {
estimateGas: { burn: estimateGas },
burn,
} = this.masa.contracts.instances.SoulboundCreditScoreContract;

let gasLimit: BigNumber = await estimateGas(creditScoreId);
if (this.masa.config.network?.gasSlippagePercentage) {
gasLimit = CreditScore.addSlippage(
gasLimit,
this.masa.config.network.gasSlippagePercentage,
);
}
const gasLimit = await this.estimateGasWithSlippage(estimateGas, [
creditScoreId,
]);

const { wait, hash } = await burn(creditScoreId, {
gasLimit,
Expand Down
22 changes: 6 additions & 16 deletions src/contracts/contract-modules/green.ts
Expand Up @@ -154,18 +154,12 @@ export class Green extends MasaSBTModuleBase {
} = this.instances.SoulboundGreenContract;

// estimate gas
let gasLimit: BigNumber = await estimateGas(
...greenMintParameters,
const gasLimit = await this.estimateGasWithSlippage(
estimateGas,
greenMintParameters,
greenMintOverrides,
);

if (this.masa.config.network?.gasSlippagePercentage) {
gasLimit = Green.addSlippage(
gasLimit,
this.masa.config.network.gasSlippagePercentage,
);
}

// execute
return mint(...greenMintParameters, { ...greenMintOverrides, gasLimit });
};
Expand Down Expand Up @@ -231,14 +225,10 @@ export class Green extends MasaSBTModuleBase {
burn,
} = this.masa.contracts.instances.SoulboundGreenContract;

let gasLimit: BigNumber = await estimateGas(greenId);
const gasLimit = await this.estimateGasWithSlippage(estimateGas, [
greenId,
]);

if (this.masa.config.network?.gasSlippagePercentage) {
gasLimit = Green.addSlippage(
gasLimit,
this.masa.config.network.gasSlippagePercentage,
);
}
const { wait, hash } = await burn(greenId, {
gasLimit,
});
Expand Down
32 changes: 7 additions & 25 deletions src/contracts/contract-modules/identity.ts
Expand Up @@ -21,14 +21,7 @@ export class Identity extends MasaSBTModuleBase {
} = this.instances.SoulStoreContract;

// estimate gas
let gasLimit: BigNumber = await estimateGas();

if (this.masa.config.network?.gasSlippagePercentage) {
gasLimit = Identity.addSlippage(
gasLimit,
this.masa.config.network.gasSlippagePercentage,
);
}
const gasLimit = await this.estimateGasWithSlippage(estimateGas);

return await purchaseIdentity({ gasLimit });
};
Expand Down Expand Up @@ -140,18 +133,12 @@ export class Identity extends MasaSBTModuleBase {
} = this.instances.SoulStoreContract;

// estimate gas
let gasLimit: BigNumber = await estimateGas(
...purchaseIdentityAndNameParameters,
const gasLimit = await this.estimateGasWithSlippage(
estimateGas,
purchaseIdentityAndNameParameters,
purchaseIdentityAndNameOverrides,
);

if (this.masa.config.network?.gasSlippagePercentage) {
gasLimit = Identity.addSlippage(
gasLimit,
this.masa.config.network.gasSlippagePercentage,
);
}

// execute tx
return purchaseIdentityAndName(...purchaseIdentityAndNameParameters, {
...purchaseIdentityAndNameOverrides,
Expand All @@ -174,14 +161,9 @@ export class Identity extends MasaSBTModuleBase {
} = this.masa.contracts.instances.SoulboundIdentityContract;

// estimate gas
let gasLimit: BigNumber = await estimateGas(identityId);

if (this.masa.config.network?.gasSlippagePercentage) {
gasLimit = Identity.addSlippage(
gasLimit,
this.masa.config.network.gasSlippagePercentage,
);
}
const gasLimit = await this.estimateGasWithSlippage(estimateGas, [
identityId,
]);

const { wait, hash } = await burn(identityId, { gasLimit });

Expand Down
67 changes: 59 additions & 8 deletions src/contracts/contract-modules/masa-module-base.ts
Expand Up @@ -52,13 +52,13 @@ export abstract class MasaModuleBase extends MasaBase {
let contractReceipt;

if (isERC20Currency(paymentMethod)) {
const contract: ERC20 = ERC20__factory.connect(
const tokenContract: ERC20 = ERC20__factory.connect(
paymentAddress,
this.masa.config.signer,
);

// get current allowance
const currentAllowance = await contract.allowance(
const currentAllowance = await tokenContract.allowance(
// owner
await this.masa.config.signer.getAddress(),
// spender
Expand All @@ -75,11 +75,22 @@ export abstract class MasaModuleBase extends MasaBase {
);
}

const { wait, hash } = await contract.approve(
const {
approve,
estimateGas: { approve: estimateGas },
} = tokenContract;

const gasLimit = await this.estimateGasWithSlippage(estimateGas, [
spenderAddress,
price,
]);

const { wait, hash } = await approve(
// spender
spenderAddress,
// amount
price,
{ gasLimit },
);

if (this.masa.config.verbose) {
Expand Down Expand Up @@ -125,7 +136,9 @@ export abstract class MasaModuleBase extends MasaBase {
protected createOverrides = async (
value?: BigNumber,
): Promise<PayableOverrides> => {
const feeData: FeeData | undefined = await this.getNetworkFeeInformation();
const feeData: FeeData | undefined = this.masa.config.network?.skipEip1559
? undefined
: await this.getNetworkFeeInformation();

return {
value,
Expand All @@ -148,15 +161,15 @@ export abstract class MasaModuleBase extends MasaBase {
protected getNetworkFeeInformation = async (): Promise<
FeeData | undefined
> => {
let result;
let feeData;

try {
result = await this.masa.config.signer.provider?.getFeeData();
feeData = await this.masa.config.signer.provider?.getFeeData();

if (this.masa.config.verbose) {
console.dir(
{
networkFeeInformation: result,
networkFeeInformation: feeData,
},
{
depth: null,
Expand All @@ -169,7 +182,7 @@ export abstract class MasaModuleBase extends MasaBase {
}
}

return result;
return feeData;
};

/**
Expand Down Expand Up @@ -200,6 +213,44 @@ export abstract class MasaModuleBase extends MasaBase {
return price;
};

/**
*
* @param estimateGas
* @param args
* @param overrides
*/
protected estimateGasWithSlippage = async (
estimateGas: (...estimateGasArgAndOverrides: never) => Promise<BigNumber>,
args?: unknown[],
overrides?: PayableOverrides,
): Promise<BigNumber | undefined> => {
let gasLimit: BigNumber | undefined;

try {
gasLimit = await (overrides
? estimateGas(...(args as never), overrides)
: estimateGas(...(args as never)));

if (this.masa.config.network?.gasSlippagePercentage) {
gasLimit = MasaModuleBase.addSlippage(
gasLimit,
this.masa.config.network.gasSlippagePercentage,
);
}
} catch (error: unknown) {
if (error instanceof Error) {
console.error(`Estimate gas failed! ${error.message}`);
}

if (!this.masa.config.forceTransactions) {
// don't throw if we force this
throw error;
}
}

return gasLimit;
};

/**
* verify a signature created during one of the SBT signing flows
* @param errorMessage
Expand Down
24 changes: 6 additions & 18 deletions src/contracts/contract-modules/sbt/ASBT/asbt-contract-wrapper.ts
Expand Up @@ -66,18 +66,12 @@ export class ASBTContractWrapper<
estimateGas: { "mint(address,address)": estimateGas },
} = this.contract;

let gasLimit: BigNumber = await estimateGas(
...mintASBTArguments,
const gasLimit = await this.estimateGasWithSlippage(
estimateGas,
mintASBTArguments,
mintASBTOverrides,
);

if (this.masa.config.network?.gasSlippagePercentage) {
gasLimit = ASBTContractWrapper.addSlippage(
gasLimit,
this.masa.config.network.gasSlippagePercentage,
);
}

const { wait, hash } = await mint(...mintASBTArguments, {
...mintASBTOverrides,
gasLimit,
Expand Down Expand Up @@ -169,18 +163,12 @@ export class ASBTContractWrapper<
estimateGas: { "mint(address,address[])": estimateGas },
} = this.contract;

let gasLimit: BigNumber = await estimateGas(
...mintASBTArguments,
const gasLimit = await this.estimateGasWithSlippage(
estimateGas,
mintASBTArguments,
mintASBTOverrides,
);

if (this.masa.config.network?.gasSlippagePercentage) {
gasLimit = ASBTContractWrapper.addSlippage(
gasLimit,
this.masa.config.network.gasSlippagePercentage,
);
}

const { wait, hash } = await mint(...mintASBTArguments, {
...mintASBTOverrides,
gasLimit,
Expand Down

0 comments on commit db2c329

Please sign in to comment.