You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
asyncfunctiongetTotalAssets(): Promise<number>{constcontract=newContract(contractId);constcall=contract.call('total_assets');constresult=awaitserver.simulateTransaction(call);// Parse the result to extract total assetsconsttotalAssets=parseBigIntFromScVal(result);console.log('Total assets:',totalAssets);returntotalAssets;}// Usageconsttotal=awaitgetTotalAssets();
Query Share Balance
asyncfunctiongetBalance(userAddress: string): Promise<number>{constaddress=newAddress(userAddress).toScVal();constcontract=newContract(contractId);constcall=contract.call('balance_of',address);constresult=awaitserver.simulateTransaction(call);// Parse the result to extract balanceconstbalance=parseBigIntFromScVal(result);console.log('User shares:',balance);returnbalance;}// Usageconstshares=awaitgetBalance(userKeypair.publicKey());
Helper: Calculate Exchange Rate
asyncfunctiongetExchangeRate(): Promise<number>{consttotalAssets=awaitgetTotalAssets();consttotalShares=awaitgetTotalShares();// Need to implementif(totalShares===0)return1;returntotalAssets/totalShares;}
Error Handling
asyncfunctiondepositWithErrorHandling(userPublicKey: string,amount: number){try{constresult=awaitdeposit(userPublicKey,amount);console.log('Success:',result.hash);}catch(error: any){consterrorCode=error.message.match(/\d+/)?.[0];switch(errorCode){case'1':
console.error('Vault not initialized');break;case'5':
console.error('Zero amount not allowed');break;case'6':
console.error('Math overflow - amount too large');break;default:
console.error('Unknown error:',error.message);}}}