forked from coral-xyz/anchor
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add has_one relations inference so you don't need to pass accou…
…nts that are referenced by a has_one (coral-xyz#2160)
- Loading branch information
1 parent
5de084f
commit d7cb300
Showing
20 changed files
with
380 additions
and
9 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
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,19 @@ | ||
use crate::Field; | ||
use syn::Expr; | ||
|
||
pub fn parse(acc: &Field, seeds_feature: bool) -> Vec<String> { | ||
if !seeds_feature { | ||
return vec![]; | ||
} | ||
acc.constraints | ||
.has_one | ||
.iter() | ||
.flat_map(|s| match &s.join_target { | ||
Expr::Path(path) => path.path.segments.first().map(|l| l.ident.to_string()), | ||
_ => { | ||
println!("WARNING: unexpected seed: {:?}", s); | ||
None | ||
} | ||
}) | ||
.collect() | ||
} |
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,15 @@ | ||
[features] | ||
seeds = true | ||
|
||
[provider] | ||
cluster = "localnet" | ||
wallet = "~/.config/solana/id.json" | ||
|
||
[programs.localnet] | ||
relations_derivation = "Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS" | ||
|
||
[workspace] | ||
members = ["programs/relations-derivation"] | ||
|
||
[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,22 @@ | ||
// 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. | ||
async function deployAsync(exampleString: string): Promise<void> { | ||
return new Promise((resolve) => { | ||
setTimeout(() => { | ||
console.log(exampleString); | ||
resolve(); | ||
}, 2000); | ||
}); | ||
} | ||
|
||
await deployAsync("Typescript migration example complete."); | ||
}; |
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": "relations-derivation", | ||
"version": "0.25.0", | ||
"license": "(MIT OR Apache-2.0)", | ||
"homepage": "https://github.com/coral-xyz/anchor#readme", | ||
"bugs": { | ||
"url": "https://github.com/coral-xyz/anchor/issues" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/coral-xyz/anchor.git" | ||
}, | ||
"engines": { | ||
"node": ">=11" | ||
}, | ||
"scripts": { | ||
"test": "anchor test" | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
tests/relations-derivation/programs/relations-derivation/Cargo.toml
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 = "relations-derivation" | ||
version = "0.1.0" | ||
description = "Created with Anchor" | ||
rust-version = "1.56" | ||
edition = "2021" | ||
|
||
[lib] | ||
crate-type = ["cdylib", "lib"] | ||
name = "relations_derivation" | ||
|
||
[features] | ||
no-entrypoint = [] | ||
no-idl = [] | ||
cpi = ["no-entrypoint"] | ||
default = [] | ||
|
||
[dependencies] | ||
anchor-lang = { path = "../../../../lang" } |
2 changes: 2 additions & 0 deletions
2
tests/relations-derivation/programs/relations-derivation/Xargo.toml
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 = [] |
68 changes: 68 additions & 0 deletions
68
tests/relations-derivation/programs/relations-derivation/src/lib.rs
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,68 @@ | ||
//! The typescript example serves to show how one would setup an Anchor | ||
//! workspace with TypeScript tests and migrations. | ||
|
||
use anchor_lang::prelude::*; | ||
|
||
declare_id!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS"); | ||
|
||
#[program] | ||
pub mod relations_derivation { | ||
use super::*; | ||
|
||
pub fn init_base(ctx: Context<InitBase>) -> Result<()> { | ||
ctx.accounts.account.my_account = ctx.accounts.my_account.key(); | ||
ctx.accounts.account.bump = ctx.bumps["account"]; | ||
Ok(()) | ||
} | ||
pub fn test_relation(_ctx: Context<TestRelation>) -> Result<()> { | ||
Ok(()) | ||
} | ||
} | ||
|
||
#[derive(Accounts)] | ||
pub struct InitBase<'info> { | ||
/// CHECK: yeah I know | ||
#[account(mut)] | ||
my_account: Signer<'info>, | ||
#[account( | ||
init, | ||
payer = my_account, | ||
seeds = [b"seed"], | ||
space = 100, | ||
bump, | ||
)] | ||
account: Account<'info, MyAccount>, | ||
system_program: Program<'info, System> | ||
} | ||
|
||
#[derive(Accounts)] | ||
pub struct Nested<'info> { | ||
/// CHECK: yeah I know | ||
my_account: UncheckedAccount<'info>, | ||
#[account( | ||
has_one = my_account, | ||
seeds = [b"seed"], | ||
bump = account.bump | ||
)] | ||
account: Account<'info, MyAccount>, | ||
} | ||
|
||
#[derive(Accounts)] | ||
pub struct TestRelation<'info> { | ||
/// CHECK: yeah I know | ||
my_account: UncheckedAccount<'info>, | ||
#[account( | ||
has_one = my_account, | ||
seeds = [b"seed"], | ||
bump = account.bump | ||
)] | ||
account: Account<'info, MyAccount>, | ||
nested: Nested<'info>, | ||
} | ||
|
||
|
||
#[account] | ||
pub struct MyAccount { | ||
pub my_account: Pubkey, | ||
pub bump: u8 | ||
} |
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,43 @@ | ||
import * as anchor from "@project-serum/anchor"; | ||
import { AnchorProvider, Program } from "@project-serum/anchor"; | ||
import { PublicKey } from "@solana/web3.js"; | ||
import { expect } from "chai"; | ||
import { RelationsDerivation } from "../target/types/relations_derivation"; | ||
|
||
describe("typescript", () => { | ||
// Configure the client to use the local cluster. | ||
anchor.setProvider(anchor.AnchorProvider.env()); | ||
|
||
const program = anchor.workspace | ||
.RelationsDerivation as Program<RelationsDerivation>; | ||
const provider = anchor.getProvider() as AnchorProvider; | ||
|
||
it("Inits the base account", async () => { | ||
await program.methods | ||
.initBase() | ||
.accounts({ | ||
myAccount: provider.wallet.publicKey, | ||
}) | ||
.rpc(); | ||
}); | ||
|
||
it("Derives relationss", async () => { | ||
const tx = await program.methods.testRelation().accounts({ | ||
nested: { | ||
account: ( | ||
await PublicKey.findProgramAddress( | ||
[Buffer.from("seed", "utf-8")], | ||
program.programId | ||
) | ||
)[0], | ||
}, | ||
}); | ||
|
||
await tx.instruction(); | ||
const keys = await tx.pubkeys(); | ||
|
||
expect(keys.myAccount.equals(provider.wallet.publicKey)).is.true; | ||
|
||
await tx.rpc(); | ||
}); | ||
}); |
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,11 @@ | ||
{ | ||
"compilerOptions": { | ||
"types": ["mocha", "chai"], | ||
"typeRoots": ["./node_modules/@types"], | ||
"lib": ["es2015"], | ||
"module": "commonjs", | ||
"target": "es6", | ||
"esModuleInterop": true, | ||
"skipLibCheck": 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
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
Oops, something went wrong.