Skip to content

Commit c94478b

Browse files
committed
refactor(docs): improve formatting and structure in Firelight vault scripts
1 parent dc9d13f commit c94478b

File tree

5 files changed

+615
-388
lines changed

5 files changed

+615
-388
lines changed

examples/developer-hub-javascript/firelight-deposit.ts

Lines changed: 83 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* FirelightVault Deposit Script
3-
*
3+
*
44
* This script deposits assets into the FirelightVault (ERC-4626).
55
* It approves tokens and deposits the specified amount, receiving vault shares in return.
66
*/
@@ -10,88 +10,121 @@ import type { IFirelightVaultInstance } from "../../typechain-types/contracts/fi
1010
import type { ERC20Instance } from "../../typechain-types/@openzeppelin/contracts/token/ERC20/ERC20";
1111
import { bnToBigInt } from "../utils/core";
1212

13-
export const FIRELIGHT_VAULT_ADDRESS = "0x91Bfe6A68aB035DFebb6A770FFfB748C03C0E40B";
13+
export const FIRELIGHT_VAULT_ADDRESS =
14+
"0x91Bfe6A68aB035DFebb6A770FFfB748C03C0E40B";
1415

1516
export const IFirelightVault = artifacts.require("IFirelightVault");
1617

1718
const tokensToDeposit = 1; // Number of tokens to deposit
1819

1920
// @ts-expect-error - Type definitions issue, but works at runtime
20-
const IERC20 = artifacts.require("@openzeppelin/contracts/token/ERC20/ERC20.sol:ERC20");
21+
const IERC20 = artifacts.require(
22+
"@openzeppelin/contracts/token/ERC20/ERC20.sol:ERC20",
23+
);
2124

2225
async function getAccount() {
23-
const [signer] = await ethers.getSigners();
24-
return { signer, account: signer.address };
26+
const [signer] = await ethers.getSigners();
27+
return { signer, account: signer.address };
2528
}
2629

2730
async function getVaultAndAsset() {
28-
const vault = await IFirelightVault.at(FIRELIGHT_VAULT_ADDRESS) as IFirelightVaultInstance;
29-
const assetAddress = await vault.asset();
30-
const assetToken = await IERC20.at(assetAddress) as ERC20Instance;
31-
return { vault, assetAddress, assetToken };
31+
const vault = (await IFirelightVault.at(
32+
FIRELIGHT_VAULT_ADDRESS,
33+
)) as IFirelightVaultInstance;
34+
const assetAddress = await vault.asset();
35+
const assetToken = (await IERC20.at(assetAddress)) as ERC20Instance;
36+
return { vault, assetAddress, assetToken };
3237
}
3338

3439
async function getAssetInfo(assetToken: ERC20Instance) {
35-
const symbol = await assetToken.symbol();
36-
const assetDecimals = (await assetToken.decimals()).toNumber();
37-
return { symbol, assetDecimals };
40+
const symbol = await assetToken.symbol();
41+
const assetDecimals = (await assetToken.decimals()).toNumber();
42+
return { symbol, assetDecimals };
3843
}
3944

