Solana attest all but post VAA and wasm.

This commit is contained in:
skojenov 2022-05-11 21:57:29 +00:00 committed by Karl Kempe
parent 9d26669a4c
commit acfdddae2b
5 changed files with 172 additions and 10 deletions

View File

@ -1,9 +1,11 @@
pub mod initialize;
pub mod init_sale;
pub mod abort_sale;
pub mod attest_sale;
pub mod contribute;
pub use initialize::*;
pub use init_sale::*;
pub use abort_sale::*;
pub use attest_sale::*;
pub use contribute::*;

View File

@ -0,0 +1,129 @@
#![allow(dead_code)]
//#![allow(unused_must_use)]
//#![allow(unused_imports)]
use crate::{
messages::*,
accounts::{
ConfigAccount,
// SaleStateAccountDerivationData,
},
errors::Error::*,
claimed_vaa::ClaimedVAA,
};
use solana_program::msg;
use solana_program::{
account_info::AccountInfo,
sysvar::clock::Clock,
sysvar::rent::Rent,
};
use solitaire::{
// CreationLamports::Exempt,
*,
};
use wormhole_sdk::{
ConsistencyLevel,
post_message, // SDK call.
// id as bridge_id, // Get Bridge Id
};
//use bridge::{ vaa::{ ClaimableVAA, }, };
#[derive(FromAccounts)]
pub struct AttestIccoSale<'b> {
pub payer: Mut<Signer<AccountInfo<'b>>>,
pub config: ConfigAccount<'b, { AccountState::Initialized }>,
pub init_sale_vaa: ClaimedVAA<'b, SaleInit>, // Was claimed.
pub rent: Sysvar<'b, Rent>,
pub clock: Sysvar<'b, Clock>,
// Sale state is in ctx.accounts[5];
}
/*
// May need this later Just for PDA verification.
impl<'a> From<&AttestIccoSale<'a>> for SaleStateAccountDerivationData {
fn from(accs: &AttestIccoSale<'a>) -> Self {
SaleStateAccountDerivationData {
sale_id: accs.attest_sale_vaa.sale_id,
}
}
}
*/
// No data so far.
#[derive(BorshDeserialize, BorshSerialize, Default)]
pub struct AttestIccoSaleData {
}
pub fn attest_icco_sale(
ctx: &ExecutionContext,
accs: &mut AttestIccoSale,
_data: AttestIccoSaleData,
) -> Result<()> {
msg!("bbrp in attest_icco_sale!");
// let now_time = accs.clock.unix_timestamp;
// let start_time = accs.init_sale_vaa.get_sale_start(&accs.init_sale_vaa.meta().payload[..]).1 as i64;
// let end_time = accs.init_sale_vaa.get_sale_end(&accs.init_sale_vaa.meta().payload[..]).1 as i64;
// msg!("time: {:?} start: {:?} end: {:?}", now_time, start_time, end_time);
// Verify that the sale_state account PDA was derived correctly
// let sale_id = accs.init_sale_vaa.sale_id;
// let derivation_data: SaleStateAccountDerivationData = (&*accs).into();
// accs.sale_state.verify_derivation(ctx.program_id, &derivation_data)?;
let sale_state_account_info = &ctx.accounts[5];
let state_data = sale_state_account_info.data.borrow();
if get_sale_state_sealed(&state_data) {
return Err(SaleHasBeenSealed.into());
}
if get_sale_state_aborted(&state_data) {
return Err(SaleHasBeenAborted.into());
}
// Let's count solana tokens.
let mut sol_cnt: u8 = 0;
let mut token_idx: u8 = 0;
while token_idx < accs.init_sale_vaa.token_cnt {
if accs.init_sale_vaa.get_accepted_token_chain(token_idx, &accs.init_sale_vaa.meta().payload) == 1 {
sol_cnt = sol_cnt+1;
}
token_idx = token_idx + 1;
}
// Allocate and fill the VAA payload.
let mut vaa_bf = Vec::with_capacity(sol_cnt as usize); // even 0 should be ok
let mut bf = & mut vaa_bf;
pack_sale_attested_vaa_header(& mut bf, accs.init_sale_vaa.sale_id, sol_cnt);
// Store solana amounts.
sol_cnt = 0;
token_idx = 0;
// let amount: u64 = 0;
while token_idx < accs.init_sale_vaa.token_cnt {
if accs.init_sale_vaa.get_accepted_token_chain(token_idx, &accs.init_sale_vaa.meta().payload) == 1 {
let amount = get_sale_state_contribution(&state_data, token_idx);
pack_sale_attested_vaa_token(& mut bf, token_idx, sol_cnt, amount);
sol_cnt = sol_cnt+1;
}
token_idx = token_idx + 1;
}
// post sale_attested_vaa.
post_message(
*ctx.program_id,
*accs.payer.key,
*accs.payer.key,
&bf,
ConsistencyLevel::Confirmed,
None, //Some(&seeds), // If needed.
ctx.accounts,
0
)?;
Ok(())
}

