-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
lang, ts: float types support (#1425)
- Loading branch information
1 parent
e7e8777
commit 90bcea1
Showing
19 changed files
with
222 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
[programs.localnet] | ||
floats = "Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS" | ||
|
||
[registry] | ||
url = "https://anchor.projectserum.com" | ||
|
||
[provider] | ||
cluster = "localnet" | ||
wallet = "~/.config/solana/id.json" | ||
|
||
[scripts] | ||
test = "yarn run ts-mocha -p ./tsconfig.json -t 1000000 tests/**/*.ts" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
[workspace] | ||
members = [ | ||
"programs/*" | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"name": "floats", | ||
"version": "0.21.0", | ||
"license": "(MIT OR Apache-2.0)", | ||
"homepage": "https://github.com/project-serum/anchor#readme", | ||
"bugs": { | ||
"url": "https://github.com/project-serum/anchor/issues" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/project-serum/anchor.git" | ||
}, | ||
"engines": { | ||
"node": ">=11" | ||
}, | ||
"scripts": { | ||
"test": "anchor test" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
[package] | ||
name = "floats" | ||
version = "0.1.0" | ||
description = "Created with Anchor" | ||
edition = "2018" | ||
|
||
[lib] | ||
crate-type = ["cdylib", "lib"] | ||
name = "floats" | ||
|
||
[features] | ||
no-entrypoint = [] | ||
no-idl = [] | ||
no-log-ix-name = [] | ||
cpi = ["no-entrypoint"] | ||
default = [] | ||
|
||
[dependencies] | ||
anchor-lang = { path = "../../../../lang" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[target.bpfel-unknown-unknown.dependencies.std] | ||
features = [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
use anchor_lang::prelude::*; | ||
|
||
declare_id!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS"); | ||
|
||
#[program] | ||
pub mod floats { | ||
use super::*; | ||
|
||
pub fn create(ctx: Context<Create>, data_f32: f32, data_f64: f64) -> ProgramResult { | ||
let account = &mut ctx.accounts.account; | ||
let authority = &mut ctx.accounts.authority; | ||
|
||
account.data_f32 = data_f32; | ||
account.data_f64 = data_f64; | ||
account.authority = authority.key(); | ||
|
||
Ok(()) | ||
} | ||
|
||
pub fn update(ctx: Context<Update>, data_f32: f32, data_f64: f64) -> ProgramResult { | ||
let account = &mut ctx.accounts.account; | ||
|
||
account.data_f32 = data_f32; | ||
account.data_f64 = data_f64; | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
#[derive(Accounts)] | ||
pub struct Create<'info> { | ||
#[account(init, payer = authority, space = 8 + 8 + 4 + 32)] | ||
pub account: Account<'info, FloatDataAccount>, | ||
#[account(mut)] | ||
pub authority: Signer<'info>, | ||
pub system_program: Program<'info, System>, | ||
} | ||
|
||
#[derive(Accounts)] | ||
pub struct Update<'info> { | ||
#[account(mut, has_one = authority)] | ||
pub account: Account<'info, FloatDataAccount>, | ||
pub authority: Signer<'info>, | ||
} | ||
|
||
#[account] | ||
pub struct FloatDataAccount { | ||
pub data_f64: f64, | ||
pub data_f32: f32, | ||
pub authority: Pubkey, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import * as anchor from "@project-serum/anchor"; | ||
import { Program, getProvider } from "@project-serum/anchor"; | ||
import { Keypair, SystemProgram } from "@solana/web3.js"; | ||
import { Floats } from "../target/types/floats"; | ||
import assert from "assert"; | ||
|
||
describe("floats", () => { | ||
// Configure the client to use the local cluster. | ||
anchor.setProvider(anchor.Provider.env()); | ||
|
||
const program = anchor.workspace.Floats as Program<Floats>; | ||
|
||
it("Creates an account with float data", async () => { | ||
const accountKeypair = Keypair.generate(); | ||
|
||
await program.methods | ||
.create(1.0, 2.0) | ||
.accounts({ | ||
account: accountKeypair.publicKey, | ||
authority: getProvider().wallet.publicKey, | ||
systemProgram: SystemProgram.programId, | ||
}) | ||
.signers([accountKeypair]) | ||
.rpc(); | ||
|
||
const account = await program.account.floatDataAccount.fetch( | ||
accountKeypair.publicKey | ||
); | ||
|
||
assert.strictEqual(account.dataF32, 1.0); | ||
assert.strictEqual(account.dataF64, 2.0); | ||
}); | ||
|
||
it("Updates an account with float data", async () => { | ||
const accountKeypair = Keypair.generate(); | ||
const authorityPublicKey = getProvider().wallet.publicKey; | ||
|
||
await program.methods | ||
.create(1.0, 2.0) | ||
.accounts({ | ||
account: accountKeypair.publicKey, | ||
authority: authorityPublicKey, | ||
systemProgram: SystemProgram.programId, | ||
}) | ||
.signers([accountKeypair]) | ||
.rpc(); | ||
|
||
let account = await program.account.floatDataAccount.fetch( | ||
accountKeypair.publicKey | ||
); | ||
|
||
await program.methods | ||
.update(3.0, 4.0) | ||
.accounts({ | ||
account: accountKeypair.publicKey, | ||
authority: authorityPublicKey, | ||
}) | ||
.rpc(); | ||
|
||
account = await program.account.floatDataAccount.fetch( | ||
accountKeypair.publicKey | ||
); | ||
|
||
assert.strictEqual(account.dataF32, 3.0); | ||
assert.strictEqual(account.dataF64, 4.0); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"compilerOptions": { | ||
"types": ["mocha", "chai"], | ||
"typeRoots": ["./node_modules/@types"], | ||
"lib": ["es2015"], | ||
"module": "commonjs", | ||
"target": "es6", | ||
"esModuleInterop": true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ | |
"errors", | ||
"escrow", | ||
"events", | ||
"floats", | ||
"ido-pool", | ||
"interface", | ||
"lockup", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters