|
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"; |
7 | 4 |
|
8 | 5 | // 1. Define constants |
9 | 6 |
|
10 | | -// AssetManager address on Flare Testnet Coston2 network |
11 | | -const ASSET_MANAGER_ADDRESS = "0xDeD50DA9C3492Bee44560a4B35cFe0e778F41eC5"; |
12 | 7 | // Number of lots to reserve |
13 | 8 | const LOTS_TO_MINT = 1; |
14 | | -// XRP Ledger address |
15 | | -const UNDERLYING_ADDRESS = "rSHYuiEvsYsKR8uUHhBTuGP5zjRcGt4nm"; |
16 | | - |
17 | 9 | // Use zero address for executor since we're not using it |
18 | 10 | const ZERO_ADDRESS = "0x0000000000000000000000000000000000000000"; |
19 | 11 |
|
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 |
21 | 16 |
|
22 | 17 | // Function from FAssets Bot repository |
23 | 18 | // 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( |
69 | 64 | } |
70 | 65 | } |
71 | 66 |
|
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 | +) { |
75 | 72 | console.log("\nParsing events...", transactionReceipt.rawLogs); |
76 | 73 |
|
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 | + ); |
97 | 80 |
|
98 | | - return collateralReservedEvent; |
99 | | - } catch (e) { |
100 | | - console.log("Error parsing event:", e); |
101 | | - } |
102 | | - } |
| 81 | + return collateralReservedEvents[0].decoded; |
103 | 82 | } |
104 | 83 |
|
105 | | -// 4. Main function |
106 | | - |
107 | 84 | 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(); |
113 | 88 |
|
114 | | - // 5. Find the best agent with enough free collateral lots |
| 89 | + // 7. Find the best agent with enough free collateral lots |
115 | 90 | const agentVaultAddress = await findBestAgent(assetManager, LOTS_TO_MINT); |
116 | 91 | if (!agentVaultAddress) { |
117 | 92 | throw new Error("No suitable agent found with enough free collateral lots"); |
118 | 93 | } |
119 | 94 | console.log(agentVaultAddress); |
120 | 95 |
|
121 | | - // 6. Get the agent info |
| 96 | + // 8. Get the agent info |
122 | 97 | const agentInfo = await assetManager.getAgentInfo(agentVaultAddress); |
123 | 98 | console.log("Agent info:", agentInfo); |
124 | 99 |
|
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 |
126 | 101 | // https://dev.flare.network/fassets/minting/#collateral-reservation-fee |
127 | 102 | const collateralReservationFee = |
128 | 103 | await assetManager.collateralReservationFee(LOTS_TO_MINT); |
129 | 104 | console.log( |
130 | 105 | "Collateral reservation fee:", |
131 | 106 | collateralReservationFee.toString(), |
132 | 107 | ); |
| 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); |
133 | 112 |
|
134 | | - const IAssetManager = artifacts.require("IAssetManager"); |
135 | | - const iAssetManager: IAssetManagerInstance = await IAssetManager.at( |
136 | | - ASSET_MANAGER_ADDRESS, |
137 | | - ); |
| 113 | + console.log("collateralReservationFee", collateralReservationFee); |
138 | 114 |
|
139 | | - // 8. Reserve collateral |
| 115 | + // 10. Reserve collateral |
140 | 116 | // https://dev.flare.network/fassets/reference/IAssetManager#reservecollateral |
141 | | - const tx = await iAssetManager.reserveCollateral( |
| 117 | + const tx = await assetManager.reserveCollateral( |
142 | 118 | agentVaultAddress, |
143 | 119 | LOTS_TO_MINT, |
144 | 120 | agentInfo.feeBIPS, |
145 | 121 | // Not using the executor |
146 | 122 | ZERO_ADDRESS, |
147 | | - [UNDERLYING_ADDRESS], |
148 | 123 | // Sending the collateral reservation fee as native tokens |
149 | 124 | { value: collateralReservationFee }, |
150 | 125 | ); |
151 | 126 |
|
152 | 127 | console.log("Collateral reservation successful:", tx); |
153 | 128 |
|
154 | | - // 9. Get the asset decimals |
| 129 | + // 11. Get the asset decimals |
155 | 130 | const decimals = await assetManager.assetMintingDecimals(); |
156 | 131 |
|
157 | | - // 10. Parse the CollateralReserved event |
| 132 | + // 12. Parse the CollateralReserved event |
158 | 133 | const collateralReservedEvent = await parseCollateralReservedEvent( |
159 | 134 | tx.receipt, |
| 135 | + decimals, |
160 | 136 | ); |
161 | 137 |
|
| 138 | + // 13. Get the collateral reservation info |
162 | 139 | const collateralReservationInfo = |
163 | 140 | await assetManager.collateralReservationInfo( |
164 | 141 | collateralReservedEvent.collateralReservationId, |
165 | 142 | ); |
166 | 143 | console.log("Collateral reservation info:", collateralReservationInfo); |
167 | 144 |
|
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; |
171 | 149 | const totalXRP = Number(totalUBA) / 10 ** decimals; |
172 | 150 | console.log(`You need to pay ${totalXRP} XRP`); |
173 | 151 | } |
|
0 commit comments