Ver código fonte

Merge pull request #3 from cdhiraj40/hello-solana-seahorse

added hello solana program using seahorse lang
Joe Caulfield 3 anos atrás
pai
commit
f6714dca05

+ 7 - 0
basics/hello-solana/seahorse/hello_solana/.gitignore

@@ -0,0 +1,7 @@
+
+../../../../.anchor
+.DS_Store
+target
+**/*.rs.bk
+node_modules
+test-ledger

+ 8 - 0
basics/hello-solana/seahorse/hello_solana/.prettierignore

@@ -0,0 +1,8 @@
+
+.anchor
+.DS_Store
+target
+node_modules
+dist
+build
+test-ledger

+ 14 - 0
basics/hello-solana/seahorse/hello_solana/Anchor.toml

@@ -0,0 +1,14 @@
+[features]
+seeds = true
+[programs.localnet]
+hello_solana = "Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS"
+
+[registry]
+url = "https://anchor.projectserum.com"
+
+[provider]
+cluster = "localnet"
+wallet = "/home/thefunnyintrovert/.config/solana/id.json"
+
+[scripts]
+test = "yarn run ts-mocha -p ./tsconfig.json -t 1000000 tests/**/*.ts"

+ 4 - 0
basics/hello-solana/seahorse/hello_solana/Cargo.toml

@@ -0,0 +1,4 @@
+[workspace]
+members = [
+    "programs/*"
+]

+ 5 - 0
basics/hello-solana/seahorse/hello_solana/README.md

@@ -0,0 +1,5 @@
+# hello_solana
+
+This project was created by Seahorse 0.1.5.
+
+To get started, just add your code to **programs_py/hello_solana.py** and run `seahorse build`.

+ 12 - 0
basics/hello-solana/seahorse/hello_solana/migrations/deploy.ts

@@ -0,0 +1,12 @@
+// Migrations are an early feature. Currently, they're nothing more than this
+// single deploy script that's invoked from the CLI, injecting a provider
+// configured from the workspace's Anchor.toml.
+
+const anchor = require("@project-serum/anchor");
+
+module.exports = async function (provider) {
+  // Configure client to use the provider.
+  anchor.setProvider(provider);
+
+  // Add your deploy script here.
+};

+ 19 - 0
basics/hello-solana/seahorse/hello_solana/package.json

@@ -0,0 +1,19 @@
+{
+    "scripts": {
+        "lint:fix": "prettier */*.js \"*/**/*{.js,.ts}\" -w",
+        "lint": "prettier */*.js \"*/**/*{.js,.ts}\" --check"
+    },
+    "dependencies": {
+        "@project-serum/anchor": "^0.24.2"
+    },
+    "devDependencies": {
+        "@types/bn.js": "^5.1.0",
+        "@types/chai": "^4.3.0",
+        "@types/mocha": "^9.0.0",
+        "chai": "^4.3.4",
+        "mocha": "^9.0.3",
+        "prettier": "^2.6.2",
+        "ts-mocha": "^10.0.0",
+        "typescript": "^4.3.5"
+    }
+}

+ 23 - 0
basics/hello-solana/seahorse/hello_solana/programs/hello_solana/Cargo.toml

@@ -0,0 +1,23 @@
+[package]
+name = "hello_solana"
+version = "0.1.0"
+description = "Created with Anchor"
+edition = "2021"
+
+[lib]
+crate-type = ["cdylib", "lib"]
+name = "hello_solana"
+
+[features]
+no-entrypoint = []
+no-idl = []
+no-log-ix-name = []
+cpi = ["no-entrypoint"]
+default = []
+
+[profile.release]
+overflow-checks = true
+
+[dependencies]
+anchor-lang = "0.24.2"
+anchor-spl = "0.24.2"

+ 2 - 0
basics/hello-solana/seahorse/hello_solana/programs/hello_solana/Xargo.toml

@@ -0,0 +1,2 @@
+[target.bpfel-unknown-unknown.dependencies.std]
+features = []

