Since 2009, we have been utilizing our extensive expertise in blockchain technologies to help businesses, both large and small, maximize their efficiency.
Explore More
With more than 400+ experts, Oodles comprises a fantastic resource of business knowledge that spans multiple industries. Whatever the circumstances, we keep to our obligations.
Explore More
At Oodles, we help our clients work with a human understanding but at superhuman speed something that others can't. They thus advance and maintain their lead
1st June 2021
9 min read
Technical Project Manager
Smart contracts are digitally self-executing contracts that run on the Ethereum blockchain. Once they've been created they cannot change. Once they are deployed to a blockchain, their code cannot be updated like any other application.
A smart contract works like a vending machine by dispensing the item to a buyer and transferring cryptoCurrency payments instantly to a seller.
1) First step is to install web3.js using npm.
npm install web3 (This is the main package of web3.js)
2) You need a wallet account in order to make transactions.
I am using MetaMask
How to connect metamask to Application
openMetaMask(){
this.contractService.openMetamask().then(resp =>{
})
}
Create a service and import web3.js into your service file. I've created a service called contract.service.ts.
import { Injectable } from '@angular/core';
import Web3 from "web3";
declare const window: any;
@Injectable({
providedIn: 'root'
})
export class ContractService {
window:any;
constructor() { }
private getAccounts = async () => {
try {
return await window.ethereum.request({ method: 'eth_accounts' });
} catch (e) {
return [];
}
}
public openMetamask = async () => {
window.web3 = new Web3(window.ethereum);
let addresses = await this.getAccounts();
console.log("service",addresses)
if (!addresses.length) {
try {
addresses = await window.ethereum.enable();
} catch (e) {
return false;
}
}
return addresses.length ? addresses[0] : null;
};
}
What is ABI and why it needs to interact with contracts?
The Application Binary Interface (ABI) is the way to interact with contracts in the Ethereum ecosystem both from outside the blockchain and from contract-to-contract interactions,
ABI is the interface between two program modules, one is often at the level of machine code. The interface is the de facto method for encoding/decoding data into/out of the machine code.
We need two things to interact
ABI
Address
and we can use Address and ABI in the same services
import { Injectable } from '@angular/core';
import Web3 from "web3";
declare const window: any;
const Address ='there will be address'
const abi = [
{
"inputs": [],
"stateMutability": "nonpayable",
"type": "constructor"
},
{
"anonymous": false,
"inputs": [
{
"indexed": false,
"internalType": "",
}
],
"name": "Token",
"type": "event"
},
]
ABI would be like that there is name Token which is actually the function name
@Injectable({
providedIn: 'root'
})
export class ContractService {
window:any;
constructor() { }
public Token = async () => {
try {
const contract = new window.web3.eth.Contract(
abi,
Address,
);
const token = await contract.methods.Token().call();
console.log("token",token)
return token
}
catch (error) {
const errorMessage = error.message;
console.log(errorMessage)
}
}
}
We are using the .call method instead of the .send method
.call for getting something and .send to send something.
Vishal Yadav
Vishal is a highly skilled backend developer with extensive 3+ years experience in developing various blockchain platforms. He has a comprehensive understanding of the technologies and has hands-on expertise in Node.js, Ethereum, Layer 1 and Layer 2 solutions, smart contract development, and databases like MySQL and MongoDB. He has a proven track record of working on a range of blockchain-related projects, including token development, staking, governance, indexes, bridges, NFT, marketplace, ICO/IDO, and more. He is adept at managing trading bots, and developing centralized exchanges, and has a creative mind with excellent analytical skills.
Technical Project Manager
By using this site, you allow our use of cookies. For more information on the cookies we use and how to delete or block them, please read our cookie notice.
We would love to
hear from you!
Innovate with confidence!