Skip to content
Discussions/App Development/usePublicParty and usePublicToken with DamlLedgerForum ↗

usePublicParty and usePublicToken with DamlLedger

App Development3 posts293 views3 likesLast activity May 2022
JO
Joao_FreitasOP
Apr 2022

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:

SA
sarah.breckenridge
Apr 2022

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

JO
Joao_Freitas
May 2022
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.

← Back to Discussions