Skip to content

Commit

Permalink
Bump anchor and borsh dependencies
Browse files Browse the repository at this point in the history
Signed-off-by: Sean Young <sean@mess.org>
  • Loading branch information
seanyoung committed Oct 18, 2023
1 parent 8e23aed commit 0740817
Show file tree
Hide file tree
Showing 17 changed files with 134 additions and 64 deletions.
7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ solang-parser = { path = "solang-parser", version = "0.3.1" }
codespan-reporting = "0.11"
phf = { version = "0.11", features = ["macros"] }
rust-lapper = "1.1"
anchor-syn = { version = "0.28.0", features = ["idl"] }
anchor-syn = { version = "0.29.0", features = ["idl-build"] }
convert_case = "0.6"
parse-display = "0.8.0"
parity-scale-codec = "3.4"
Expand All @@ -60,7 +60,7 @@ scale-info = "2.4"
petgraph = "0.6.3"
wasmparser = "0.110.0"
wasm-encoder = "0.31"
toml = "0.7"
toml = "0.8"
wasm-opt = { version = "0.112.0", optional = true }
contract-build = { version = "3.0.1", optional = true }
primitive-types = { version = "0.12", features = ["codec"] }
Expand All @@ -80,7 +80,8 @@ ed25519-dalek = { version = "2", features = ["rand_core"] }
path-slash = "0.2"
pretty_assertions = "1.3"
byte-slice-cast = "1.2"
borsh = "0.10"
borsh = "1.1"
borsh-derive = "1.1"
rayon = "1"
walkdir = "2.3.3"
ink_primitives = "4.2.0"
Expand Down
2 changes: 1 addition & 1 deletion integration/anchor/programs/anchor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ cpi = ["no-entrypoint"]
default = []

[dependencies]
anchor-lang = "0.28.0"
anchor-lang = "0.29.0"
solana-program = "1.16.1"
15 changes: 15 additions & 0 deletions integration/anchor/programs/anchor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@ pub mod anchor {
})
}

pub fn test_event(_ctx: Context<State>) -> Result<()> {
emit!(MyEvent {
data: 6,
label: "bye".to_string(),
});
Ok(())
}

pub fn multi_dimensional(
_ctx: Context<NoAccountsNeeded>,
arr: [[u16; 3]; 4],
Expand Down Expand Up @@ -181,3 +189,10 @@ pub struct State<'info> {
#[account()]
pub my_account: Account<'info, MyAccount>,
}

#[event]
pub struct MyEvent {
pub data: i64,
#[index]
pub label: String,
}
4 changes: 4 additions & 0 deletions integration/anchor/tests/call_anchor.sol
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,8 @@ contract call_anchor {
require(ret8[1][2] == 8000, "array 2");
require(ret8[2][0] == 3000, "array 3");
}

function test_event() public {
emit MyEvent({data: 102, label: "yadayada" });
}
}
20 changes: 19 additions & 1 deletion integration/anchor/tests/call_anchor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import expect from 'expect';
import { AnchorProvider, Program } from '@coral-xyz/anchor';
import {
PublicKey, AccountMeta,
Keypair, Signer,
Keypair,
Connection,
LAMPORTS_PER_SOL,
BpfLoader, Transaction,
Expand Down Expand Up @@ -69,6 +69,24 @@ describe('Call Anchor program from Solidity via IDL', () => {
.remainingAccounts(remainingAccounts)
.signers([data, payer])
.rpc();

let seen = false;

const listenId = program.addEventListener("MyEvent", (ev) => {
expect(ev.data.toNumber()).toBe(102);
expect(ev.label).toBe("yadayada");
seen = true;
});

await program.methods.testEvent()
.accounts({
dataAccount: storage.publicKey,
})
.rpc();

program.removeEventListener(listenId);

expect(seen).toBe(true);
});
});

Expand Down
6 changes: 5 additions & 1 deletion src/abi/anchor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use crate::sema::ast::{
ArrayLength, Contract, Function, Namespace, Parameter, StructDecl, StructType, Tag, Type,
};
use anchor_syn::idl::{
use anchor_syn::idl::types::{
Idl, IdlAccount, IdlAccountItem, IdlEnumVariant, IdlEvent, IdlEventField, IdlField,
IdlInstruction, IdlType, IdlTypeDefinition, IdlTypeDefinitionTy,
};
Expand Down Expand Up @@ -243,6 +243,7 @@ impl TypeManager<'_> {
func.name
)]),
ty: IdlTypeDefinitionTy::Struct { fields },
generics: None,
});

IdlType::Defined(name)
Expand Down Expand Up @@ -313,6 +314,7 @@ impl TypeManager<'_> {
name,
docs,
ty: IdlTypeDefinitionTy::Struct { fields },
generics: None,
});
}

Expand All @@ -337,6 +339,7 @@ impl TypeManager<'_> {
name,
docs: item.docs,
ty: item.ty,
generics: None,
});
}

Expand Down Expand Up @@ -372,6 +375,7 @@ impl TypeManager<'_> {
name,
docs,
ty: IdlTypeDefinitionTy::Enum { variants },
generics: None,
});
}

Expand Down
35 changes: 24 additions & 11 deletions src/abi/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::codegen::{codegen, Options};
use crate::file_resolver::FileResolver;
use crate::sema::ast::Namespace;
use crate::{codegen, parse_and_resolve, Target};
use anchor_syn::idl::{
use anchor_syn::idl::types::{
IdlAccount, IdlAccountItem, IdlEnumVariant, IdlEvent, IdlEventField, IdlField, IdlType,
IdlTypeDefinition, IdlTypeDefinitionTy,
};
Expand Down Expand Up @@ -311,7 +311,8 @@ fn instructions_and_types() {
ty: IdlType::String,
}
]
}
},
generics: None
}
);

