|
1 | | -import { ethers } from "hardhat"; |
2 | | - |
3 | | -import { |
4 | | - IAssetManagerInstance, |
5 | | - IAssetManagerContract, |
6 | | -} from "../../typechain-types"; |
7 | | - |
8 | | -// Initialize the FAssets FXRP AssetManager contract |
9 | | -const AssetManager = artifacts.require("IAssetManager"); |
| 1 | +import { getFXRPAssetManager } from "../utils/fassets"; |
| 2 | +import { IAssetManagerInstance } from "../../typechain-types"; |
| 3 | +import { logEvents } from "../../scripts/utils/core"; |
10 | 4 |
|
11 | 5 | // 1. Define constants |
12 | 6 |
|
13 | | -// AssetManager address on Flare Testnet Coston2 network |
14 | | -const ASSET_MANAGER_ADDRESS = "0xDeD50DA9C3492Bee44560a4B35cFe0e778F41eC5"; |
15 | 7 | // Number of lots to reserve |
16 | 8 | const LOTS_TO_MINT = 1; |
17 | | -// XRP Ledger address |
18 | | -const UNDERLYING_ADDRESS = "rSHYuiEvsYsKR8uUHhBTuGP5zjRcGt4nm"; |
19 | 9 |
|
20 | 10 | // Executor address |
21 | 11 | const EXECUTOR_ADDRESS = "0xb292348a4Cb9f5F008589B3596405FBba6986c55"; |
22 | 12 |
|
23 | | -// 2. Function to find the best agent with enough free collateral lots |
| 13 | +// 2. Get the AssetManager artifact |
| 14 | +const AssetManager = artifacts.require("IAssetManager"); |
| 15 | + |
| 16 | +// 3. Function to find the best agent with enough free collateral lots |
24 | 17 |
|
25 | 18 | // Function from FAssets Bot repository |
26 | 19 | // https://github.com/flare-foundation/fasset-bots/blob/main/packages/fasset-bots-core/src/commands/InfoBotCommands.ts#L83 |
@@ -72,107 +65,95 @@ async function findBestAgent( |
72 | 65 | } |
73 | 66 | } |
74 | 67 |
|
75 | | -// 3. Function to parse the CollateralReserved event |
76 | | - |
77 | | -async function parseCollateralReservedEvent(transactionReceipt) { |
| 68 | +// 4. Function to parse the CollateralReserved event |
| 69 | +async function parseCollateralReservedEvent( |
| 70 | + transactionReceipt: any, |
| 71 | + decimals: number, |
| 72 | +) { |
78 | 73 | console.log("\nParsing events...", transactionReceipt.rawLogs); |
79 | 74 |
|
80 | | - const assetManager = (await ethers.getContractAt( |
81 | | - "IAssetManager", |
82 | | - ASSET_MANAGER_ADDRESS, |
83 | | - )) as IAssetManagerContract; |
84 | | - |
85 | | - for (const log of transactionReceipt.rawLogs) { |
86 | | - try { |
87 | | - const parsedLog = assetManager.interface.parseLog({ |
88 | | - topics: log.topics, |
89 | | - data: log.data, |
90 | | - }); |
91 | | - |
92 | | - if (!parsedLog) continue; |
93 | | - |
94 | | - const collateralReservedEvents = ["CollateralReserved"]; |
95 | | - if (!collateralReservedEvents.includes(parsedLog.name)) continue; |
96 | | - |
97 | | - console.log(`\nEvent: ${parsedLog.name}`); |
98 | | - console.log("Arguments:", parsedLog.args); |
99 | | - const collateralReservedEvent = parsedLog.args; |
| 75 | + // The logEvents function is included in the Flare starter kit |
| 76 | + const collateralReservedEvents = logEvents( |
| 77 | + transactionReceipt.rawLogs, |
| 78 | + "CollateralReserved", |
| 79 | + AssetManager.abi, |
| 80 | + ); |
100 | 81 |
|
101 | | - return collateralReservedEvent; |
102 | | - } catch (e) { |
103 | | - console.log("Error parsing event:", e); |
104 | | - } |
105 | | - } |
| 82 | + return collateralReservedEvents[0].decoded; |
106 | 83 | } |
107 | 84 |
|
108 | | -// 4. Main function |
109 | | - |
| 85 | +// 5. Main function |
110 | 86 | async function main() { |
111 | | - const assetManager: IAssetManagerInstance = await AssetManager.at( |
112 | | - ASSET_MANAGER_ADDRESS, |
113 | | - ); |
| 87 | + // 6. Get the AssetManager contract from the Flare Contract Registry |
| 88 | + const assetManager: IAssetManagerInstance = await getAssetManagerFXRP(); |
114 | 89 |
|
115 | | - // 5. Find the best agent with enough free collateral lots |
| 90 | + // 7. Find the best agent with enough free collateral lots |
116 | 91 | const agentVaultAddress = await findBestAgent(assetManager, LOTS_TO_MINT); |
117 | 92 | if (!agentVaultAddress) { |
118 | 93 | throw new Error("No suitable agent found with enough free collateral lots"); |
119 | 94 | } |
120 | 95 | console.log(agentVaultAddress); |
121 | 96 |
|
122 | | - // 6. Get the agent info |
| 97 | + // 8. Get the agent info |
123 | 98 | const agentInfo = await assetManager.getAgentInfo(agentVaultAddress); |
124 | 99 | console.log("Agent info:", agentInfo); |
125 | 100 |
|
126 | | - // 7. Get the collateral reservation fee according to the number of lots to reserve |
| 101 | + // 9. Get the collateral reservation fee according to the number of lots to reserve |
127 | 102 | // https://dev.flare.network/fassets/minting/#collateral-reservation-fee |
128 | 103 | const collateralReservationFee = |
129 | 104 | await assetManager.collateralReservationFee(LOTS_TO_MINT); |
130 | 105 | console.log( |
131 | 106 | "Collateral reservation fee:", |
132 | 107 | collateralReservationFee.toString(), |
133 | 108 | ); |
134 | | - const assetManager: IAssetManagerInstance = await AssetManager.at( |
135 | | - ASSET_MANAGER_ADDRESS, |
136 | | - ); |
137 | 109 |
|
138 | | - // 8. To make this example simpler we're using the same fee for the executor and the agent |
| 110 | + // 10. To make this example simpler we're using the same fee for the executor and the agent |
139 | 111 | const executorFee = collateralReservationFee; |
140 | 112 | const totalFee = collateralReservationFee.add(executorFee); |
141 | 113 |
|
142 | 114 | console.log("Total reservation fee:", totalFee.toString()); |
143 | 115 |
|
144 | | - // 9. Reserve collateral |
| 116 | + console.log("agentVaultAddress", agentVaultAddress); |
| 117 | + console.log("LOTS_TO_MINT", LOTS_TO_MINT); |
| 118 | + console.log("agentInfo.feeBIPS", agentInfo.feeBIPS); |
| 119 | + console.log("EXECUTOR_ADDRESS", EXECUTOR_ADDRESS); |
| 120 | + |
| 121 | + console.log("collateralReservationFee", collateralReservationFee); |
| 122 | + |
| 123 | + // 11. Reserve collateral |
145 | 124 | // https://dev.flare.network/fassets/reference/IAssetManager#reservecollateral |
146 | 125 | const tx = await assetManager.reserveCollateral( |
147 | 126 | agentVaultAddress, |
148 | 127 | LOTS_TO_MINT, |
149 | 128 | agentInfo.feeBIPS, |
150 | 129 | // Not using the executor |
151 | 130 | EXECUTOR_ADDRESS, |
152 | | - [UNDERLYING_ADDRESS], |
153 | 131 | // Sending the collateral reservation fee as native tokens |
154 | 132 | { value: totalFee }, |
155 | 133 | ); |
156 | 134 |
|
157 | 135 | console.log("Collateral reservation successful:", tx); |
158 | 136 |
|
159 | | - // 10. Get the asset decimals |
| 137 | + // 112 Get the asset decimals |
160 | 138 | const decimals = await assetManager.assetMintingDecimals(); |
161 | 139 |
|
162 | | - // 11. Parse the CollateralReserved event |
| 140 | + // 13. Parse the CollateralReserved event |
163 | 141 | const collateralReservedEvent = await parseCollateralReservedEvent( |
164 | 142 | tx.receipt, |
| 143 | + decimals, |
165 | 144 | ); |
166 | 145 |
|
| 146 | + // 14. Get the collateral reservation info |
167 | 147 | const collateralReservationInfo = |
168 | 148 | await assetManager.collateralReservationInfo( |
169 | 149 | collateralReservedEvent.collateralReservationId, |
170 | 150 | ); |
171 | 151 | console.log("Collateral reservation info:", collateralReservationInfo); |
172 | 152 |
|
173 | | - // 11. Calculate the total amount of XRP to pay |
174 | | - const totalUBA = |
175 | | - collateralReservedEvent.valueUBA + collateralReservedEvent.feeUBA; |
| 153 | + // 14. Calculate the total XRP value required for payment |
| 154 | + const valueUBA = BigInt(collateralReservedEvent.valueUBA.toString()); |
| 155 | + const feeUBA = BigInt(collateralReservedEvent.feeUBA.toString()); |
| 156 | + const totalUBA = valueUBA + feeUBA; |
176 | 157 | const totalXRP = Number(totalUBA) / 10 ** decimals; |
177 | 158 | console.log(`You need to pay ${totalXRP} XRP`); |
178 | 159 | } |
|
0 commit comments