Code Monkey home page Code Monkey logo

solana-solidity.js's Introduction

@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

solana-solidity.js's People

Contributors

jordaaash avatar seanyoung avatar lucasste avatar kelonye avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.