40-
function logDepositInfo(account: string, assetAddress: string, symbol: string, assetDecimals: number, amount: bigint) {
41-
console.log("=== Deposit (ERC-4626) ===");
42-
console.log("Sender:", account);
43-
console.log("Vault:", FIRELIGHT_VAULT_ADDRESS);
44-
console.log("Asset:", assetAddress, `(${symbol}, decimals=${assetDecimals})`);
45-
console.log("Deposit amount:", amount.toString(), `(= ${tokensToDeposit} ${symbol})`);
45+
function logDepositInfo(
46+
account: string,
47+
assetAddress: string,
48+
symbol: string,
49+
assetDecimals: number,
50+
amount: bigint,
51+
) {
52+
console.log("=== Deposit (ERC-4626) ===");
53+
console.log("Sender:", account);
54+
console.log("Vault:", FIRELIGHT_VAULT_ADDRESS);
55+
console.log("Asset:", assetAddress, `(${symbol}, decimals=${assetDecimals})`);
56+
console.log(
57+
"Deposit amount:",
58+
amount.toString(),
59+
`(= ${tokensToDeposit} ${symbol})`,
60+
);
4661
}
4762

48-
async function validateDeposit(vault: IFirelightVaultInstance, account: string, amount: bigint) {
49-
const maxDeposit = bnToBigInt(await vault.maxDeposit(account));
50-
console.log("Max deposit:", maxDeposit.toString());
51-
if (amount > maxDeposit) {
52-
console.error(`Cannot deposit ${amount.toString()} assets. Max allowed: ${maxDeposit.toString()}`);
53-
process.exit(1);
54-
}
63+
async function validateDeposit(
64+
vault: IFirelightVaultInstance,
65+
account: string,
66+
amount: bigint,
67+
) {
68+
const maxDeposit = bnToBigInt(await vault.maxDeposit(account));
69+
console.log("Max deposit:", maxDeposit.toString());
70+
if (amount > maxDeposit) {
71+
console.error(
72+
`Cannot deposit ${amount.toString()} assets. Max allowed: ${maxDeposit.toString()}`,
73+
);
74+
process.exit(1);
75+
}
5576
}
5677

57-
async function approveTokens(assetToken: ERC20Instance, vault: IFirelightVaultInstance, amount: bigint, account: string) {
58-
const approveTx = await assetToken.approve(vault.address, amount.toString(), { from: account });
59-
console.log("Approve tx:", approveTx.tx);
78+
async function approveTokens(
79+
assetToken: ERC20Instance,
80+
vault: IFirelightVaultInstance,
81+
amount: bigint,
82+
account: string,
83+
) {
84+
const approveTx = await assetToken.approve(vault.address, amount.toString(), {
85+
from: account,
86+
});
87+
console.log("Approve tx:", approveTx.tx);
6088
}
6189

62-
async function executeDeposit(vault: IFirelightVaultInstance, amount: bigint, account: string) {
63-
const depositTx = await vault.deposit(amount.toString(), account, { from: account });
64-
console.log("Deposit tx:", depositTx.tx);
90+
async function executeDeposit(
91+
vault: IFirelightVaultInstance,
92+
amount: bigint,
93+
account: string,
94+
) {
95+
const depositTx = await vault.deposit(amount.toString(), account, {
96+
from: account,
97+
});
98+
console.log("Deposit tx:", depositTx.tx);
6599
}
66100

67101
async function main() {
68-
// 1. Get the account
69-
const { account } = await getAccount();
102+
// 1. Get the account
103+
const { account } = await getAccount();
70104

71-
// 2. Get the vault and asset token
72-
const { vault, assetAddress, assetToken } = await getVaultAndAsset();
105+
// 2. Get the vault and asset token
106+
const { vault, assetAddress, assetToken } = await getVaultAndAsset();
73107

74-
// 3. Get asset info (symbol, decimals)
75-
const { symbol, assetDecimals } = await getAssetInfo(assetToken);
108+
// 3. Get asset info (symbol, decimals)
109+
const { symbol, assetDecimals } = await getAssetInfo(assetToken);
76110

77-
// 4. Calculate the deposit amount
78-
const depositAmount = BigInt(tokensToDeposit * (10 ** assetDecimals));
111+
// 4. Calculate the deposit amount
112+
const depositAmount = BigInt(tokensToDeposit * 10 ** assetDecimals);
79113

80-
// 5. Log deposit info
81-
logDepositInfo(account, assetAddress, symbol, assetDecimals, depositAmount);
114+
// 5. Log deposit info
115+
logDepositInfo(account, assetAddress, symbol, assetDecimals, depositAmount);
82116

83-
// 6. Validate the deposit (check max deposit)
84-
await validateDeposit(vault, account, depositAmount);
117+
// 6. Validate the deposit (check max deposit)
118+
await validateDeposit(vault, account, depositAmount);
85119

86-
// 7. Approve tokens for transfer
87-
await approveTokens(assetToken, vault, depositAmount, account);
120+
// 7. Approve tokens for transfer
121+
await approveTokens(assetToken, vault, depositAmount, account);
88122

89-
// 8. Execute the deposit
90-
await executeDeposit(vault, depositAmount, account);
123+
// 8. Execute the deposit
124+
await executeDeposit(vault, depositAmount, account);
91125
}
92126

93127
main().catch((error) => {
94-
console.error(error);
95-
process.exitCode = 1;
128+
console.error(error);
129+
process.exitCode = 1;
96130
});
97-

examples/developer-hub-javascript/firelight-mint.ts

Lines changed: 92 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* FirelightVault Mint Script
3-
*
3+
*
44
* This script mints vault shares (ERC-4626) by depositing assets into the FirelightVault.
55
* It checks max mint capacity, calculates required assets, approves tokens, and mints shares.
66
*/
@@ -10,97 +10,133 @@ import { bnToBigInt } from "../utils/core";
1010
import type { IFirelightVaultInstance } from "../../typechain-types/contracts/firelight/IFirelightVault";
1111
import type { ERC20Instance } from "../../typechain-types/@openzeppelin/contracts/token/ERC20/ERC20";
1212

13-
export const FIRELIGHT_VAULT_ADDRESS = "0x91Bfe6A68aB035DFebb6A770FFfB748C03C0E40B";
13+
export const FIRELIGHT_VAULT_ADDRESS =
14+
"0x91Bfe6A68aB035DFebb6A770FFfB748C03C0E40B";
1415

1516
const sharesToMint = 1; // Number of shares to mint
1617

1718
// @ts-expect-error - Type definitions issue, but works at runtime
18-
const IERC20 = artifacts.require("@openzeppelin/contracts/token/ERC20/ERC20.sol:ERC20");
19+
const IERC20 = artifacts.require(
20+
"@openzeppelin/contracts/token/ERC20/ERC20.sol:ERC20",
21+
);
1922

2023
const FirelightVault = artifacts.require("IFirelightVault");
2124

2225
async function getAccount() {
23-
const [signer] = await ethers.getSigners();
24-
return { signer, account: signer.address };
26+
const [signer] = await ethers.getSigners();
27+
return { signer, account: signer.address };
2528
}
2629

2730
async function getVaultAndAsset() {
28-
const vault = await FirelightVault.at(FIRELIGHT_VAULT_ADDRESS) as IFirelightVaultInstance;
29-
const assetAddress = await vault.asset();
30-
const assetToken = await IERC20.at(assetAddress) as ERC20Instance;
31-
return { vault, assetAddress, assetToken };
31+
const vault = (await FirelightVault.at(
32+
FIRELIGHT_VAULT_ADDRESS,
33+
)) as IFirelightVaultInstance;
34+
const assetAddress = await vault.asset();
35+
const assetToken = (await IERC20.at(assetAddress)) as ERC20Instance;
36+
return { vault, assetAddress, assetToken };
3237
}
3338

3439
async function getAssetInfo(assetToken: ERC20Instance) {
35-
const symbol = await assetToken.symbol();
36-
const assetDecimals = (await assetToken.decimals()).toNumber();
37-
return { symbol, assetDecimals };
40+
const symbol = await assetToken.symbol();
41+
const assetDecimals = (await assetToken.decimals()).toNumber();
42+
return { symbol, assetDecimals };
3843
}
3944

40-
function logMintInfo(account: string, assetAddress: string, symbol: string, assetDecimals: number, sharesAmount: bigint) {
41-
console.log("=== Mint vault shares (ERC-4626) ===");
42-
console.log("Sender:", account);
43-
console.log("Vault:", FIRELIGHT_VAULT_ADDRESS);
44-
console.log("Asset:", assetAddress, `(${symbol}, decimals=${assetDecimals})`);
45-
console.log("Shares to mint:", sharesAmount.toString(), `(= ${sharesToMint} share${sharesToMint > 1 ? 's' : ''})`);
45+
function logMintInfo(
46+
account: string,
47+
assetAddress: string,
48+
symbol: string,
49+
assetDecimals: number,
50+
sharesAmount: bigint,
51+
) {
52+
console.log("=== Mint vault shares (ERC-4626) ===");
53+
console.log("Sender:", account);
54+
console.log("Vault:", FIRELIGHT_VAULT_ADDRESS);
55+
console.log("Asset:", assetAddress, `(${symbol}, decimals=${assetDecimals})`);
56+
console.log(
57+
"Shares to mint:",
58+
sharesAmount.toString(),
59+
`(= ${sharesToMint} share${sharesToMint > 1 ? "s" : ""})`,
60+
);
4661
}
4762

48-
async function validateMint(vault: IFirelightVaultInstance, account: string, sharesAmount: bigint) {
49-
const maxMint = bnToBigInt(await vault.maxMint(account));
50-
console.log("Max mint:", maxMint.toString());
51-
if (sharesAmount > maxMint) {
52-
console.error(`Cannot mint ${sharesAmount.toString()} shares. Max allowed: ${maxMint.toString()}`);
53-
process.exit(1);
54-
}
63+
async function validateMint(
64+
vault: IFirelightVaultInstance,
65+
account: string,
66+
sharesAmount: bigint,
67+
) {
68+
const maxMint = bnToBigInt(await vault.maxMint(account));
69+
console.log("Max mint:", maxMint.toString());
70+
if (sharesAmount > maxMint) {
71+
console.error(
72+
`Cannot mint ${sharesAmount.toString()} shares. Max allowed: ${maxMint.toString()}`,
73+
);
74+
process.exit(1);
75+
}
5576
}
5677

57-
async function calculateAssetsNeeded(vault: IFirelightVaultInstance, sharesAmount: bigint) {
58-
const assetsNeeded = await vault.previewMint(sharesAmount.toString());
59-
console.log("Assets needed (from previewMint):", assetsNeeded.toString());
60-
return assetsNeeded;
78+
async function calculateAssetsNeeded(
79+
vault: IFirelightVaultInstance,
80+
sharesAmount: bigint,
81+
) {
82+
const assetsNeeded = await vault.previewMint(sharesAmount.toString());
83+
console.log("Assets needed (from previewMint):", assetsNeeded.toString());
84+
return assetsNeeded;
6185
}
6286

63-
async function approveTokens(assetToken: ERC20Instance, vault: IFirelightVaultInstance, amount: bigint, account: string) {
64-
const approveTx = await assetToken.approve(vault.address, amount.toString(), { from: account });
65-
console.log("Approve tx:", approveTx.tx);
87+
async function approveTokens(
88+
assetToken: ERC20Instance,
89+
vault: IFirelightVaultInstance,
90+
amount: bigint,
91+
account: string,
92+
) {
93+
const approveTx = await assetToken.approve(vault.address, amount.toString(), {
94+
from: account,
95+
});
96+
console.log("Approve tx:", approveTx.tx);
6697
}
6798

68-
async function executeMint(vault: IFirelightVaultInstance, sharesAmount: bigint, account: string) {
69-
const mintTx = await vault.mint(sharesAmount.toString(), account, { from: account });
70-
console.log("Mint tx:", mintTx.tx);
99+
async function executeMint(
100+
vault: IFirelightVaultInstance,
101+
sharesAmount: bigint,
102+
account: string,
103+
) {
104+
const mintTx = await vault.mint(sharesAmount.toString(), account, {
105+
from: account,
106+
});
107+
console.log("Mint tx:", mintTx.tx);
71108
}
72109

73110
async function main() {
74-
// 1. Get the account
75-
const { account } = await getAccount();
111+
// 1. Get the account
112+
const { account } = await getAccount();
76113

77-
// 2. Get the vault and asset token
78-
const { vault, assetAddress, assetToken } = await getVaultAndAsset();
114+
// 2. Get the vault and asset token
115+
const { vault, assetAddress, assetToken } = await getVaultAndAsset();
79116

80-
// 3. Get asset info (symbol, decimals)
81-
const { symbol, assetDecimals } = await getAssetInfo(assetToken);
117+
// 3. Get asset info (symbol, decimals)
118+
const { symbol, assetDecimals } = await getAssetInfo(assetToken);
82119

83-
// 4. Calculate the shares amount to mint
84-
const sharesAmount = BigInt(sharesToMint * (10 ** assetDecimals));
120+
// 4. Calculate the shares amount to mint
121+
const sharesAmount = BigInt(sharesToMint * 10 ** assetDecimals);
85122

86-
// 5. Log mint info
87-
logMintInfo(account, assetAddress, symbol, assetDecimals, sharesAmount);
123+
// 5. Log mint info
124+
logMintInfo(account, assetAddress, symbol, assetDecimals, sharesAmount);
88125

89-
// 6. Validate the mint (check max mint)
90-
await validateMint(vault, account, sharesAmount);
126+
// 6. Validate the mint (check max mint)
127+
await validateMint(vault, account, sharesAmount);
91128

92-
// 7. Calculate assets needed using previewMint
93-
const assetsNeeded = await calculateAssetsNeeded(vault, sharesAmount);
129+
// 7. Calculate assets needed using previewMint
130+
const assetsNeeded = await calculateAssetsNeeded(vault, sharesAmount);
94131

95-
// 8. Approve tokens for transfer
96-
await approveTokens(assetToken, vault, bnToBigInt(assetsNeeded), account);
132+
// 8. Approve tokens for transfer
133+
await approveTokens(assetToken, vault, bnToBigInt(assetsNeeded), account);
97134

98-
// 9. Execute the mint
99-
await executeMint(vault, sharesAmount, account);
135+
// 9. Execute the mint
136+
await executeMint(vault, sharesAmount, account);
100137
}
101138

102139
main().catch((error) => {
103-
console.error(error);
104-
process.exitCode = 1;
140+
console.error(error);
141+
process.exitCode = 1;
105142
});
106-

0 commit comments

Comments
 (0)