Skip to content

Commit d20a30d

Browse files
committed
refactor(script): update payment calculation
1 parent 3ae7dc9 commit d20a30d

File tree

1 file changed

+40
-62
lines changed

1 file changed

+40
-62
lines changed

examples/developer-hub-javascript/fassetsReserveCollateral.ts

Lines changed: 40 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,18 @@
1-
import { ethers } from "hardhat";
2-
3-
import {
4-
IAssetManagerInstance,
5-
IAssetManagerContract,
6-
} from "../../typechain-types";
1+
import { getFXRPAssetManagerAddress } from "../utils/fassets";
2+
import { IAssetManagerInstance } from "../../typechain-types";
3+
import { logEvents } from "../../scripts/utils/core";
74

85
// 1. Define constants
96

10-
// AssetManager address on Flare Testnet Coston2 network
11-
const ASSET_MANAGER_ADDRESS = "0xDeD50DA9C3492Bee44560a4B35cFe0e778F41eC5";
127
// Number of lots to reserve
138
const LOTS_TO_MINT = 1;
14-
// XRP Ledger address
15-
const UNDERLYING_ADDRESS = "rSHYuiEvsYsKR8uUHhBTuGP5zjRcGt4nm";
16-
179
// Use zero address for executor since we're not using it
1810
const ZERO_ADDRESS = "0x0000000000000000000000000000000000000000";
1911

20-
// 2. Function to find the best agent with enough free collateral lots
12+
// 2. Get the AssetManager artifact
13+
const AssetManager = artifacts.require("IAssetManager");
14+
15+
// 3. Function to find the best agent with enough free collateral lots
2116

2217
// Function from FAssets Bot repository
2318
// https://github.com/flare-foundation/fasset-bots/blob/main/packages/fasset-bots-core/src/commands/InfoBotCommands.ts#L83
@@ -69,105 +64,88 @@ async function findBestAgent(
6964
}
7065
}
7166

