Skip to content

Commit e0a2934

Browse files
Use transaction.toJSON() as argument to csprclick.send()
Use correct contract hash for testnet. Remove csprclick event listeners when App is unmounted.
1 parent 73ddcf1 commit e0a2934

4 files changed

Lines changed: 23 additions & 18 deletions

File tree

‎client/public/config.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const config = {
22
donation_contract_package_hash:
3-
"ca0f4eedc84e03b6bc39ce664ef05dff00a96214194e706d50bfc43d84124035",
4-
donation_api_url: "http://localhost:4000",
3+
"c447e9d334a710bc3e0a47cbea854c269e41637d7b9aa9d37a745596f651ed7a",
4+
donation_api_url: "https://donation-demo-api.casper.network",
55
cspr_click_app_name: "Donation Demo",
66
cspr_click_app_id: "csprclick-template",
77
cspr_click_providers: ['casper-wallet', 'ledger', 'metamask-snap'],

‎client/src/App.tsx‎

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,25 @@ const MainSection = styled.section(({ theme }) =>
1818
const App = () => {
1919
const clickRef = useClickRef();
2020
const [themeMode, setThemeMode] = useState<ThemeModeType>(ThemeModeType.light);
21-
const [activeAccount, setActiveAccount] = useState<AccountType | null>(null);
21+
const [connectedAccount, setConnectedAccount] = useState<AccountType | null>(null);
2222
const [refetchSignal, setRefetchSignal] = useState<number>(0);
2323

2424
useEffect(() => {
25-
clickRef?.on('csprclick:signed_in', (evt: any) => setActiveAccount(evt.account));
26-
clickRef?.on('csprclick:switched_account', (evt: any) => setActiveAccount(evt.account));
27-
clickRef?.on('csprclick:signed_out', () => setActiveAccount(null));
28-
clickRef?.on('csprclick:switched_account', () => setActiveAccount(null));
25+
if (!clickRef) return;
26+
27+
const handleSignedIn = (evt: any) => setConnectedAccount(evt.account);
28+
const handleSwitchedAccount = (evt: any) => setConnectedAccount(evt.account);
29+
const handleSignedOut = () => setConnectedAccount(null);
30+
31+
clickRef.on('csprclick:signed_in', handleSignedIn);
32+
clickRef.on('csprclick:switched_account', handleSwitchedAccount);
33+
clickRef.on('csprclick:signed_out', handleSignedOut);
34+
35+
return () => {
36+
clickRef.off('csprclick:signed_in', handleSignedIn);
37+
clickRef.off('csprclick:switched_account', handleSwitchedAccount);
38+
clickRef.off('csprclick:signed_out', handleSignedOut);
39+
};
2940
}, [clickRef?.on]);
3041

3142
return (
@@ -38,7 +49,7 @@ const App = () => {
3849
/>
3950
<Container>
4051
<HeroSection
41-
isConnected={!!activeAccount}
52+
isConnected={!!connectedAccount}
4253
onUpdateTipsList={() => setRefetchSignal(Date.now())}
4354
/>
4455
<MainSection>

‎client/src/components/hero-section/index.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export const HeroSection: React.FC<WelcomeProps> = ({ isConnected, onUpdateTipsL
6060

6161
setModalScreen('loading');
6262

63-
clickRef?.send(tipTransaction, sender, onStatusUpdate).catch((err: any) => {
63+
clickRef?.send(tipTransaction.toJSON() as object, sender, onStatusUpdate).catch((err: any) => {
6464
setModalScreen('error');
6565
alert('Error: ' + err);
6666
});

‎client/src/utils/tip-transaction.ts‎

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ import {
55
Hash,
66
PublicKey,
77
SessionBuilder,
8-
TransactionWrapper
8+
Transaction,
99
} from 'casper-js-sdk';
1010
import { getProxyWasm } from '@/api';
1111

12-
export const buildTipTransaction = async (sender: string, amount: string, message: string) => {
12+
export const buildTipTransaction = async (sender: string, amount: string, message: string): Promise<Transaction> => {
1313
const contractWasm = await getProxyWasm();
1414

1515
const tipArgs = Args.fromMap({
@@ -33,17 +33,11 @@ export const buildTipTransaction = async (sender: string, amount: string, messag
3333
args: serializedArgs
3434
});
3535

36-
const sessionTransaction = new SessionBuilder()
36+
return new SessionBuilder()
3737
.from(PublicKey.fromHex(sender))
3838
.runtimeArgs(args)
3939
.wasm(contractWasm)
4040
.payment(Number.parseInt(config.transaction_payment, 10))
4141
.chainName(window.csprclick?.chainName!)
4242
.build();
43-
44-
return {
45-
transaction: {
46-
Version1: sessionTransaction.toJSON()
47-
}
48-
};
4943
};

0 commit comments

Comments
 (0)