+ 35 - 0
basics/hello-solana/seahorse/hello_solana/programs/hello_solana/src/lib.rs

@@ -0,0 +1,35 @@
+use anchor_lang::prelude::*;
+use anchor_lang::solana_program;
+use anchor_spl::associated_token;
+use anchor_spl::token;
+use std::convert::TryFrom;
+
+declare_id!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS");
+
+pub fn hello_handler(mut ctx: Context<Hello>) -> Result<()> {
+    let mut signer = &mut ctx.accounts.signer;
+
+    msg!("{}", "Hello, Solana from Seahorse!");
+
+    msg!(
+        "{}",
+        format!("This is the public key of the signer: {}", signer.key())
+    );
+
+    Ok(())
+}
+
+#[derive(Accounts)]
+pub struct Hello<'info> {
+    #[account(mut)]
+    pub signer: Signer<'info>,
+}
+
+#[program]
+pub mod hello_solana {
+    use super::*;
+
+    pub fn hello(ctx: Context<Hello>) -> Result<()> {
+        hello_handler(ctx)
+    }
+}

+ 13 - 0
basics/hello-solana/seahorse/hello_solana/programs_py/hello_solana.py

@@ -0,0 +1,13 @@
+# hello_solana
+# Built with Seahorse v0.1.5
+
+from seahorse.prelude import *
+
+declare_id('Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS')
+
+
+@instruction
+def hello(signer: Signer):
+    print("Hello, Solana from Seahorse!")
+
+    print(f"This is the public key of the signer: {signer.key()}")

+ 0 - 0
basics/hello-solana/seahorse/hello_solana/programs_py/seahorse/__init__.py


+ 330 - 0
basics/hello-solana/seahorse/hello_solana/programs_py/seahorse/prelude.py

