Skip to content

Commit

Permalink
feat: Get the contractId to the frontend
Browse files Browse the repository at this point in the history
  • Loading branch information
kovipu committed Sep 17, 2024
1 parent 21d786f commit 9075106
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 18 deletions.
3 changes: 2 additions & 1 deletion initialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,8 @@ function importContract(contract) {
const importContent =
`import * as Client from '${filenameNoExt}';\n` +
`import { rpcUrl } from './util';\n\n` +
`export default new Client.Client({\n` +
`export const contractId = Client.networks.${process.env.SOROBAN_NETWORK}.contractId;\n\n` +
`export const contractClient = new Client.Client({\n` +
` ...Client.networks.${process.env.SOROBAN_NETWORK},\n` +
` rpcUrl,\n` +
`${process.env.SOROBAN_NETWORK === 'local' || 'standalone' ? ` allowHttp: true,\n` : null}` +
Expand Down
13 changes: 8 additions & 5 deletions src/currencies.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import XLMPoolContract from '@contracts/loan_pool';
import USDCPoolContract from '@contracts/usdc_pool';
import * as XLMPoolContract from '@contracts/loan_pool';
import * as USDCPoolContract from '@contracts/usdc_pool';
import StellarIcon from '@images/Stellar_Symbol.png';
import USDCIcon from '@images/usdc.svg';
import type { SupportedCurrency } from './stellar-wallet';
Expand All @@ -8,20 +8,23 @@ export type Currency = {
name: string;
symbol: SupportedCurrency;
icon: string;
loanPoolContract: typeof XLMPoolContract;
loanPoolContract: typeof XLMPoolContract.contractClient;
contractId: string;
};

export const CURRENCIES: Currency[] = [
{
name: 'Stellar Lumen',
symbol: 'XLM',
icon: StellarIcon.src,
loanPoolContract: XLMPoolContract,
loanPoolContract: XLMPoolContract.contractClient,
contractId: XLMPoolContract.contractId,
},
{
name: 'USD Coin',
symbol: 'USDC',
icon: USDCIcon.src,
loanPoolContract: USDCPoolContract,
loanPoolContract: USDCPoolContract.contractClient,
contractId: USDCPoolContract.contractId,
},
] as const;
15 changes: 6 additions & 9 deletions src/pages/_borrow/BorrowableAssetCard.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
import { Button } from '@components/Button';
import { Card } from '@components/Card';
import loanManagerContract from '@contracts/loan_manager';
import { contractClient } from '@contracts/loan_manager';
import type { Currency } from 'src/currencies';
import { useWallet } from 'src/stellar-wallet';

// Temporary hack to use XLM pool for all loans and collaterals.
const XLM_LOAN_POOL_ID = 'CCRWVENKGBUX7EP273GOUMY542VWRFEEIHUICTTDDG5ESLSDNENCH6AT';

interface BorrowableAssetCardProps {
currency: Currency;
}

export const BorrowableAssetCard = ({ currency }: BorrowableAssetCardProps) => {
const { icon, name, symbol } = currency;
const { icon, name, symbol, contractId } = currency;

const { wallet, signTransaction } = useWallet();

Expand All @@ -23,14 +20,14 @@ export const BorrowableAssetCard = ({ currency }: BorrowableAssetCardProps) => {
}

try {
loanManagerContract.options.publicKey = wallet.address;
contractClient.options.publicKey = wallet.address;

const tx = await loanManagerContract.initialize({
const tx = await contractClient.initialize({
user: wallet.address,
borrowed: BigInt(10),
borrowed_from: XLM_LOAN_POOL_ID,
borrowed_from: contractId,
collateral: BigInt(1000),
collateral_from: XLM_LOAN_POOL_ID,
collateral_from: contractId,
});
await tx.signAndSend({ signTransaction });
alert('Loan created succesfully!');
Expand Down
6 changes: 3 additions & 3 deletions src/pages/_lend/LendableAssetCard.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Button } from '@components/Button';
import { Card } from '@components/Card';
import loanManager from '@contracts/loan_manager';
import { contractClient } from '@contracts/loan_manager';
import type { xdr } from '@stellar/stellar-base';
import { Api as RpcApi } from '@stellar/stellar-sdk/rpc';
import { useCallback, useEffect, useState } from 'react';
Expand Down Expand Up @@ -63,10 +63,10 @@ export const LendableAssetCard = ({ currency }: LendableAssetCardProps) => {
}, []);

const fetchPriceData = useCallback(async () => {
if (!loanManager) return;
if (!contractClient) return;

try {
const { simulation } = await loanManager.get_price({ token: currency.symbol });
const { simulation } = await contractClient.get_price({ token: currency.symbol });

if (!simulation || !RpcApi.isSimulationSuccess(simulation)) {
throw 'get_price simulation was unsuccessful.';
Expand Down

0 comments on commit 9075106

Please sign in to comment.