-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: #27 check balance before minting or creating NFT
- Loading branch information
Showing
18 changed files
with
230 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import _get from 'lodash/get'; | ||
import { useQuery, UseQueryOptions } from 'react-query'; | ||
|
||
import { QueryKeys } from '@/enums/queryKeys.enum'; | ||
import { getAccounts } from '@/services/casperdash/user'; | ||
import { hexToNumber } from '@/utils/format'; | ||
|
||
type GetAccountBalanceResponse = { | ||
balanace: number; | ||
}; | ||
|
||
export const useGetAccountBalance = ( | ||
{ publicKey }: { publicKey?: string | null }, | ||
options?: Omit< | ||
UseQueryOptions< | ||
GetAccountBalanceResponse, | ||
unknown, | ||
GetAccountBalanceResponse, | ||
[QueryKeys.ACCOUNT_BALANCE, string | null | undefined] | ||
>, | ||
'queryKey' | 'queryFn' | ||
> | ||
) => { | ||
return useQuery( | ||
[QueryKeys.ACCOUNT_BALANCE, publicKey], | ||
async () => { | ||
const accounts = await getAccounts({ | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
publicKeys: [publicKey!], | ||
}); | ||
if (!accounts || accounts.length === 0) { | ||
throw new Error('Can not get account'); | ||
} | ||
|
||
const [account] = accounts; | ||
const balanceHex = _get(account, 'balance.hex', ''); | ||
|
||
return { | ||
balanace: hexToNumber(balanceHex), | ||
}; | ||
}, | ||
{ | ||
...options, | ||
enabled: !!publicKey, | ||
refetchOnWindowFocus: true, | ||
} | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,10 @@ | ||
import axios from 'axios'; | ||
|
||
import { Config } from '@/config'; | ||
import request from './request'; | ||
|
||
export const deploy = async (signedDeploy: unknown) => { | ||
const { | ||
data: { deployHash }, | ||
} = await axios.post(Config.deployUrl, signedDeploy); | ||
const data = await request.post<{ deployHash: string }>( | ||
'/deploy', | ||
signedDeploy | ||
); | ||
|
||
return deployHash; | ||
return (<{ deployHash: string }>(<unknown>data)).deployHash; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import axios, { AxiosResponse } from 'axios'; | ||
import qs from 'qs'; | ||
|
||
import { Config } from '@/config'; | ||
|
||
const casperDashRequest = axios.create({ | ||
baseURL: Config.casperDashUrl, | ||
timeout: 100 * 1000, | ||
paramsSerializer: { | ||
serialize: (params: Record<string, unknown>) => | ||
qs.stringify(params, { arrayFormat: 'repeat' }), | ||
}, | ||
}); | ||
|
||
casperDashRequest.interceptors.response.use( | ||
(response: AxiosResponse) => response.data, | ||
(error) => { | ||
const { status } = error.response; | ||
|
||
if (status === 400) { | ||
const { | ||
data: { message }, | ||
} = error.response; | ||
|
||
alert(message); | ||
} | ||
|
||
return Promise.reject(error); | ||
} | ||
); | ||
|
||
export default casperDashRequest; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './type'; | ||
export * from './user.service'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
export type Account = { | ||
_accountHash: string; | ||
namedKeys: { | ||
name: string; | ||
key: string; | ||
}[]; | ||
mainPurse: string; | ||
associatedKeys: { | ||
accountHash: string; | ||
weight: number; | ||
}[]; | ||
actionThresholds: { | ||
deployment: number; | ||
keyManagement: number; | ||
}; | ||
balance: { | ||
hex: string; | ||
type: string; | ||
}; | ||
publicKey: string; | ||
}; | ||
|
||
export type GetAccountsResponse = Account[]; | ||
|
||
export type GetAccountsParams = { | ||
publicKeys: string[]; | ||
}; | ||
|
||
export type TBalance = { | ||
hex: string; | ||
type: string; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { GetAccountsParams, GetAccountsResponse } from './type'; | ||
import request from '../request'; | ||
|
||
export const getAccounts = async ({ | ||
publicKeys, | ||
}: GetAccountsParams): Promise<GetAccountsResponse> => { | ||
return request.post('/users', { publicKeys }); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.