Extensions Framework
In addition to generic read/write contract functions, the SDK by automatically detecting common patterns on your contract, known as Solidity Extensions
, and provides a catalog of high level APIs that simplify the interaction with your contract.
A typical Example
Let's say you have a contract that implements a ERC721
standard. The SDK will automatically detect this pattern and provide a set of APIs that will allow you to interact with your contract in a more convenient way.
const contract = await sdk.getContract("0x...");
// read all ERC721 NFTs, along with metadata and owners
const nfts = await contract.erc721.getAll();
// get all the NFTs owned by a specific wallet address
const ownedNFTs = await contract.erc721.getOwned("0x...");
// upload metadata to IPFS and mint a new NFT
await contract.erc721.mint({
name: "Cool NFT",
description: "Minted NFT from code!",
image: fs.readFileSync("path/to/image.png"), // This can be an image url or file
});
Because the contract implements the ERC721
standard, a set of APIs become available under the erc721
namespace.
- The
erc721.getAll()
anderc721.getOwned()
functions will handle querying the supply, fetching the metadata and owner for each NFT and return a convenient, typed NFT array. - The
erc721.mint()
will handle uploading the metadata to IPFS, and minting a new NFT with the given metadata to the connected wallet address.
This saves a lot of time and effort, as you don't have to manually implement these common functions yourself.
Detecting avilable extensions
You can check which extensions are available on a contract by calling the getAllDetectedExtensionNames()
function.
const contract = await sdk.getContract("0x...");
// Get an array of the extensions the contract supports
const extensions = getAllDetectedExtensionNames(contract.abi);
console.log(extensions);
// output: ['ERC721','ERC721Mintable','Royalty','Permissions','Gasless',...]
// Detect whether the contract supports a given extension
const isERC721 = await isExtensionEnabled(contract.abi, "ERC721");
Preparing transactions
Just like with contract.prepare()
, you can prepare a transaction for a specific extension by calling contract.{{extension}}.{{functionName}}.prepare(args)
. This will return a Transaction
object that you can use to encode, estimate, simulate, sign or send the transaction.
// Prepare a transaction, but DON'T send it
const tokenId = 0;
const quantity = 1;
const tx = await contract.erc1155.claim.prepare(tokenId, quantity);
// Some example use cases for the transaction
const encoded = await tx.encode(); // Encode the transaction
const gasCost = await tx.estimateGasCost(); // Estimate the gas cost
const simulatedTx = await tx.simulate(); // Simulate the transaction
const signedTx = await tx.sign(); // Sign the transaction for later use
// Submit the transaction, but don't wait for confirmations
const sentTx = await tx.send();
console.log("Submitted transaction:", sentTx.hash);
One common use case for example is to prepare and encoded 2 different transactions, then execute them atomically using multicall
:
// Prepare an ERC721 burn and a ERC721 mint to be executed in one tx
const tx1 = await contract.erc721.burn.prepare(tokenId);
const tx2 = await contract.erc721.mint.prepare(nftMetadata);
// Encode both transactions and execute them via multicall
// this will only prompt the user for 1 transaction to sign instead of 2
await contract.call("multicall", [tx1.encode(), tx2.encode()]);
All Available Extensions
All extensions are available under their own namespace in the SmartContract
class, and map to a corresponding Solidity Extension
Solidity Extension | Namespace | Description |
---|---|---|
ERC20 | contract.erc20 | Standard ERC20 functions and more |
ERC20SignatureMintable | contract.erc20.signature | Mint tokens with signature |
ERC20ClaimPhases | contract.erc20.claimConditions | Configure claim conditions for Drop contracts |
ERC721 | contract.erc721 | Standard ERC721 functions and more |
ERC721ClaimPhases | contract.erc721.signature | Mint NFTs with signature |
ERC721SignatureMintable | contract.erc721.claimConditions | Configure claim conditions for Drop contracts |
ERC721SharedMetadata | contract.erc721.sharedMetadata | OpenEditionERC721 shared metadata |
ERC721DelayedReveal | contract.erc721.revealer | Manage delayed reveal NFTs |
ERC1155 | contract.erc1155 | Stadard ERC1155 functions and more |
ERC1155SignatureMintable | contract.erc1155.signature | Mint NFTs with signature |
ERC1155 | contract.erc1155.claimConditions | Configure claim conditions for Drop contracts |
ERC4337 Factory | contract.accountFactory | Smart Account Factory helpers and state |
ERC4337 Account | contract.account | Smart Account utilities |
DirectListings | contract.directListings | Marketplace direct listing functionality |
EnglishAuctions | contract.englishAuction | Marketplace english auction functionality |
Offers | contract.offers | Marketplace offers functionality |
AirdropERC20 | contract.airdrop20 | Airdrop tokens |
AirdropERC721 | contract.airdrop721 | Airdrop NFTs |
AirdropERC1155 | contract.airdrop1155 | Airdrop NFTs |
ContractMetadata | contract.metadata | Contract metadata upload/download |
PermissionsEnumerable | contract.roles | Contract roles and permissions |
PlatformFee | contract.platformFees | Manage platform fees |
Royalty | contract.royalties | Manage royalties |
Owner | contract.owner | Manage owners |
DynamicContract | contract.extensions | Add/remove extensions on a dynamic contract |