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.
- 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
This is a short guide to deploying and interacting with the standard ERC20 Solidity contract on Solana.
git clone https://github.com/solana-labs/solana-solidity.js.git
cd solana-solidity.js
- Pull the Docker images to compile and deploy your contracts:
yarn docker
- Start the Solana test validator:
yarn validator
- 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
- Compile the Solidity contract:
docker run --rm -it -v $PWD:/project \
ghcr.io/hyperledger/solang \
-o /project/build --target solana -v /project/contracts/ERC20.sol
This outputs ERC20.abi
and bundle.so
files to the build
directory.
- Install the library:
yarn add @solana/solidity
# OR
npm install @solana/solidity
- Create a script file to run:
touch erc20.js
- Paste this code in the file and save it:
const { Connection, LAMPORTS_PER_SOL, Keypair } = require('@solana/web3.js');
const { Contract } = 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(recipient.publicKey.toBytes(), 1000000000000000000);
process.exit(0);
})();
- Run the script to deploy and interact with your contract on Solana!
node erc20.js
- Clone the project:
git clone https://github.com/solana-labs/solana-solidity.js.git
cd solana-solidity.js
- Install the dependencies:
yarn install
- Compile the library from TypeScript to JavaScript:
yarn build
- Pull the Docker images to build and run the tests:
yarn docker
- Start the test validator:
yarn validator
- In another terminal window, build and run the tests:
yarn build:test
yarn test