Skip to content

Commit

Permalink
Fix user wallet connect and admin funding bugs (#436)
Browse files Browse the repository at this point in the history
  • Loading branch information
AmirAgassi authored Feb 6, 2025
2 parents 0bf3784 + 418a847 commit ceeecb0
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ function FundingModal({ isOpen, onClose, onSubmit, isLoading }: FundingModalProp
const [error, setError] = useState('');

// SPURCOIN conversion rate (we need to fetch this once the coin, uhh, exists.)
const SPURCOIN_TO_CAD = 69.0;
const SPURCOIN_TO_CAD = 1.5;
const estimatedSPUR = amount ? (parseFloat(amount) / SPURCOIN_TO_CAD).toFixed(2) : '0';

const handleSubmit = async (e: React.FormEvent) => {
Expand Down Expand Up @@ -169,7 +169,7 @@ function RouteComponent() {

tx.transferObjects(
[splitCoinTx],
tx.pure.address('0x2b3e5cd101c75ee3828d85f798c426cba145554431c49898f26e01d6f17bcbfc')
tx.pure.address(company?.wallet_address || '')
);

if (wallet.account?.address) {
Expand Down Expand Up @@ -377,7 +377,8 @@ function RouteComponent() {
) : (
<button
onClick={() => setIsModalOpen(true)}
disabled={isSendingFunds}
disabled={isSendingFunds || !company.wallet_address}
title={!company.wallet_address ? "Company hasn't set up their wallet address" : ""}
className="px-4 py-2 text-sm font-medium text-white bg-gray-900 rounded-md hover:bg-gray-800 disabled:bg-gray-400 disabled:cursor-not-allowed flex items-center gap-2"
>
<FiDollarSign className="w-4 h-4" />
Expand Down
75 changes: 68 additions & 7 deletions frontend/src/pages/user/_auth/_appshell/settings.wallet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import { SettingsPage } from '@/templates/SettingsPage/SettingsPage'
import { FiCopy } from 'react-icons/fi'
import { WalletConnectButton } from '@/components/wallet/WalletConnectButton'
import { useWallet } from '@suiet/wallet-kit'
import { useAuth } from '@/contexts'
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
import { getCompany, updateCompany } from '@/services'

export const Route = createFileRoute('/user/_auth/_appshell/settings/wallet')({
component: WalletSettings,
Expand All @@ -13,6 +16,40 @@ export const Route = createFileRoute('/user/_auth/_appshell/settings/wallet')({
function WalletSettings() {
const [error, setError] = useState<string | null>(null)
const { connected, address, disconnect } = useWallet()
const { accessToken } = useAuth()
const queryClient = useQueryClient()

// Fetch company data
const { data: company } = useQuery({
queryKey: ['company'],
queryFn: () => {
if (!accessToken) throw new Error('No access token')
return getCompany(accessToken)
},
enabled: !!accessToken,
})

// Update company mutation
const { mutate: updateWallet, isLoading: isUpdating } = useMutation({
mutationFn: async () => {
if (!accessToken) throw new Error('No access token')
if (!address) throw new Error('No wallet address')
return updateCompany(accessToken, {
wallet_address: address || ''
})
},
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ['company'] })
setError(null)
},
onError: (err) => {
if (err instanceof Error) {
setError(err.message)
} else {
setError('Failed to update wallet address')
}
},
})

const handleCopyAddress = () => {
if (address) {
Expand All @@ -23,11 +60,24 @@ function WalletSettings() {
const handleDisconnectWallet = async () => {
try {
await disconnect()
// Clear wallet address from company when disconnecting
if (accessToken) {
await updateCompany(accessToken, { wallet_address: '' })
queryClient.invalidateQueries({ queryKey: ['company'] })
}
} catch (err) {
setError('Failed to disconnect wallet')
}
}

const handleSaveWallet = async () => {
if (connected && address) {
updateWallet()
}
}

const isWalletSaved = company?.wallet_address === address

return (
<SettingsPage title="Wallet" error={error}>
<div className="max-w-2xl">
Expand Down Expand Up @@ -60,13 +110,24 @@ function WalletSettings() {
</div>
</div>
</div>
<Button
variant="secondary"
onClick={handleDisconnectWallet}
className="w-full"
>
Disconnect Wallet
</Button>
<div className="space-y-3">
{!isWalletSaved && (
<Button
onClick={handleSaveWallet}
disabled={isUpdating}
className="w-full"
>
{isUpdating ? 'Saving...' : 'Save Wallet Address'}
</Button>
)}
<Button
variant="secondary"
onClick={handleDisconnectWallet}
className="w-full"
>
Disconnect Wallet
</Button>
</div>
</div>
</div>
)}
Expand Down
1 change: 1 addition & 0 deletions frontend/src/services/company.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export async function updateCompany(
...(data.stage && { stages: data.stage.map((s) => s.value) }),
...(data.website && { website: data.website }),
...(data.linkedin && { linkedin_url: data.linkedin }),
...(data.wallet_address !== undefined && { wallet_address: data.wallet_address }),
};

const res = await fetch(url, {
Expand Down
2 changes: 2 additions & 0 deletions frontend/src/types/company.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export interface CompanyInformation {
stage: DropdownOption[];
website?: string;
linkedin?: string;
wallet_address?: string;
}

export interface CompanyFormErrors {
Expand All @@ -32,6 +33,7 @@ export interface CreateCompanyRequest {
stages: string[];
website?: string;
linkedin_url?: string;
wallet_address?: string;
}

export interface CompanyResponse {
Expand Down

0 comments on commit ceeecb0

Please sign in to comment.