stellar / soroban-example-dapp

End-to-End Example Soroban Dapp
Apache License 2.0
1.03k stars 835 forks source link

contract events for ui updates to pledge display #105

Closed sreuland closed 1 year ago

sreuland commented 1 year ago

Added contract events to the dapp examples. Demonstrating how to wire up events from a contract to the dapp, and some aspects of async, event driven application state changes.

Most of the aspects of subscribing to get events has been implemented as a reusable react provider in soroban-react/pr-26 and used here in the pledge components.

The pr changes the Pledged Amount at top of screen to be event driven, rest of app display state remains user request based. A short video demo's the effect, it shows two browser panels, both have dapp loaded and using the same rpc server and freighter wallet. Left side browser has no user interaction, but its Pledged Amount state will change as it receives contract events from the rpc server, while on the right side browser, the user initiates pledges(contract invocations).

https://user-images.githubusercontent.com/10703845/235037075-8c0e8edc-8d26-4c74-9499-873600124c02.mp4

Closes soroban-tools:458 Closes https://github.com/stellar/soroban-example-dapp/issues/107

This is based on Soroban Preview 9

sreuland commented 1 year ago

after updating to all preview9 versions available, it seems close, am trying to run locally, seeing a problem with initialize.sh it's having issue invoking 'initialize` contract fn, running cli 0.8.0:

$ ./initialize.sh standalone
Using standalone network
Add the standalone network to cli client
Fund token-admin account from friendbot
Wrap the Stellar asset
Token wrapped succesfully with TOKEN_ID: 6b5be340632ae1b59e6730b610e4bfb3aa9bacb9c649feb926ac8959af3f714f
Build the crowdfund contract
cargo fmt --all
cargo build --target wasm32-unknown-unknown --no-default-features --release
    Finished release [optimized] target(s) in 0.21s
cd target/wasm32-unknown-unknown/release/ && \
        for i in *.wasm ; do \
            ls -l "$i"; \
        done
-rwxr-xr-x  1 5974 May 25 21:48 soroban_crowdfund_contract.wasm
Deploy the crowdfund contract
Contract deployed succesfully with ID: 9645a9aac1526834a3622b4556f36444054af68d55b9e16d3e8ffaaa5b4f19d6
Initialize the crowdfund contract
error: parsing argument token: value is not parseable to Some(
    Address,
)
Done

the token param on that fn call needs to be Address, it looks like cli just accept strkey format, not sure what to use from within script to convert the binary hex of token 6b5be340632ae1b59e6730b610e4bfb3aa9bacb9c649feb926ac8959af3f714f into a strkey C...

Shaptic commented 1 year ago

@sreuland you can use the SDK, e.g. SorobanClient.Address.contract(Buffer.from('6b5be340...', 'hex')) (something like that)

Shaptic commented 1 year ago

or for something more raw, you can use SorobanClient.StrKey.encodeContract(Buffer.from(...

Shaptic commented 1 year ago

oh, I think this thread is relevant: https://discord.com/channels/897514728459468821/1111673776317399103/1111673776317399103

sreuland commented 1 year ago

@Shaptic , thanks for insight on the StrKey usage, I added a workaround based on that.

sreuland commented 1 year ago

@Shaptic, @paulbellamy

I'm debugging an issue here with latest versions, getting TypeError: this._buffer.readBigInt64BE is not a function when trying to use fromXDR() from SorobanClient.xdr on simulation responses, able to replicate directly as:

let test = SorobanClient.xdr.ScVal.fromXDR(Buffer.from("AAAACgAAAAAAAAAAAAAAADuaygA=", 'base64'));

this works in node but from browser(using dist) get the error. seems like something is not getting included up the dependency tree by webpack, as it's gen'd xdr code packaged from dts-xdr->xdr-gen->stellar-base->soroban-client. any insights on this appreciated.

sreuland commented 1 year ago

runtime looks stable, need to test pledges to confirm that area works before merging, will do that once freighter 5.0.2 comes out as pledges go through that as payments.

Julian-dev28 commented 1 year ago

Dockerfile still has RUN cargo install --locked --version 0.7.1 soroban-cli

Does this need to be updated RUN cargo install --locked --version 0.8.0 soroban-cli?

sreuland commented 1 year ago

Dockerfile still has RUN cargo install --locked --version 0.7.1 soroban-cli

Does this need to be updated RUN cargo install --locked --version 0.8.0 soroban-cli?

@Julian-dev28 , I believe that ref for cli has been updated in pr change set - https://github.com/stellar/soroban-example-dapp/pull/105/files#diff-dd2c0eb6ea5cfc6c4bd4eac30934e2d5746747af48fef6da689e85b752f39557R15