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

Add a tutorial for smart contract basic interaction #6089

Merged
merged 11 commits into from
May 22, 2023

Conversation

Muhammad-Altabba
Copy link
Contributor

Description

Closes #6078

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

Checklist:

  • I have selected the correct base branch.
  • I have performed a self-review of my own code.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have made corresponding changes to the documentation.
  • My changes generate no new warnings.
  • Any dependent changes have been merged and published in downstream modules.
  • I ran npm run lint with success and extended the tests and types if necessary.
  • I ran npm run test:unit with success.
  • I ran npm run test:coverage and my test cases cover all the lines and branches of the added code.
  • I ran npm run build and tested dist/web3.min.js in a browser.
  • I have tested my code on the live network.
  • I have checked the Deploy Preview and it looks correct.
  • I have updated the CHANGELOG.md file in the root folder.
  • I have linked Issue(s) with this PR in "Linked Issues" menu.

@Muhammad-Altabba Muhammad-Altabba linked an issue May 14, 2023 that may be closed by this pull request
@github-actions
Copy link

github-actions bot commented May 14, 2023

Bundle Stats

Hey there, this message comes from a github action that helps you and reviewers to understand how these changes affect the size of this project's bundle.

As this PR is updated, I'll keep you updated on how the bundle size is impacted.

Total

Asset Old size New size Diff Diff %
Total 645 KB 645 KB 0 0.00%
View detailed bundle breakdown

Added

No assets were added

Removed

No assets were removed

Bigger

No assets were bigger

Smaller

No assets were smaller

Unchanged

Asset Old size New size Diff Diff %
web3.min.js 628 KB 628 KB 0 0.00%
../lib/commonjs/index.d.ts 8.43 KB 8.43 KB 0 0.00%
../lib/commonjs/accounts.d.ts 3.67 KB 3.67 KB 0 0.00%
../lib/commonjs/types.d.ts 2.37 KB 2.37 KB 0 0.00%
../lib/commonjs/abi.d.ts 1000 bytes 1000 bytes 0 0.00%
../lib/commonjs/web3.d.ts 808 bytes 808 bytes 0 0.00%
../lib/commonjs/eth.exports.d.ts 280 bytes 280 bytes 0 0.00%
../lib/commonjs/providers.exports.d.ts 148 bytes 148 bytes 0 0.00%
../lib/commonjs/version.d.ts 60 bytes 60 bytes 0 0.00%

@cloudflare-pages
Copy link

cloudflare-pages bot commented May 14, 2023

Deploying with  Cloudflare Pages  Cloudflare Pages

Latest commit: 25ebc9d
Status: ✅  Deploy successful!
Preview URL: https://a377327c.web3-js-docs.pages.dev
Branch Preview URL: https://6078-contracts-tutorial.web3-js-docs.pages.dev

View logs

@codecov
Copy link

codecov bot commented May 14, 2023

Codecov Report

Merging #6089 (25ebc9d) into 4.x (edc7a84) will not change coverage.
The diff coverage is n/a.

Additional details and impacted files
@@           Coverage Diff           @@
##              4.x    #6089   +/-   ##
=======================================
  Coverage   87.41%   87.41%           
=======================================
  Files         199      199           
  Lines        7612     7612           
  Branches     2063     2063           
=======================================
  Hits         6654     6654           
  Misses        958      958           
Flag Coverage Δ
UnitTests 87.41% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Components Coverage Δ
web3 ∅ <ø> (∅)
web3-core ∅ <ø> (∅)
web3-errors ∅ <ø> (∅)
web3-eth ∅ <ø> (∅)
web3-eth-abi ∅ <ø> (∅)
web3-eth-accounts ∅ <ø> (∅)
web3-eth-contract ∅ <ø> (∅)
web3-eth-ens ∅ <ø> (∅)
web3-eth-iban ∅ <ø> (∅)
web3-eth-personal ∅ <ø> (∅)
web3-net ∅ <ø> (∅)
web3-providers-http ∅ <ø> (∅)
web3-providers-ipc ∅ <ø> (∅)
web3-providers-ws ∅ <ø> (∅)
web3-rpc-methods ∅ <ø> (∅)
web3-utils ∅ <ø> (∅)
web3-validator ∅ <ø> (∅)

@mpetrunic
Copy link
Contributor

I feel like it would make a lot more sense, to write tutorial using typescript^^

Next, create a new file called `index.js` in your project directory and add the following code to it:

```javascript
const { Web3 } = require('web3');
Copy link
Contributor

Choose a reason for hiding this comment

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

I think also mention in desc that we also support ESM imports as we have native ESM builds.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Seems a good idea. I will add a hint for this.

Create a file named `deploy.js` and fill it with the following code:

```javascript
const { Web3 } = require('web3');
Copy link
Contributor

Choose a reason for hiding this comment

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

wt do you think about encouraging users to use individual packages instead of importing main web3, as it brings many not-required packages as well , if some one is not doing optimizations for that non-required pkgs.

Copy link
Contributor

Choose a reason for hiding this comment

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

My 2c: tutorias are meant for people first time interacting with web3.js. Goal is to make it as simple as possible. Using individual packages would make sense in some "Advanced tutorials"

Copy link
Contributor

