Skip to content

Commit cf8ebeb

Browse files
committed
udpate swap demo, change api to fractal-testnet.
1 parent 52cbcb8 commit cf8ebeb

File tree

11 files changed

+109
-107
lines changed

11 files changed

+109
-107
lines changed

brc20-swap-demo/src/App.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ function App() {
1515
<li>Input your Api Key and refresh page</li>
1616
<li>Connect UniSat wallet.</li>
1717
<li>Click 'Deposit' tab, click 'Mint sats' to mint some sats.(<a
18-
href="https://coinfaucet.eu/btc-testnet/">get tBTC</a>)
18+
href="https://fractal-testnet.unisat.io/explorer/faucet">get tFB</a>)
1919
</li>
2020
<li>Choose 'sats' and click 'inscribe transfer' to create transfer-inscription</li>
2121
<li>Choose one transfer-inscription to deposit(it may need some confirmations).</li>

brc20-swap-demo/src/components/ConfirmSwap.tsx

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ export function ConfirmSwap({
4040
slippage,
4141
exactType: ExactType.exactIn,
4242
ts,
43+
feeTick:'test_sats'
4344
}
4445
swapApi.preSwap(params).then(res => {
4546
setSwapReqParams(params);
@@ -58,14 +59,24 @@ export function ConfirmSwap({
5859
if (!preSwap || !swapReqParams) return;
5960
setIsLoading(true);
6061
try {
61-
const {signMsg} = preSwap;
62+
const {signMsgs} = preSwap;
6263

6364
//sign message
64-
const sig = await signMessage(signMsg)
65+
let sigs = [];
66+
for (let i = 0; i < preSwap.signMsgs.length; i += 1) {
67+
const signMsg = preSwap.signMsgs[i];
68+
69+
const sig = await signMessage(signMsg);
70+
71+
sigs.push(sig);
72+
}
73+
6574

6675
const params = {
6776
...swapReqParams,
68-
sig,
77+
sigs,
78+
feeAmount: preSwap.feeAmount,
79+
feeTickPrice: preSwap.feeTickPrice,
6980
}
7081

7182
await swapApi.swap(params);
@@ -109,30 +120,11 @@ export function ConfirmSwap({
109120
<Col span={10}>
110121
<Statistic
111122
title="Rollup Fee"
112-
value={preSwap.serviceFeeL2}
123+
value={preSwap.feeAmount}
113124
precision={6}
114125
suffix={'sats'}
115126
/>
116127
</Col>
117-
<Col span={1}>
118-
=
119-
</Col>
120-
<Col span={5}>
121-
<Statistic
122-
title="Tx Size"
123-
value={preSwap.bytesL2}
124-
/>
125-
</Col>
126-
<Col span={1}>
127-
*
128-
</Col>
129-
<Col span={7}>
130-
<Statistic
131-
title="Gas Price"
132-
value={preSwap.gasPrice}
133-
precision={6}
134-
/>
135-
</Col>
136128

137129

138130
</>
Lines changed: 67 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,71 @@
1-
import {Button, message} from "antd";
2-
import {AddLiqReq, DeployPoolReq, swapApi} from "../utils/swapApi";
3-
import {useUnisat} from "../provider/UniSatProvider";
4-
import {handleError} from "../utils/utils";
1+
// import {Button, message} from "antd";
2+
// import {AddLiqReq, DeployPoolReq, swapApi} from "../utils/swapApi";
3+
// import {useUnisat} from "../provider/UniSatProvider";
4+
// import {handleError} from "../utils/utils";
5+
//
6+
// export function CreatePools() {
7+
//
8+
// const {address, signMessage} = useUnisat();
9+
//
10+
// async function create() {
11+
// try {
12+
//
13+
// const ts = Math.floor(Date.now() / 1000);
14+
// const params: DeployPoolReq = {
15+
// address,
16+
// tick0: "sats",
17+
// tick1: "ordi",
18+
// ts,
19+
// }
20+
//
21+
// const {signMsg} = await swapApi.preDeployPool(params)
22+
//
23+
// params.sig = await signMessage(signMsg)
24+
//
25+
// await swapApi.deployPool(params)
26+
//
27+
// message.success("Create pool success")
28+
// } catch (e) {
29+
// handleError(e)
30+
// }
31+
// }
32+
//
33+
// async function addLiq() {
34+
// try {
35+
// const ts = Math.floor(Date.now() / 1000);
36+
// const params: AddLiqReq = {
37+
// address,
38+
// tick0: "sats",
39+
// tick1: "ordi",
40+
// amount0: "10",
41+
// amount1: "100",
42+
// slippage: "0.005",
43+
// lp: "31.622776601683793",
44+
// ts,
45+
// }
46+
//
47+
// const {signMsg} = await swapApi.preAddLiquidity(params)
48+
//
49+
// params.sig = await signMessage(signMsg)
50+
//
51+
// await swapApi.addLiquidity(params)
52+
//
53+
// message.success("Add liquidity success")
54+
// } catch (e) {
55+
// handleError(e)
56+
// }
57+
// }
58+
//
59+
// return <>
60+
// <Button onClick={create}>
61+
// Create Pools (sats/ordi)
62+
// </Button>
63+
// <Button onClick={addLiq}>
64+
// Add Liquidity (sats/ordi)
65+
// </Button>
66+
// </>
67+
// }
568

669
export function CreatePools() {
770

8-
const {address, signMessage} = useUnisat();
9-
10-
async function create() {
11-
try {
12-
13-
const ts = Math.floor(Date.now() / 1000);
14-
const params: DeployPoolReq = {
15-
address,
16-
tick0: "sats",
17-
tick1: "ordi",
18-
ts,
19-
}
20-
21-
const {signMsg} = await swapApi.preDeployPool(params)
22-
23-
params.sig = await signMessage(signMsg)
24-
25-
await swapApi.deployPool(params)
26-
27-
message.success("Create pool success")
28-
} catch (e) {
29-
handleError(e)
30-
}
31-
}
32-
33-
async function addLiq() {
34-
try {
35-
const ts = Math.floor(Date.now() / 1000);
36-
const params: AddLiqReq = {
37-
address,
38-
tick0: "sats",
39-
tick1: "ordi",
40-
amount0: "10",
41-
amount1: "100",
42-
slippage: "0.005",
43-
lp: "31.622776601683793",
44-
ts,
45-
}
46-
47-
const {signMsg} = await swapApi.preAddLiquidity(params)
48-
49-
params.sig = await signMessage(signMsg)
50-
51-
await swapApi.addLiquidity(params)
52-
53-
message.success("Add liquidity success")
54-
} catch (e) {
55-
handleError(e)
56-
}
57-
}
58-
59-
return <>
60-
<Button onClick={create}>
61-
Create Pools (sats/ordi)
62-
</Button>
63-
<Button onClick={addLiq}>
64-
Add Liquidity (sats/ordi)
65-
</Button>
66-
</>
6771
}

brc20-swap-demo/src/components/Deposit.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ export function Deposit() {
101101
return <>
102102
{
103103
<div style={{marginBottom: 16}}>
104-
<a href={'https://testnet.unisat.io/brc20/sats'} target={"_blank"} rel="noreferrer">Mint sats</a>
104+
<a href={'https://fractal-testnet.unisat.io/explorer/brc20/test_sats'} target={"_blank"} rel="noreferrer">Mint test_sats</a>
105105
</div>
106106
}
107107
{

brc20-swap-demo/src/components/Swap.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import {handleError} from "../utils/utils";
77
import {ReloadOutlined} from "@ant-design/icons";
88

99

10-
const tickIn = 'sats';
11-
const tickOut = 'ordi';
10+
const tickIn = 'test_sats';
11+
const tickOut = 'test_ordi';
1212
const slippage = '0.005'; // 0.5%
1313

1414

@@ -85,7 +85,7 @@ export function Swap() {
8585
<Statistic
8686
title={tickIn}
8787
value={swapBalanceMap[tickIn]?.balance.swap || '0'}
88-
precision={6}
88+
precision={0}
8989
/>
9090
{
9191
// wait for confirm balance
@@ -117,7 +117,7 @@ export function Swap() {
117117
<Col span={12}>
118118
<InputNumber
119119
style={{width: '100%'}}
120-
addonAfter={'sats'}
120+
addonAfter={tickIn}
121121
value={amountIn}
122122
stringMode={true}
123123
onChange={(e) => {
@@ -135,7 +135,7 @@ export function Swap() {
135135
value={amountOut}
136136
stringMode={true}
137137
disabled={true}
138-
addonAfter={'ordi'}
138+
addonAfter={tickOut}
139139
/>
140140
</Col>
141141
<Col>

brc20-swap-demo/src/provider/UniSatProvider.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,14 @@ export default function UnisatProvider({children}: {
5757
}
5858

5959
if (install) {
60+
await unisatUtils.switchChain('FRACTAL_BITCOIN_TESTNET')
61+
6062
const address = await unisatUtils.getAccounts()
6163
if (address) {
6264
// connected
6365
setIsConnected(true)
6466
setAddress(address)
67+
6568
}
6669
}
6770
}
@@ -72,7 +75,8 @@ export default function UnisatProvider({children}: {
7275

7376
const connect = useCallback(async () => {
7477
try {
75-
await unisatUtils.checkNetwork(NETWORK);
78+
await unisatUtils.switchChain('FRACTAL_BITCOIN_TESTNET')
79+
7680
const address = await unisatUtils.requestAccounts();
7781
if (address) {
7882
setIsConnected(true)

brc20-swap-demo/src/types.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export type UnisatWalletInterface = {
2424
ticker: string
2525
}>;
2626
getInscriptions(num: number): Promise<void>;
27+
switchChain(network: string): Promise<void>;
2728
}
2829

2930
declare global {

brc20-swap-demo/src/utils/brc20Api.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export const brc20Api = {
2121
start?: number,
2222
limit?: number
2323
}): Promise<Brc20AddressTransferable> {
24-
return get(`/v1/indexer/address/${address}/brc20/${stringToHex(tick)}/transferable-inscriptions`, {
24+
return get(`/v1/indexer/address/${address}/brc20/${(tick)}/transferable-inscriptions`, {
2525
limit,
2626
start,
2727
})

brc20-swap-demo/src/utils/httpUtils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export function setApiKey(key: string) {
88
}
99

1010
const api = axios.create({
11-
baseURL: isTestnet ? "https://open-api-testnet.unisat.io" : "https://open-api.unisat.io/",
11+
baseURL: isTestnet ? "https://open-api-fractal-testnet.unisat.io" : "https://open-api.unisat.io/",
1212
timeout: 10000,
1313
headers: {
1414
"Content-Type": "application/json",

brc20-swap-demo/src/utils/swapApi.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ export type SwapReq = {
6363
exactType: ExactType;
6464
ts: number;
6565
sig?: string;
66+
feeTick: string;
6667
};
6768
export type SwapRes = {
6869
id: string;
@@ -76,20 +77,17 @@ export type SwapRes = {
7677
}
7778
export type PreRes = {
7879
// signedMsg: string;
79-
signMsg: string;
80+
signMsgs: string[];
8081
} & FeeRes;
8182

8283
export type FeeRes = {
83-
bytesL1: number;
84-
bytesL2: number;
85-
feeRate: string; // l1
86-
gasPrice: string; // l2
87-
serviceFeeL1: string;
88-
serviceFeeL2: string;
89-
unitUsdPriceL1: string;
90-
unitUsdPriceL2: string;
91-
92-
serviceTickBalance: string;
84+
feeAmount: string;
85+
feeTick: string;
86+
feeTickPrice: string;
87+
feeBalance: string;
88+
89+
originalFeeAmount: string;
90+
freeQuota: string;
9391
};
9492

9593

0 commit comments

Comments
 (0)