usePublicParty and usePublicToken with DamlLedger

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:

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:

and

Let me know if that helps!

Best,

Sarah

2 Likes

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

1 Like