Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

contract events for ui updates to pledge display #105

Merged
merged 27 commits into from
Jun 1, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
90aac66
#458: added contract events driven ui updates to pledge amount display
sreuland Apr 28, 2023
fcbf6f1
Merge remote-tracking branch 'upstream/main' into 458_events
sreuland Apr 28, 2023
693665f
#458: updated soroban-react and soroban-client versions, fixed event …
sreuland Apr 28, 2023
1d170b3
#458: fixed lint error on typescript interface
sreuland Apr 28, 2023
9f05cd6
#458: changed crowdfund event method naming, review feedback
sreuland May 2, 2023
b8d4b2f
#458: prepare for preview 9 versions
sreuland May 5, 2023
a9a58ed
Merge remote-tracking branch 'upstream/main' into 458_events
sreuland May 5, 2023
2c31d9a
updating component versions to preview 9 as they become available
sreuland May 19, 2023
08e01b0
Merge remote-tracking branch 'origin/main' into 458_events
May 22, 2023
bf5c1b1
Merge remote-tracking branch 'upstream/main' into 458_events
sreuland May 22, 2023
fec9c82
update contract to be compliant to 0.8.2 soroban sdk
sreuland May 22, 2023
37cdd4e
Merge remote-tracking branch 'origin/458_events' into 458_events
sreuland May 22, 2023
720136a
remove cargo fmt errors
sreuland May 22, 2023
6ad8514
resolved clippy warnings
sreuland May 22, 2023
cf7097d
use release versions of soroban client and react
sreuland May 24, 2023
26a7297
updated soroban client and react libs
sreuland May 25, 2023
0cb4429
update soroban client and react versions, and refs to any older versions
sreuland May 26, 2023
7338d0c
fixed some xdr schema problems
sreuland May 26, 2023
a51624a
udpated to soroban-react 4.5.2, soroban-client 0.7.2
sreuland May 26, 2023
3aac031
used Address js workaround during shell initialize script for cli param
sreuland May 27, 2023
68c6264
added comment on workarounds script for address conversion
sreuland May 30, 2023
6f646f5
#458: buffer polyfill fix and update to latest soroban-react packages
sreuland May 31, 2023
f483e05
#458: fixed filename collision, dont use .soroban, it's used by cli a…
sreuland May 31, 2023
88b4ae2
Update node to v18
Jun 1, 2023
aacd8f7
We need to actually build soroban-preview:9 image
Jun 1, 2023
93c84e1
#458: review feedback for preview9
sreuland Jun 1, 2023
f6e653c
#458: fixed number convert for i128
sreuland Jun 1, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,6 @@ yarn-error.log*
# Rust contract build
/target
/target-tiny

*.sublime-project
*.sublime-workspace
62 changes: 35 additions & 27 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 4 additions & 22 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,11 @@ panic = "abort"
codegen-units = 1
lto = true

[workspace.dependencies.soroban-env-common]
version = "0.0.15"

[workspace.dependencies.soroban-env-guest]
version = "0.0.15"

[workspace.dependencies.soroban-env-macros]
version = "0.0.15"

[workspace.dependencies.soroban-env-host]
version = "0.0.15"

[workspace.dependencies.soroban-native-sdk-macros]
version = "0.0.15"

[workspace.dependencies.soroban-spec]
version = "0.7.0"

[workspace.dependencies.soroban-sdk]
version = "0.7.0"
git = "https://github.com/stellar/rs-soroban-sdk"
rev = "29b505cf95f2fd4fbe639fdb163b606c44920837"



[workspace.dependencies.soroban-sdk-macros]
version = "0.7.0"

[workspace.dependencies.stellar-xdr]
version = "0.0.15"
11 changes: 6 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
all: check build test

export RUSTFLAGS=-Dwarnings -Dclippy::all -Dclippy::pedantic
CARGO_BUILD_TARGET?=wasm32-unknown-unknown

test: fmt
cargo test

build: fmt
cargo build --target wasm32-unknown-unknown --release
cd target/wasm32-unknown-unknown/release/ && \
cargo build --target $(CARGO_BUILD_TARGET) --no-default-features --release
cd target/$(CARGO_BUILD_TARGET)/release/ && \
for i in *.wasm ; do \
ls -l "$$i"; \
done

build-optimized: fmt
CARGO_TARGET_DIR=target-tiny cargo +nightly build --target wasm32-unknown-unknown --release \
CARGO_TARGET_DIR=target-tiny cargo +nightly build --target $(CARGO_BUILD_TARGET) --release \
-Z build-std=std,panic_abort \
-Z build-std-features=panic_immediate_abort
cd target-tiny/wasm32-unknown-unknown/release/ && \
cd target-tiny/$(CARGO_BUILD_TARGET)/release/ && \
for i in *.wasm ; do \
wasm-opt -Oz "$$i" -o "$$i.tmp" && mv "$$i.tmp" "$$i"; \
ls -l "$$i"; \
Expand All @@ -27,7 +28,7 @@ build-docker:

check: fmt
cargo clippy --all-targets
cargo clippy --release --target wasm32-unknown-unknown
cargo clippy --release --target $(CARGO_BUILD_TARGET)

watch:
cargo watch --clear --watch-when-idle --shell '$(MAKE)'
Expand Down
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,5 @@ the user's freighter wallet, as well as some React hooks to talk to a
soroban-rpc server (e.g. `soroban-cli serve`), to fetch data and send
transactions.

