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

feat: Generate typescript clients automagically! #97

Merged
merged 7 commits into from Jul 14, 2022

Conversation

octalmage
Copy link
Contributor

Apologies for this giant PR, it seems larger than it really is since I'm including a couple of modified npm packages.

This PR adds a new command, terrain contract:generateClient [contract name]. When you run this command Terrain will generate a wallet provider and Terra.js compatible library you can import into automation scripts, tests, or your frontend to interact with your smart contracts.

Lets say you have a smart contract named "party", to generate the client you will run:

terrain contract:generateClient party

This will output PartyClient.ts in ./lib/clients/PartyClient.ts.

Using the generated client is easy. Import it where you'd like to call your smart contract, instantiate it will a few parameters, then start calling methods or queries exposed by the smart contract:

import { PartyClient } from './contract/clients/PartyClient';

const contract = new PartyClient(lcd, wallet, 'terra14khjxsvmklm8kg97vhrn5wc2tz3w57na5seeke3qnwashe4pxd3qmljkek');
await contract.increment();
console.log(await contract.getCount());

Generated clients accept a configured LCDClient, a Wallet instance, and the address to the deployed contract.

@@ -78,7 +80,7 @@ export default class New extends Command {
cli.action.start('- frontend');
if (flags.framework === 'react') {
await TemplateScaffolding.from({
remoteUrl: 'https://codeload.github.com/terra-money/terrain-frontend-template/zip/refs/heads/main',
remoteUrl: 'https://codeload.github.com/octalmage/terrain-frontend-template/zip/refs/heads/main',
Copy link
Contributor Author

Choose a reason for hiding this comment

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

can't forget to set this back!

Copy link

@github-advanced-security github-advanced-security bot left a comment

Choose a reason for hiding this comment

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

Found 99 potential problems in the proposed changes. Check the Files changed tab for more details.

@octalmage
Copy link
Contributor Author

When reviewing I'd recommend starting here: https://github.com/terra-money/terrain/pull/97/files#diff-c189f236d6e2a2b55daa0229225e3283b0d61abd993ae3aecad63bbb5ba9a885

Ignore the packages directory as much as possible, there's a lot of boilerplate in there.

Copy link

@github-advanced-security github-advanced-security bot left a comment

Choose a reason for hiding this comment

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

Found 99 potential problems in the proposed changes. Check the Files changed tab for more details.

package.json Show resolved Hide resolved
import TerrainCLI from '../../TerrainCLI';
import generateClient from '../../lib/generateClient';

export default class GenerateClient extends Command {
Copy link
Contributor

Choose a reason for hiding this comment

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

Good name! What do you think about having a generate folder with client (or types) as it's own entry point? To have a syntax like terrain contract:generate:client party instead of terrain contract:generateClient party.

If we apply this suggestion I will create an issue to also modify terrain contract:updateAdmin

packages/cosmwasm-typescript-gen/.vscode/settings.json Outdated Show resolved Hide resolved
packages/cosmwasm-typescript-gen/.eslintrc.js Outdated Show resolved Hide resolved
@emidev98
Copy link
Contributor

emidev98 commented Jul 6, 2022

Cool feature! Looking forward to use it in my projects!

Copy link

@github-advanced-security github-advanced-security bot left a comment

Choose a reason for hiding this comment

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

Found 99 potential problems in the proposed changes. Check the Files changed tab for more details.

@octalmage octalmage merged commit 5038864 into main Jul 14, 2022
@octalmage octalmage deleted the feat/generateClient branch July 14, 2022 16:46
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.

None yet

2 participants