Prechádzať zdrojové kódy

Upgrade Anchor (#1233)

Signed-off-by: Lucas Steuernagel <lucas.tnagel@gmail.com>
Lucas Steuernagel 2 rokov pred
rodič
commit
58a01d3cd0

+ 1 - 1
Cargo.toml

@@ -51,7 +51,7 @@ solang-parser = { path = "solang-parser", version = "0.2.2" }
 codespan-reporting = "0.11"
 phf = { version = "0.11", features = ["macros"] }
 rust-lapper = "1.1"
-anchor-syn = { version = "0.26", features = ["idl"] }
+anchor-syn = { version = "0.27.0", features = ["idl"] }
 convert_case = "0.6"
 parse-display = "0.8.0"
 parity-scale-codec = "3.3"

+ 1 - 1
integration/anchor/programs/anchor/Cargo.toml

@@ -16,4 +16,4 @@ cpi = ["no-entrypoint"]
 default = []
 
 [dependencies]
-anchor-lang = "0.25.0"
+anchor-lang = "0.27.0"

+ 1 - 4
integration/anchor/tests/anchor.ts

@@ -11,17 +11,14 @@ describe("Anchor", () => {
 
   anchor.setProvider(provider);
 
-  const program = anchor.workspace.Anchor as Program<Anchor>;
-
   it("test anchor program with anchor tests", async () => {
     // The Account to create.
     const seed = Buffer.from('ChiaSeeds', 'utf8');
 
-    const program = anchor.workspace.Anchor;
+    const program = anchor.workspace.Anchor as Program<Anchor>;
 
     const [myAccount, bump] = await anchor.web3.PublicKey.findProgramAddress([seed], program.programId);
 
-    const myPubkey = new anchor.web3.PublicKey("AddressLookupTab1e1111111111111111111111111");
 
     const { SystemProgram } = anchor.web3;
 

+ 0 - 1
src/abi/anchor.rs

@@ -58,7 +58,6 @@ pub fn generate_anchor_idl(contract_no: usize, ns: &Namespace) -> Idl {
         docs,
         constants: vec![],
         instructions,
-        state: None,
         accounts: vec![],
         types: type_manager.generate_custom_idl_types(),
         events,

+ 0 - 4
src/abi/tests.rs

@@ -334,7 +334,6 @@ fn instructions_and_types() {
         Some(IdlType::Defined("multipleReturns_returns".to_string()))
     );
 
-    assert!(idl.state.is_none());
     assert!(idl.accounts.is_empty());
 
     assert_eq!(idl.types.len(), 1);
@@ -452,7 +451,6 @@ contract caller {
     );
     assert!(idl.instructions[1].returns.is_none());
 
-    assert!(idl.state.is_none());
     assert!(idl.accounts.is_empty());
 
     assert_eq!(idl.types.len(), 1);
@@ -606,7 +604,6 @@ fn types() {
         ]
     );
     assert!(idl.instructions[1].returns.is_none());
-    assert!(idl.state.is_none());
     assert!(idl.accounts.is_empty());
     assert!(idl.types.is_empty());
     assert_eq!(
@@ -695,7 +692,6 @@ fn constructor() {
     assert!(idl.instructions[1].args.is_empty());
     assert_eq!(idl.instructions[1].returns, Some(IdlType::U64));
 
-    assert!(idl.state.is_none());
     assert!(idl.accounts.is_empty());
     assert!(idl.types.is_empty());
     assert!(idl.events.is_none());

+ 12 - 17
src/bin/idl/mod.rs

@@ -1,6 +1,6 @@
 // SPDX-License-Identifier: Apache-2.0
 
-use anchor_syn::idl::{Idl, IdlInstruction, IdlType, IdlTypeDefinitionTy};
+use anchor_syn::idl::{Idl, IdlAccountItem, IdlInstruction, IdlType, IdlTypeDefinitionTy};
 use clap::ArgMatches;
 use itertools::Itertools;
 use serde_json::Value as JsonValue;
@@ -230,22 +230,10 @@ fn write_solidity(idl: &Idl, mut f: File) -> Result<(), std::io::Error> {
         .map(|instr| (instr.name.to_string(), instr.name.to_string()))
         .collect::<Vec<(String, String)>>();
 
-    if let Some(state) = &idl.state {
-        state.methods.iter().for_each(|instr| {
-            instruction_names.push((instr.name.to_string(), instr.name.to_string()))
-        });
-    }
-
     rename_keywords(&mut instruction_names);
 
-    if let Some(state) = &idl.state {
-        for instr in &state.methods {
-            instruction(&mut f, instr, true, &instruction_names, &ty_names)?;
-        }
-    }
-
     for instr in &idl.instructions {
-        instruction(&mut f, instr, false, &instruction_names, &ty_names)?;
+        instruction(&mut f, instr, &instruction_names, &ty_names)?;
     }
 
     writeln!(f, "}}")?;
@@ -256,7 +244,6 @@ fn write_solidity(idl: &Idl, mut f: File) -> Result<(), std::io::Error> {
 fn instruction(
     f: &mut File,
     instr: &IdlInstruction,
-    state: bool,
     instruction_names: &[(String, String)],
     ty_names: &[(String, String)],
 ) -> std::io::Result<()> {
@@ -282,7 +269,7 @@ fn instruction(
             .1;
 
         // The anchor discriminator is what Solidity calls a selector
-        let selector = discriminator(if state { "state" } else { "global" }, &instr.name);
+        let selector = discriminator("global", &instr.name);
 
         write!(
             f,
@@ -310,7 +297,8 @@ fn instruction(
             )?;
         }
 
-        write!(f, ") {}external", if state { "" } else { "view " })?;
+        let is_view = instr.returns.is_some() && !mutable_account_exists(&instr.accounts);
+        write!(f, ") {}external", if is_view { "view " } else { "" })?;
 
         if let Some(ty) = &instr.returns {
             writeln!(
@@ -332,6 +320,13 @@ fn instruction(
     Ok(())
 }
 
+fn mutable_account_exists(accounts: &[IdlAccountItem]) -> bool {
+    accounts.iter().any(|item| match item {
+        IdlAccountItem::IdlAccount(acc) => acc.is_mut,
+        IdlAccountItem::IdlAccounts(accs) => mutable_account_exists(&accs.accounts),
+    })
+}
+
 fn docs(f: &mut File, indent: usize, docs: &Option<Vec<String>>) -> std::io::Result<()> {
     if let Some(docs) = docs {
         for doc in docs {