@@ -24,9 +24,8 @@ This guide is a perfect first step for developers working with FAssets.
2424- [ Node.js] ( https://nodejs.org/en/download/ )
2525- [ npm] ( https://docs.npmjs.com/downloading-and-installing-node-js-and-npm/ )
2626- [ TypeScript] ( https://www.typescriptlang.org/download/ )
27- - [ Ethers.js] ( https://docs.ethers.org/v5/ )
28- - [ TypeChain] ( https://www.npmjs.com/package/typechain )
29- - [ Flare Periphery Contract Artifacts] ( https://www.npmjs.com/package/@flarenetwork/flare-periphery-contract-artifacts )
27+ - [ Viem] ( https://viem.sh )
28+ - [ Flare Wagmi Periphery Package] ( https://www.npmjs.com/package/@flarenetwork/flare-wagmi-periphery-package )
3029
3130## Project Setup
3231
@@ -47,140 +46,115 @@ Install the following dependencies:
4746``` bash
4847npm install --save-dev \
4948 typescript \
50- typechain \
51- ethers \
52- @typechain/ethers-v6 \
53- @flarenetwork/flare-periphery-contract-artifacts
49+ viem \
50+ @flarenetwork/flare-wagmi-periphery-package
5451```
5552
5653### Configure TypeScript
5754
58- Create a config file `tsconfig.json to control TypeScript behavior :
55+ Create a ` tsconfig.json ` file :
5956
6057``` bash
6158npx tsc --init
6259```
6360
64- Change the ` tsconfig.json ` file so it can find the Flare generated types :
61+ Update ` tsconfig.json ` as follows :
6562
6663``` json title="tsconfig.json"
6764{
6865 "compilerOptions" : {
69- "target" : " ES2020" ,
70- "module" : " ESNext" ,
66+ "rootDir" : " ./scripts" ,
67+ "outDir" : " ./dist" ,
68+ "module" : " esnext" ,
7169 "moduleResolution" : " node" ,
70+ "target" : " esnext" ,
71+ "lib" : [" dom" , " dom.iterable" , " esnext" ],
72+ "types" : [" node" ],
7273 "esModuleInterop" : true ,
73- "strict" : true ,
74- "skipLibCheck" : true ,
7574 "forceConsistentCasingInFileNames" : true ,
76- "outDir" : " ./dist"
75+ "strict" : true ,
76+ "skipLibCheck" : true
7777 },
78- "include" : [" scripts/**/*.ts" , " typechain/**/*.ts " , " typechain/**/*.d.ts " ],
78+ "include" : [" scripts/**/*.ts" ],
7979 "exclude" : [" node_modules" ]
8080}
8181```
8282
8383### Update Package Configuration
8484
85- For convenience, add the Typescript build and type generation commands to the ` scripts ` section of the ` package.json ` file :
85+ Change ` package.json ` to use ES modules and add a build script :
8686
8787``` json
88+ "type" : " module" ,
8889"scripts" : {
89- "build" : " tsc" ,
90- "generate-types" : " typechain --target ethers-v6 --out-dir typechain './node_modules/@flarenetwork/flare-periphery-contract-artifacts/coston2/artifacts/contracts/**/*.json'"
90+ "build" : " tsc"
9191}
9292```
9393
94- The ` build ` script will compile the TypeScript code.
95-
96- Using the Coston2 network artifacts, the ` typechain ` generates TypeScript types from the Flare Periphery contracts, which are provided through a [ package] ( https://www.npmjs.com/package/@flarenetwork/flare-periphery-contract-artifacts ) containing the necessary contract artifacts.
97-
98- Change the ` package.json ` file to use the ` module ` type to use ES modules and avoid issues with the ` import ` statement:
99-
100- ``` json
101- "type" : " module" ,
102- ```
103-
104- ### Generate TypeScript Types
105-
106- To generate the TypeScript types, run the following command:
107-
108- ``` bash
109- npm run generate-types
110- ```
111-
112- It will generate the TypeScript types in the ` typechain ` directory.
113-
11494## Implementation
11595
11696### Create Script File
11797
118- First, you must create a file to write the TypeScript code for this guide.
119-
12098``` bash
12199mkdir scripts
122100touch scripts/fassets-settings.ts
123101```
124102
125- Open the ` scripts/fassets-settings.ts ` file in your favorite code editor.
103+ Open ` scripts/fassets-settings.ts ` in your favorite code editor.
126104
127105### Import Dependencies
128106
129- Import the ethers library to interact with the blockchain :
107+ Import viem to interact with the blockchain and the ` coston2 ` namespace from the [ ` @flarenetwork/flare-wagmi-periphery-package ` ]( https : // www.npmjs.com/package/@flarenetwork/flare-wagmi-periphery-package), which provides all typed contract ABIs for the Coston2 network :
130108
131109``` typescript
132- import { ethers } from " ethers" ;
110+ import { createPublicClient , http } from " viem" ;
111+ import { flareTestnet } from " viem/chains" ;
112+ import { coston2 } from " @flarenetwork/flare-wagmi-periphery-package" ;
133113```
134114
135- You need to import the [ Flare Contracts Registry ] ( /network/guides/flare-contracts-registry ) and the [ FAssets asset manager ] ( /fassets/reference/IAssetManager ) contract factory types:
115+ ### Define Constants
136116
137117``` typescript
138- import { IAssetManager__factory } from " ../typechain/factories/IAssetManager__factory.js" ;
139- import { IFlareContractRegistry__factory } from " ../typechain/factories/IFlareContractRegistry__factory.js" ;
118+ const FLARE_CONTRACT_REGISTRY_ADDRESS =
119+ " 0xaD67FE66660Fb8dFE9d6b1b4240d8650e30F6019" as const ;
120+ const XRP_USD_FEED_ID = " 0x015852502f55534400000000000000000000000000" as const ;
140121```
141122
142- ### Define Constants
143-
144- Define two constants:
123+ ### Create a Client
145124
146- - ` COSTON2_RPC_URL ` : The RPC URL for the Coston2 network.
147- - ` FLARE_CONTRACT_REGISTRY_ADDRESS ` : The address of the [ Flare Contract Registry] ( /network/guides/flare-contracts-registry ) .
125+ Create a viem public client connected to Coston2:
148126
149127``` typescript
150- const COSTON2_RPC = " https://coston-api.flare.network/ext/C/rpc" ;
151- const FLARE_CONTRACT_REGISTRY_ADDRESS =
152- " 0xaD67FE66660Fb8dFE9d6b1b4240d8650e30F6019" ;
128+ const client = createPublicClient ({
129+ chain: flareTestnet ,
130+ transport: http (),
131+ });
153132```
154133
155134### Get the FAssets FXRP Asset Manager Address
156135
157- You can get the FAssets FXRP Asset Manager address by calling the ` getAssetManagerFXRP ` function of the Flare Contract Registry.
158- After that, create a new ethers provider, connect to the Coston2 network, and connect to the FAssets asset manager contract.
136+ Resolve the FXRP Asset Manager address from the [ Flare Contract Registry] ( /network/guides/flare-contracts-registry ) :
159137
160138``` typescript
161- const provider = new ethers .JsonRpcProvider (COSTON2_RPC );
162-
163- const flareContractRegistry = IFlareContractRegistry__factory .connect (
164- FLARE_CONTRACT_REGISTRY_ADDRESS ,
165- provider ,
166- );
167-
168- const assetManagerAddress =
169- await flareContractRegistry .getContractAddressByName (" AssetManagerFXRP" );
170- const assetManager = IAssetManager__factory .connect (
171- assetManagerAddress ,
172- provider ,
173- );
139+ const assetManagerAddress = await client .readContract ({
140+ address: FLARE_CONTRACT_REGISTRY_ADDRESS ,
141+ abi: coston2 .iFlareContractRegistryAbi ,
142+ functionName: " getContractAddressByName" ,
143+ args: [" AssetManagerFXRP" ],
144+ });
174145```
175146
176147### Implement Settings Retrieval
177148
178- Next, you must fetch the FAssets configuration settings using the [ ` getSettings ` ] ( /fassets/reference/IAssetManager#getsettings ) function of the FAssets asset manager contract.
179-
180- The last step is to get the lot size of FXRP in XRP and print it to the console.
149+ Fetch the FAssets configuration settings using the [ ` getSettings ` ] ( /fassets/reference/IAssetManager#getsettings ) function and calculate the FXRP lot size:
181150
182151``` typescript
183- const settings = await assetManager .getSettings ();
152+ const settings = await client .readContract ({
153+ address: assetManagerAddress ,
154+ abi: coston2 .iAssetManagerAbi ,
155+ functionName: " getSettings" ,
156+ });
157+
184158const lotSizeFXRP =
185159 Number (settings .lotSizeAMG ) / Math .pow (10 , Number (settings .assetDecimals ));
186160console .log (" Lot Size (FXRP):" , lotSizeFXRP );
@@ -192,126 +166,109 @@ The [`getSettings`](/fassets/reference/IAssetManager#getsettings) function retur
192166
193167## Convert Lot Size to USD
194168
195- To convert the lot size to USD you need to use the [ FTSO] ( /ftso/overview ) to get the [ anchor price feed] ( /ftso/scaling/anchor-feeds/ ) of XRP/USD and convert the FXRP lot size to USD.
196-
197- ### Import Dependencies
169+ To convert the lot size to USD, fetch the XRP/USD [ anchor price feed] ( /ftso/scaling/anchor-feeds/ ) from the [ FTSO] ( /ftso/overview ) .
198170
199- Import the ` FtsoV2Interface__factory ` type which is the factories for the [FTSO contracts](/ftso/solidity-reference/FtsoV2Interface).
171+ ### Get the FtsoV2 Address
200172
201173``` typescript
202- import { FtsoV2Interface__factory } from " ../typechain/factories/FtsoV2Interface__factory.js" ;
174+ const ftsoAddress = await client .readContract ({
175+ address: FLARE_CONTRACT_REGISTRY_ADDRESS ,
176+ abi: coston2 .iFlareContractRegistryAbi ,
177+ functionName: " getContractAddressByName" ,
178+ args: [" FtsoV2" ],
179+ });
203180```
204181
205- ### Define Constants
182+ ### Get the Price Feed
206183
207- Define the constants for the [ XRP/USD feed ID ] ( /ftso/scaling/anchor-feeds ) .
184+ ` getFeedById ` is a ` payable ` function, so use ` simulateContract ` to call it without sending a transaction:
208185
209186``` typescript
210- const XRP_USD_FEED_ID = " 0x015852502f55534400000000000000000000000000" ;
211- ```
212-
213- ### Get the Price feed XRP/USD
214-
215- You can get the price feed XRP/USD by calling the [ ` getFeedById ` ] ( /ftso/solidity-reference/FtsoV2Interface#getfeedbyid ) function of the ` FtsoV2 ` contract.
216-
217- ``` typescript
218- const registry = IFlareContractRegistry__factory .connect (
219- FLARE_CONTRACT_REGISTRY_ADDRESS ,
220- provider ,
221- );
222- const ftsoAddress =
223- await flareContractRegistry .getContractAddressByName (" FtsoV2" );
224- const ftsoV2 = FtsoV2Interface__factory .connect (ftsoAddress , provider );
225- const priceFeed = await ftsoV2 .getFeedById .staticCall (XRP_USD_FEED_ID );
187+ const {
188+ result : [_value, _decimals, _timestamp],
189+ } = await client .simulateContract ({
190+ address: ftsoAddress ,
191+ abi: coston2 .ftsoV2InterfaceAbi ,
192+ functionName: " getFeedById" ,
193+ args: [XRP_USD_FEED_ID ],
194+ value: 0n ,
195+ });
226196```
227197
228198### Convert Lot Size to USD
229199
230- Convert the lot size to USD by multiplying the lot size by the price of XRP in USD.
231-
232200``` typescript
233- const xrpUsdPrice = Number (priceFeed [ 0 ] ) / Math .pow (10 , Number (priceFeed [ 1 ] ));
201+ const xrpUsdPrice = Number (_value ) / Math .pow (10 , Number (_decimals ));
234202const lotValueUSD = lotSizeFXRP * xrpUsdPrice ;
235203
236204console .log (" XRP/USD Price:" , xrpUsdPrice );
237205console .log (" Lot value in USD:" , lotValueUSD );
238- console .log (" Timestamp:" , priceFeed [ 2 ] .toString ());
206+ console .log (" Timestamp:" , _timestamp .toString ());
239207```
240208
241209## Putting All Together
242210
243- To put all together, you have the following code:
244-
245211``` typescript title="scripts/fassets-settings.ts"
246- // Importing necessary modules and contract factories
247- import { ethers } from " ethers" ;
248- import { IAssetManager__factory } from " ../typechain/factories/IAssetManager__factory.js" ;
249- import { IFlareContractRegistry__factory } from " ../typechain/factories/IFlareContractRegistry__factory.js" ;
250- import { FtsoV2Interface__factory } from " ../typechain/factories/FtsoV2Interface__factory.js" ;
251-
252- // Constants for RPC endpoint and contract addresses
253- const COSTON2_RPC = " https://coston2-api.flare.network/ext/C/rpc" ; // RPC URL for the Coston2 network
212+ import { createPublicClient , http } from " viem" ;
213+ import { flareTestnet } from " viem/chains" ;
214+ import { coston2 } from " @flarenetwork/flare-wagmi-periphery-package" ;
215+
254216const FLARE_CONTRACT_REGISTRY_ADDRESS =
255- " 0xaD67FE66660Fb8dFE9d6b1b4240d8650e30F6019" ; // Address of the Flare Contract Registry
256- const XRP_USD_FEED_ID = " 0x015852502f55534400000000000000000000000000" ; // Feed ID for XRP/USD price https://dev.flare.network/ftso/scaling/anchor-feeds
257-
258- async function getSettings() {
259- // Create a provider for interacting with the blockchain
260- const provider = new ethers .JsonRpcProvider (COSTON2_RPC );
261-
262- // Connect to the Flare Contract Registry
263- const flareContractRegistry = IFlareContractRegistry__factory .connect (
264- FLARE_CONTRACT_REGISTRY_ADDRESS ,
265- provider ,
266- );
267-
268- // Get the address of the FXRP Asset Manager
269- const assetManagerAddress =
270- await flareContractRegistry .getContractAddressByName (" AssetManagerFXRP" );
271-
272- // Connect to the FXRP Asset Manager
273- const assetManager = IAssetManager__factory .connect (
274- assetManagerAddress ,
275- provider ,
276- );
277-
278- // Fetch settings from the Asset Manager contract
279- const settings = await assetManager .getSettings ();
280-
281- // Calculate the lot size in FXRP (Flare XRP)
282- const lotSizeFXRP =
283- Number (settings .lotSizeAMG ) / Math .pow (10 , Number (settings .assetDecimals ));
284- console .log (" Lot Size (FXRP):" , lotSizeFXRP );
285-
286- // Fetch the address of the FtsoV2 contract from the registry
287- const ftsoAddress =
288- await flareContractRegistry .getContractAddressByName (" FtsoV2" );
289-
290- // Connect to the FtsoV2 contract
291- const ftsoV2 = FtsoV2Interface__factory .connect (ftsoAddress , provider );
292-
293- // Fetch the XRP/USD price feed using the feed ID
294- const priceFeed = await ftsoV2 .getFeedById .staticCall (XRP_USD_FEED_ID );
295-
296- // Calculate the XRP/USD price and the lot value in USD
297- const xrpUsdPrice = Number (priceFeed [0 ]) / Math .pow (10 , Number (priceFeed [1 ]));
298- const lotValueUSD = lotSizeFXRP * xrpUsdPrice ;
299-
300- console .log (" XRP/USD Price:" , xrpUsdPrice );
301- console .log (" Lot value in USD:" , lotValueUSD );
302- console .log (" Timestamp:" , priceFeed [2 ].toString ());
303- }
217+ " 0xaD67FE66660Fb8dFE9d6b1b4240d8650e30F6019" as const ;
218+ const XRP_USD_FEED_ID = " 0x015852502f55534400000000000000000000000000" as const ;
219+
220+ const client = createPublicClient ({
221+ chain: flareTestnet ,
222+ transport: http (),
223+ });
224+
225+ const assetManagerAddress = await client .readContract ({
226+ address: FLARE_CONTRACT_REGISTRY_ADDRESS ,
227+ abi: coston2 .iFlareContractRegistryAbi ,
228+ functionName: " getContractAddressByName" ,
229+ args: [" AssetManagerFXRP" ],
230+ });
231+
232+ const settings = await client .readContract ({
233+ address: assetManagerAddress ,
234+ abi: coston2 .iAssetManagerAbi ,
235+ functionName: " getSettings" ,
236+ });
237+
238+ const lotSizeFXRP =
239+ Number (settings .lotSizeAMG ) / Math .pow (10 , Number (settings .assetDecimals ));
240+ console .log (" Lot Size (FXRP):" , lotSizeFXRP );
304241
305- getSettings ();
242+ const ftsoAddress = await client .readContract ({
243+ address: FLARE_CONTRACT_REGISTRY_ADDRESS ,
244+ abi: coston2 .iFlareContractRegistryAbi ,
245+ functionName: " getContractAddressByName" ,
246+ args: [" FtsoV2" ],
247+ });
248+
249+ const {
250+ result : [_value, _decimals, _timestamp],
251+ } = await client .simulateContract ({
252+ address: ftsoAddress ,
253+ abi: coston2 .ftsoV2InterfaceAbi ,
254+ functionName: " getFeedById" ,
255+ args: [XRP_USD_FEED_ID ],
256+ value: 0n ,
257+ });
258+
259+ const xrpUsdPrice = Number (_value ) / Math .pow (10 , Number (_decimals ));
260+ const lotValueUSD = lotSizeFXRP * xrpUsdPrice ;
261+
262+ console .log (" XRP/USD Price:" , xrpUsdPrice );
263+ console .log (" Lot value in USD:" , lotValueUSD );
264+ console .log (" Timestamp:" , _timestamp .toString ());
306265```
307266
308267## Running the Script
309268
310- To execute the script, run the following commands to compile the TypeScript code and run the script from the ` dist ` directory:
311-
312269``` bash
313270npm run build
314- node dist/scripts/ fassets-settings.js
271+ node dist/fassets-settings.js
315272```
316273
317274You should see the following output:
0 commit comments