Skip to content

Commit cfcde11

Browse files
feat: connected to backend api
1 parent 3c10d96 commit cfcde11

File tree

28 files changed

+800
-220
lines changed

28 files changed

+800
-220
lines changed

app/components/apiClient/base.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,7 @@ export function isAPIError(reason: unknown): reason is APIError {
1616

1717
export type FetchLike<R extends any = any> = (
1818
input: string,
19-
init?: {
20-
method?: string
21-
body?: string
22-
headers: { [key in string]: string }
23-
},
19+
init?: NodeJS.fetch.RequestInit,
2420
) => Promise<{
2521
status: number
2622
json(): Promise<R>
@@ -46,22 +42,29 @@ export class APIClient {
4642
params: P = {} as P,
4743
) {
4844
return new Promise<R>((resolve, reject) => {
49-
this.fetch(`${this.origin}/api/${endpoint}`, {
45+
const init: NodeJS.fetch.RequestInit = {
5046
method,
51-
body: JSON.stringify({
52-
...params,
53-
}),
5447
headers: {
5548
'Content-Type': 'application/json',
5649
},
57-
})
50+
}
51+
52+
if (Object.keys(params).length !== 0) {
53+
Object.assign(init, {
54+
body: JSON.stringify({
55+
...params,
56+
}),
57+
})
58+
}
59+
60+
this.fetch(`${this.origin}${endpoint}`, init)
5861
.then(async (res) => {
5962
const body = res.status === 204 ? null : await res.json()
6063

6164
if (res.status === 200) {
62-
resolve(body)
65+
resolve(body.data ?? body)
6366
} else if (res.status === 204) {
64-
resolve(body)
67+
resolve(body.data ?? body)
6568
} else {
6669
reject({
6770
[API_ERROR]: true,

app/components/apiClient/index.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ import {
33
Assets,
44
AddressHashParams,
55
TokenCreateData,
6+
TokenUpdateData,
67
TokenTransferParams,
78
TokenMintParams,
89
Transaction,
10+
ServerTransaction,
911
} from '@/app/type'
10-
import { RawTransaction } from '@ckb-lumos/base'
1112
import { APIClient, APIClientOptions } from './base'
1213
import { MockApi } from './mock'
1314

@@ -21,19 +22,24 @@ export class SUDTApi extends APIClient {
2122
this.get<Token[]>(`/token?${new URLSearchParams(params)}`),
2223
detail: (args: string) => this.get<Token>(`/token/${args}`),
2324
create: (data: TokenCreateData) =>
24-
this.post<TokenCreateData, RawTransaction>('/token', data),
25-
update: (data: TokenCreateData) =>
26-
this.put<TokenCreateData, Token>('/token', data),
27-
transfer: (data: TokenTransferParams) =>
28-
this.post<TokenTransferParams, RawTransaction>('/token/transfer', data),
25+
this.post<TokenCreateData, ServerTransaction>('/token', data),
26+
update: (typeId: string, data: TokenUpdateData) =>
27+
this.put<TokenUpdateData, Token>(`/token/${typeId}`, data),
28+
transfer: (typeId: string, data: TokenTransferParams) =>
29+
this.post<TokenTransferParams, ServerTransaction>(
30+
`/token/send/${typeId}/`,
31+
data,
32+
),
2933
mint: (typeId: string, params: TokenMintParams) =>
30-
this.post<TokenMintParams, RawTransaction>(
34+
this.post<TokenMintParams, ServerTransaction>(
3135
`/token/mint/${typeId}/`,
3236
params,
3337
),
3438
}
3539

3640
account = {
41+
meta: (addressHash: string) =>
42+
this.get<{ capacity: string }>(`/account/meta/${addressHash}`),
3743
asyncAddress: (addressHash: string, addresses: string[]) =>
3844
this.post(`/account/${addressHash}`, { addresses }),
3945
listAssets: (addressHash: string) =>

app/components/apiClient/mock.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {
33
TokenCreateData,
44
TokenTransferParams,
55
TokenMintParams,
6-
History,
6+
TokenUpdateData,
77
} from '@/app/type'
88
import { APIClient } from './base'
99
import {
@@ -19,10 +19,8 @@ export class MockApi extends APIClient {
1919
detail: (args: string) =>
2020
Promise.resolve(MOCK_TOKENS.find((token) => token.symbol === args)),
2121
create: (_: TokenCreateData) => Promise.resolve(MOCK_RAW_TRANSACTION),
22-
update: (data: TokenCreateData) =>
23-
Promise.resolve(
24-
MOCK_TOKENS.find((token) => token.symbol === data.symbol),
25-
),
22+
update: (typeId: string, data: TokenUpdateData) =>
23+
Promise.resolve(MOCK_TOKENS.find((token) => token.typeId === typeId)),
2624
transfer: (_: TokenTransferParams) => Promise.resolve(MOCK_RAW_TRANSACTION),
2725
mint: (_: string, __: TokenMintParams) =>
2826
Promise.resolve(MOCK_RAW_TRANSACTION),

app/components/apiClient/mockData.ts

Lines changed: 62 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import type { Transaction, Assets, Token } from '@/app/type'
2-
import type { RawTransaction } from '@ckb-lumos/base'
1+
import type { Transaction, Assets, Token, ServerTransaction } from '@/app/type'
2+
// import type { RawTransaction } from '@ckb-lumos/base'
33

44
export const MOCK_ACCOUNTS: {
55
[namke: string]: Record<'address' | 'balance', string>
@@ -28,24 +28,28 @@ export const MOCK_ACCOUNTS: {
2828

2929
export const MOCK_ASSETS: Assets[] = [
3030
{
31+
typeId: '1',
3132
displayName: 'CKB',
3233
uan: 'CKB',
3334
decimal: '8',
3435
amount: '10000000000',
3536
},
3637
{
38+
typeId: '2',
3739
displayName: 'SUDT1',
3840
uan: 'SUDT1',
3941
decimal: '8',
4042
amount: '10000000000',
4143
},
4244
{
45+
typeId: '3',
4346
displayName: 'SUDT2',
4447
uan: 'SUDT2',
4548
decimal: '8',
4649
amount: '20000000000',
4750
},
4851
{
52+
typeId: '4',
4953
displayName: 'SUDT3',
5054
uan: 'SUDT3',
5155
decimal: '8',
@@ -59,11 +63,15 @@ export const MOCK_TOKENS: Token[] = [
5963
decimal: '8',
6064
name: 'Demo Simple User Define Token',
6165
66+
typeId: '1',
67+
amount: '10000',
6268
description: '',
6369
website: '',
6470
icon: '',
6571
},
6672
{
73+
typeId: '2',
74+
amount: '10000',
6775
symbol: 'SUDT2',
6876
decimal: '8',
6977
name: 'Demo Simple User Define Token',
@@ -73,6 +81,8 @@ export const MOCK_TOKENS: Token[] = [
7381
icon: '',
7482
},
7583
{
84+
typeId: '3',
85+
amount: '10000',
7686
symbol: 'SUDT3',
7787
decimal: '8',
7888
name: 'Demo Simple User Define Token',
@@ -83,75 +93,89 @@ export const MOCK_TOKENS: Token[] = [
8393
},
8494
]
8595

86-
export const MOCK_RAW_TRANSACTION: RawTransaction = {
87-
version: '0x0',
96+
export const MOCK_RAW_TRANSACTION: ServerTransaction = {
8897
cellDeps: [
8998
{
9099
outPoint: {
91100
txHash:
92-
'0xbcdb11e9815b3d9fb6278af097e2ae54fe4f8c9c97d352d8a15538ed0398ac83',
93-
index: '0x1',
101+
'0xec26b0f85ed839ece5f11c4c4e837ec359f5adc4420410f6453b1f6b60fb96a6',
102+
index: '0x0',
94103
},
95104
depType: 'depGroup',
96105
},
97106
{
98107
outPoint: {
99108
txHash:
100-
'0xbcdb11e9815b3d9fb6278af097e2ae54fe4f8c9c97d352d8a15538ed0398ac83',
109+
'0xe12877ebd2c3c364dc46c5c992bcfaf4fee33fa13eebdf82c591fc9825aab769',
101110
index: '0x0',
102111
},
103-
depType: 'depGroup',
112+
depType: 'code',
104113
},
105114
],
106115
headerDeps: [],
107116
inputs: [
108117
{
109-
since: '0x0',
110-
previousOutput: {
111-
txHash:
112-
'0xa401e0b880329ea492e95f3fc085fe03e33a66f5e010aadbf8fcd0d5ecc09e5f',
113-
index: '0x0',
118+
cellOutput: {
119+
capacity: '0x90fcedc3f20',
120+
lock: {
121+
codeHash:
122+
'0x3419a1c09eb2567f6552ee7a8ecffd64155cffe0f1796e6e61ec088d740c1356',
123+
hashType: 'type',
124+
args: '0x1b3e74a036a21f94eba3d7c94b9d5619e1e84f7c',
125+
},
114126
},
115-
},
116-
{
117-
since: '0x0',
118-
previousOutput: {
127+
data: '0x',
128+
outPoint: {
119129
txHash:
120-
'0x3fdc5faa485a9687dcf7b12445cb77376798cbbc6efbc9fd5e8e22589c385921',
130+
'0x6827d1fb91b10d589303e66ec574bde9f4b1ca6eacbd05c475cd194391e6fc78',
121131
index: '0x1',
122132
},
133+
blockNumber: '0xb0d5de',
123134
},
124135
],
125136
outputs: [
126137
{
127-
capacity: '0x2540be400',
128-
lock: {
129-
codeHash:
130-
'0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8',
131-
hashType: 'type',
132-
args: '0x6cd8ae51f91bacd7910126f880138b30ac5d3015',
138+
cellOutput: {
139+
capacity: '0x34e62ce00',
140+
lock: {
141+
codeHash:
142+
'0x3419a1c09eb2567f6552ee7a8ecffd64155cffe0f1796e6e61ec088d740c1356',
143+
hashType: 'type',
144+
args: '0x1b3e74a036a21f94eba3d7c94b9d5619e1e84f7c',
145+
},
146+
type: {
147+
codeHash:
148+
'0xc5e5dcf215925f7ef4dfaf5f4b4f105bc321c02776d6e7d52a1db3fcd9d011a4',
149+
hashType: 'type',
150+
args: '0x0226fc667898d411d97a2603210b3636737b18b8d36bebad98c3eeca7562acdb',
151+
},
133152
},
153+
data: '0xa0860100000000000000000000000000',
134154
},
135155
{
136-
capacity: '0x5af0bc6e5c00',
137-
lock: {
138-
codeHash:
139-
'0x5c5069eb0857efc65e1bca0c07df34c31663b3622fd3876c876320fc9634e2a8',
140-
hashType: 'type',
141-
args: '0x8bebce3e7dd7b7179defe4d06ecf9776b1ba686d',
156+
cellOutput: {
157+
lock: {
158+
codeHash:
159+
'0x3419a1c09eb2567f6552ee7a8ecffd64155cffe0f1796e6e61ec088d740c1356',
160+
hashType: 'type',
161+
args: '0x1b3e74a036a21f94eba3d7c94b9d5619e1e84f7c',
162+
},
163+
capacity: '0x90c8077ea80',
142164
},
165+
data: '0x',
143166
},
167+
],
168+
witnesses: [
169+
'0x55000000100000005500000055000000410000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
170+
],
171+
fixedEntries: [
144172
{
145-
capacity: '0x1bc0b78127dd9f00',
146-
lock: {
147-
codeHash:
148-
'0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8',
149-
hashType: 'type',
150-
args: '0xe390d4b9b4c7637ec80799bdaf644ae625cdb922',
151-
},
173+
field: 'outputs',
174+
index: 0,
152175
},
153176
],
154-
outputsData: ['0x', '0x', '0x'],
177+
signingEntries: [],
178+
inputSinces: {},
155179
}
156180

157181
export const MOCK_TRANSACTION: Transaction[] = [

app/components/asset/AssetList.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ interface AssetListProps
1010

1111
export const AssetList: FC<AssetListProps> = ({ className, ...attrs }) => {
1212
const { addressHash } = useAccount()
13-
const { data: assets } = useSWR(['assets'], () =>
14-
sudtApi.account.listAssets(addressHash),
13+
const { data: assets } = useSWR(
14+
addressHash ? ['assets', addressHash] : null,
15+
() => sudtApi.account.listAssets(addressHash),
1516
)
1617

1718
if (!assets) {

app/components/body/index.tsx

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'use client'
2+
import { useAccount } from '@/app/hooks/useAccount'
3+
import { Inter } from 'next/font/google'
4+
import { Account } from '../account'
5+
import { MainSection } from '../section'
6+
import { ConnectAccount } from '../connect-account'
7+
const inter = Inter({ subsets: ['latin'] })
8+
9+
export function Body({ children }: { children: React.ReactNode }) {
10+
const account = useAccount()
11+
12+
return (
13+
<body className={inter.className}>
14+
<main className="flex min-h-screen flex-col items-center p-10">
15+
{account.isConnected ? (
16+
<>
17+
<Account />
18+
<MainSection>{children}</MainSection>
19+
</>
20+
) : (
21+
<MainSection>
22+
<ConnectAccount />
23+
</MainSection>
24+
)}
25+
</main>
26+
</body>
27+
)
28+
}

app/components/token/TokenInfomationForm.tsx

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,6 @@ export function TokenInfomationForm({
2323
return (
2424
<form className="flex flex-col gap-4" onSubmit={handleSubmit(onSubmit)}>
2525
<div className="bg-lighter-color p-4 rounded-xl flex flex-col gap-4">
26-
<div className="flex flex-col">
27-
<label>Symbol</label>
28-
<Input
29-
disabled={readonly}
30-
{...register('symbol')}
31-
error={errors.symbol !== undefined}
32-
/>
33-
</div>
34-
3526
<div className="flex flex-col">
3627
<label>Name</label>
3728
<Input
@@ -50,6 +41,15 @@ export function TokenInfomationForm({
5041
/>
5142
</div>
5243

44+
<div className="flex flex-col">
45+
<label>Amount</label>
46+
<Input
47+
disabled={readonly}
48+
{...register('amount')}
49+
error={errors.amount !== undefined}
50+
/>
51+
</div>
52+
5353
<div className="flex flex-col">
5454
<label>Decimal</label>
5555
<Input

app/components/token/TokenItem.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ export const TokenItem: FC<TokenItemProps> = ({
1919
} w-full flex items-center bg-lighter-color p-3 rounded-lg text-sm`}
2020
>
2121
<span>{token.symbol}</span>
22-
<Link className="ml-1" href={`/token/${token.symbol}`}>
22+
<Link className="ml-1" href={`/token/${token.typeId}`}>
2323
<img src="/icons/open.svg" alt="open" />
2424
</Link>
2525
<div className="ml-auto flex gap-1 text-primary-color">
2626
<a className="cursor-pointer">Distribution</a>
27-
<Link href={`/token/${token.symbol}`}>View</Link>
28-
<Link href={`/token/${token.symbol}/modify`}>Modify</Link>
29-
<Link href={`/token/${token.symbol}/mint`}>Mint</Link>
27+
<Link href={`/token/${token.typeId}`}>View</Link>
28+
<Link href={`/token/${token.typeId}/modify`}>Modify</Link>
29+
<Link href={`/token/${token.typeId}/mint`}>Mint</Link>
3030
</div>
3131
</div>
3232
)

0 commit comments

Comments
 (0)