@@ -0,0 +1,330 @@
+# seahorse.prelude: the basis for writing Seahorse programs.
+#
+# NOTE: this file just contains types and documentation for your editor. This
+# is NOT executable code, and you won't be able to change the behavior of your
+# Seahorse programs by editing this file.
+
+from typing import *
+from math import floor, ceil
+
+T = TypeVar('T')
+N = TypeVar('N')
+
+
+# ===========================================================
+# Internal types - here for completeness, but not really used
+# ===========================================================
+    
+class ProgramResult:
+    """Result from executing an instruction - either a success, or a failure with an error message."""
+
+
+# ==========
+# Rust types
+# ==========
+
+class u8:
+    """Single-byte unsigned integer."""
+
+    def __init__(self, _: Any):
+        return self
+
+    def __add__(self, _: Any):
+        return self
+
+    def __radd__(self, _: Any):
+        return self
+
+    def __iadd__(self, _: Any):
+        return self
+
+    def __sub__(self, _: Any):
+        return self
+
+    def __rsub__(self, _: Any):
+        return self
+
+    def __isub__(self, _: Any):
+        return self
+
+    def __mul__(self, _: Any):
+        return self
+
+    def __rmul__(self, _: Any):
+        return self
+
+    def __imul__(self, _: Any):
+        return self
+
+    def __div__(self, _: Any):
+        return self
+
+    def __rdiv__(self, _: Any):
+        return self
+
+    def __idiv__(self, _: Any):
+        return self
+
+class u64:
+    """64-bit unsigned integer."""
+
+    def __init__(self, _: Any):
+        return self
+
+    def __add__(self, _: Any):
+        return self
+
+    def __radd__(self, _: Any):
+        return self
+
+    def __iadd__(self, _: Any):
+        return self
+
+    def __sub__(self, _: Any):
+        return self
+
+    def __rsub__(self, _: Any):
+        return self
+
+    def __isub__(self, _: Any):
+        return self
+
+    def __mul__(self, _: Any):
+        return self
+
+    def __rmul__(self, _: Any):
+        return self
+
+    def __imul__(self, _: Any):
+        return self
+
+    def __div__(self, _: Any):
+        return self
+
+    def __rdiv__(self, _: Any):
+        return self
+
+    def __idiv__(self, _: Any):
+        return self
+
+class i64:
+    """64-bit signed integer."""
+
+    def __init__(self, _: Any):
+        return self
+
+    def __add__(self, _: Any):
+        return self
+
+    def __radd__(self, _: Any):
+        return self
+
+    def __iadd__(self, _: Any):
+        return self
+
+    def __sub__(self, _: Any):
+        return self
+
+    def __rsub__(self, _: Any):
+        return self
+
+    def __isub__(self, _: Any):
+        return self
+
+    def __mul__(self, _: Any):
+        return self
+
+    def __rmul__(self, _: Any):
+        return self
+
+    def __imul__(self, _: Any):
+        return self
+
+    def __div__(self, _: Any):
+        return self
+
+    def __rdiv__(self, _: Any):
+        return self
+
+    def __idiv__(self, _: Any):
+        return self
+
+class f64:
+    """64-bit floating point number."""
+
+    def __add__(self, _: Any):
+        return self
+
+    def __radd__(self, _: Any):
+        return self
+
+    def __iadd__(self, _: Any):
+        return self
+
+    def __sub__(self, _: Any):
+        return self
+
+    def __rsub__(self, _: Any):
+        return self
+
+    def __isub__(self, _: Any):
+        return self
+
+    def __mul__(self, _: Any):
+        return self
+
+    def __rmul__(self, _: Any):
+        return self
+
+    def __imul__(self, _: Any):
+        return self
+
+    def __div__(self, _: Any):
+        return self
+
+    def __rdiv__(self, _: Any):
+        return self
+
+    def __idiv__(self, _: Any):
+        return self
+
+
+class Array(Generic[T, N]):
+    """A fixed-length array: contains type T and has size N.
+
+    Lists (Python builtin type) can coerce to this type. Example:
+
+    ```
+    class MyData(Account):
+        data: Array[u64, 4]
+
+    @instruction
+    def set_data(my_data: MyData):
+        # Will successfully set `data` to [0, 1, 2, 3]
+        my_data.data = [i for i in range(0, 4)]
+        # Will attempt (and fail, crashing the instruction at runtime!) to set `data` to [0, 1, 2, 3, 4]
+        my_data.data = [i for i in range(0, 5)]
+    ```
+    """
+
+class Enum:
+    """A type that can have one of multiple named values.
+
+    Note that unlike Rust enums, these cannot contain any data (other than the variant itself). Example:
+
+    ```
+    class MyEnum(Enum):
+        ONE = 1
+        TWO = 2
+        THREE = 3
+
+    @instruction
+    def use_enum(code: MyEnum):
+        if code == MyEnum.ONE:
+            print(1)
+        # ...
+    ```
+    """
+
+# ============
+# Solana types
+# ============
+
+class Pubkey:
+    """32-byte account identifier."""
+
+class SolanaAccount:
+    """Generic Solana account."""
+
+    def key(self) -> Pubkey:
+        """Get this account's key."""
+
+    def transfer_lamports(self, to: SolanaAccount, amount: u64):
+        """Transfer some SOL (as an amount of lamports) to another account.
+
+        Note: this will successfully transfer from a program-owned account without needing to
+        provide the seeds for a PDA, so no signer field is required (unlike the SPL methods).
+        """
+
+class Account(SolanaAccount):
+    """User-defined Solana account."""
+
+class Signer(SolanaAccount):
+    """Instruction signer."""
+
+class Empty(Generic[T]):
+    """An account that needs to be initialized."""
+
+    def bump(self) -> u8:
+        """Get this account's bump, needed if you want to use this account to sign CPI calls."""
+
+    def init(self, payer: Signer, seeds: List[Union[str, Account, u8]], mint: TokenMint, authority: Account) -> T:
+        """
+        Initialize the account.
+        
+        @param payer: The account that will pay for the rent cost of the initialized account. Must be an instruction signer.
+        @param seeds: A list of parameters to uniquely identify this account among all accounts created by your program. These may be string literals or other accounts.
+        @param mint: If initializing a TokenAccount, this is the mint that the account belongs to.
+        @param decimals: If initializing a TokenMint, this is the number of decimals the new token has.
+        @param authority: If initializing a TokenAccount/TokenMint, this is the account that has authority over the account.
+        @returns: The new, initialized account. All of the data in this account will be set to 0.
+        """
+
+class TokenAccount(SolanaAccount):
+    """SPL token account."""
+
+    def authority(self) -> Pubkey:
+        """Get the owner of this token account."""
+
+    def amount(self) -> u64:
+        """Get the amount of token stored in this account."""
+
+    def transfer(self, authority: SolanaAccount, to: TokenAccount, amount: u64, signer: List[Union[str, Account, u8]] = None):
+        """
+        Transfer funds from this SPL token account to another.
+        
+        @param authority: The account that owns this TokenAccount. Must be an instruction signer or the account given by the `signer` param.
+        @param to: The recipient TokenAccount.
+        @param amount: How much (in *native* token units) to transfer.
+        @param signer: (Optional) seeds for the signature of a PDA.
+        """
+
+class TokenMint(SolanaAccount):
+    """SPL token mint."""
+
+    def authority(self) -> Pubkey:
+        """Get the owner of this token account."""
+
+    def mint(self, authority: SolanaAccount, to: TokenAccount, amount: u64, signer: List[Union[str, Account, u8]] = None):
+        """
+        Mint new tokens to a token account.
+
+        @param authority: The account that owns this TokenMint. Must be an instruction signer or the account given by the `signer` param.
+        @param to: The recipient TokenAccount.
+        @param amount: How much (in *native* token units) to mint.
+        @param signer: (Optional) seeds for the signature of a PDA.
+        """
+
+    def burn(self, authority: SolanaAccount, holder: TokenAccount, amount: u64, signer: List[Union[str, Account, u8]] = None):
+        """
+        Burn tokens from a token account.
+
+        @param authority: The account that owns the `holder` TokenAccount. Must be an instruction signer or the account given by the `signer` param.
+        @param holder: The TokenAccount to burn from.
+        @param amount: How much (in *native* token units) to burn.
+        @param signer: (Optional) seeds for the signature of a PDA.
+        """
+
+
+# ================
+# Helper functions
+# ================
+
+def declare_id(id: str):
+    """Inform Anchor what this program's ID is.
+
+    @param id: The program's ID, generated by Anchor in /target/idl/<program>.json. This must be copied-pasted straight from there as a string literal.
+    """
+
+def instruction(function: Callable[..., None]) -> Callable[..., ProgramResult]:
+    """Decorator to turn a function into a program instruction."""

