Skip to content

Commit e76064b

Browse files
fix: tipping (#1767)
- show modal connect to near if click tipping when wallet is near - sign in to near website - update near api js package - add new translate
1 parent 365bafe commit e76064b

6 files changed

Lines changed: 115 additions & 18 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
"lodash": "^4.17.21",
7171
"millify": "^4.0.0",
7272
"mux.js": "^6.0.1",
73-
"near-api-js": "^0.44.2",
73+
"near-api-js": "^1.1.0",
7474
"next": "latest",
7575
"next-auth": "4.10.3",
7676
"next-http-proxy-middleware": "^1.2.4",

src/components/common/SendTipButton/SendTipButton.tsx

Lines changed: 82 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import React, { useState } from 'react';
44
import { useCookies } from 'react-cookie';
55
import { shallowEqual, useSelector } from 'react-redux';
66

7+
import getConfig from 'next/config';
78
import { useRouter } from 'next/router';
89

910
import {
@@ -20,19 +21,25 @@ import ShowIf from '../show-if.component';
2021
import { useStyles } from './SendTipButton.style';
2122

2223
import { COOKIE_INSTANCE_URL } from 'components/SelectServer';
24+
import * as nearAPI from 'near-api-js';
2325
import { PromptComponent } from 'src/components/atoms/Prompt/prompt.component';
2426
import { useUserHook } from 'src/hooks/use-user.hook';
2527
import { Comment } from 'src/interfaces/comment';
2628
import { ReferenceType } from 'src/interfaces/interaction';
29+
import { NetworkIdEnum } from 'src/interfaces/network';
2730
import { People } from 'src/interfaces/people';
2831
import { Post } from 'src/interfaces/post';
29-
import { User } from 'src/interfaces/user';
32+
import { User, Wallet } from 'src/interfaces/user';
3033
import { WalletDetail } from 'src/interfaces/wallet';
3134
import * as CommentAPI from 'src/lib/api/comment';
3235
import * as PostAPI from 'src/lib/api/post';
3336
import * as UserAPI from 'src/lib/api/user';
37+
import { Near } from 'src/lib/services/near-api-js';
3438
import i18n from 'src/locale';
3539
import { RootState } from 'src/reducers';
40+
import { UserState } from 'src/reducers/user/reducer';
41+
42+
const { publicRuntimeConfig } = getConfig();
3643

3744
type SendTipButtonProps = ButtonProps & {
3845
label?: string;
@@ -66,16 +73,23 @@ export const SendTipButton: React.FC<SendTipButtonProps> = props => {
6673
const [promptFailedTip, setPromptFailedTip] = useState(false);
6774
const [tipInfoOpened, setTipInfoOpened] = useState(false);
6875
const [prompWeb2Users, setPrompWeb2Users] = useState(false);
76+
const [promptNearConnection, setPromptNearConnection] = useState(false);
6977

7078
const { wallets } = user || { wallets: [] };
79+
console.log('wallet', wallets);
7180

72-
const anonymous = useSelector<RootState, boolean>(
73-
state => state.userState.anonymous,
81+
const { anonymous, networks } = useSelector<RootState, UserState>(
82+
state => state.userState,
7483
shallowEqual,
7584
);
7685

7786
const isWeb2Users = !wallets.length && !anonymous;
7887

88+
const isNear =
89+
wallets.length && (wallets[0] as Wallet).blockchainPlatform === 'near';
90+
91+
console.log('is near', isNear);
92+
7993
const icon = (
8094
<SvgIcon
8195
color="inherit"
@@ -92,6 +106,10 @@ export const SendTipButton: React.FC<SendTipButtonProps> = props => {
92106
setPrompWeb2Users(false);
93107
};
94108

109+
const handleCloseNearWallet = () => {
110+
setPromptNearConnection(false);
111+
};
112+
95113
const handleConnectWeb3Wallet = () => {
96114
router.push(`/wallet?type=manage`);
97115
};
@@ -118,6 +136,20 @@ export const SendTipButton: React.FC<SendTipButtonProps> = props => {
118136
return;
119137
}
120138

139+
if (isNear) {
140+
const network = networks.find(
141+
network => network.id === NetworkIdEnum.NEAR,
142+
);
143+
144+
if (!network) return;
145+
const near = await Near.connect(network);
146+
const wallet = near.provider.wallet;
147+
if (!wallet.isSignedIn()) {
148+
setPromptNearConnection(true);
149+
return;
150+
}
151+
}
152+
121153
try {
122154
// if tipping to User
123155
if ('username' in reference) {
@@ -180,9 +212,53 @@ export const SendTipButton: React.FC<SendTipButtonProps> = props => {
180212
</Button>
181213

182214
<PromptComponent
183-
icon="tip"
184-
title={i18n.t('Confirm.Anonymous.SendTip.Title')}
185-
subtitle={i18n.t('Confirm.Anonymous.SendTip.Desc')}
215+
icon="warning"
216+
title={i18n.t('Tipping.Prompt_Near.Title')}
217+
open={promptNearConnection}
218+
subtitle={''}
219+
onCancel={handleCloseNearWallet}>
220+
<div className={styles.wrapperButtonFlex}>
221+
<Button
222+
variant="outlined"
223+
color="secondary"
224+
onClick={handleCloseNearWallet}>
225+
{i18n.t('Tipping.Prompt_Near.Btn.Left')}
226+
</Button>
227+
<Button
228+
variant="contained"
229+
color="primary"
230+
onClick={async () => {
231+
const network = networks.find(
232+
network => network.id === NetworkIdEnum.NEAR,
233+
);
234+
235+
if (!network) return;
236+
const near = await Near.connect(network);
237+
const wallet = near.provider.wallet;
238+
239+
const keyStore =
240+
new nearAPI.keyStores.BrowserLocalStorageKeyStore();
241+
const signInOptions = {
242+
contractId: publicRuntimeConfig.nearTippingContractId,
243+
methodNames: ['claim_tip', 'batch_claim_tips'],
244+
successUrl: publicRuntimeConfig.appAuthURL + router.asPath,
245+
failureUrl: publicRuntimeConfig.appAuthURL + router.asPath,
246+
};
247+
248+
await Promise.all([
249+
keyStore.clear(),
250+
wallet.requestSignIn(signInOptions),
251+
]);
252+
}}>
253+
{i18n.t('Tipping.Prompt_Near.Btn.Right')}
254+
</Button>
255+
</div>
256+
</PromptComponent>
257+
258+
<PromptComponent
259+
icon="warning"
260+
title={i18n.t('Tipping.Prompt_Mobile.Title')}
261+
subtitle={i18n.t('Tipping.Prompt_Mobile.Subtitle')}
186262
open={tipInfoOpened}
187263
onCancel={handleCloseTipInfo}>
188264
<div className={styles.wrapperButtonFlex}>

src/locale/en.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -749,6 +749,13 @@
749749
"Title": "Wallet Not Connected",
750750
"Subtitle": "You don’t have any wallet connected to your account, let’s try to connect your wallet to send tip"
751751
},
752+
"Prompt_Near": {
753+
"Title": "Allow connection to NEAR network?",
754+
"Btn": {
755+
"Left": "Cancel",
756+
"Right": "Allow"
757+
}
758+
},
752759
"Prompt_Success": {
753760
"Title": "Tip sent!",
754761
"Subtitle_1": "Tip to ",

src/locale/id.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -745,6 +745,13 @@
745745
"Title": "Wallet Not Connected",
746746
"Subtitle": "You don’t have any wallet connected to your account, let’s try to connect your wallet to send tip"
747747
},
748+
"Prompt_Near": {
749+
"Title": "Allow connection to NEAR network?",
750+
"Btn": {
751+
"Left": "Cancel",
752+
"Right": "Allow"
753+
}
754+
},
748755
"Prompt_Success": {
749756
"Title": "Tip terkirim!",
750757
"Subtitle_1": "Tip ke ",

src/locale/ru.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,13 @@
737737
"Title": "Wallet Not Connected",
738738
"Subtitle": "You don’t have any wallet connected to your account, let’s try to connect your wallet to send tip"
739739
},
740+
"Prompt_Near": {
741+
"Title": "Allow connection to NEAR network?",
742+
"Btn": {
743+
"Left": "Cancel",
744+
"Right": "Allow"
745+
}
746+
},
740747
"Prompt_Success": {
741748
"Title": "Отправлено!",
742749
"Subtitle_1": "Вознаградить ",

yarn.lock

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7766,7 +7766,7 @@ bluebird@^3.3.5, bluebird@^3.5.5:
77667766
resolved "https://registry.yarnpkg.com/bluebird/-/bluebird-3.7.2.tgz#9f229c15be272454ffa973ace0dbee79a1b0c36f"
77677767
integrity sha512-XpNj6GDQzdfW+r2Wnn7xiSAd7TM3jzkxGXBGTtWKuSXv1xUV+azxAm8jdWZN06QTQk+2N2XB9jRDkvbmQmcRtg==
77687768

7769-
bn.js@5.2.0, bn.js@^4.0.0, bn.js@^4.1.0, bn.js@^4.11.9, bn.js@^5.0.0, bn.js@^5.1.1, bn.js@^5.2.0, bn.js@^5.2.1:
7769+
bn.js@5.2.1, bn.js@^4.0.0, bn.js@^4.1.0, bn.js@^4.11.9, bn.js@^5.0.0, bn.js@^5.1.1, bn.js@^5.2.0, bn.js@^5.2.1:
77707770
version "5.2.1"
77717771
resolved "https://registry.yarnpkg.com/bn.js/-/bn.js-5.2.1.tgz#0bc527a6a0d18d0aa8d5b0538ce4a77dccfa7b70"
77727772
integrity sha512-eXRvHzWyYPBuB4NBy0cmYQjGitUrtqwbvlzP3G6VFnNRbsZQIxQ10PbKKHt8gZ/HW/D/747aDl+QkDqg3KQLMQ==
@@ -7792,10 +7792,10 @@ boolbase@^1.0.0:
77927792
resolved "https://registry.yarnpkg.com/boolbase/-/boolbase-1.0.0.tgz#68dff5fbe60c51eb37725ea9e3ed310dcc1e776e"
77937793
integrity sha1-aN/1++YMUes3cl6p4+0xDcwed24=
77947794

7795-
borsh@^0.6.0:
7796-
version "0.6.0"
7797-
resolved "https://registry.yarnpkg.com/borsh/-/borsh-0.6.0.tgz#a7c9eeca6a31ca9e0607cb49f329cb659eb791e1"
7798-
integrity sha512-sl5k89ViqsThXQpYa9XDtz1sBl3l1lI313cFUY1HKr+wvMILnb+58xpkqTNrYbelh99dY7K8usxoCusQmqix9Q==
7795+
borsh@^0.7.0:
7796+
version "0.7.0"
7797+
resolved "https://registry.yarnpkg.com/borsh/-/borsh-0.7.0.tgz#6e9560d719d86d90dc589bca60ffc8a6c51fec2a"
7798+
integrity sha512-CLCsZGIBCFnPtkNnieW/a8wmreDmfUtjU2m9yHrzPXIlNbqVs0AQrSatSG6vdNYUqdc83tkQi2eHfF98ubzQLA==
77997799
dependencies:
78007800
bn.js "^5.2.0"
78017801
bs58 "^4.0.0"
@@ -14519,13 +14519,13 @@ natural-compare@^1.4.0:
1451914519
resolved "https://registry.yarnpkg.com/natural-compare/-/natural-compare-1.4.0.tgz#4abebfeed7541f2c27acfb29bdbbd15c8d5ba4f7"
1452014520
integrity sha1-Sr6/7tdUHywnrPspvbvRXI1bpPc=
1452114521

14522-
near-api-js@^0.44.2:
14523-
version "0.44.2"
14524-
resolved "https://registry.yarnpkg.com/near-api-js/-/near-api-js-0.44.2.tgz#e451f68f2c56bd885c7b918db5818a3e6e9423d0"
14525-
integrity sha512-eMnc4V+geggapEUa3nU2p8HSHn/njtloI4P2mceHQWO8vDE1NGpnAw8FuTBrLmXSgIv9m6oocgFc9t3VNf5zwg==
14522+
near-api-js@^1.1.0:
14523+
version "1.1.0"
14524+
resolved "https://registry.yarnpkg.com/near-api-js/-/near-api-js-1.1.0.tgz#907e807f052c1f043c6fbf28f61872de3c02235a"
14525+
integrity sha512-qYKv1mYsaDZc2uYndhS+ttDhR9+60qFc+ZjD6lWsAxr3ZskMjRwPffDGQZYhC7BRDQMe1HEbk6d5mf+TVm0Lqg==
1452614526
dependencies:
14527-
bn.js "5.2.0"
14528-
borsh "^0.6.0"
14527+
bn.js "5.2.1"
14528+
borsh "^0.7.0"
1452914529
bs58 "^4.0.0"
1453014530
depd "^2.0.0"
1453114531
error-polyfill "^0.1.3"

0 commit comments

Comments
 (0)