11import { actionCreators } from "@near-js/transactions" ;
22import { Account , KeyPair , keyStores , Near } from "near-api-js" ;
33import type { Quote } from "./types" ;
4- import { INTENTS_CONTRACT_ID , NEAR_RPC_URL , TGas } from "./utils" ;
4+ import {
5+ INTENTS_CONTRACT_ID ,
6+ NEAR_RPC_URL ,
7+ TGas ,
8+ USDC_CONTRACT ,
9+ } from "./utils" ;
510import { getEnvVar } from "./env" ;
611
12+ const FIFTY_TGAS = BigInt ( TGas * 50 ) ;
13+ const ONE_YOCTO = BigInt ( 1 ) ;
14+
715export async function getTokenBalance (
816 account : Account ,
917 assetId : string ,
@@ -57,9 +65,6 @@ export function buildTransactionPayload(quote: Quote) {
5765 } ;
5866}
5967
60- const USDC_CONTRACT =
61- "17208628f84f5d6ad33f0da3bbbeb27ffcb398eac501a31bd6ad2011e36133a1" ;
62-
6368export async function getUSDCBalance ( account : Account ) : Promise < bigint > {
6469 try {
6570 const result = await account . viewFunction ( {
@@ -74,6 +79,27 @@ export async function getUSDCBalance(account: Account): Promise<bigint> {
7479 }
7580}
7681
82+ export async function intentsUSDCBalance ( account : Account ) : Promise < bigint > {
83+ return intentsBalance ( account , USDC_CONTRACT ) ;
84+ }
85+
86+ export async function intentsBalance (
87+ account : Account ,
88+ token : string ,
89+ ) : Promise < bigint > {
90+ try {
91+ const result = await account . viewFunction ( {
92+ contractId : INTENTS_CONTRACT_ID ,
93+ methodName : "mt_balance_of" ,
94+ args : { token_id : `nep141:${ token } ` , account_id : account . accountId } ,
95+ } ) ;
96+ return BigInt ( result as string ) ;
97+ } catch ( error ) {
98+ console . warn ( "Failed to fetch USDC balance:" , error ) ;
99+ return BigInt ( 0 ) ;
100+ }
101+ }
102+
77103export async function depositUSDC ( account : Account , amount : bigint ) {
78104 const result = await account . signAndSendTransaction ( {
79105 receiverId : USDC_CONTRACT ,
@@ -85,8 +111,8 @@ export async function depositUSDC(account: Account, amount: bigint) {
85111 amount : amount . toString ( ) ,
86112 msg : account . accountId ,
87113 } ,
88- BigInt ( TGas * 50 ) ,
89- BigInt ( 1 ) ,
114+ FIFTY_TGAS ,
115+ ONE_YOCTO ,
90116 ) ,
91117 ] ,
92118 } ) ;
@@ -109,3 +135,51 @@ export async function depositUSDC(account: Account, amount: bigint) {
109135
110136 return result ;
111137}
138+
139+ export async function withdrawUSDC ( account : Account , amount : bigint ) {
140+ return withdrawToken ( account , USDC_CONTRACT , amount ) ;
141+ }
142+
143+ export async function withdrawToken (
144+ account : Account ,
145+ token : string ,
146+ amount : bigint ,
147+ ) {
148+ const result = await account . signAndSendTransaction ( {
149+ receiverId : INTENTS_CONTRACT_ID ,
150+ actions : [
151+ actionCreators . functionCall (
152+ "ft_withdraw" ,
153+ {
154+ token,
155+ amount : amount . toString ( ) ,
156+ receiver_id : account . accountId ,
157+ // Docs suggest refund is not necessarily possible if msg is specified!
158+ // https://docs.near-intents.org/near-intents/market-makers/verifier/deposits-and-withdrawals/withdrawals#refunds-on-failed-withdrawals-warning
159+ // msg: null
160+ } ,
161+ FIFTY_TGAS ,
162+ ONE_YOCTO ,
163+ ) ,
164+ ] ,
165+ } ) ;
166+
167+ const hasSuccess = result . receipts_outcome . some ( ( receipt ) =>
168+ receipt . outcome . logs . some (
169+ ( log ) => log . includes ( "ft_transfer" ) && log . includes ( account . accountId ) ,
170+ ) ,
171+ ) ;
172+ const hasRefund = result . receipts_outcome . some ( ( receipt ) =>
173+ receipt . outcome . logs . some (
174+ ( log ) => log . includes ( "ft_transfer" ) && log . includes ( '"memo":"refund"' ) ,
175+ ) ,
176+ ) ;
177+
178+ if ( hasRefund || ! hasSuccess ) {
179+ throw new Error (
180+ `Withdraw failed - transaction was refunded ${ result . transaction . hash } ` ,
181+ ) ;
182+ }
183+
184+ return result ;
185+ }
0 commit comments