-
Notifications
You must be signed in to change notification settings - Fork 212
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(lazer): add solana contract migration script, add message parsing to protocol #2181
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,75 @@ | ||
import * as anchor from "@coral-xyz/anchor"; | ||
import { Program } from "@coral-xyz/anchor"; | ||
import { PythLazerSolanaContract } from "../target/types/pyth_lazer_solana_contract"; | ||
import * as pythLazerSolanaContractIdl from "../target/idl/pyth_lazer_solana_contract.json"; | ||
import yargs from "yargs/yargs"; | ||
import { readFileSync } from "fs"; | ||
import NodeWallet from "@coral-xyz/anchor/dist/cjs/nodewallet"; | ||
|
||
// This script tops up the storage PDA and calls `migrateFrom010` on the contract. | ||
async function main() { | ||
let argv = await yargs(process.argv.slice(2)) | ||
.options({ | ||
url: { type: "string", demandOption: true }, | ||
"keypair-path": { type: "string", demandOption: true }, | ||
treasury: { type: "string", demandOption: true }, | ||
}) | ||
.parse(); | ||
|
||
const keypair = anchor.web3.Keypair.fromSecretKey( | ||
new Uint8Array(JSON.parse(readFileSync(argv.keypairPath, "ascii"))) | ||
); | ||
const wallet = new NodeWallet(keypair); | ||
const connection = new anchor.web3.Connection(argv.url, { | ||
commitment: "confirmed", | ||
}); | ||
const provider = new anchor.AnchorProvider(connection, wallet); | ||
|
||
const program: Program<PythLazerSolanaContract> = new Program( | ||
pythLazerSolanaContractIdl as PythLazerSolanaContract, | ||
provider | ||
); | ||
|
||
const storagePdaKey = new anchor.web3.PublicKey( | ||
"3rdJbqfnagQ4yx9HXJViD4zc4xpiSqmFsKpPuSCQVyQL" | ||
); | ||
const storagePdaInfo = await provider.connection.getAccountInfo( | ||
storagePdaKey | ||
); | ||
const newStorageSize = 381; | ||
if (storagePdaInfo.data.length == newStorageSize) { | ||
console.log("Already migrated"); | ||
const storage = await program.account.storage.all(); | ||
console.log("storage account: ", storage); | ||
return; | ||
} | ||
const minBalance = | ||
await provider.connection.getMinimumBalanceForRentExemption(newStorageSize); | ||
if (storagePdaInfo.lamports < minBalance) { | ||
console.log("storage PDA needs top-up"); | ||
const transaction = new anchor.web3.Transaction().add( | ||
anchor.web3.SystemProgram.transfer({ | ||
fromPubkey: keypair.publicKey, | ||
toPubkey: storagePdaKey, | ||
lamports: minBalance - storagePdaInfo.lamports, | ||
}) | ||
); | ||
const signature = await anchor.web3.sendAndConfirmTransaction( | ||
provider.connection, | ||
transaction, | ||
[keypair] | ||
); | ||
console.log("signature:", signature); | ||
} else { | ||
console.log("storage PDA doesn't need top-up"); | ||
} | ||
|
||
console.log("executing migration"); | ||
const signature2 = await program.methods | ||
.migrateFrom010(new anchor.web3.PublicKey(argv.treasury)) | ||
.accounts({}) | ||
.rpc(); | ||
console.log("signature:", signature2); | ||
} | ||
|
||
main(); |
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
//! Protocol types. | ||
|
||
pub mod message; | ||
pub mod payload; | ||
pub mod publisher; | ||
pub mod router; | ||
|
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,113 @@ | ||
use { | ||
crate::payload::{EVM_FORMAT_MAGIC, SOLANA_FORMAT_MAGIC_LE}, | ||
anyhow::bail, | ||
byteorder::{ReadBytesExt, WriteBytesExt, BE, LE}, | ||
std::io::{Cursor, Read, Write}, | ||
}; | ||
|
||
/// EVM signature enveope. | ||
#[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
pub struct EvmMessage { | ||
pub payload: Vec<u8>, | ||
pub signature: [u8; 64], | ||
pub recovery_id: u8, | ||
} | ||
|
||
impl EvmMessage { | ||
pub fn serialize(&self, mut writer: impl Write) -> anyhow::Result<()> { | ||
writer.write_u32::<BE>(EVM_FORMAT_MAGIC)?; | ||
writer.write_all(&self.signature)?; | ||
writer.write_u8(self.recovery_id)?; | ||
writer.write_u16::<BE>(self.payload.len().try_into()?)?; | ||
writer.write_all(&self.payload)?; | ||
Ok(()) | ||
} | ||
|
||
pub fn deserialize_slice(data: &[u8]) -> anyhow::Result<Self> { | ||
Self::deserialize(Cursor::new(data)) | ||
} | ||
|
||
pub fn deserialize(mut reader: impl Read) -> anyhow::Result<Self> { | ||
let magic = reader.read_u32::<BE>()?; | ||
if magic != EVM_FORMAT_MAGIC { | ||
bail!("magic mismatch"); | ||
} | ||
let mut signature = [0u8; 64]; | ||
reader.read_exact(&mut signature)?; | ||
let recovery_id = reader.read_u8()?; | ||
let payload_len: usize = reader.read_u16::<BE>()?.into(); | ||
let mut payload = vec![0u8; payload_len]; | ||
reader.read_exact(&mut payload)?; | ||
Ok(Self { | ||
payload, | ||
signature, | ||
recovery_id, | ||
}) | ||
} | ||
} | ||
|
||
/// Solana signature envelope. | ||
#[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
pub struct SolanaMessage { | ||
pub payload: Vec<u8>, | ||
pub signature: [u8; 64], | ||
pub public_key: [u8; 32], | ||
} | ||
|
||
impl SolanaMessage { | ||
pub fn serialize(&self, mut writer: impl Write) -> anyhow::Result<()> { | ||
writer.write_u32::<LE>(SOLANA_FORMAT_MAGIC_LE)?; | ||
writer.write_all(&self.signature)?; | ||
writer.write_all(&self.public_key)?; | ||
writer.write_u16::<LE>(self.payload.len().try_into()?)?; | ||
writer.write_all(&self.payload)?; | ||
Ok(()) | ||
} | ||
|
||
pub fn deserialize_slice(data: &[u8]) -> anyhow::Result<Self> { | ||
Self::deserialize(Cursor::new(data)) | ||
} | ||
|
||
pub fn deserialize(mut reader: impl Read) -> anyhow::Result<Self> { | ||
let magic = reader.read_u32::<LE>()?; | ||
if magic != SOLANA_FORMAT_MAGIC_LE { | ||
bail!("magic mismatch"); | ||
} | ||
let mut signature = [0u8; 64]; | ||
reader.read_exact(&mut signature)?; | ||
let mut public_key = [0u8; 32]; | ||
reader.read_exact(&mut public_key)?; | ||
let payload_len: usize = reader.read_u16::<LE>()?.into(); | ||
let mut payload = vec![0u8; payload_len]; | ||
reader.read_exact(&mut payload)?; | ||
Ok(Self { | ||
payload, | ||
signature, | ||
public_key, | ||
}) | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_evm_serde() { | ||
let m1 = EvmMessage { | ||
payload: vec![1, 2, 4, 3], | ||
signature: [5; 64], | ||
recovery_id: 1, | ||
}; | ||
let mut buf = Vec::new(); | ||
m1.serialize(&mut buf).unwrap(); | ||
assert_eq!(m1, EvmMessage::deserialize_slice(&buf).unwrap()); | ||
} | ||
|
||
#[test] | ||
fn test_solana_serde() { | ||
let m1 = SolanaMessage { | ||
payload: vec![1, 2, 4, 3], | ||
signature: [5; 64], | ||
public_key: [6; 32], | ||
}; | ||
let mut buf = Vec::new(); | ||
m1.serialize(&mut buf).unwrap(); | ||
assert_eq!(m1, SolanaMessage::deserialize_slice(&buf).unwrap()); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe these are somewhere else but i think having round-trip ser/de tests are good to have.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added.