+ 23 - 0
basics/hello-solana/seahorse/hello_solana/tests/hello_solana.ts

@@ -0,0 +1,23 @@
+import * as anchor from "@project-serum/anchor";
+import { HelloSolana } from "../target/types/hello_solana";
+
+describe("hello_solana", () => {
+
+  // Configure the client to use the local cluster.
+  const provider = anchor.AnchorProvider.env();
+  anchor.setProvider(provider);
+  const program = anchor.workspace.HelloSolana as anchor.Program<HelloSolana>;
+  const payer = provider.wallet as anchor.Wallet;
+
+  it("Say hello!", async () => {
+
+    // Just run Anchor's IDL method to build a transaction
+    // and sign it via a signer.
+    await program.methods.hello()
+        .accounts({
+          signer: provider.wallet.publicKey,
+        })
+        .signers([payer.payer])
+        .rpc();
+  });
+});

+ 10 - 0
basics/hello-solana/seahorse/hello_solana/tsconfig.json

@@ -0,0 +1,10 @@
+{
+  "compilerOptions": {
+    "types": ["mocha", "chai"],
+    "typeRoots": ["./node_modules/@types"],
+    "lib": ["es2015"],
+    "module": "commonjs",
+    "target": "es6",
+    "esModuleInterop": true
+  }
+}