View File

@ -41,8 +41,8 @@ use solitaire::{
use bridge::{
vaa::{
ClaimableVAA,
DeserializePayload,
PayloadMessage,
// DeserializePayload,
// PayloadMessage,
},
error::Error::{
VAAAlreadyExecuted,

View File

@ -24,8 +24,8 @@ pub mod claimed_vaa;
pub mod simple_account;
pub use api::{
contribute_icco_sale, init_icco_sale, initialize, create_icco_sale_custody_account, abort_icco_sale,
ContributeIccoSale, InitIccoSale, Initialize, CreateIccoSaleCustodyAccount, AbortIccoSale,
contribute_icco_sale, init_icco_sale, initialize, create_icco_sale_custody_account, abort_icco_sale, attest_icco_sale,
ContributeIccoSale, InitIccoSale, Initialize, CreateIccoSaleCustodyAccount, AbortIccoSale, AttestIccoSale,
};
use solitaire::*;
@ -35,5 +35,6 @@ solitaire! {
CreateIccoSaleCustodyAccount => create_icco_sale_custody_account,
InitIccoSale => init_icco_sale,
AbortIccoSale => abort_icco_sale,
AttestIccoSale => attest_icco_sale,
ContributeIccoSale => contribute_icco_sale,
}

View File

@ -82,6 +82,7 @@ impl DeserializePayload for SaleAbort {
}
}
/// -------------------------------------------------------------------
/// Zero-copy from VAA payload for Init Sale.
@ -184,18 +185,47 @@ impl SaleInit {
// Accepted tokens data getters
// tokenAddress: Pubkey,
pub fn get_accepted_token_address(&self, idx: usize, bf: &[u8]) -> Pubkey {
let t_offset: usize = 228 + idx * 50;
pub fn get_accepted_token_address(&self, idx: u8, bf: &[u8]) -> Pubkey {
let t_offset: usize = 228 + (idx as usize) * 50;
Pubkey::new(&bf[t_offset..t_offset + 32])
}
pub fn get_accepted_token_chain(&self, idx: usize, bf: &[u8]) -> u16 {
let t_offset: usize = 228 + idx * 50 + 32;
pub fn get_accepted_token_chain(&self, idx: u8, bf: &[u8]) -> u16 {
let t_offset: usize = 228 + (idx as usize) * 50 + 32;
read_u16(&bf[t_offset..])
}
pub fn get_accepted_token_conversion_rate(&self, idx: usize, bf: &[u8]) -> u128 {
let t_offset: usize = 228 + idx * 50 + 34;
pub fn get_accepted_token_conversion_rate(&self, idx: u8, bf: &[u8]) -> u128 {
let t_offset: usize = 228 + (idx as usize) * 50 + 34;
read_u128(&bf[t_offset..])
}
}
/// -------------------------------------------------------------------
/// VAA saleAttested reply payload [2]
// Layout
// uint8 payloadID; // PayloadID uint8 = 2
// uint256 saleID; // Sale ID
// uint16 chainID; // Chain ID
// uint8 tokens_cnt // Contribution[] contributions; // sealed contributions for this sale
// uint8 contributions[i].tokenIndex,
// uint256 contributions[i].contributed
pub fn get_sale_attested_size(solana_tokens_cnt: u8) -> usize {
((1+32+2+1) + solana_tokens_cnt*(1+32)) as usize
}
pub fn pack_sale_attested_vaa_header(bf: & mut [u8], sale_id: u128, solana_tokens_cnt: u8) {
bf[0] = 2;
bf[33..65].clone_from_slice(&sale_id.to_be_bytes()[..]); // first 16 bytes s/b 0.
bf[65..67].clone_from_slice(&(1 as u16).to_be_bytes()[..]); // ChainId
bf[67] = solana_tokens_cnt;
}
pub fn pack_sale_attested_vaa_token(bf: & mut [u8], token_idx: u8, slot_idx: u8, amount: u64) {
let step = (1+32) as usize;
let base = (8+32+2+1) as usize + step * (slot_idx as usize);
bf[base] = token_idx;
bf[base+33..base+65].clone_from_slice(&amount.to_be_bytes()[..]);
}