Skip to content
CCPEDIAby Unity Nodes
App Development

usePublicParty and usePublicToken with DamlLedger

3 posts308 views3 likesLast activity May 2022

  1. Joao_FreitasOP

    Hi everyone,
    I am trying to create a PublicLedger component for one project of mine.

    I am using the following code:

    export const PublicLedger = ({ children }: { children: React.ReactNode }) => {
        //FETCH public party and token
        const publicParty = usePublicParty()
        let pubParty: string
        let pubToken: string
        console.log('publicParty', publicParty)
        const publicToken = usePublicToken()?.token
        console.log('publicToken', publicToken)
    
        if (publicParty !== undefined) {
            pubParty = publicParty
        }
    
        if (publicToken !== undefined) {
            pubToken = publicToken
        }
    
        const [party] = useState(() =>
            isLocalDev ? computeLocalCreds('Public').party : pubParty
        )
        const [token] = useState(() =>
            isLocalDev ? computeLocalCreds('Public').token : pubToken
        )
    
        return (
            <DamlLedger party={party} token={token}>
                {children}
            </DamlLedger>
        )
    }
    

    It works fine locally, but when I try to use it on a ledger deployed on DAMLHub the useLedger hook returns me a ledger with the token undefined and I can´t query using the public party.

         <PublicLedger>
                    <SomeComponent>
          </PublicLedger>
    
    function SomeComponent() {
        const ledger = useLedger()
        console.log('ledger', ledger) //here we get the token property as undefined
    
        const { contracts, loading } = useQuery(SomeTemplate)
        console.log('Contracts', contracts) //Retrieves empty array []
    
        return <div>...</div>
    }
    

    Is there any workaround or alternative that i may use?

    Thanks in advance :slight_smile:

  2. sarah.breckenridge 2

    Hi there Joao,

    After talking with @cocreature, my initial suggestion would be to ensure that each of your Daml Ledger contexts have different identifiers. For reference please see the examples of two such contexts in create-daml-app:

    github.com

    digital-asset/daml/blob/a230de2f82186ea549d15ae961862320f3cd6a43/templates/create-daml-app/ui/src/components/App.tsx#L41

          
            
    1. const App: React.FC = () => {
    2. const [credentials, setCredentials] = React.useState<
    3. Credentials | undefined
    4. >();
    5. if (credentials) {
    6. const PublicPartyLedger: React.FC = ({ children }) => {
    7. const publicToken = usePublicToken();
    8. const publicParty = usePublicParty();
    9. if (publicToken && publicParty) {
    10. return (
    11. <publicContext.DamlLedger
    12. token={publicToken.token}
    13. party={publicParty}>
    14. {children}
    15. </publicContext.DamlLedger>
    16. );
    17. } else {
    18. return <h1>Loading ...</h1>;
    19. }
    20. };
    21. const Wrap: React.FC = ({ children }) =>

    and

    github.com

    digital-asset/daml/blob/a230de2f82186ea549d15ae961862320f3cd6a43/templates/create-daml-app/ui/src/components/App.tsx#L61

          
            
    1. const Wrap: React.FC = ({ children }) =>
    2. isRunningOnHub() ? (
    3. <DamlHub token={credentials.token}>
    4. <PublicPartyLedger>{children}</PublicPartyLedger>
    5. </DamlHub>
    6. ) : (
    7. <div>{children}</div>
    8. );
    9. return (
    10. <Wrap>
    11. <userContext.DamlLedger
    12. token={credentials.token}
    13. party={credentials.party}
    14. user={credentials.user}>
    15. <MainScreen
    16. getPublicParty={credentials.getPublicParty}
    17. onLogout={() => {
    18. if (authConfig.provider === "daml-hub") {
    19. damlHubLogout();
    20. }
    21. setCredentials(undefined);

    Let me know if that helps!

    Best,

    Sarah

  3. Joao_Freitas 1
    sarah.breckenridge:
    licPartyLedger: React.FC = ({ children }) => {
                      const publicToken = usePublicToken();
                      const publicParty = usePublicParty();
                      if (publicToken && publicParty) {
                        return (
                          <publicContext.DamlLedger
                            token={publicToken.token}
    

    Thanks, Sarah. :slight_smile: It seemed that helped with the problem I am facing.