[ad_1]
In as we speak’s article, we’ll present you tips on how to construct decentralized purposes (dapps) on OP Mainnet utilizing Moralis’ industry-leading Web3 APIs. With our premier APIs, you may seamlessly fetch and combine on-chain knowledge into your initiatives with solely single API calls. Consequently, when leveraging Moralis, you can begin constructing OP Mainnet initiatives sooner and extra effectively!
In case you’re desperate to get going with the code, right here’s somewhat sneak peek of three endpoints for querying a pockets’s native and NFT balances, together with the value of a token:
getNativeBalance() – Fetch the native steadiness of any pockets:const response = await Moralis.EvmApi.steadiness.getNativeBalance({
“chain”: “0xa”,
“tackle”: “0xCBA6a2397b322CF1389f6d1adc05F75F36B20116”
}); getWalletNFTs() – Get the NFT steadiness of any pockets:const response = await Moralis.EvmApi.nft.getWalletNFTs({
“chain”: “0xa”,
“tackle”: “0xCBA6a2397b322CF1389f6d1adc05F75F36B20116”
}); getTokenPrice() – Question the value of any fungible token:const response = await Moralis.EvmApi.token.getTokenPrice({
“chain”: “0xa”,
“tackle”: “0x4200000000000000000000000000000000000042”
});
To discover different endpoints and our APIs in additional element, take a look at our official Moralis documentation web page or be part of us on this article as we present you tips on how to construct on OP Mainnet with out breaking a sweat! Now, if you wish to observe alongside all through this tutorial, don’t neglect to enroll with Moralis. You possibly can create an account in a heartbeat, and also you’ll get on the spot entry to our premier Web3 API suite!
Overview
We’ll kickstart as we speak’s article by exploring the ins and outs of OP Mainnet. In doing so, we’ll clarify what the community is, the way it works, and canopy some advantages of constructing dapps on OP Mainnet. From there, we’ll introduce you to Moralis and our industry-leading Optimism API suite. After getting an summary of Moralis, we’ll leap straight into the principle tutorial and present you tips on how to construct on OP Mainnet in three steps:
Step 1: Create a Moralis Account and Get Your API KeyStep 2: Select OP Mainnet API EndpointsStep 3: Write a Script and Run the Code
To high issues off, we’ll cowl a quick record of OP Mainnet initiatives to present you some inspiration to your subsequent dapp!
![Graphic art design illustration title - Build on OP Mainnet using Moralis](https://moralis.io/wp-content/uploads/2024/02/Blog-Build-on-OP-Mainnet-Moralis.png)
If this sounds thrilling, be part of us within the following part as we begin by discovering what OP Mainnet is!
What’s OP Mainnet?
OP Mainnet – often known as Optimism – launched in 2021, and it’s a low-cost and lightning-fast ETH layer-2 (L2) scaling resolution. This blockchain community serves as a basis for an unlimited ecosystem that encompasses a big selection of decentralized purposes (dapps). This consists of cryptocurrency wallets, decentralized exchanges (DEXs), and different distinguished Web3 initiatives!
![Red letters with white background stating OPTIMISM](https://moralis.io/wp-content/uploads/2024/02/Blog-Build-on-OP-Mainnet.png)
So, why do you have to construct dapps on OP Mainnet? To reply this query, let’s have a look at three advantages of this community:
Pace and Effectivity: OP Mainnet leverages “optimistic rollup” know-how to bundle a whole bunch of off-chain operations right into a single Ethereum transaction. In doing so, the community can considerably enhance Ethereum’s scalability by facilitating faster transactions at a low price. This makes OP Mainnet a extra environment friendly and cost-effective resolution for finish customers and builders alike. Modern Infrastructure: By way of OP Mainnet’s Superchain mission, the community seeks to maximise interoperability and composability between blockchains utilizing the OP Stack. In doing so, OP Mainnet goals to combine a number of siloed L2 scaling options right into a single, unified layer. Scalable Ecosystem: With EVM-equivalence, all Ethereum dapps will work completely on OP Mainnet. This implies you should use the identical established growth instruments to construct on OP Mainnet as you’d on Ethereum.
That provides you an summary of OP Mainnet and the advantages of this community. Within the subsequent part, we’ll introduce you to the simplest strategy to construct OP Mainnet dapps: Moralis!
Best Strategy to Construct on OP Mainnet – Introducing Moralis
Moralis is an industry-leading Web3 API supplier, and its toolset of absolutely OP Mainnet-compatible APIs at present powers blockchain and crypto purposes for tens of millions of customers worldwide!
In Moralis’ suite of Web3 APIs, you’ll discover greater than ten interfaces for numerous use instances, together with the Pockets API, NFT API, and Token API. With these premier APIs, it’s doable to question blockchain knowledge with solely single strains of code. As such, you may seamlessly construct OP Mainnet dapps each sooner and extra effectively utilizing Moralis.
![Graphic art illustration title: Build on OP Mainnet - Create Cross-Chain Optimism Web3 Projects using Moralis](https://moralis.io/wp-content/uploads/2024/02/Blog-Build-on-OP-Mainnet-Moralis.jpg)
Nevertheless, what makes Moralis stand out in comparison with the competitors? To reply this query, let’s discover three advantages of our premier Web3 APIs:
Complete: All our API responses are enriched with metadata, transaction decodings, market knowledge, tackle labels, and rather more from a number of sources. This allows us to offer the {industry}’s most complete APIs which are designed with the result in thoughts, minimizing the variety of calls you have to construct Web3 initiatives. Easy: Moralis is the one Web3 API supplier that’s easy and intuitive to make use of. All of our requests and responses adhere to the identical normal and construction throughout numerous APIs tailor-made for particular use instances. Trusted: Our Web3 APIs are trusted by a whole bunch of hundreds of builders and enterprise prospects like MetaMask, Polygon, Delta, and so forth. So, if you wish to emulate a few of the Web3 {industry}’s greatest names, think about using Moralis.
In case you’d prefer to discover all our interfaces, take a look at the official Web3 API web page!
Additionally, do you know which you can entry our Web3 APIs totally free? Merely join with Moralis, and also you’ll be capable of construct OP Mainnet dapps with out breaking a sweat!
3-Step Tutorial: How you can Construct on OP Mainnet
Now, with an summary of Moralis, let’s dive into the principle part of this information and present you tips on how to construct on OP Mainnet in three simple steps:
Step 1: Create a Moralis Account and Get Your API KeyStep 2: Select OP Mainnet API EndpointsStep 3: Write a Script and Run the Code
Nevertheless, earlier than you will get going, you have to take care of a few stipulations!
Conditions
Moralis’ Web3 APIs are appropriate with a number of programming languages, together with Python, Go, and so forth. Nevertheless, on this tutorial, we’ll be utilizing JavaScript and NodeJS. As such, earlier than you proceed, be sure you have the next prepared:
Step 1: Create a Moralis Account and Get Your API Key
The very first thing you’ll want is a Moralis account. As such, should you haven’t already, click on on the ”Begin for Free” button on the high proper of Moralis’ homepage and arrange an account:
![Moralis Homepage with an arrow pointing at the "Start Building for Free" button](https://moralis.io/wp-content/uploads/2024/02/Blog-Build-on-OP-Mainnet-Sign-Up-for-Free.png)
After creating an account and logging in, you’ll discover that Moralis has robotically arrange a brand new mission for you:
![Moralis Admin UI - Left column showing OP Mainnet project](https://moralis.io/wp-content/uploads/2024/02/Blog-Build-on-OP-Mainnet-Moralis-Project.png)
The very first thing you might need to do is rename the mission beneath ”Settings”:
![Renaming project input parameter inside Moralis Admin Settings UI](https://moralis.io/wp-content/uploads/2024/02/Blog-Build-on-OP-Mainnet-Rename-Project.png)
Subsequent, scroll right down to the ”API Keys” part and replica your Moralis API key:
![Copying API key](https://moralis.io/wp-content/uploads/2024/02/Blog-Build-on-OP-Mainnet-Copy-API-Key.png)
Please preserve it for now, as you’ll want it within the third step!
Step 2: Select OP Mainnet API Endpoints
On this part, we’ll dive a bit deeper into the assorted APIs of Moralis to present you an summary of what OP Mainnet initiatives you may construct with our premier instruments. Extra particularly, we’ll deal with the next three APIs and their respective endpoints:
Pockets API NFT APIToken API
So, let’s kick this off by exploring the ins and outs of Moralis’ Pockets API!
Pockets API
Moralis’ Pockets API is the last word instrument for constructing cryptocurrency wallets and integrating pockets performance into OP Mainnet initiatives. The Pockets API helps 500+ million addresses throughout the most important blockchains, together with Ethereum, Polygon, BSC, and, after all, OP Mainnet!
![Graphic art illustration - Title stating, "Build OP Mainnet Dapps Using Moralis Wallet API."](https://moralis.io/wp-content/uploads/2024/02/Blog-Build-on-OP-Mainnet-Wallet-API.png)
With this industry-leading instrument, you may effortlessly question the native steadiness, NFT steadiness, profile knowledge, token transfers, transactions, and extra of any cryptocurrency tackle. As such, with the Pockets API, you may construct every little thing from portfolio trackers to cryptocurrency wallets on OP Mainnet with out breaking a sweat.
To spotlight the accessibility of this instrument, listed here are three distinguished endpoints you’ll possible discover useful when constructing dapps:
getNativeBalance() – Question the native steadiness of any pockets tackle: const response = await Moralis.EvmApi.steadiness.getNativeBalance({
“chain”: “0xa”,
“tackle”: “0xCBA6a2397b322CF1389f6d1adc05F75F36B20116”
});
getNativeBalance() response:
{
“steadiness”: “50640324378835108031”
} getWalletTransactions() – Fetch the native transaction historical past of any pockets:const response = await Moralis.EvmApi.transaction.getWalletTransactions({
“chain”: “0xa”,
“tackle”: “0x2a82ae142b2e62cb7d10b55e323acb1cab663a26”
});
getWalletTransactions() response:
[
//…
{
“hash”: “0x6cbb692563bfb4adfa0850a4d68cbed848ba9be2b73fe04f42a49b81305e3eab”,
“nonce”: “13”,
“transaction_index”: “5”,
“from_address”: “0xcd3031fb7da6c6764466a6f77ec74e52a4cea810”,
“from_address_label”: null,
“to_address”: “0x2a82ae142b2e62cb7d10b55e323acb1cab663a26”,
“to_address_label”: null,
“value”: “27200000”,
“gas”: “21000”,
“gas_price”: “50000000”,
“input”: “0x”,
“receipt_cumulative_gas_used”: “363490”,
“receipt_gas_used”: “21000”,
“receipt_contract_address”: null,
“receipt_root”: null,
“receipt_status”: “0”,
“block_timestamp”: “2023-11-18T09:10:23.000Z”,
“block_number”: “112349923”,
“block_hash”: “0xd92193788b4f8ed5c991819ecf22433a4a5eca8b45974c90115c852635dcc4d3”,
“transfer_index”: [
112349923,
5
]
},
//…
] getWalletTokenBalances() – Get the token balances of any pockets: const response = await Moralis.EvmApi.token.getWalletTokenBalances({
“chain”: “0xa”,
“tackle”: “0x2a82ae142b2e62cb7d10b55e323acb1cab663a26”
});
getWalletTokenBalances() response:
[
{
“token_address”: “0x7f5c764cbc14f9669b88837ca1490cca17c31607”,
“symbol”: “USDC”,
“name”: “USD Coin”,
“logo”: null,
“thumbnail”: null,
“decimals”: 6,
“balance”: “2000000”,
“possible_spam”: false,
“verified_contract”: true
},
{
“token_address”: “0x4200000000000000000000000000000000000042”,
“symbol”: “OP”,
“name”: “Optimism”,
“logo”: null,
“thumbnail”: null,
“decimals”: 18,
“balance”: “1294044371320286807123702580”,
“possible_spam”: false,
“verified_contract”: true
},
//…
]
NFT API
The NFT API helps over three million collections throughout ten+ EVM-compatible chains, together with every little thing from well-established initiatives to tokens that dropped simply seconds in the past. Consequently, this instrument takes care of all of your NFT data-related growth wants!
![marketing banner stating "Build NFT projects on OP Mainnet"](https://moralis.io/wp-content/uploads/2024/02/Blog-Build-on-OP-Mainnet-NFT-API.png)
With the NFT API, you solely want single API calls to seamlessly fetch real-time NFT transfers, on-chain worth knowledge, enriched metadata, and extra. So, when working with this premier instrument, you may construct every little thing from NFT marketplaces to Web3 video games with ease.
Listed here are three distinguished endpoints to focus on the accessibility of our NFT API:
getNFTMetadata() – Get the metadata of any NFT: const response = await Moralis.EvmApi.nft.getNFTMetadata({
“chain”: “0xa”,
“tackle”: “0xB8Df6Cc3050cC02F967Db1eE48330bA23276A492”,
“tokenId”: “1”
});
getNFTMetadata() response:
{
“quantity”: “1”,
“token_id”: “1”,
“token_address”: “0xb8df6cc3050cc02f967db1ee48330ba23276a492”,
“contract_type”: “ERC721”,
“owner_of”: “0xb09fa44ef3bcbd1bf2d35d5644a19e188d4c05f6”,
“last_metadata_sync”: “2024-02-01T15:09:43.956Z”,
“last_token_uri_sync”: “2024-02-01T15:09:42.794Z”,
“metadata”: “{“identify”:”Optipunk #1″,”description”:”10,000 distinctive collectible characters with proof of possession saved on Optimism”,”attributes”:[{“trait_type”:”Type”,”value”:”Male”},{“trait_type”:”Accessory”,”value”:”Smile”},{“trait_type”:”Accessory”,”value”:”Mohawk”}],”picture”:”ipfs://QmbAhtqQqiSQqwCwQgrRB6urGc3umTskiuVpgX7FvHhutU/1.png”}”,
“block_number”: “97749”,
“block_number_minted”: “97749”,
“identify”: “OptiPunk”,
“image”: “OPPUNK”,
“token_hash”: “364af8044f93145cf5859151b46223a3”,
“token_uri”: “https://ipfs-metadata.moralis.io:2053/ipfs/QmZYqgu73bpovLt7Y4kYfCPUbbgdRfvcsja8YZsprKismB/1”,
“minter_address”: “0xb09fa44ef3bcbd1bf2d35d5644a19e188d4c05f6”,
“verified_collection”: false,
“possible_spam”: false
} getWalletNFTs() – Fetch the NFT steadiness of a pockets:const response = await Moralis.EvmApi.nft.getWalletNFTs({
“chain”: “0xa”,
“tackle”: “0xCBA6a2397b322CF1389f6d1adc05F75F36B20116”
});
getWalletNFTs() response:
[
{
“amount”: “1”,
“token_id”: “3569948”,
“token_address”: “0xd12999440402d30f69e282d45081999412013844”,
“contract_type”: “ERC721”,
“owner_of”: “0xcba6a2397b322cf1389f6d1adc05f75f36b20116”,
“last_metadata_sync”: “2024-02-01T15:03:42.174Z”,
“last_token_uri_sync”: “2024-02-01T15:03:42.039Z”,
“metadata”: “//…”,
“block_number”: “107697133”,
“block_number_minted”: “107306857”,
“name”: “Omnichain Adventures”,
“symbol”: “OMNIA”,
“token_hash”: “f50ea59436c7eda79995a4e694d53f72”,
“token_uri”: “https://yvsbrs5apkpplrjba65gcet4zbe3wsv6i5y3e44vgxcunjwopsmq.arweave.net/xWQYy6B6nvXFIQe6YRJ8yEm7Sr5HcbJzlTXFRqbOfJk”,
“minter_address”: “0x8caf185283211beab492601825fee0b44f8633aa”,
“verified_collection”: false,
“possible_spam”: false
}
] getNFTOwners() – Question an array of all NFT house owners for a given contract:const response = await Moralis.EvmApi.nft.getNFTOwners({
“chain”: “0xa”,
“tackle”: “0xB8Df6Cc3050cC02F967Db1eE48330bA23276A492”
});
getNFTOwners() response:
[
{
“amount”: “1”,
“token_id”: “2329”,
“token_address”: “0xb8df6cc3050cc02f967db1ee48330ba23276a492”,
“contract_type”: “ERC721”,
“owner_of”: “0x0331969e189d63fbc31d771bb04ab44227d748d8”,
“last_metadata_sync”: “2024-02-01T15:10:32.987Z”,
“last_token_uri_sync”: “2024-02-01T15:10:31.531Z”,
“metadata”: “//…”,
“block_number”: “135838”,
“block_number_minted”: “135838”,
“name”: “OptiPunk”,
“symbol”: “OPPUNK”,
“token_hash”: “6206b15eef897e716a8f943c61718039”,
“token_uri”: “https://ipfs-metadata.moralis.io:2053/ipfs/QmZYqgu73bpovLt7Y4kYfCPUbbgdRfvcsja8YZsprKismB/2329”,
“minter_address”: “0x0331969e189d63fbc31d771bb04ab44227d748d8”,
“verified_collection”: false,
“possible_spam”: false
},
//…
]
Token API
The Token API is the {industry}’s most complete instrument for fungible token knowledge, supporting each single token throughout greater than ten blockchains, together with OP Mainnet, Ethereum, Polygon, and BNB Good Chain!
![Token API from Moralis let's you build dapps on Optimism's mainnet](https://moralis.io/wp-content/uploads/2024/02/Blog-Build-on-OP-Mainnet-Token-API.png)
With the Token API, you may seamlessly fetch costs, transfers, balances, metadata, and rather more for any ERC-20 token. As such, when utilizing this premier API, you may construct every little thing from DEXs to token explorers very quickly.
To spotlight the accessibility of the Token API, listed here are three endpoints you’ll discover useful when constructing OP Mainnet dapps:
getTokenPrice() – Get the value of any fungible token: const response = await Moralis.EvmApi.token.getTokenPrice({
“chain”: “0xa”,
“tackle”: “0x4200000000000000000000000000000000000042”
});
getTokenPrice() response:
{
“tokenName”: “Optimism”,
“tokenSymbol”: “OP”,
“tokenLogo”: null,
“tokenDecimals”: “18”,
“nativePrice”: {
“worth”: “1346688388542655”,
“decimals”: 18,
“identify”: “Ether”,
“image”: “ETH”,
“tackle”: “0x4200000000000000000000000000000000000006”
},
“usdPrice”: 3.357083520606333,
“usdPriceFormatted”: “3.357083520606332971”,
“exchangeName”: “Uniswap v3”,
“exchangeAddress”: “0x1F98431c8aD98523631AE4a59f267346ea31F984”,
“tokenAddress”: “0x4200000000000000000000000000000000000042”,
“priceLastChangedAtBlock”: “115945253”,
“verifiedContract”: true
} getTokenMetadata() – Fetch the metadata of any token:const response = await Moralis.EvmApi.token.getTokenMetadata({
“chain”: “0xa”,
“addresses”: [
“0x4200000000000000000000000000000000000042”
]
});
getTokenMetadata() response:
[
{
“address”: “0x4200000000000000000000000000000000000042”,
“address_label”: null,
“name”: “Optimism”,
“symbol”: “OP”,
“decimals”: “18”,
“logo”: null,
“logo_hash”: null,
“thumbnail”: null,
“block_number”: “0”,
“validated”: 1,
“created_at”: null,
“possible_spam”: false,
“verified_contract”: true
}
] getTokenTransfers() – Question all transactions for a given token contract: const response = await Moralis.EvmApi.token.getTokenTransfers({
“chain”: “0xa”,
“tackle”: “0x4200000000000000000000000000000000000042”
});
getTokenTransfers() response:
[
{
“token_name”: “Optimism”,
“token_symbol”: “OP”,
“token_logo”: null,
“token_decimals”: “18”,
“from_address”: “0x666936a007c8c2e70dd5aae9151fd50de858fb16”,
“from_address_label”: null,
“to_address”: “0x2580fe0cdd3a2300f4fe9637e7d4b44d47f1f63b”,
“to_address_label”: null,
“address”: “0x4200000000000000000000000000000000000042”,
“block_hash”: “0x4f99ed7e331b422c87d73eed2a60e98417d072ed64d0ceab57e7479d76aeb58b”,
“block_number”: “115945333”,
“block_timestamp”: “2024-02-09T14:37:23.000Z”,
“transaction_hash”: “0xf8e82cbe28afb6e3adea9ef0c34132588ff29a8cc16b0cc48cf516efc0a68d05”,
“transaction_index”: 10,
“log_index”: 48,
“value”: “600000000000000000000”,
“possible_spam”: false,
“value_decimal”: “600”,
“verified_contract”: true
},
]
Step 3: Write a Script and Run the Code
When you select the suitable endpoints to your explicit mission, all that continues to be is so that you can name them. And we’ll present you precisely how to take action on this last step of our tutorial!
On this case, we’ll use the getWalletTokenBalances() endpoint for instance. Nevertheless, should you’re all in favour of some other endpoint, fear not, as you may observe just about the identical steps for all endpoints.
Begin by organising a folder in your IDE and run the terminal command beneath to initialize a brand new mission:
npm init
Subsequent, run the instructions beneath within the terminal to put in the Moralis SDK:
npm set up moralis @moralisweb3/common-evm-utils
From right here, open your ”package deal.json” file and add ”sort”: ”module”:
![Showing how to open your ”package.json” file and add ”type”: ”module”](https://moralis.io/wp-content/uploads/2024/02/Blog-Build-on-OP-Mainnet-Add-Type.png)
You possibly can then create a brand new ”index.js” file and add the next code:
import Moralis from ‘moralis’;
strive {
await Moralis.begin({
apiKey: “YOUR_API_KEY”
});
const response = await Moralis.EvmApi.token.getWalletTokenBalances({
“chain”: “0xa”,
“tackle”: “0x2a82ae142b2e62cb7d10b55e323acb1cab663a26”
});
console.log(response.uncooked);
} catch (e) {
console.error(e);
}
You now must make just a few small configurations to the code. Initially, you have to change YOUR_API_KEY with the important thing you copied throughout step one. Subsequent, you additionally must configure the tackle parameter to suit your question:
![Showing where to paste the OP mainnet API key into code parameter](https://moralis.io/wp-content/uploads/2024/02/Blog-Build-on-OP-Mainnet-Configure-Code-1024x566.png)
From right here, you solely must run the code. To take action, open a brand new terminal and execute the command beneath within the mission’s root folder:
node index.js
In return, you’ll get a response containing an array of the token balances of the required tackle. It is going to look one thing like this:
[
{
“token_address”: “0x7f5c764cbc14f9669b88837ca1490cca17c31607”,
“symbol”: “USDC”,
“name”: “USD Coin”,
“logo”: null,
“thumbnail”: null,
“decimals”: 6,
“balance”: “2000000”,
“possible_spam”: false,
“verified_contract”: true
},
{
“token_address”: “0x4200000000000000000000000000000000000042”,
“symbol”: “OP”,
“name”: “Optimism”,
“logo”: null,
“thumbnail”: null,
“decimals”: 18,
“balance”: “1294044371320286807123702580”,
“possible_spam”: false,
“verified_contract”: true
},
//…
]
That’s it! You now know tips on how to question every little thing from pockets balances to token transactions utilizing Moralis’ premier Web3 API suite. In flip, you’re now able to take your growth endeavors to the following degree and begin constructing OP Mainnet dapps!
Dapp Concepts – Exploring a Checklist of Optimism Initiatives
With a greater understanding of how one can construct dapps on OP Mainnet utilizing Moralis’ Web3 APIs, you would possibly now be on the lookout for some inspiration to your subsequent mission. Consequently, we’ll take this part to discover 5 distinguished OP Mainnet initiatives:
If you wish to discover initiatives not coated above, please take a look at our complete record of high Optimism dapps on Moralis’ Web3 Wiki!
Abstract: How you can Construct on OP Mainnet
In as we speak’s article, we confirmed you tips on how to construct on OP Mainnet with Moralis in three easy steps:
Step 1: Create a Moralis Account and Get Your API KeyStep 2: Select OP Mainnet API EndpointsStep 3: Write a Script and Run the Code
When you’ve got adopted alongside this far, you now know tips on how to construct dapps on Optimism seamlessly! Additionally, should you haven’t already, don’t neglect to enroll with Moralis. You possibly can join with the clicking of some buttons, and also you’ll achieve on the spot entry to our industry-leading OP Mainnet API suite so you can begin constructing dapps sooner and extra effectively!
[ad_2]
Source link