1- import { encodeAbiParameters , encodeFunctionData , formatUnits , keccak256 , parseAbi , type Address } from "viem" ;
2- import { Client , Wallet } from "xrpl" ;
1+ import {
2+ encodeAbiParameters ,
3+ encodeFunctionData ,
4+ formatUnits ,
5+ keccak256 ,
6+ parseAbi ,
7+ type Address ,
8+ } from "viem" ;
9+ import type { Client , Wallet } from "xrpl" ;
310import { abi as ERC20Abi } from "../abis/ERC20" ;
411import { abi as MorphoBlueAbi } from "../abis/MorphoBlue" ;
512import { account , publicClient , walletClient } from "../utils/client" ;
613import { sendMemoFieldInstruction } from "../utils/smart-accounts" ;
714
815// Coston2 Morpho Blue test stack (mock tokens, mock oracle, mock IRM).
9- export const MORPHO_BLUE_ADDRESS = "0x8aE0b3CE90F16E88063516f2d88C8ac2ab552d95" as Address ;
10- export const LOAN_TOKEN_ADDRESS = "0x4984B127c3065f4348858fAFdBa020f2c8633905" as Address ;
11- export const COLLATERAL_TOKEN_ADDRESS = "0x98bf2F2fF322d5eb61D6aE04Df50856525a85D16" as Address ;
12- export const ORACLE_ADDRESS = "0x1e80830e9903c839Db803442c976DD2360D47FE0" as Address ;
13- export const IRM_ADDRESS = "0xDC275701300865D882D44ffe7cb1153535636d1a" as Address ;
16+ export const MORPHO_BLUE_ADDRESS =
17+ "0x8aE0b3CE90F16E88063516f2d88C8ac2ab552d95" as Address ;
18+ export const LOAN_TOKEN_ADDRESS =
19+ "0x4984B127c3065f4348858fAFdBa020f2c8633905" as Address ;
20+ export const COLLATERAL_TOKEN_ADDRESS =
21+ "0x98bf2F2fF322d5eb61D6aE04Df50856525a85D16" as Address ;
22+ export const ORACLE_ADDRESS =
23+ "0x1e80830e9903c839Db803442c976DD2360D47FE0" as Address ;
24+ export const IRM_ADDRESS =
25+ "0xDC275701300865D882D44ffe7cb1153535636d1a" as Address ;
1426export const LLTV = 860000000000000000n ; // 86 %
1527
1628// MorphoMarketShim deployed on Coston2 — see flare-hardhat-starter/scripts/morpho/deploys.ts.
17- export const MORPHO_MARKET_SHIM_ADDRESS = "0x33d81a1d7986bB3AbAB4F67Ad6117233ADd6F87A" as Address ;
29+ export const MORPHO_MARKET_SHIM_ADDRESS =
30+ "0x33d81a1d7986bB3AbAB4F67Ad6117233ADd6F87A" as Address ;
1831
1932export const WAD = 10n ** 18n ;
2033export const MAX_UINT256 = 2n ** 256n - 1n ;
2134// Allowance >= this counts as "approved unlimited" for setup-skip purposes.
2235const APPROVAL_THRESHOLD = 2n ** 255n ;
2336
2437// The mock collateral and loan tokens expose an unauthenticated setBalance(account, amount).
25- export const MOCK_ERC20_ABI = parseAbi ( [ "function setBalance(address account, uint256 amount)" ] ) ;
38+ export const MOCK_ERC20_ABI = parseAbi ( [
39+ "function setBalance(address account, uint256 amount)" ,
40+ ] ) ;
2641export const ORACLE_ABI = parseAbi ( [ "function price() view returns (uint256)" ] ) ;
2742export const POSITION_ABI = parseAbi ( [
2843 "function position(bytes32 id, address user) view returns (uint256 supplyShares, uint128 borrowShares, uint128 collateral)" ,
@@ -51,8 +66,8 @@ export const marketId = keccak256(
5166 ] ,
5267 } ,
5368 ] ,
54- [ marketParams ]
55- )
69+ [ marketParams ] ,
70+ ) ,
5671) ;
5772
5873// Reads the loan and collateral token decimals and derives the oracle's price
@@ -76,11 +91,16 @@ export async function fetchMarketDecimals() {
7691 readDecimalsOrDefault ( LOAN_TOKEN_ADDRESS ) ,
7792 readDecimalsOrDefault ( COLLATERAL_TOKEN_ADDRESS ) ,
7893 ] ) ;
79- const oraclePriceScale = 10n ** ( 36n + BigInt ( loanDecimals ) - BigInt ( collateralDecimals ) ) ;
94+ const oraclePriceScale =
95+ 10n ** ( 36n + BigInt ( loanDecimals ) - BigInt ( collateralDecimals ) ) ;
8096 return { loanDecimals, collateralDecimals, oraclePriceScale } ;
8197}
8298
83- export async function mintMock ( tokenAddress : Address , recipient : Address , amount : bigint ) {
99+ export async function mintMock (
100+ tokenAddress : Address ,
101+ recipient : Address ,
102+ amount : bigint ,
103+ ) {
84104 const { request } = await publicClient . simulateContract ( {
85105 account,
86106 address : tokenAddress ,
@@ -108,33 +128,45 @@ export async function ensureShimSetup({
108128 xrplWallet : Wallet ;
109129 amountXrp : number ;
110130} ) {
111- const [ collateralAllowance , loanAllowance , morphoAuthorized ] = ( await Promise . all ( [
112- publicClient . readContract ( {
113- address : COLLATERAL_TOKEN_ADDRESS ,
114- abi : ERC20Abi ,
115- functionName : "allowance" ,
116- args : [ personalAccount , MORPHO_MARKET_SHIM_ADDRESS ] ,
117- } ) ,
118- publicClient . readContract ( {
119- address : LOAN_TOKEN_ADDRESS ,
120- abi : ERC20Abi ,
121- functionName : "allowance" ,
122- args : [ personalAccount , MORPHO_MARKET_SHIM_ADDRESS ] ,
123- } ) ,
124- publicClient . readContract ( {
125- address : MORPHO_BLUE_ADDRESS ,
126- abi : MorphoBlueAbi ,
127- functionName : "isAuthorized" ,
128- args : [ personalAccount , MORPHO_MARKET_SHIM_ADDRESS ] ,
129- } ) ,
130- ] ) ) as [ bigint , bigint , boolean ] ;
131+ const [ collateralAllowance , loanAllowance , morphoAuthorized ] =
132+ ( await Promise . all ( [
133+ publicClient . readContract ( {
134+ address : COLLATERAL_TOKEN_ADDRESS ,
135+ abi : ERC20Abi ,
136+ functionName : "allowance" ,
137+ args : [ personalAccount , MORPHO_MARKET_SHIM_ADDRESS ] ,
138+ } ) ,
139+ publicClient . readContract ( {
140+ address : LOAN_TOKEN_ADDRESS ,
141+ abi : ERC20Abi ,
142+ functionName : "allowance" ,
143+ args : [ personalAccount , MORPHO_MARKET_SHIM_ADDRESS ] ,
144+ } ) ,
145+ publicClient . readContract ( {
146+ address : MORPHO_BLUE_ADDRESS ,
147+ abi : MorphoBlueAbi ,
148+ functionName : "isAuthorized" ,
149+ args : [ personalAccount , MORPHO_MARKET_SHIM_ADDRESS ] ,
150+ } ) ,
151+ ] ) ) as [ bigint , bigint , boolean ] ;
131152
132- if ( collateralAllowance >= APPROVAL_THRESHOLD && loanAllowance >= APPROVAL_THRESHOLD && morphoAuthorized ) {
133- console . log ( "Smart account → shim setup already complete — skipping setup memos.\n" ) ;
153+ if (
154+ collateralAllowance >= APPROVAL_THRESHOLD &&
155+ loanAllowance >= APPROVAL_THRESHOLD &&
156+ morphoAuthorized
157+ ) {
158+ console . log (
159+ "Smart account → shim setup already complete — skipping setup memos.\n" ,
160+ ) ;
134161 return ;
135162 }
136163
137- const sharedMemoFields = { amountXrp, personalAccount, xrplClient, xrplWallet } ;
164+ const sharedMemoFields = {
165+ amountXrp,
166+ personalAccount,
167+ xrplClient,
168+ xrplWallet,
169+ } ;
138170
139171 if ( collateralAllowance < APPROVAL_THRESHOLD ) {
140172 await sendMemoFieldInstruction ( {
@@ -192,7 +224,7 @@ export async function ensureShimSetup({
192224export async function getAndLogState (
193225 label : string ,
194226 smartAccount : Address ,
195- marketDecimals : { loanDecimals : number ; collateralDecimals : number }
227+ marketDecimals : { loanDecimals : number ; collateralDecimals : number } ,
196228) {
197229 const [ position , collateralBalance , loanBalance ] = await Promise . all ( [
198230 publicClient . readContract ( {
@@ -221,11 +253,26 @@ export async function getAndLogState(
221253 // (loanDecimals + 6). The conversion drifts slightly as interest accrues.
222254 const sharesScale = marketDecimals . loanDecimals + 6 ;
223255 console . log ( `=== ${ label } ===` ) ;
224- console . log ( " position supply (≈loan tokens): " , formatUnits ( supplyShares , sharesScale ) ) ;
225- console . log ( " position borrow (≈loan tokens): " , formatUnits ( borrowShares , sharesScale ) ) ;
226- console . log ( " position collateral: " , formatUnits ( collateral , marketDecimals . collateralDecimals ) ) ;
227- console . log ( " smart-account collateral balance:" , formatUnits ( collateralBalance , marketDecimals . collateralDecimals ) ) ;
228- console . log ( " smart-account loan-token balance:" , formatUnits ( loanBalance , marketDecimals . loanDecimals ) ) ;
256+ console . log (
257+ " position supply (≈loan tokens): " ,
258+ formatUnits ( supplyShares , sharesScale ) ,
259+ ) ;
260+ console . log (
261+ " position borrow (≈loan tokens): " ,
262+ formatUnits ( borrowShares , sharesScale ) ,
263+ ) ;
264+ console . log (
265+ " position collateral: " ,
266+ formatUnits ( collateral , marketDecimals . collateralDecimals ) ,
267+ ) ;
268+ console . log (
269+ " smart-account collateral balance:" ,
270+ formatUnits ( collateralBalance , marketDecimals . collateralDecimals ) ,
271+ ) ;
272+ console . log (
273+ " smart-account loan-token balance:" ,
274+ formatUnits ( loanBalance , marketDecimals . loanDecimals ) ,
275+ ) ;
229276 console . log ( "" ) ;
230277
231278 return { supplyShares, borrowShares, collateral } ;
0 commit comments