bridge, token_bridge: Use Many<T>

Change-Id: I8c252e137cd092144ec8af2c2a857c7b1247dbfc
This commit is contained in:
Stan Drozd 2021-07-12 16:04:34 +02:00
parent 6868cc7177
commit 0891d9e433
6 changed files with 56 additions and 57 deletions

View File

@ -60,7 +60,7 @@ pub struct UpgradeContract<'b> {
pub payer: Signer<Info<'b>>,
/// Upgrade VAA
pub vaa: ClaimableVAA<'b, GovernancePayloadUpgrade>,
pub vaa: Many<ClaimableVAA<'b, GovernancePayloadUpgrade>>,
/// PDA authority for the loader
pub upgrade_authority: Derive<Info<'b>, "upgrade">,
@ -80,13 +80,13 @@ pub fn upgrade_contract(
accs: &mut UpgradeContract,
_data: UpgradeContractData,
) -> Result<()> {
verify_claim(&accs.vaa)?;
verify_claim(&(*accs.vaa))?;
accs.vaa.claim(ctx, accs.payer.key)?;
(*accs.vaa).claim(ctx, accs.payer.key)?;
let upgrade_ix = solana_program::bpf_loader_upgradeable::upgrade(
ctx.program_id,
&accs.vaa.message.new_contract,
&(*accs.vaa).message.new_contract,
accs.upgrade_authority.key,
accs.spill.key,
);
@ -106,7 +106,7 @@ pub struct UpgradeGuardianSet<'b> {
pub bridge: Mut<Bridge<'b, { AccountState::Initialized }>>,
/// GuardianSet change VAA
pub vaa: ClaimableVAA<'b, GovernancePayloadGuardianSetChange>,
pub vaa: Many<ClaimableVAA<'b, GovernancePayloadGuardianSetChange>>,
/// Old guardian set
pub guardian_set_old: GuardianSet<'b, { AccountState::Initialized }>,
@ -117,11 +117,11 @@ pub struct UpgradeGuardianSet<'b> {
impl<'b> InstructionContext<'b> for UpgradeGuardianSet<'b> {
fn verify(&self, _program_id: &Pubkey) -> Result<()> {
if self.guardian_set_old.index != self.vaa.new_guardian_set_index - 1 {
if self.guardian_set_old.index != (*self.vaa).new_guardian_set_index - 1 {
return Err(InvalidGuardianSetUpgrade.into());
}
if self.bridge.guardian_set_index != self.vaa.new_guardian_set_index - 1 {
if self.bridge.guardian_set_index != (*self.vaa).new_guardian_set_index - 1 {
return Err(InvalidGuardianSetUpgrade.into());
}
@ -137,30 +137,30 @@ pub fn upgrade_guardian_set(
accs: &mut UpgradeGuardianSet,
_data: UpgradeGuardianSetData,
) -> Result<()> {
verify_claim(&accs.vaa)?;
verify_claim(&(*accs.vaa))?;
accs.guardian_set_old.verify_derivation(
ctx.program_id,
&GuardianSetDerivationData {
index: accs.vaa.new_guardian_set_index - 1,
index: (*accs.vaa).new_guardian_set_index - 1,
},
)?;
accs.guardian_set_new.verify_derivation(
ctx.program_id,
&GuardianSetDerivationData {
index: accs.vaa.new_guardian_set_index,
index: (*accs.vaa).new_guardian_set_index,
},
)?;
accs.vaa.claim(ctx, accs.payer.key)?;
(*accs.vaa).claim(ctx, accs.payer.key)?;
// Set expiration time for the old set
accs.guardian_set_old.expiration_time =
accs.vaa.meta().vaa_time + accs.bridge.config.guardian_set_expiration_time;
(*accs.vaa).meta().vaa_time + accs.bridge.config.guardian_set_expiration_time;
// Initialize new guardian Set
accs.guardian_set_new.index = accs.vaa.new_guardian_set_index;
accs.guardian_set_new.creation_time = accs.vaa.meta().vaa_time;
accs.guardian_set_new.keys = accs.vaa.new_guardian_set.clone();
accs.guardian_set_new.index = (*accs.vaa).new_guardian_set_index;
accs.guardian_set_new.creation_time = (*accs.vaa).meta().vaa_time;
accs.guardian_set_new.keys = (*accs.vaa).new_guardian_set.clone();
// Create new guardian set
// This is done after populating it to properly allocate space according to key vec length.
@ -174,7 +174,7 @@ pub fn upgrade_guardian_set(
)?;
// Set guardian set index
accs.bridge.guardian_set_index = accs.vaa.new_guardian_set_index;
accs.bridge.guardian_set_index = (*accs.vaa).new_guardian_set_index;
Ok(())
}
@ -188,7 +188,7 @@ pub struct SetFees<'b> {
pub bridge: Mut<Bridge<'b, { AccountState::Initialized }>>,
/// Governance VAA
pub vaa: ClaimableVAA<'b, GovernancePayloadSetMessageFee>,
pub vaa: Many<ClaimableVAA<'b, GovernancePayloadSetMessageFee>>,
}
impl<'b> InstructionContext<'b> for SetFees<'b> {
@ -198,12 +198,12 @@ impl<'b> InstructionContext<'b> for SetFees<'b> {
pub struct SetFeesData {}
pub fn set_fees(ctx: &ExecutionContext, accs: &mut SetFees, _data: SetFeesData) -> Result<()> {
verify_claim(&accs.vaa)?;
verify_claim(&(*accs.vaa))?;
accs.vaa.claim(ctx, accs.payer.key)?;
(*accs.vaa).claim(ctx, accs.payer.key)?;
accs.bridge.config.fee = accs.vaa.fee.as_u64();
accs.bridge.config.fee_persistent = accs.vaa.persisted_fee.as_u64();
accs.bridge.config.fee = (*accs.vaa).fee.as_u64();
accs.bridge.config.fee_persistent = (*accs.vaa).persisted_fee.as_u64();
Ok(())
}
@ -217,7 +217,7 @@ pub struct TransferFees<'b> {
pub bridge: Bridge<'b, { AccountState::Initialized }>,
/// Governance VAA
pub vaa: ClaimableVAA<'b, GovernancePayloadTransferFees>,
pub vaa: Many<ClaimableVAA<'b, GovernancePayloadTransferFees>>,
/// Account collecting tx fees
pub fee_collector: Mut<Derive<Info<'b>, "fee_collector">>,
@ -231,7 +231,7 @@ pub struct TransferFees<'b> {
impl<'b> InstructionContext<'b> for TransferFees<'b> {
fn verify(&self, _program_id: &Pubkey) -> Result<()> {
if self.vaa.to != self.recipient.key.to_bytes() {
if (*self.vaa).to != self.recipient.key.to_bytes() {
return Err(InvalidFeeRecipient.into());
}
@ -247,24 +247,24 @@ pub fn transfer_fees(
accs: &mut TransferFees,
_data: TransferFeesData,
) -> Result<()> {
verify_claim(&accs.vaa)?;
verify_claim(&(*accs.vaa))?;
if accs
.fee_collector
.lamports()
.saturating_sub(accs.vaa.amount.as_u64())
.saturating_sub((*accs.vaa).amount.as_u64())
< accs.rent.minimum_balance(accs.fee_collector.data_len())
{
return Err(InvalidGovernanceWithdrawal.into());
}
accs.vaa.claim(ctx, accs.payer.key)?;
(*accs.vaa).claim(ctx, accs.payer.key)?;
// Transfer fees
let transfer_ix = solana_program::system_instruction::transfer(
accs.fee_collector.key,
accs.recipient.key,
accs.vaa.amount.as_u64(),
(*accs.vaa).amount.as_u64(),
);
let seeds = accs.fee_collector.self_bumped_seeds(None, ctx.program_id);

View File

@ -29,7 +29,6 @@ use crate::{
SignatureSetDerivationData,
},
InitializeData,
PayloadMessage,
PostMessageData,
PostVAAData,
SetFeesData,

View File

@ -70,7 +70,7 @@ fn main() -> Result<(), ErrBox> {
let payer = read_keypair_file(&*shellexpand::tilde("~/.config/solana/id.json"))
.expect("Example requires a keypair file");
let (ix, signers) = init.to_ix(program_id, ix_data.as_slice())?;
let (ix, signers) = init.gen_client_ix(program_id, ix_data.as_slice())?;
send_ix_in_tx(&client, ix, &payer, signers.iter().collect())?;
let payer = read_keypair_file(&*shellexpand::tilde("~/.config/solana/id.json"))

View File

@ -24,7 +24,7 @@ pub struct CompleteNative<'b> {
pub payer: Signer<AccountInfo<'b>>,
pub config: ConfigAccount<'b, { AccountState::Initialized }>,
pub vaa: ClaimableVAA<'b, PayloadTransfer>,
pub vaa: Many<ClaimableVAA<'b, PayloadTransfer>>,
pub chain_registration: Endpoint<'b, { AccountState::Initialized }>,
pub to: Data<'b, SplAccount, { AccountState::Initialized }>,
@ -37,8 +37,8 @@ pub struct CompleteNative<'b> {
impl<'a> From<&CompleteNative<'a>> for EndpointDerivationData {
fn from(accs: &CompleteNative<'a>) -> Self {
EndpointDerivationData {
emitter_chain: accs.vaa.meta().emitter_chain,
emitter_address: accs.vaa.meta().emitter_address,
emitter_chain: accs.vaa.0.meta().emitter_chain,
emitter_address: accs.vaa.0.meta().emitter_address,
}
}
}
@ -72,10 +72,10 @@ impl<'b> InstructionContext<'b> for CompleteNative<'b> {
}
// Verify VAA
if self.vaa.token_address != self.mint.info().key.to_bytes() {
if self.vaa.0.token_address != self.mint.info().key.to_bytes() {
return Err(InvalidMint.into());
}
if self.vaa.token_chain != 1 {
if self.vaa.0.token_chain != 1 {
return Err(InvalidChain.into());
}
@ -92,7 +92,7 @@ pub fn complete_native(
data: CompleteNativeData,
) -> Result<()> {
// Prevent vaa double signing
accs.vaa.claim(ctx, accs.payer.key)?;
accs.vaa.0.claim(ctx, accs.payer.key)?;
// Transfer tokens
let transfer_ix = spl_token::instruction::transfer(
@ -101,7 +101,7 @@ pub fn complete_native(
accs.to.info().key,
accs.custody_signer.key,
&[],
accs.vaa.amount.as_u64(),
accs.vaa.0.amount.as_u64(),
)?;
invoke_seeded(&transfer_ix, ctx, &accs.custody_signer, None)?;
@ -116,7 +116,7 @@ pub struct CompleteWrapped<'b> {
pub config: ConfigAccount<'b, { AccountState::Initialized }>,
// Signed message for the transfer
pub vaa: ClaimableVAA<'b, PayloadTransfer>,
pub vaa: Many<ClaimableVAA<'b, PayloadTransfer>>,
pub chain_registration: Endpoint<'b, { AccountState::Initialized }>,
@ -129,8 +129,8 @@ pub struct CompleteWrapped<'b> {
impl<'a> From<&CompleteWrapped<'a>> for EndpointDerivationData {
fn from(accs: &CompleteWrapped<'a>) -> Self {
EndpointDerivationData {
emitter_chain: accs.vaa.meta().emitter_chain,
emitter_address: accs.vaa.meta().emitter_address,
emitter_chain: accs.vaa.0.meta().emitter_chain,
emitter_address: accs.vaa.0.meta().emitter_address,
}
}
}
@ -138,8 +138,8 @@ impl<'a> From<&CompleteWrapped<'a>> for EndpointDerivationData {
impl<'a> From<&CompleteWrapped<'a>> for WrappedDerivationData {
fn from(accs: &CompleteWrapped<'a>) -> Self {
WrappedDerivationData {
token_chain: accs.vaa.token_chain,
token_address: accs.vaa.token_address,
token_chain: accs.vaa.0.token_chain,
token_address: accs.vaa.0.token_address,
}
}
}
@ -169,7 +169,7 @@ pub fn complete_wrapped(
accs: &mut CompleteWrapped,
data: CompleteWrappedData,
) -> Result<()> {
accs.vaa.claim(ctx, accs.payer.key)?;
accs.vaa.0.claim(ctx, accs.payer.key)?;
// Mint tokens
let mint_ix = spl_token::instruction::mint_to(
@ -178,7 +178,7 @@ pub fn complete_wrapped(
accs.to.info().key,
accs.mint_authority.key,
&[],
accs.vaa.amount.as_u64(),
accs.vaa.0.amount.as_u64(),
)?;
invoke_seeded(&mint_ix, ctx, &accs.mint_authority, None)?;

View File

@ -23,7 +23,7 @@ pub struct CreateWrapped<'b> {
pub config: ConfigAccount<'b, { AccountState::Initialized }>,
pub chain_registration: Endpoint<'b, { AccountState::Initialized }>,
pub vaa: ClaimableVAA<'b, PayloadAssetMeta>,
pub vaa: Many<ClaimableVAA<'b, PayloadAssetMeta>>,
// New Wrapped
pub mint: WrappedMint<'b, { AccountState::Uninitialized }>,
@ -35,8 +35,8 @@ pub struct CreateWrapped<'b> {
impl<'a> From<&CreateWrapped<'a>> for EndpointDerivationData {
fn from(accs: &CreateWrapped<'a>) -> Self {
EndpointDerivationData {
emitter_chain: accs.vaa.meta().emitter_chain,
emitter_address: accs.vaa.meta().emitter_address,
emitter_chain: accs.vaa.0.meta().emitter_chain,
emitter_address: accs.vaa.0.meta().emitter_address,
}
}
}
@ -44,8 +44,8 @@ impl<'a> From<&CreateWrapped<'a>> for EndpointDerivationData {
impl<'a> From<&CreateWrapped<'a>> for WrappedDerivationData {
fn from(accs: &CreateWrapped<'a>) -> Self {
WrappedDerivationData {
token_chain: accs.vaa.token_chain,
token_address: accs.vaa.token_address,
token_chain: accs.vaa.0.token_chain,
token_address: accs.vaa.0.token_address,
}
}
}
@ -69,7 +69,7 @@ pub fn create_wrapped(
accs: &mut CreateWrapped,
data: CreateWrappedData,
) -> Result<()> {
accs.vaa.claim(ctx, accs.payer.key)?;
accs.vaa.0.claim(ctx, accs.payer.key)?;
// Create mint account
accs.mint
@ -90,8 +90,8 @@ pub fn create_wrapped(
.create(&((&*accs).into()), ctx, accs.payer.key, Exempt);
// Populate meta account
accs.meta.chain = accs.vaa.token_chain;
accs.meta.token_address = accs.vaa.token_address;
accs.meta.chain = accs.vaa.0.token_chain;
accs.meta.token_address = accs.vaa.0.token_address;
Ok(())
}

View File

@ -15,14 +15,14 @@ pub struct RegisterChain<'b> {
pub endpoint: Endpoint<'b, { AccountState::Uninitialized }>,
pub vaa: ClaimableVAA<'b, PayloadGovernanceRegisterChain>,
pub vaa: Many<ClaimableVAA<'b, PayloadGovernanceRegisterChain>>,
}
impl<'a> From<&RegisterChain<'a>> for EndpointDerivationData {
fn from(accs: &RegisterChain<'a>) -> Self {
EndpointDerivationData {
emitter_chain: accs.vaa.meta().emitter_chain,
emitter_address: accs.vaa.meta().emitter_address,
emitter_chain: (*accs.vaa).meta().emitter_chain,
emitter_address: (*accs.vaa).meta().emitter_address,
}
}
}
@ -43,14 +43,14 @@ pub fn register_chain(
data: RegisterChainData,
) -> Result<()> {
// Claim VAA
accs.vaa.claim(ctx, accs.payer.key)?;
(*accs.vaa).claim(ctx, accs.payer.key)?;
// Create endpoint
accs.endpoint
.create(&((&*accs).into()), ctx, accs.payer.key, Exempt);
accs.endpoint.chain = accs.vaa.chain;
accs.endpoint.contract = accs.vaa.endpoint_address;
accs.endpoint.chain = (*accs.vaa).chain;
accs.endpoint.contract = (*accs.vaa).endpoint_address;
Ok(())
}