72-
// 3. Function to parse the CollateralReserved event
73-
74-
async function parseCollateralReservedEvent(transactionReceipt) {
67+
// 4. Function to parse the CollateralReserved event
68+
async function parseCollateralReservedEvent(
69+
transactionReceipt: any,
70+
decimals: number,
71+
) {
7572
console.log("\nParsing events...", transactionReceipt.rawLogs);
7673

77-
const assetManager = (await ethers.getContractAt(
78-
"IAssetManager",
79-
ASSET_MANAGER_ADDRESS,
80-
)) as IAssetManagerContract;
81-
82-
for (const log of transactionReceipt.rawLogs) {
83-
try {
84-
const parsedLog = assetManager.interface.parseLog({
85-
topics: log.topics,
86-
data: log.data,
87-
});
88-
89-
if (!parsedLog) continue;
90-
91-
const collateralReservedEvents = ["CollateralReserved"];
92-
if (!collateralReservedEvents.includes(parsedLog.name)) continue;
93-
94-
console.log(`\nEvent: ${parsedLog.name}`);
95-
console.log("Arguments:", parsedLog.args);
96-
const collateralReservedEvent = parsedLog.args;
74+
// 5. The logEvents function is included in the Flare starter kit
75+
const collateralReservedEvents = logEvents(
76+
transactionReceipt.rawLogs,
77+
"CollateralReserved",
78+
AssetManager.abi,
79+
);
9780

98-
return collateralReservedEvent;
99-
} catch (e) {
100-
console.log("Error parsing event:", e);
101-
}
102-
}
81+
return collateralReservedEvents[0].decoded;
10382
}
10483

105-
// 4. Main function
106-
10784
async function main() {
108-
// Initialize the FAssets FXRP AssetManager contract
109-
const AssetManager = artifacts.require("IAssetManager");
110-
const assetManager: IAssetManagerInstance = await AssetManager.at(
111-
ASSET_MANAGER_ADDRESS,
112-
);
85+
// 6. Get the AssetManager contract from the Flare Contract Registry
86+
const assetManager: IAssetManagerInstance =
87+
await getFXRPAssetManagerAddress();
11388

114-
// 5. Find the best agent with enough free collateral lots
89+
// 7. Find the best agent with enough free collateral lots
11590
const agentVaultAddress = await findBestAgent(assetManager, LOTS_TO_MINT);
11691
if (!agentVaultAddress) {
11792
throw new Error("No suitable agent found with enough free collateral lots");
11893
}
11994
console.log(agentVaultAddress);
12095

121-
// 6. Get the agent info
96+
// 8. Get the agent info
12297
const agentInfo = await assetManager.getAgentInfo(agentVaultAddress);
12398
console.log("Agent info:", agentInfo);
12499

125-
// 7. Get the collateral reservation fee according to the number of lots to reserve
100+
// 9. Get the collateral reservation fee according to the number of lots to reserve
126101
// https://dev.flare.network/fassets/minting/#collateral-reservation-fee
127102
const collateralReservationFee =
128103
await assetManager.collateralReservationFee(LOTS_TO_MINT);
129104
console.log(
130105
"Collateral reservation fee:",
131106
collateralReservationFee.toString(),
132107
);
108+
console.log("agentVaultAddress", agentVaultAddress);
109+
console.log("LOTS_TO_MINT", LOTS_TO_MINT);
110+
console.log("agentInfo.feeBIPS", agentInfo.feeBIPS);
111+
console.log("ZERO_ADDRESS", ZERO_ADDRESS);
133112

134-
const IAssetManager = artifacts.require("IAssetManager");
135-
const iAssetManager: IAssetManagerInstance = await IAssetManager.at(
136-
ASSET_MANAGER_ADDRESS,
137-
);
113+
console.log("collateralReservationFee", collateralReservationFee);
138114

139-
// 8. Reserve collateral
115+
// 10. Reserve collateral
140116
// https://dev.flare.network/fassets/reference/IAssetManager#reservecollateral
141-
const tx = await iAssetManager.reserveCollateral(
117+
const tx = await assetManager.reserveCollateral(
142118
agentVaultAddress,
143119
LOTS_TO_MINT,
144120
agentInfo.feeBIPS,
145121
// Not using the executor
146122
ZERO_ADDRESS,
147-
[UNDERLYING_ADDRESS],
148123
// Sending the collateral reservation fee as native tokens
149124
{ value: collateralReservationFee },
150125
);
151126

152127
console.log("Collateral reservation successful:", tx);
153128

154-
// 9. Get the asset decimals
129+
// 11. Get the asset decimals
155130
const decimals = await assetManager.assetMintingDecimals();
156131

157-
// 10. Parse the CollateralReserved event
132+
// 12. Parse the CollateralReserved event
158133
const collateralReservedEvent = await parseCollateralReservedEvent(
159134
tx.receipt,
135+
decimals,
160136
);
161137

138+
// 13. Get the collateral reservation info
162139
const collateralReservationInfo =
163140
await assetManager.collateralReservationInfo(
164141
collateralReservedEvent.collateralReservationId,
165142
);
166143
console.log("Collateral reservation info:", collateralReservationInfo);
167144

168-
// 11. Calculate the total amount of XRP to pay
169-
const totalUBA =
170-
collateralReservedEvent.valueUBA + collateralReservedEvent.feeUBA;
145+
// 14. Calculate the total XRP value required for payment
146+
const valueUBA = BigInt(collateralReservedEvent.valueUBA.toString());
147+
const feeUBA = BigInt(collateralReservedEvent.feeUBA.toString());
148+
const totalUBA = valueUBA + feeUBA;
171149
const totalXRP = Number(totalUBA) / 10 ** decimals;
172150
console.log(`You need to pay ${totalXRP} XRP`);
173151
}

0 commit comments

Comments
 (0)