Deploy a smart contract

Here is an example of how to deploy a simple storage contract, so you can get and set an arbitrary value at a contract address using the Radius SDKs.

import {
  ABI,
  Account,
  BytecodeFromHex,
  Client,
  Contract,
  NewAccount,
  NewClient,
  withPrivateKey,
} from '@radiustechsystems/sdk';

// Replace the following values with your own
const RADIUS_ENDPOINT = "";
const PRIVATE_KEY = "";

async function main() {
  const client: Client = await NewClient(RADIUS_ENDPOINT);
  const account: Account = await NewAccount(withPrivateKey(PRIVATE_KEY, client));

  const bytecode = BytecodeFromHex("6080604052348015600e575f5ffd5b5060a580601a5f395ff3fe6080604052348015600e575f5ffd5b50600436106030575f3560e01c806360fe47b11460345780636d4ce63c146045575b5f5ffd5b6043603f3660046059565b5f55565b005b5f5460405190815260200160405180910390f35b5f602082840312156068575f5ffd5b503591905056fea26469706673582212207655d86666fa8aa75666db8416e0f5db680914358a57e84aa369d9250218247f64736f6c634300081c0033")
  const abi = new ABI("[{\"inputs\":[],\"name\":\"get\",\"outputs\":[{\"internalType\":\"uint256\",\"name\":\"\",\"type\":\"uint256\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"uint256\",\"name\":\"x\",\"type\":\"uint256\"}],\"name\":\"set\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"}]")
  const contract: Contract = await client.deployContract(account.signer, bytecode, abi)

  console.log(`Contract address: ${contract.address().hex()}`);

  const receipt = await contract.execute(client, account.signer, 'set', BigInt(42));
  console.log(`Set method receipt: ${receipt.txHash.hex()}`);

  const result = await contract.call(client, 'get');
  console.log(`Get method result: ${result}`);
}

main().catch(console.error);

Here is the simple storage smart contract, written using the Solidity programming language:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract SimpleStorage {
    uint256 private storedData;

    function set(uint256 x) public {
        storedData = x;
    }

    function get() public view returns (uint256) {
        return storedData;
    }
}

The ABI and bytecode in the code examples were generated by compiling the Solidity code using solc-js, using this command:

solcjs SimpleStorage.sol --output-dir . --abi --bin --optimize --base-path . --include-path ./node_modules

Last updated

Was this helpful?