ArweaveKit Docs
Ask or search…
K
Comment on page

Create Contract

Create a smart contract on Arweave
The createContract function creates a contract based on the input parameters. The contract can be created either on the mainnet or on a testnet/ local network for testing purposes.

Basic Syntax

The function is called as follows:
import { createContract } from 'arweavekit/contract'
const contract = await createContract({params});

Input Parameters

The following params are available for this function and they must be passed in as an object:
  • wallet: ArWallet | string (optional) : The wallet for deploying the contract with. The type ArWallet basically checks for Arweave wallet of the type JWKInterface and type String expects the private key of the Ethereum wallet and if the wallet param is not passed, it falls back to check for a web-based wallet like Metamask or ArConnect. When falling back to a web wallet, an Ethereum Wallet (like Metamask) is triggered since contract functions use bundling by default, which supports EVM web wallets. If this attempt fails, the function will retry with an Arweave Web Wallet (like ArConnect or arweave.app). The wallet key file can be loaded as follows and passed to the wallet param:
import { readFileSync } from 'fs';
// Arweave JWK
const key = JSON.parse(readFileSync('wallet.json').toString());
// Ethereum Private Key
const key = readFileSync('privatekey.txt').toString();
// Now pass key to wallet param as wallet: key
// For passing to wallet param using the arweave.app wallet
import { ArweaveWebWallet } from "arweave-wallet-connector";
const webWallet = new ArweaveWebWallet();
webWallet.setUrl("arweave.app");
await webWallet.connect();
// Now pass webWallet to wallet param as wallet: webWallet
Arweave JWK or Ethereum Private key is needed as a wallet parameter in the node environment. And, the wallet param needed not to be passed in the browser environment for use with Arconnect or an Ethereum-based wallet (like Metamask).
In order to explicitly use an Arweave Web Wallet, use_wallet must be passed into the wallet param, which also disables bundling in order to successfully process the function call.
For deploying a contract on mainnet please ensure the wallet of choice has funds.
  • initialState: string : The initial state of information for the contract at the time of deployment in string format. The initState file can be passed in directly as a string or read from a file as follows:
import { readFileSync } from 'fs';
const initState = readFileSync('ABSOLUTE_FILE_PATH', 'utf-8');
  • contractSource: string | Buffer : The contract source parameter can either be the source code or a transaction ID to an existing source code. Contract data (logic) read from a file that must be encoded in utf-8 a format that returns a string. The contract source file can be loaded or it can be a contract source transaction ID as follows:
    // Load contract source code
    const contractSource = readFileSync('ABSOLUTE_FILE_PATH', 'utf-8');
    // Or, it can be a contract source transaction ID
    const contractSource = "Of9pi--Gj7hCTawhgxOwbuWnFI1h24TTgO5pw8ENJNQ"
  • tags: array (optional) : Tags can be added to any transaction for ease of indexing and querying. The syntax for adding tags is as follows:
    tags: [
    { name: "key_name1", value: "some_value1" },
    { name: "key_name2", value: "some_value2" },
    ]
    The name is the key for a given value that can be used for querying the transaction in the future. By default, the library adds the 'ArweaveKit' : '1.5.1' tag on the backend to help identify the library and version used to deploy the function.
  • data: object (optional): It accepts Content-Type of the data and the body of the data so the data can be accessed with the same deployed contract transaction ID making it an atomic asset.
    data: {
    'Content-Type': 'text/html',
    body: '<p>Hello World!</p>',
    }
  • evaluationManifest : object (optional): The evaluation manifest defines rules that must be used when evaluating the contract being deployed. This includes evaluation options as well as any plugins that the contract deployment may need. Read more about evaluation options and contract manifests to learn about the available configurations and their uses. Learn more about plugins here.
  • environment: 'local' | 'testnet' | 'mainnet' : The environment of choice to create the smart contract in. The testnet is a pseudo-testing environment created on top of the mainnet with the help of custom tags.
An arlocal instance must be running on the port 1984 for the function to work with the local environment. To create one, simply run npx arlocal in the command line. Learn more about arlocal here.
  • strategy : 'arweave' | 'ethereum' | 'both' (optional) : The strategy specifies the wallet (Ethereum or Arweave) to be used for contract creation. While the wallet can be determined by the wallet param passed in, it can be beneficial to pass in when using the browser environment. However, by default, the strategy is set to 'both' and thus tries the function call with Ethereum first and fallback on an Arweave Wallet.
Example
const contract = await createContract({
wallet: wallet_key,
initialState: initState,
contractSource: contractSource,
environment: 'local',
});
This uploads the contract logic on the local network along with the initial state with the help of the wallet provided.

Returned Data

The function call returns the following data:
{
status: {
code: 200,
message: 'SUCCESSFUL',
},
contract: object,
contractTxId: 'CONTRACT_TRANSACTION_ID',
}
  • contract: ContractDeploy : The contract object has relevant transaction IDs for future interactions. Read more about it here.
  • contractTxId: string : The contractTxId is a unique identifier for further interactions with the contract like read and write operations. It is an instance of the contract source that is uploaded on the network.
  • status: object : The result object returns easy to understand information showing the status of the contract deployment. code: 200 and message: 'SUCCESSFUL' indicate that the contract has been successfully deployed to the network.