Choose a reason for hiding this comment

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

We need to keep in mind that auto-signing requests with the local wallet are not working when you use individual packages

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@jdevcs @mpetrunic
Thanks, and yes, I kept it using one package for simplicity. But I should add a hint regarding this.

@avkos
Thanks for sharing this interesting information. Do we have it mentioned somewhere? If not, I think we should highlight this in multiple places.

Copy link
Contributor

@jdevcs jdevcs May 16, 2023

Choose a reason for hiding this comment

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

@avkos auto signing works if there is wallet in context.

for example one of working snippit:

const {Wallet, privateKeyToAccount} = require('web3-eth-accounts');
const {Contract} = require('web3-eth-contract');
const {Web3Context} = require('web3-core');

const con_json = require('contract.json');

async function test() {
    
//create a context with wallet and provider
    const account = privateKeyToAccount("PRIVATE KEY");
    const context = new Web3Context(
        { 
            provider: 'http://127.0.0.1:7545',
            wallet: new Wallet(account)
        });

//pass that context to other packages instead of only provider
    const contract = new Contract(con_json.abi, context);
    
    const contractInstance = await contract.deploy(
        {
            data: con_json.bytecode
        }).send({ 
            gas: 10000,
            from: account.address });

    console.log(contractInstance.options.address);


    const receipt = await contractInstance.methods.store(12).send(
        {
            from: account.address, 
            gas: 10000
        });

    console.log(receipt);


    //.....
}


However as @mpetrunic said , it should not be added in current tutorial , so we will not do it.

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for example @jdevcs , It will be great to have this example somewhere in tutorials

@avkos
Copy link
Contributor

avkos commented May 15, 2023

I feel like it would make a lot more sense, to write tutorial using typescript^^

it will be great to have tutorials in TS and JS. so people who use JS or TS can just copy/paste the code and it should works. But I think it is another task. Because we need to change all our tutorials

@Muhammad-Altabba
Copy link
Contributor Author

I feel like it would make a lot more sense, to write tutorial using typescript^^

it will be great to have tutorials in TS and JS. so people who use JS or TS can just copy/paste the code and it should works. But I think it is another task. Because we need to change all our tutorials

Yes, I thought about using TS but this would add more steps to configure the samples for using TypeScript. So, I thought about keeping it simple as the tutorials are for absolute beginners. And for this, we may do something like a bit more advanced tutorial for TypeSecript users that would contain more bit advanced usage.

@mpetrunic
Copy link
Contributor

I feel like it would make a lot more sense, to write tutorial using typescript^^

it will be great to have tutorials in TS and JS. so people who use JS or TS can just copy/paste the code and it should works. But I think it is another task. Because we need to change all our tutorials

Yes, I thought about using TS but this would add more steps to configure the samples for using TypeScript. So, I thought about keeping it simple as the tutorials are for absolute beginners. And for this, we may do something like a bit more advanced tutorial for TypeSecript users that would contain more bit advanced usage.

The thing is, that configuration and types is the main issue for the majority users. Js users can just cp typescript code and remove types so you kill 2 birds with one stone.

@Muhammad-Altabba
Copy link
Contributor Author

I feel like it would make a lot more sense, to write tutorial using typescript^^

it will be great to have tutorials in TS and JS. so people who use JS or TS can just copy/paste the code and it should works. But I think it is another task. Because we need to change all our tutorials

Yes, I thought about using TS but this would add more steps to configure the samples for using TypeScript. So, I thought about keeping it simple as the tutorials are for absolute beginners. And for this, we may do something like a bit more advanced tutorial for TypeSecript users that would contain more bit advanced usage.

The thing is, that configuration and types is the main issue for the majority users. Js users can just cp typescript code and remove types so you kill 2 birds with one stone.

I agree. However, I hope to give a smooth experience with a strait forward copy/past then it works. But using TypeScript in the samples would also involve adding additional small steps for installing and compiling the code before running.

So, I thought about having 2 tabs. One for JavaScript and the other for TypeScript. And I needed to update docusaurus to enable taps as they are available in new version. But the taps have issues with code blocks if they are placed inside. So, I am thinking about keeping the javascript here similar to other tutorials. And create an issue to investigate how to put code snippets inside taps with docusaurus and update all the tutorials thereafter.

@mpetrunic
Copy link
Contributor

I agree. However, I hope to give a smooth experience with a strait forward copy/past then it works. But using TypeScript in the samples would also involve adding additional small steps for installing and compiling the code before running.

Or ts-node :)

So, I thought about having 2 tabs. One for JavaScript and the other for TypeScript. And I needed to update docusaurus to enable taps as they are available in new version. But the taps have issues with code blocks if they are placed inside. So, I am thinking about keeping the javascript here similar to other tutorials. And create an issue to investigate how to put code snippets inside taps with docusaurus and update all the tutorials thereafter.

Two tabs is also fine^^

Copy link
Contributor

@jdevcs jdevcs left a comment

Choose a reason for hiding this comment

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

LGTM, except few parts where changes required by other members.

@djrann13 djrann13 linked an issue May 20, 2023 that may be closed by this pull request
This was unlinked from issues May 20, 2023
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.

Contracts Tutorial
5 participants