Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transfer from starknet to starknet-kit #399

Closed
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23,899 changes: 23,899 additions & 0 deletions frontend/package-lock.json
whateverfw marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shuld be removed, use yarn.

whateverfw marked this conversation as resolved.
Show resolved Hide resolved

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions frontend/package.json
whateverfw marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"react-router-dom": "^6.27.0",
"react-scripts": "^5.0.1",
"react-toastify": "^10.0.6",
"starknetkit": "^2.6.1",
"web-vitals": "^2.1.4",
"zustand": "^5.0.1"
},
Expand Down
29 changes: 22 additions & 7 deletions frontend/src/services/contract.js
davedumto marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,23 +1,34 @@
import { connect } from 'get-starknet';
import { connect } from 'starknetkit';
import { getDeployContractData } from '../utils/constants';
import { axiosInstance } from '../utils/axios';

export async function deployContract(walletId) {
try {
// Connect to Starknet wallet
console.log('Connecting to StarkNet wallet...');

const starknet = await connect();
if (!starknet.isConnected) {


if (!starknet || !starknet.isConnected) {
throw new Error('Wallet not connected');
}

// Prepare the deploy contract transaction object
console.log('Wallet connected:');


const deployContractTransaction = getDeployContractData(walletId);
if (!deployContractTransaction) {
throw new Error('Failed to retrieve contract deployment data');
}

// Execute the deployment transaction
// const result = await starknet.account.execute([deployContractTransaction]);
console.log('Deploying contract...');
const result = await starknet.account.deployContract(deployContractTransaction);
console.log('Contract deployed successfully:', result);


await starknet.account.waitForTransaction(result.transaction_hash);
console.log('Transaction confirmed:', result.transaction_hash);

return {
transactionHash: result.transaction_hash,
contractAddress: result.contract_address,
Expand All @@ -31,17 +42,20 @@ export async function deployContract(walletId) {
export async function checkAndDeployContract(walletId) {
try {
console.log('Checking if contract is deployed for wallet ID:', walletId);


const response = await axiosInstance.get(`/api/check-user?wallet_id=${walletId}`);
console.log('Backend response:', response.data);


if (!response.data.is_contract_deployed) {
console.log('Contract not deployed. Deploying...');
const result = await deployContract(walletId);
const contractAddress = result.contractAddress;

console.log('Contract address:', contractAddress);

// Update the backend with transaction hash and wallet ID

await axiosInstance.post(`/api/update-user-contract`, {
wallet_id: walletId,
contract_address: contractAddress,
Expand All @@ -52,5 +66,6 @@ export async function checkAndDeployContract(walletId) {
}
} catch (error) {
console.error('Error checking contract status:', error);
throw error;
}
}
10 changes: 5 additions & 5 deletions frontend/src/services/transaction.js
whateverfw marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { connect } from 'get-starknet';
import { connect } from 'starknetkit';
import { CallData } from 'starknet';
import { erc20abi } from '../abis/erc20';
whateverfw marked this conversation as resolved.
Show resolved Hide resolved
import { abi } from '../abis/abi';
Expand Down Expand Up @@ -42,16 +42,16 @@ export async function sendTransaction(loopLiquidityData, contractAddress) {
throw error;
}
}
/* eslint-disable-next-line no-unused-vars */
async function waitForTransaction(txHash) {

export async function waitForTransaction(txHash) {
const starknet = await connect();
let receipt = null;
while (receipt === null) {
try {
receipt = await starknet.provider.getTransactionReceipt(txHash);
} catch (error) {
console.log('Waiting for transaction to be accepted...');
await new Promise((resolve) => setTimeout(resolve, 5000)); // Wait for 5 seconds before trying again
await new Promise((resolve) => setTimeout(resolve, 5000));
}
}
console.log('Transaction accepted:', receipt);
Expand Down Expand Up @@ -94,7 +94,7 @@ export const handleTransaction = async (connectedWalletId, formData, setError, s

openPositionResponse == openPositionResponse

// Reset form data

setTokenAmount('');
} catch (err) {
console.error('Failed to create position:', err);
Expand Down
61 changes: 30 additions & 31 deletions frontend/src/services/wallet.js
whateverfw marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,32 +1,30 @@
import React from 'react';
import { connect } from 'get-starknet';
import { connect } from 'starknetkit';
import { ETH_ADDRESS, STRK_ADDRESS, USDC_ADDRESS } from '../utils/constants';
import { ReactComponent as ETH } from 'assets/icons/ethereum.svg';
import { ReactComponent as USDC } from 'assets/icons/borrow_usdc.svg';
import { ReactComponent as STRK } from 'assets/icons/strk.svg';

const CRM_TOKEN_ADDRESS = "0x051c4b1fe3bf6774b87ad0b15ef5d1472759076e42944fff9b9f641ff13e5bbe";

// Check if the connected wallet holds the CRM token
export const checkForCRMToken = async (walletAddress) => {
if (process.env.REACT_APP_IS_DEV === "true") {
console.log("Development mode: Skipping CRM token check.");
return true;
}

try {
const starknet = await connect();
if (!starknet.isConnected) {
const { wallet } = await connect();
if (!wallet) {
throw new Error('Wallet not connected');
}

const response = await starknet.provider.callContract({
const response = await wallet.account.callContract({
contractAddress: CRM_TOKEN_ADDRESS,
entrypoint: 'balanceOf',
calldata: [walletAddress],
});

const balance = BigInt(response.result[0]).toString();
const balance = BigInt(response[0]).toString();

if (Number(balance) > 0) {
return true;
Expand All @@ -36,34 +34,36 @@ export const checkForCRMToken = async (walletAddress) => {
}
} catch (error) {
console.error("Error checking CRM token balance:", error);
throw error; // Ensures test will catch errors as thrown
throw error;
}
};

export const connectWallet = async () => {
try {
console.log('Attempting to connect to wallet...');

const starknet = await connect({
const { wallet } = await connect({
include: ['argentX', 'braavos'],
modalMode: "alwaysAsk",
modalTheme: "light",
modalTheme: "light"
});

if (!starknet) {
console.error('No StarkNet object found');
if (!wallet) {
console.error('No wallet found');
throw new Error('Failed to connect to wallet');
}

await starknet.enable();
// Ensure the wallet is connected
await wallet.enable();

if (starknet.isConnected) {
const address = starknet.selectedAddress;
console.log('Wallet successfully connected. Address:', address);
return address;
} else {
throw new Error('Wallet connection failed');
const address = wallet.selectedAddress || wallet.account.address;

if (!address) {
throw new Error('No wallet address found');
}

console.log('Wallet successfully connected. Address:', address);
return address;
} catch (error) {
console.error('Error connecting wallet:', error.message);
throw error;
Expand All @@ -76,15 +76,15 @@ export function logout() {

export async function getTokenBalances(walletAddress) {
try {
const starknet = await connect();
if (!starknet.isConnected) {
const { wallet } = await connect();
if (!wallet) {
throw new Error('Wallet not connected');
}

const tokenBalances = {
ETH: await getTokenBalance(starknet, walletAddress, ETH_ADDRESS),
USDC: await getTokenBalance(starknet, walletAddress, USDC_ADDRESS),
STRK: await getTokenBalance(starknet, walletAddress, STRK_ADDRESS),
ETH: await getTokenBalance(wallet, walletAddress, ETH_ADDRESS),
USDC: await getTokenBalance(wallet, walletAddress, USDC_ADDRESS),
STRK: await getTokenBalance(wallet, walletAddress, STRK_ADDRESS),
};

return tokenBalances;
Expand All @@ -94,15 +94,16 @@ export async function getTokenBalances(walletAddress) {
}
}

async function getTokenBalance(starknet, walletAddress, tokenAddress) {
async function getTokenBalance(wallet, walletAddress, tokenAddress) {
try {
const response = await starknet.provider.callContract({
const response = await wallet.account.callContract({
contractAddress: tokenAddress,
entrypoint: 'balanceOf',
calldata: [walletAddress],
});

const tokenDecimals = (tokenAddress === USDC_ADDRESS) ? 6 : 18;
const balance = BigInt(response.result[0]).toString();
const balance = BigInt(response[0]).toString();
const readableBalance = (Number(balance) / (10 ** tokenDecimals)).toFixed(4);
console.log(`Balance for token ${tokenAddress}:`, readableBalance);
return readableBalance;
Expand All @@ -112,7 +113,6 @@ async function getTokenBalance(starknet, walletAddress, tokenAddress) {
}
}


export const getBalances = async (walletId, setBalances) => {
if (!walletId) return;
try {
Expand All @@ -134,7 +134,6 @@ export const getBalances = async (walletId, setBalances) => {
title: 'STRK',
balance: data.STRK !== undefined ? data.STRK.toString() : '0.00',
},
// { icon: <DAI />, title: 'DAI', balance: data.DAI !== undefined ? data.DAI.toString() : '0.00' }, dont have DAI in the constants file
];

setBalances(updatedBalances);
Expand All @@ -143,5 +142,5 @@ export const getBalances = async (walletId, setBalances) => {
}
};

// Add this line for environments that don't recognize BigInt
const BigInt = window.BigInt;

const BigInt = window.BigInt;
12 changes: 0 additions & 12 deletions frontend/test/__mocks__/get-starknet.js

This file was deleted.

17 changes: 17 additions & 0 deletions frontend/test/__mocks__/starknetkit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module.exports = {
connect: jest.fn(() =>
Promise.resolve({
address: '0xMockedAddress12345',
chainId: 'mocked-chain-id',
})
),

disconnect: jest.fn(() => Promise.resolve(true)),

getNetwork: jest.fn(() =>
Promise.resolve({
network: 'MockedNetwork',
rpcUrl: 'https://mocked.rpc.url',
})
),
};
Loading
Loading