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

fix(contract-client): allow undefined publicKey #941

Merged
merged 1 commit into from
Apr 25, 2024

Conversation

chadoh
Copy link
Contributor

@chadoh chadoh commented Apr 22, 2024

Before ContractClient lived in stellar-sdk, we allowed (the equivalent of [1]) setting publicKey to undefined.

This is handy, if you're trying to get the result of view calls and you only need the simulation. Our main onboarding tutorial assumes that we have this functionality, and it definitely makes for a more incremental build-up of mental models about how Soroban and Stellar work.

While making ContractClient, we thought maybe this added unnecessary complexity to the interface, and further thought people would be able to pass Keypair.random() if they wanted a fake account.

The trouble with the Keypair.random() approach is that the call to server.getAccount fails, because the account has not been funded.

We could fix this by doing:

let account: Account;
try {
  account = tx.server.getAccount(options.publicKey)
} catch () {
  account = new Account(options.publicKey, '0')
}

But this seems clumsy. Even if there's a better way to check that the caught error is the error we expect ({ code: 404, message: 'Account not found' }), why allow passing a publicKey if it's actually ignored? It seems like a clearer expression of intent to allow the publicKey to be undefined.

Copy link
Contributor

@Shaptic Shaptic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm honestly kinda anti this whole concept of being able to interact with Soroban stuff in general without having an account since it's very leechy of resources, but I think I'm in the minority here so ✔️

@Shaptic
Copy link
Contributor

Shaptic commented Apr 24, 2024

@chadoh can you add a changelog entry?

Before ContractClient lived in stellar-sdk, we allowed (the equivalent
of [[1]]) setting `publicKey` to `undefined`.

This is handy, if you're trying to get the result of view calls and you
only need the simulation. Our main onboarding tutorial assumes that we
have this functionality, and it definitely makes for a more incremental
build-up of mental models about how Soroban and Stellar work.

While making ContractClient, we thought maybe this added unnecessary
complexity to the interface, and further thought people would be able to
pass `Keypair.random()` if they wanted a fake account.

The trouble with the `Keypair.random()` approach is that the call to
`server.getAccount` fails, because the account has not been funded.

We _could_ fix this by doing:

    let account: Account;
    try {
      account = tx.server.getAccount(options.publicKey)
    } catch () {
      account = new Account(options.publicKey, '0')
    }

But this seems clumsy. Even if there's a better way to check that the
caught error is the error we expect (`{ code: 404, message: 'Account not
found' }`), why allow passing a publicKey if it's actually ignored? It
seems like a clearer expression of intent to allow the publicKey to be
`undefined`.

  [1]: https://github.com/stellar/soroban-cli/blob/5f367edb3cf4511794b64aed60a23c5d8b6b55e6/cmd/crates/soroban-spec-typescript/src/project_template/src/assembled-tx.ts#L247-L257
@chadoh chadoh force-pushed the fix/contract-client/pk-undefined branch from 6bedd33 to 732de36 Compare April 24, 2024 19:17
@chadoh
Copy link
Contributor Author

chadoh commented Apr 24, 2024

Changelog added

@Shaptic Shaptic merged commit 87db95b into stellar:master Apr 25, 2024
10 checks passed
@chadoh chadoh deleted the fix/contract-client/pk-undefined branch May 3, 2024 19:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants