Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions packages/apps/staking/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
VITE_APP_DASHBOARD_API_URL=http://0.0.0.0:5006
VITE_APP_ENVIRONMENT=testnet
VITE_APP_SUPPORTED_CHAINS=80002
VITE_APP_WALLETCONNECT_PROJECT_ID=replace-me

# Links to header
VITE_HEADER_LINK_DASHBOARD=http://localhost:3004
Expand Down
13 changes: 8 additions & 5 deletions packages/apps/staking/src/hooks/useKVStore.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ChainId, IKVStore, KVStoreClient } from '@human-protocol/sdk';
import { ethers } from 'ethers';
import { Eip1193Provider, ethers } from 'ethers';
import { useEffect, useState } from 'react';
import { useAccount, useWalletClient } from 'wagmi';
import { SUPPORTED_CHAIN_IDS } from '../constants/chains';
Expand All @@ -8,7 +8,7 @@ import { parseErrorMessage } from '../utils/string';
import { getKVStoreData } from '../services/dashboard';

export const useKVStore = () => {
const { address, chainId } = useAccount();
const { address, chainId, connector } = useAccount();
const { data: walletClient } = useWalletClient();
const { showError, openSnackbar } = useSnackbar();

Expand Down Expand Up @@ -36,9 +36,12 @@ export const useKVStore = () => {
useEffect(() => {
const initStakingClient = async () => {
try {
if (walletClient && address) {
if (walletClient && address && connector) {
checkSupportedChain();
const provider = new ethers.BrowserProvider(window.ethereum);
const eeip193Provider = await connector?.getProvider();
const provider = new ethers.BrowserProvider(
eeip193Provider as Eip1193Provider
);
const signer = await provider.getSigner();

const client = await KVStoreClient.build(signer);
Expand All @@ -53,7 +56,7 @@ export const useKVStore = () => {

initStakingClient();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [walletClient, address, chainId]);
}, [walletClient, address, chainId, connector]);

const checkSupportedChain = () => {
const isSupportedChain = SUPPORTED_CHAIN_IDS.includes(chainId as ChainId);
Expand Down
46 changes: 23 additions & 23 deletions packages/apps/staking/src/hooks/useStake.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
StakerInfo,
StakingClient,
} from '@human-protocol/sdk';
import { ethers } from 'ethers';
import { Eip1193Provider, ethers } from 'ethers';
import { useEffect, useState } from 'react';
import { useAccount, useWalletClient } from 'wagmi';
import { useSnackbar } from '../providers/SnackProvider';
Expand All @@ -14,7 +14,7 @@ import { formatAmount } from '../utils/units';
import { SUPPORTED_CHAIN_IDS } from '../constants/chains';

export const useStake = () => {
const { address, chainId } = useAccount();
const { address, chainId, connector } = useAccount();
const { data: walletClient } = useWalletClient();
const { showError, openSnackbar } = useSnackbar();

Expand All @@ -23,13 +23,19 @@ export const useStake = () => {
);
const [stakingData, setStakingData] = useState<StakerInfo | null>(null);
const [tokenBalance, setTokenBalance] = useState<number>(0);
const [browserProvider, setBrowserProvider] =
useState<ethers.BrowserProvider | null>(null);

useEffect(() => {
const initStakingClient = async () => {
try {
if (walletClient && address) {
if (walletClient && address && connector) {
checkSupportedChain();
const provider = new ethers.BrowserProvider(window.ethereum);
const eeip193Provider = await connector?.getProvider();
const provider = new ethers.BrowserProvider(
eeip193Provider as Eip1193Provider
);
setBrowserProvider(provider);
const signer = await provider.getSigner();

const client = await StakingClient.build(signer);
Expand All @@ -45,7 +51,7 @@ export const useStake = () => {

initStakingClient();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [walletClient, address, chainId]);
}, [walletClient, address, chainId, connector]);

const checkSupportedChain = () => {
const isSupportedChain = SUPPORTED_CHAIN_IDS.includes(chainId as ChainId);
Expand Down Expand Up @@ -99,18 +105,16 @@ export const useStake = () => {
};

const handleStake = async (amount: string) => {
if (!browserProvider) return;

try {
checkSupportedChain();
if (stakingClient && amount) {
if (stakingClient && amount && address) {
const weiAmount = ethers.parseUnits(amount, 'ether');
await stakingClient.approveStake(weiAmount);
await stakingClient.stake(weiAmount);
await fetchStakingData(stakingClient);
await fetchTokenBalance(
new ethers.BrowserProvider(window.ethereum),
address!,
chainId
);
await fetchTokenBalance(browserProvider, address, chainId);
openSnackbar('Stake successful', 'success');
}
} catch (error) {
Expand All @@ -120,17 +124,15 @@ export const useStake = () => {
};

const handleUnstake = async (amount: string) => {
if (!browserProvider) return;

try {
checkSupportedChain();
if (stakingClient && amount) {
if (stakingClient && amount && address) {
const weiAmount = ethers.parseUnits(amount, 'ether');
await stakingClient.unstake(weiAmount);
await fetchStakingData(stakingClient);
await fetchTokenBalance(
new ethers.BrowserProvider(window.ethereum),
address!,
chainId
);
await fetchTokenBalance(browserProvider, address, chainId);
openSnackbar('Unstake successful', 'success');
}
} catch (error) {
Expand All @@ -140,16 +142,14 @@ export const useStake = () => {
};

const handleWithdraw = async () => {
if (!browserProvider) return;

try {
checkSupportedChain();
if (stakingClient) {
if (stakingClient && address) {
await stakingClient.withdraw();
await fetchStakingData(stakingClient);
await fetchTokenBalance(
new ethers.BrowserProvider(window.ethereum),
address!,
chainId
);
await fetchTokenBalance(browserProvider, address, chainId);
openSnackbar('Withdraw successful', 'success');
}
} catch (error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,13 @@ def get_escrows(

escrows = []

statuses = None
if filter.status:
if isinstance(filter.status, list):
statuses = [s.name for s in filter.status]
else:
statuses = [filter.status.name]

escrows_data = get_data_from_subgraph(
network,
query=get_escrows_query(filter),
Expand All @@ -207,7 +214,7 @@ def get_escrows(
filter.exchange_oracle.lower() if filter.exchange_oracle else None
),
"jobRequesterId": filter.job_requester_id,
"status": filter.status.name if filter.status else None,
"status": statuses,
"from": (
int(filter.date_from.timestamp()) if filter.date_from else None
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def __init__(
recording_oracle: Optional[str] = None,
exchange_oracle: Optional[str] = None,
job_requester_id: Optional[str] = None,
status: Optional[Status] = None,
status: Optional[Status | List[Status]] = None,
date_from: Optional[datetime] = None,
date_to: Optional[datetime] = None,
first: int = 10,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def get_escrows_query(filter: EscrowFilter):
$recordingOracle: String
$exchangeOracle: String
$jobRequesterId: String
$status: String
$status: [String!]
$from: Int
$to: Int
$orderDirection: String
Expand Down Expand Up @@ -76,7 +76,7 @@ def get_escrows_query(filter: EscrowFilter):
job_requester_clause=(
"jobRequesterId: $jobRequesterId" if filter.job_requester_id else ""
),
status_clause="status: $status" if filter.status else "",
status_clause="status_in: $status" if filter.status else "",
from_clause="createdAt_gte: $from" if filter.date_from else "",
to_clause="createdAt_lte: $to" if filter.date_to else "",
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def side_effect(subgraph_url, query, params):
"recordingOracle": None,
"exchangeOracle": None,
"jobRequesterId": "1",
"status": "Pending",
"status": ["Pending"],
"from": 1683811973,
"to": 1683812007,
"first": 10,
Expand Down Expand Up @@ -104,6 +104,83 @@ def side_effect(subgraph_url, query, params):
self.assertEqual(len(filtered), 1)
self.assertEqual(filtered[0].chain_id, ChainId.POLYGON_AMOY)

def test_get_escrows_with_status_array(self):
"""Test get_escrows with an array of statuses, similar to the TypeScript test."""
with patch(
"human_protocol_sdk.escrow.escrow_utils.get_data_from_subgraph"
) as mock_function:
mock_escrow_1 = {
"id": "0x1234567890123456789012345678901234567891",
"address": "0x1234567890123456789012345678901234567891",
"amountPaid": "1000000000000000000",
"balance": "1000000000000000000",
"count": "1",
"factoryAddress": "0x1234567890123456789012345678901234567890",
"finalResultsUrl": "https://example.com",
"intermediateResultsUrl": "https://example.com",
"launcher": "0x1234567890123456789012345678901234567891",
"manifestHash": "0x1234567890123456789012345678901234567891",
"manifestUrl": "https://example.com",
"recordingOracle": "0x1234567890123456789012345678901234567891",
"reputationOracle": "0x1234567890123456789012345678901234567891",
"exchangeOracle": "0x1234567890123456789012345678901234567891",
"status": "Pending",
"token": "0x1234567890123456789012345678901234567891",
"totalFundedAmount": "1000000000000000000",
}
mock_escrow_2 = {
"id": "0x1234567890123456789012345678901234567891",
"address": "0x1234567890123456789012345678901234567891",
"amountPaid": "1000000000000000000",
"balance": "1000000000000000000",
"count": "1",
"factoryAddress": "0x1234567890123456789012345678901234567890",
"finalResultsUrl": "https://example.com",
"intermediateResultsUrl": "https://example.com",
"launcher": "0x1234567890123456789012345678901234567891",
"manifestHash": "0x1234567890123456789012345678901234567891",
"manifestUrl": "https://example.com",
"recordingOracle": "0x1234567890123456789012345678901234567891",
"reputationOracle": "0x1234567890123456789012345678901234567891",
"exchangeOracle": "0x1234567890123456789012345678901234567891",
"status": "Complete",
"token": "0x1234567890123456789012345678901234567891",
"totalFundedAmount": "1000000000000000000",
}

def side_effect(subgraph_url, query, params):
if subgraph_url == NETWORKS[ChainId.POLYGON_AMOY]:
return {"data": {"escrows": [mock_escrow_1, mock_escrow_2]}}

mock_function.side_effect = side_effect

filter = EscrowFilter(
chain_id=ChainId.POLYGON_AMOY,
status=[Status.Pending, Status.Complete],
)
filtered = EscrowUtils.get_escrows(filter)

mock_function.assert_called_with(
NETWORKS[ChainId.POLYGON_AMOY],
query=get_escrows_query(filter),
params={
"launcher": None,
"reputationOracle": None,
"recordingOracle": None,
"exchangeOracle": None,
"jobRequesterId": None,
"status": ["Pending", "Complete"],
"from": None,
"to": None,
"first": 10,
"skip": 0,
"orderDirection": "desc",
},
)
self.assertEqual(len(filtered), 2)
self.assertEqual(filtered[0].address, mock_escrow_1["address"])
self.assertEqual(filtered[1].address, mock_escrow_2["address"])

def test_get_escrow(self):
with patch(
"human_protocol_sdk.escrow.escrow_utils.get_data_from_subgraph"
Expand Down
7 changes: 3 additions & 4 deletions packages/sdk/typescript/human-protocol-sdk/example/escrow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@ export const getEscrows = async () => {
}

const escrows = await EscrowUtils.getEscrows({
status: EscrowStatus.Pending,
from: new Date(2023, 4, 8),
to: new Date(2023, 5, 8),
status: [EscrowStatus.Pending, EscrowStatus.Complete],
chainId: ChainId.POLYGON_AMOY,
first: 1000,
});

console.log('Pending escrows:', escrows);
console.log('Pending escrows:', escrows.length);
};

(async () => {
Expand Down
12 changes: 6 additions & 6 deletions packages/sdk/typescript/human-protocol-sdk/src/escrow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1688,6 +1688,11 @@ export class EscrowUtils {
throw ErrorUnsupportedChainID;
}

let statuses;
if (filter.status !== undefined) {
statuses = Array.isArray(filter.status) ? filter.status : [filter.status];
statuses = statuses.map((status) => EscrowStatus[status]);
}
const { escrows } = await gqlFetch<{ escrows: EscrowData[] }>(
getSubgraphUrl(networkData),
GET_ESCROWS_QUERY(filter),
Expand All @@ -1697,12 +1702,7 @@ export class EscrowUtils {
reputationOracle: filter.reputationOracle?.toLowerCase(),
recordingOracle: filter.recordingOracle?.toLowerCase(),
exchangeOracle: filter.exchangeOracle?.toLowerCase(),
status:
filter.status !== undefined
? Object.entries(EscrowStatus).find(
([, value]) => value === filter.status
)?.[0]
: undefined,
status: statuses,
from: filter.from ? getUnixTimestamp(filter.from) : undefined,
to: filter.to ? getUnixTimestamp(filter.to) : undefined,
orderDirection: orderDirection,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export const GET_ESCROWS_QUERY = (filter: IEscrowsFilter) => {
${reputationOracle ? `reputationOracle: $reputationOracle,` : ''}
${recordingOracle ? `recordingOracle: $recordingOracle,` : ''}
${exchangeOracle ? `exchangeOracle: $exchangeOracle,` : ''}
${status !== undefined ? `status: $status,` : ''}
${status !== undefined ? `status_in: $status,` : ''}
${from ? `createdAt_gte: $from,` : ''}
${to ? `createdAt_lte: $to,` : ''}
}
Expand All @@ -66,7 +66,7 @@ export const GET_ESCROWS_QUERY = (filter: IEscrowsFilter) => {
$reputationOracle: String
$recordingOracle: String
$exchangeOracle: String
$status: String
$status: [String!]
$from: Int
$to: Int
$orderDirection: String
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export interface IEscrowsFilter extends IPagination {
recordingOracle?: string;
exchangeOracle?: string;
jobRequesterId?: string;
status?: EscrowStatus;
status?: EscrowStatus | EscrowStatus[];
from?: Date;
to?: Date;
chainId: ChainId;
Expand Down
Loading
Loading