Expand Down Expand Up @@ -408,7 +409,8 @@ contract caller {
fields: None,
}
]
}
},
generics: None
}
);

Expand Down Expand Up @@ -664,6 +666,7 @@ contract Testing {
}
],
},
generics: None
}
);
}
Expand Down Expand Up @@ -765,7 +768,8 @@ contract Testing {
ty: IdlType::U64
}
]
}
},
generics: None
}
);

Expand All @@ -789,7 +793,8 @@ contract Testing {
ty: IdlType::I32
}
]
}
},
generics: None
}
);
}
Expand Down Expand Up @@ -832,6 +837,7 @@ fn name_collision() {
ty: IdlType::String,
}]
},
generics: None
}
);

Expand All @@ -855,7 +861,8 @@ fn name_collision() {
ty: IdlType::U64
}
]
}
},
generics: None
}
);

Expand All @@ -879,7 +886,8 @@ fn name_collision() {
ty: IdlType::I32
}
]
}
},
generics: None
}
);
}
Expand Down Expand Up @@ -926,6 +934,7 @@ fn double_name_collision() {
ty: IdlType::String,
}]
},
generics: None
}
);

Expand All @@ -940,7 +949,8 @@ fn double_name_collision() {
docs: None,
ty: IdlType::Bytes
},]
}
},
generics: None
}
);

Expand All @@ -964,7 +974,8 @@ fn double_name_collision() {
ty: IdlType::U64
}
]
}
},
generics: None
}
);

Expand All @@ -988,7 +999,8 @@ fn double_name_collision() {
ty: IdlType::I32
}
]
}
},
generics: None
}
);
}
Expand Down Expand Up @@ -1054,7 +1066,8 @@ fn deduplication() {
ty: IdlType::PublicKey
}
]
}
},
generics: None
}
);

Expand Down
9 changes: 6 additions & 3 deletions src/bin/idl/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: Apache-2.0

use crate::cli::IdlCommand;
use anchor_syn::idl::{Idl, IdlAccountItem, IdlInstruction, IdlType, IdlTypeDefinitionTy};
use anchor_syn::idl::types::{Idl, IdlAccountItem, IdlInstruction, IdlType, IdlTypeDefinitionTy};
use itertools::Itertools;
use serde_json::Value as JsonValue;
use solang::abi::anchor::discriminator;
Expand Down Expand Up @@ -176,7 +176,7 @@ fn write_solidity(idl: &Idl, mut f: File) -> Result<(), std::io::Error> {

let name = &ty_names.iter().find(|e| *e.0 == event.name).unwrap().1;

writeln!(f, "event {name} {{")?;
writeln!(f, "event {name} (")?;
let mut iter = event.fields.iter().enumerate();
let mut next = iter.next();
while let Some((no, e)) = next {
Expand All @@ -191,7 +191,7 @@ fn write_solidity(idl: &Idl, mut f: File) -> Result<(), std::io::Error> {
if next.is_some() { "," } else { "" }
)?;
}
writeln!(f, "}}")?;
writeln!(f, ");")?;
} else {
eprintln!(
"event {} has fields of type {} which is not supported on Solidity",
Expand Down Expand Up @@ -367,6 +367,9 @@ fn idltype_to_solidity(ty: &IdlType, ty_names: &[(String, String)]) -> Result<St
Ok(ty) => Ok(format!("{ty}[{size}]")),
Err(ty) => Err(format!("{ty}[{size}]")),
},
IdlType::Generic(..)
| IdlType::GenericLenArray(..)
| IdlType::DefinedWithTypeArgs { .. } => Err("generics are not supported".into()),
}
}

Expand Down
12 changes: 10 additions & 2 deletions tests/borsh_encoding.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: Apache-2.0

use anchor_syn::idl::{IdlType, IdlTypeDefinition, IdlTypeDefinitionTy};
use anchor_syn::idl::types::{IdlType, IdlTypeDefinition, IdlTypeDefinitionTy};
use byte_slice_cast::AsByteSlice;
use num_bigint::{BigInt, Sign};
use num_traits::ToPrimitive;
Expand Down Expand Up @@ -341,6 +341,9 @@ pub fn decode_at_offset(

BorshToken::Tuple(read_items)
}
IdlTypeDefinitionTy::Alias { value } => {
decode_at_offset(data, offset, value, custom_types)
}
}
}
IdlType::Bytes => {
Expand All @@ -353,7 +356,12 @@ pub fn decode_at_offset(
BorshToken::Bytes(read_data.to_vec())
}

IdlType::Option(_) | IdlType::F32 | IdlType::F64 => {
IdlType::Option(_)
| IdlType::F32
| IdlType::F64
| IdlType::Generic(..)
| IdlType::DefinedWithTypeArgs { .. }
| IdlType::GenericLenArray(..) => {
unreachable!("Type not available in Solidity")
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/solana.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: Apache-2.0

use crate::borsh_encoding::{decode_at_offset, encode_arguments, BorshToken};
use anchor_syn::idl::{Idl, IdlAccountItem};
use anchor_syn::idl::types::{Idl, IdlAccountItem};
use base58::{FromBase58, ToBase58};
use byteorder::{ByteOrder, LittleEndian, WriteBytesExt};
use itertools::Itertools;
Expand Down
Loading

0 comments on commit 0740817

Please sign in to comment.