Function insights
This page provides a behind-the-scenes look at the text-based token minting and contract deployment processes within the Xyxyx API. We will illustrate how the Ethers v6 Library is used under the hood, and why the wallet private key is critical to sign transactions autonomously — allowing calls to be made without user interaction via a front-end.
Overview
By leveraging Ethers, the API can:
Connect to the blockchain via a provider (e.g.,
ethers.JsonRpcProvider
).Create a wallet (signer) using the private key that you provide in each request.
Deploy contracts through the
ContractFactory
.Mint tokens by calling contract methods.
Because the private key is included with every request (rather than stored server-side), you remain fully responsible for its confidentiality. However, this design still allows an entirely automated flow for contract deployment and token minting, without interactive wallet confirmations.
Mint Token
Explanation
Import Ethers: Using
require("ethers")
imports the core Ethers library.Load Contract ABI: The ABI (Application Binary Interface)
abi.json
describes the functions, events, and error signatures of the ERC-721F contract. It allows Ethers to understand how to encode calls tomint()
and decode contract responses.Create Contract Instance:
contractAddress
is the on-chain address of the deployed contract.contractAbi
is the ABI loaded from the JSON file.wallet
is an instance ofethers.Wallet
, which includes the private key (provided by the user in the request) and a provider.Passing the
wallet
as the third parameter ensures that all contract calls are signed by the wallet.
Calculate Mint Cost in Wei:
ethers.parseUnits()
converts a string or numeric value into a BigInt representing the smallest unit (in this case, wei).This is crucial for Ethereum transactions, which require values in wei rather than ETH.
Call the Mint Function:
mint(tokenId, svgBody, { value: weiCost })
executes the smart contract’smint()
method.The
value
field indicates how much Ether (in wei) to send along with the transaction.Behind the scenes, the wallet signs this transaction, requiring no additional user confirmation beyond providing the private key up front.
Await Transaction Confirmation:
Ethers
wait()
method suspends execution until the transaction is mined on the blockchain.The returned
receipt
includes details like block number, gas used, and transaction status.
Reference Material
Deploy Smart Contract
Explanation
Initialize ContractFactory:
deployData.abi
is the ABI for the contract being deployed.deployData.bytecode
contains the compiled contract bytecode.The
wallet
signer is provided by the user on each request and used to sign the deployment transaction.
Deploy the Contract:
factory.deploy(...)
sends a transaction to create a new contract on-chain.The parameters (
name
,ticker
,supply
,mintPrice
,onlyOwnerMint
) are passed to the constructor of the contract.Under the hood, Ethers automatically calculates the gas limit and constructs the transaction data required for deployment.
Await Mining:
Similar to the mint example, this waits until the deployment transaction is confirmed.
Once confirmed, the contract is live on the blockchain.
Obtain Deployment Details:
getAddress()
retrieves the newly deployed contract address.deploymentTransaction().hash
gives the transaction hash of the deploy transaction.These details can be used for logging, user notifications, or subsequent on-chain interactions.
Reference Material
Why the private key is necessary?
Automatic signing: Without the private key provided in each request, every transaction would require interactive approval from a front-end wallet (e.g., MetaMask). This manual process is not suitable for automated or large-scale operations.
Autonomous execution: Including the private key in the API call allows the service to sign and broadcast transactions programmatically, enabling continuous, unattended minting and deployment.
Security & Custody
User responsibility: The Xyxyx API does not store or persist your private key. You are required to provide the private key each time you make a request. As a result, you have full responsibility for its security.
Protecting your key: If your private key is compromised, unauthorized transactions could be performed on your behalf. Always use secure channels and best practices when transmitting private keys.
No server-side sustody: Since the API does not manage or retain your private key, there is no server-side wallet custody. Every signed transaction is generated by you, on demand, within each request.
Summary
By integrating Ethers v6 and requiring the wallet private key on every request, the Xyxyx API lets you deploy contracts or mint tokens without building a separate front-end signing flow. This approach empowers full autonomy while keeping private key custody in your hands.
Last updated