Skip to content

Commit

Permalink
build: add deployment cost in gas benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
CJ42 committed Sep 29, 2023
1 parent f099ac2 commit 41ddf39
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/benchmark.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,4 @@ jobs:
with:
token: ${{ secrets.GITHUB_TOKEN }}
issue-number: ${{ github.event.pull_request.number }}
body-file: "./benchmark.md"
body-file: "./gas_benchmark.md"
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ out/
forge-cache/

# generated gas benchmark
benchmark.md
gas_benchmark.md

# Exclude build output folders
/common
Expand Down
32 changes: 29 additions & 3 deletions scripts/ci/gas_benchmark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ task('gas-benchmark', 'Benchmark gas usage of the smart contracts based on prede
const currentBenchmark = JSON.parse(fs.readFileSync(args.compare, 'utf8'));
const baseBenchmark = JSON.parse(fs.readFileSync(args.against, 'utf8'));

const deploymentCosts: Row[] = [];

const casesEOAExecute: Row[] = [];
const casesEOASetData: Row[] = [];
const casesEOATokens: Row[] = [];
Expand All @@ -40,6 +42,22 @@ task('gas-benchmark', 'Benchmark gas usage of the smart contracts based on prede
return `${formatNumber(gasDiff)} ${emoji}`;
};

// Deployment costs
for (const [key, value] of Object.entries(currentBenchmark['deployment_costs'])) {
const gasCost: any = value;
const gasDiff = gasCost - baseBenchmark['deployment_costs'][key];

deploymentCosts.push([key, value + ` (${displayGasDiff(gasDiff)})`]);
}

const generatedDeploymentCostsTable = getMarkdownTable({
table: {
head: ['Deployed contracts', '⛽ Deployment cost'],
body: deploymentCosts,
},
alignment: [Align.Left],
});

// EOA - execute
for (const [key, value] of Object.entries(
currentBenchmark['runtime_costs']['EOA_owner']['execute'],
Expand Down Expand Up @@ -162,8 +180,16 @@ task('gas-benchmark', 'Benchmark gas usage of the smart contracts based on prede
⛽ I am the Gas Bot Reporter. I keep track of the gas costs of common interactions using Universal Profiles 🆙 !
📊 Here is a summary of the gas cost with the code introduced by this PR.
## ⛽📊 Gas Benchmark Report
### Deployment Costs
${generatedDeploymentCostsTable}
### Runtime Costs
<details>
<summary>⛽📊 See Gas Benchmark report of Using UniversalProfile owned by an 🔑 EOA</summary>
<summary>UniversalProfile owned by an 🔑 EOA</summary>
### 🔀 \`execute\` scenarios
Expand All @@ -180,7 +206,7 @@ ${generatedEOATokensTable}
</details>
<details>
<summary>⛽📊 See Gas Benchmark report of Using UniversalProfile owned by an 🔒📄 LSP6KeyManager</summary>
<summary>UniversalProfile owned by a 🔒📄 LSP6KeyManager</summary>
### 🔀 \`execute\` scenarios
Expand All @@ -194,7 +220,7 @@ ${generatedKeyManagerSetDataTable}
`;

const file = 'new_gas_benchmark.md';
const file = 'gas_benchmark.md';

fs.writeFileSync(file, markdownContent, 'utf8');
});
10 changes: 5 additions & 5 deletions scripts/ci/gas_benchmark_template.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"deployment_costs": {
"Universal Profile": "",
"Key Manager": "",
"LSP1 Delegate UP": "",
"LSP7 Digital Asset": "",
"LSP8 Identifiable Digital Asset": ""
"UniversalProfile": "",
"KeyManager": "",
"LSP1DelegateUP": "",
"LSP7Mintable": "",
"LSP8Mintable": ""
},
"runtime_costs": {
"EOA_owner": {
Expand Down
72 changes: 68 additions & 4 deletions tests/Benchmark.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import { expect } from 'chai';
import { SignerWithAddress } from '@nomiclabs/hardhat-ethers/signers';

import {
LSP1UniversalReceiverDelegateUP,
LSP1UniversalReceiverDelegateUP__factory,
LSP6KeyManager,
LSP6KeyManager__factory,
LSP7Mintable,
LSP7Mintable__factory,
Expand Down Expand Up @@ -79,6 +81,69 @@ describe('⛽📊 Gas Benchmark', () => {
fs.writeFileSync('./gas_benchmark_result.json', JSON.stringify(gasBenchmark, null, 2));
});

describe('Deployment costs', () => {
it('deploy contracts + save deployment costs', async () => {
const accounts = await ethers.getSigners();

// Universal Profile
const universalProfile = await new UniversalProfile__factory(accounts[0]).deploy(
accounts[0].address,
);

const universalProfileDeployTransaction = universalProfile.deployTransaction;
const universalProfileDeploymentReceipt = await universalProfileDeployTransaction.wait();

gasBenchmark['deployment_costs']['UniversalProfile'] =
universalProfileDeploymentReceipt.gasUsed.toNumber();

// Key Manager
const keyManager = await new LSP6KeyManager__factory(accounts[0]).deploy(
universalProfile.address,
);

const keyManagerDeployTransaction = keyManager.deployTransaction;
const keyManagerDeploymentReceipt = await keyManagerDeployTransaction?.wait();

gasBenchmark['deployment_costs']['KeyManager'] =
keyManagerDeploymentReceipt?.gasUsed.toNumber();

// LSP1 Delegate
const lsp1Delegate = await new LSP1UniversalReceiverDelegateUP__factory(accounts[0]).deploy();

const lsp1DelegateDeployTransaction = lsp1Delegate.deployTransaction;
const lsp1DelegateDeploymentReceipt = await lsp1DelegateDeployTransaction.wait();

gasBenchmark['deployment_costs']['LSP1DelegateUP'] =
lsp1DelegateDeploymentReceipt.gasUsed.toNumber();

// LSP7 Token (Mintable preset)
const lsp7Mintable = await new LSP7Mintable__factory(accounts[0]).deploy(
'Token',
'MTKN',
accounts[0].address,
false,
);

const lsp7DeployTransaction = lsp7Mintable.deployTransaction;
const lsp7DeploymentReceipt = await lsp7DeployTransaction.wait();

gasBenchmark['deployment_costs']['LSP7Mintable'] = lsp7DeploymentReceipt.gasUsed.toNumber();

// LSP8 NFT (Mintable preset)
const lsp8Mintable = await new LSP8Mintable__factory(accounts[0]).deploy(
'My NFT',
'MNFT',
accounts[0].address,
LSP8_TOKEN_ID_TYPES.NUMBER,
);

const lsp8DeployTransaction = lsp8Mintable.deployTransaction;
const lsp8DeploymentReceipt = await lsp8DeployTransaction.wait();

gasBenchmark['deployment_costs']['LSP8Mintable'] = lsp8DeploymentReceipt.gasUsed.toNumber();
});
});

describe('UniversalProfile', () => {
let context: UniversalProfileContext;

Expand Down Expand Up @@ -154,7 +219,7 @@ describe('⛽📊 Gas Benchmark', () => {
});

describe('execute Array', () => {
let universalProfile1, universalProfile2, universalProfile3;
let universalProfile1: UniversalProfile, universalProfile2, universalProfile3;

before(async () => {
context = await buildUniversalProfileContext(ethers.utils.parseEther('50'));
Expand Down Expand Up @@ -615,9 +680,8 @@ describe('⛽📊 Gas Benchmark', () => {
const deployedContracts = await setupProfileWithKeyManagerWithURD(context.accounts[2]);
aliceUP = deployedContracts[0] as UniversalProfile;

const lsp1Delegate = await new LSP1UniversalReceiverDelegateUP__factory(
context.accounts[0],
).deploy();
const lsp1Delegate: LSP1UniversalReceiverDelegateUP =
await new LSP1UniversalReceiverDelegateUP__factory(context.accounts[0]).deploy();

// the function `setupKeyManager` gives ALL PERMISSIONS to the owner as the first data key
// We also setup the following:
Expand Down

0 comments on commit 41ddf39

Please sign in to comment.