Options
All
  • Public
  • Public/Protected
  • All
Menu

@solana/solidity

@solana/solidity

The Solang Compiler compiles Solidity contracts to native Solana BPF programs.

This TypeScript library, inspired by Ethers.js, can deploy and interact with Solidity contracts on Solana.

Features

  • Compile, load, and deploy Solidity contracts
  • Redeploy and reuse existing contract programs
  • Call contract functions to read and write data
  • Subscribe to contract events and program logs

Quick Setup

This is a short guide to deploying and interacting with the standard ERC20 Solidity contract on Solana.

  1. Install Docker and Node.js (version 14 or higher).

  2. Clone the repositoy.

git clone https://github.com/solana-labs/solana-solidity.js.git
cd solana-solidity.js
  1. Pull the Docker images to compile and deploy your contracts:
yarn docker
  1. Start the Solana test validator:
yarn validator
  1. In a new terminal window, initialize a project:
mkdir -p project/contracts project/build
cd project
curl -o contracts/ERC20.sol \
https://raw.githubusercontent.com/solana-labs/solana-solidity.js/master/test/examples/erc20/contracts/ERC20.sol
  1. Compile the Solidity contract:
docker run --rm -it -v $PWD:/project \
ghcr.io/hyperledger-labs/solang \
-o /project/build --target solana -v /project/contracts/ERC20.sol

This outputs ERC20.abi and bundle.so files to the build directory.

  1. Install the library:
yarn add @solana/solidity

# OR

npm install @solana/solidity
  1. Create a script file to run:
touch erc20.js
  1. Paste this code in the file and save it:
const { Connection, LAMPORTS_PER_SOL, Keypair } = require('@solana/web3.js');
const { Contract, publicKeyToHex } = require('@solana/solidity');
const { readFileSync } = require('fs');

const ERC20_ABI = JSON.parse(readFileSync('./build/ERC20.abi', 'utf8'));
const BUNDLE_SO = readFileSync('./build/bundle.so');

(async function () {
console.log('Connecting to your local Solana node ...');
const connection = new Connection('http://localhost:8899', 'confirmed');

const payer = Keypair.generate();

console.log('Airdropping SOL to a new wallet ...');
const signature = await connection.requestAirdrop(payer.publicKey, 10 * LAMPORTS_PER_SOL);
await connection.confirmTransaction(signature, 'confirmed');

const address = publicKeyToHex(payer.publicKey);
const program = Keypair.generate();
const storage = Keypair.generate();

const contract = new Contract(
connection,
program.publicKey,
storage.publicKey,
ERC20_ABI,
payer
);

console.log('Deploying the Solang-compiled ERC20 program ...');
await contract.load(program, BUNDLE_SO);

console.log('Program deployment finished, deploying the ERC20 contract ...');
await contract.deploy(
'ERC20',
['Solana', 'SOL', '1000000000000000000'],
storage,
4096 * 8
);

console.log('Contract deployment finished, invoking some contract functions ...');
const symbol = await contract.symbol();
const balance = await contract.balanceOf(address);

console.log(`ERC20 contract for ${symbol} deployed!`);
console.log(`Your wallet at ${address} has a balance of ${balance} tokens.`);

contract.addEventListener(function (event) {
console.log(`${event.name} event emitted!`);
console.log(`${event.args[0]} sent ${event.args[2]} tokens to ${event.args[1]}`);
});

console.log('Sending tokens will emit a "Transfer" event ...');
const recipient = Keypair.generate();
await contract.transfer(publicKeyToHex(recipient.publicKey), '1000000000000000000');

process.exit(0);
})();
  1. Run the script to deploy and interact with your contract on Solana!
node erc20.js

Build from source

  1. Clone the project:
git clone https://github.com/solana-labs/solana-solidity.js.git
cd solana-solidity.js
  1. Install the dependencies:
yarn install
  1. Compile the library from TypeScript to JavaScript:
yarn build
  1. Pull the Docker images to build and run the tests:
yarn docker
  1. Start the test validator:
yarn validator
  1. In another terminal window, build and run the tests:
yarn build:test
yarn test

Generated using TypeDoc