Data from contracts is fetched using the `useContractValue` hook in
`./wallet/hooks/useContractValue.tsx`. Transactions are submitted to the network
using the `useSendTransactions` hook in `./wallet/hooks/useSendTransaction.tsx`.
Data from contracts is fetched using the `useContractValue` hook from @soroban-react/contracts. Transactions are submitted to the network
using the `useSendTransaction` from @soroban-react/contracts.
8 changes: 5 additions & 3 deletions components/ProviderExample.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
import React from 'react'
import {SorobanReactProvider} from '@soroban-react/core';
import {SorobanEventsProvider} from '@soroban-react/events';
import {futurenet, sandbox, standalone} from '@soroban-react/chains';
import {freighter} from '@soroban-react/freighter';
import {ChainMetadata, Connector} from "@soroban-react/types";

const chains: ChainMetadata[] = [sandbox, standalone, futurenet];
const connectors: Connector[] = [freighter()]


const connectors: Connector[] = [freighter()];

export default function ProviderExample({children}:{children: React.ReactNode}) {
return (
<SorobanReactProvider
chains={chains}
appName={"Example Stellar App"}
connectors={connectors}>
<SorobanEventsProvider>
{children}
</SorobanEventsProvider>
</SorobanReactProvider>
)
}
2 changes: 1 addition & 1 deletion components/molecules/deposits/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export interface IDepositsProps {
export function Deposits(props: IDepositsProps) {
const useLoadDeposits = (): ContractValueType => {
return useContractValue({
contractId: Constants.CrowdfundId,
contractId: props.idCrowdfund,
method: 'balance',
params: [new SorobanClient.Address(props.address).toScVal()],
sorobanContext: useSorobanReact()
Expand Down
53 changes: 49 additions & 4 deletions components/organisms/pledge/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { FunctionComponent } from 'react'
import React, { FunctionComponent, useState, useRef } from 'react'
import { Card, ConnectButton, Loading, ProgressBar } from '../../atoms'
import styles from './style.module.css'
import { Spacer } from '../../atoms/spacer'
Expand All @@ -7,17 +7,20 @@ import {
useAccount,
useNetwork,
} from '../../../wallet'
import BigNumber from 'bignumber.js'
import { useContractValue } from '@soroban-react/contracts'
import * as SorobanClient from 'soroban-client'
import { Deposits, FormPledge } from '../../molecules'
import * as convert from '../../../convert'
import { Constants } from '../../../shared/constants'
import { useSorobanReact } from '@soroban-react/core'
import { useSorobanEvents, EventSubscription } from '@soroban-react/events'
let xdr = SorobanClient.xdr

const Pledge: FunctionComponent = () => {
const { data: account } = useAccount()
const { activeChain } = useNetwork()
const sorobanEventsContext = useSorobanEvents()

const networkPassphrase = activeChain?.networkPassphrase ?? ''

Expand Down Expand Up @@ -64,9 +67,8 @@ const Pledge: FunctionComponent = () => {
method: 'target',
sorobanContext
})

// Convert the result ScVals to js types
const tokenBalance = convert.scvalToBigNumber(token.balance.result)
const tokenDecimals =
token.decimals.result && (token.decimals.result?.u32() ?? 7)
const tokenName =
Expand All @@ -82,7 +84,20 @@ const Pledge: FunctionComponent = () => {
) * 1000
)
const targetAmount = convert.scvalToBigNumber(targetAmountXdr.result)


// hook or event go through same state to update ui
const [tokenBalance, setTokenBalance] = useState<BigNumber>(BigNumber(0))
const [targetReached, setTargetReached] = useState<boolean>(false)

React.useEffect(() => {
// hook is providing data, let it update the ui model,
// events will update these later also
const tokenBalance = convert.scvalToBigNumber(token.balance.result)
const targetAmount = convert.scvalToBigNumber(targetAmountXdr.result)
setTokenBalance(tokenBalance)
setTargetReached(tokenBalance.gte(targetAmount))
}, [token.balance, targetAmountXdr]);

const isLoading = (): boolean | undefined => {
return (
token.balance.loading ||
Expand All @@ -93,12 +108,42 @@ const Pledge: FunctionComponent = () => {
)
}

const crowdfundPledgedEventSubscription: EventSubscription = useRef({
contractId: Constants.CrowdfundId,
topics: ['pledged_amount_changed'],
cb: (event: SorobanClient.SorobanRpc.EventResponse): void => {
let eventTokenBalance = xdr.ScVal.fromXDR(Buffer.from(event.value.xdr, 'base64'))
setTokenBalance(convert.scvalToBigNumber(eventTokenBalance))
},
id: Math.random()});

const crowdfundTargetReachedSubscription: EventSubscription = useRef({
contractId: Constants.CrowdfundId,
topics: ['target_reached'],
cb: (event: SorobanClient.SorobanRpc.EventResponse): void => {
setTargetReached(true)
},
id: Math.random()});

React.useEffect(() => {
const pledgedSubId = sorobanEventsContext.subscribe(crowdfundPledgedEventSubscription)
const reachedSubId = sorobanEventsContext.subscribe(crowdfundTargetReachedSubscription)

return () => {
sorobanEventsContext.unsubscribe(pledgedSubId);
sorobanEventsContext.unsubscribe(reachedSubId);
}
}, [sorobanEventsContext]);

return (
<Card>
{isLoading() ? (
<Loading size={64} />
) : (
<>
{targetReached && (
<h6>SUCCESSFUL CAMPAIGN !!</h6>
)}
<h6>PLEDGED</h6>
<div className={styles.pledgeAmount}>
{Utils.formatAmount(tokenBalance, tokenDecimals)} {tokenSymbol}
Expand Down
Loading