Skip to content

Commit

Permalink
Merge pull request #179 from nomic-io/evmos-incentives-migration
Browse files Browse the repository at this point in the history
Evmos incentives migration
  • Loading branch information
mappum authored Aug 1, 2023
2 parents ee63c80 + 7d92a62 commit 45c9bc6
Show file tree
Hide file tree
Showing 6 changed files with 170 additions and 226 deletions.
102 changes: 51 additions & 51 deletions src/airdrop.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use orga::coins::Decimal;
use orga::coins::{Address, Amount};
use orga::coins::Address;
#[cfg(feature = "full")]
use orga::coins::{Amount, Decimal};
use orga::collections::{ChildMut, Map};
use orga::context::GetContext;
use orga::migrate::MigrateFrom;
use orga::orga;
use orga::plugins::MIN_FEE;
use orga::plugins::{Paid, Signer};
use orga::{Error, Result};
#[cfg(feature = "full")]
Expand Down Expand Up @@ -64,7 +64,7 @@ impl Airdrop {

self.accounts
.get_mut(signer)?
.ok_or_else(|| Error::Coins("No airdrop account for signer".into()))
.ok_or_else(|| Error::App("No airdrop account for signer".into()))
}

fn pay_as_funding(&mut self, amount: u64) -> Result<()> {
Expand All @@ -84,45 +84,19 @@ impl Airdrop {
}

#[call]
pub fn claim_btc_deposit(&mut self) -> Result<()> {
let mut acct = self.signer_acct_mut()?;
let amount = acct.btc_deposit.claim()?;
self.pay_as_funding(amount)?;
Ok(())
}

#[call]
pub fn claim_btc_withdraw(&mut self) -> Result<()> {
pub fn claim_airdrop2(&mut self) -> Result<()> {
let mut acct = self.signer_acct_mut()?;
let amount = acct.btc_withdraw.claim()?;
let amount = acct.airdrop2.claim()?;
self.pay_as_funding(amount)?;
Ok(())
}

#[call]
pub fn claim_ibc_transfer(&mut self) -> Result<()> {
pub fn join_accounts(&mut self, dest_addr: Address) -> Result<()> {
let mut acct = self.signer_acct_mut()?;
let amount = acct.ibc_transfer.claim()?;
self.pay_as_funding(amount)?;
Ok(())
}

#[call]
pub fn claim_testnet_participation(&mut self) -> Result<()> {
#[cfg(not(feature = "testnet"))]
{
let mut acct = self.signer_acct_mut()?;
let amount = acct.testnet_participation.claim()?;
self.pay_as_funding(amount)?;
if acct.joined {
return Err(Error::App("Account already joined".to_string()));
}
Ok(())
}

#[call]
pub fn join_accounts(&mut self, dest_addr: Address) -> Result<()> {
self.pay_as_funding(MIN_FEE)?;

let mut acct = self.signer_acct_mut()?;
if acct.is_empty() {
return Err(Error::App("Account has no airdrop balance".to_string()));
}
Expand All @@ -143,12 +117,9 @@ impl Airdrop {
};

add_part(&mut dest.airdrop1, src.airdrop1);
add_part(&mut dest.btc_deposit, src.btc_deposit);
add_part(&mut dest.ibc_transfer, src.ibc_transfer);
add_part(&mut dest.btc_withdraw, src.btc_withdraw);
add_part(&mut dest.airdrop2, src.airdrop2);

#[cfg(not(feature = "testnet"))]
add_part(&mut dest.testnet_participation, src.testnet_participation);
dest.joined = true;

Ok(())
}
Expand Down Expand Up @@ -233,18 +204,14 @@ impl Airdrop {

#[cfg(feature = "testnet")]
{
acct.btc_deposit.locked = unom / 3;
acct.btc_withdraw.locked = unom / 3;
acct.ibc_transfer.locked = unom / 3;
acct.airdrop2.claimable = unom;

Ok((0, 0))
}

#[cfg(not(feature = "testnet"))]
{
acct.btc_deposit.locked = unom / 4;
acct.btc_withdraw.locked = unom / 4;
acct.ibc_transfer.locked = unom / 4;
acct.airdrop2.claimable = unom;
assert!(testnet_completions <= 3);
acct.testnet_participation.locked = unom / 12 * (3 - testnet_completions);
acct.testnet_participation.claimable = unom / 12 * testnet_completions;
Expand Down Expand Up @@ -354,7 +321,7 @@ impl Airdrop {
// }

#[cfg(not(feature = "testnet"))]
#[orga(version = 1)]
#[orga(version = 2)]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Account {
#[orga(version(V0))]
Expand All @@ -370,16 +337,30 @@ pub struct Account {
pub ibc_transfer: Part,
#[orga(version(V1))]
pub testnet_participation: Part,

#[orga(version(V2))]
pub airdrop2: Part,
#[orga(version(V2))]
pub joined: bool,
}

#[cfg(feature = "testnet")]
#[orga(version = 1)]
#[orga(version = 2)]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Account {
pub airdrop1: Part,

#[orga(version(V0, V1))]
pub btc_deposit: Part,
#[orga(version(V0, V1))]
pub btc_withdraw: Part,
#[orga(version(V0, V1))]
pub ibc_transfer: Part,

#[orga(version(V2))]
pub airdrop2: Part,
#[orga(version(V2))]
pub joined: bool,
}

impl Account {
Expand Down Expand Up @@ -410,6 +391,27 @@ impl MigrateFrom<AccountV0> for AccountV1 {
}
}

impl MigrateFrom<AccountV1> for AccountV2 {
fn migrate_from(value: AccountV1) -> Result<Self> {
let add_part = |dest: &mut Part, src: Part| {
dest.claimable += src.claimable + src.locked;
dest.claimed += src.claimed;
};

let mut airdrop2 = Part::default();

add_part(&mut airdrop2, value.btc_deposit);
add_part(&mut airdrop2, value.btc_withdraw);
add_part(&mut airdrop2, value.ibc_transfer);

Ok(Self {
airdrop1: value.airdrop1,
airdrop2,
joined: false,
})
}
}

#[orga]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Part {
Expand Down Expand Up @@ -477,9 +479,7 @@ nomic100000aeu2lh0jrrnmn2npc88typ25u7t3aa64x,1,1,1,1,1,1,1,1,1,1,true,true,true"
.get_mut(Address::from_str("nomic100000aeu2lh0jrrnmn2npc88typ25u7t3aa64x").unwrap())
.unwrap()
.unwrap();
let airdrop2_total = account.btc_deposit.total()
+ account.btc_withdraw.total()
+ account.ibc_transfer.total();
let airdrop2_total = account.airdrop2.total();

assert_approx_eq(airdrop2_total, AIRDROP_II_TOTAL);
}
Expand Down
72 changes: 13 additions & 59 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use orga::macros::build_call;
use orga::migrate::Migrate;
use orga::orga;
use orga::plugins::sdk_compat::{sdk, sdk::Tx as SdkTx, ConvertSdkTx};
use orga::plugins::{DefaultPlugins, PaidCall, Signer, Time, MIN_FEE};
use orga::plugins::{disable_fee, DefaultPlugins, Paid, PaidCall, Signer, Time, MIN_FEE};
use orga::prelude::*;
use orga::upgrade::Version;
use orga::upgrade::{Upgrade, UpgradeV0};
Expand Down Expand Up @@ -159,9 +159,6 @@ impl InnerApp {

let signer = self.signer()?;
let mut coins = self.bitcoin.accounts.withdraw(signer, amount)?;
if let Some(mut acct) = self.airdrop.get_mut(signer)? {
acct.ibc_transfer.unlock();
}

let fee = ibc_fee(amount)?;
let fee = coins.take(fee)?;
Expand Down Expand Up @@ -233,9 +230,6 @@ impl InnerApp {
)?;
match dest {
DepositCommitment::Address(addr) => {
if let Some(mut acct) = self.airdrop.get_mut(addr)? {
acct.btc_deposit.unlock();
}
self.bitcoin.accounts.deposit(addr, nbtc.into())?
}
DepositCommitment::Ibc(dest) => {
Expand Down Expand Up @@ -286,14 +280,19 @@ impl InnerApp {
script_pubkey: Adapter<bitcoin::Script>,
amount: Amount,
) -> Result<()> {
let signer = self.signer()?;
if let Some(mut acct) = self.airdrop.get_mut(signer)? {
acct.btc_withdraw.unlock();
}

Ok(self.bitcoin.withdraw(script_pubkey, amount)?)
}

#[call]
fn join_accounts(&mut self, dest_addr: Address) -> Result<()> {
disable_fee();

self.airdrop.join_accounts(dest_addr)?;
self.incentives.join_accounts(dest_addr)?;

Ok(())
}

fn signer(&mut self) -> Result<Address> {
self.context::<Signer>()
.ok_or_else(|| Error::Signer("No Signer context available".into()))?
Expand Down Expand Up @@ -695,51 +694,6 @@ impl ConvertSdkTx for InnerApp {
Ok(PaidCall { payer, paid })
}

"nomic/MsgClaimBtcDepositAirdrop" => {
let msg = msg
.value
.as_object()
.ok_or_else(|| Error::App("Invalid message value".to_string()))?;
if !msg.is_empty() {
return Err(Error::App("Message should be empty".to_string()));
}

let payer = build_call!(self.airdrop.claim_btc_deposit());
let paid = build_call!(self.accounts.give_from_funding_all());

Ok(PaidCall { payer, paid })
}

"nomic/MsgClaimBtcWithdrawAirdrop" => {
let msg = msg
.value
.as_object()
.ok_or_else(|| Error::App("Invalid message value".to_string()))?;
if !msg.is_empty() {
return Err(Error::App("Message should be empty".to_string()));
}

let payer = build_call!(self.airdrop.claim_btc_withdraw());
let paid = build_call!(self.accounts.give_from_funding_all());

Ok(PaidCall { payer, paid })
}

"nomic/MsgClaimIbcTransferAirdrop" => {
let msg = msg
.value
.as_object()
.ok_or_else(|| Error::App("Invalid message value".to_string()))?;
if !msg.is_empty() {
return Err(Error::App("Message should be empty".to_string()));
}

let payer = build_call!(self.airdrop.claim_ibc_transfer());
let paid = build_call!(self.accounts.give_from_funding_all());

Ok(PaidCall { payer, paid })
}

#[cfg(feature = "stakenet")]
"nomic/MsgClaimTestnetParticipationAirdrop" => {
let msg = msg
Expand Down Expand Up @@ -862,7 +816,7 @@ impl ConvertSdkTx for InnerApp {
Ok(PaidCall { payer, paid })
}

"nomic/MsgJoinAirdropAccounts" => {
"nomic/MsgJoinRewardAccounts" => {
let msg = msg
.value
.as_object()
Expand All @@ -874,7 +828,7 @@ impl ConvertSdkTx for InnerApp {
.parse()
.map_err(|_| Error::App("Invalid destination address".to_string()))?;

let payer = build_call!(self.airdrop.join_accounts(dest_addr));
let payer = build_call!(self.join_accounts(dest_addr));
let paid = build_call!(self.app_noop());

Ok(PaidCall { payer, paid })
Expand Down
37 changes: 3 additions & 34 deletions src/bin/nomic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -996,45 +996,14 @@ impl ClaimAirdropCmd {
claimed = true;
}

if acct.btc_deposit.claimable > 0 {
if acct.airdrop2.claimable > 0 {
app_client()
.call(
|app| build_call!(app.airdrop.claim_btc_deposit()),
|app| build_call!(app.airdrop.claim_airdrop2()),
|app| build_call!(app.accounts.give_from_funding_all()),
)
.await?;
println!(
"Claimed BTC deposit airdrop ({} uNOM)",
acct.btc_deposit.claimable
);
claimed = true;
}

if acct.btc_withdraw.claimable > 0 {
app_client()
.call(
|app| build_call!(app.airdrop.claim_btc_withdraw()),
|app| build_call!(app.accounts.give_from_funding_all()),
)
.await?;
println!(
"Claimed BTC withdraw airdrop ({} uNOM)",
acct.btc_withdraw.claimable
);
claimed = true;
}

if acct.ibc_transfer.claimable > 0 {
app_client()
.call(
|app| build_call!(app.airdrop.claim_ibc_transfer()),
|app| build_call!(app.accounts.give_from_funding_all()),
)
.await?;
println!(
"Claimed IBC transfer airdrop ({} uNOM)",
acct.ibc_transfer.claimable
);
println!("Claimed airdrop 2 ({} uNOM)", acct.airdrop2.claimable);
claimed = true;
}

Expand Down
Loading

0 comments on commit 45c9bc6

Please sign in to comment.