switch language from tokens to LP
This commit is contained in:
parent
928c68f550
commit
7dfdf4844e
@ -28,7 +28,7 @@ message Contract {
|
||||
string dtrfs_sha = 12;
|
||||
string created_at = 13;
|
||||
string updated_at = 14;
|
||||
// total nanotoken cost per minute (for all units)
|
||||
// total nanoLP cost per minute (for all units)
|
||||
uint64 nano_per_minute = 15;
|
||||
uint64 locked_nano = 16;
|
||||
string collected_at = 17;
|
||||
@ -59,7 +59,7 @@ message RegisterNodeReq {
|
||||
string country = 4;
|
||||
string region = 5;
|
||||
string city = 6;
|
||||
// nanotokens per unit per minute
|
||||
// nanoLP per unit per minute
|
||||
uint64 price = 7;
|
||||
}
|
||||
|
||||
@ -171,7 +171,7 @@ message NodeListResp {
|
||||
string ip = 5; // required for latency test
|
||||
uint32 server_rating = 6;
|
||||
uint32 provider_rating = 7;
|
||||
// nanotokens per unit per minute
|
||||
// nanoLP per unit per minute
|
||||
uint64 price = 8;
|
||||
}
|
||||
|
||||
|
70
src/data.rs
70
src/data.rs
@ -10,7 +10,7 @@ use tokio::sync::oneshot::Sender as OneshotSender;
|
||||
|
||||
#[derive(thiserror::Error, Debug)]
|
||||
pub enum Error {
|
||||
#[error("We do not allow locking of more than 100000 tokens.")]
|
||||
#[error("We do not allow locking of more than 100000 LP.")]
|
||||
TxTooBig,
|
||||
#[error("Account has insufficient funds for this operation")]
|
||||
InsufficientFunds,
|
||||
@ -21,13 +21,13 @@ pub enum Error {
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct AccountNanoTokens {
|
||||
pub struct AccountNanoLP {
|
||||
pub balance: u64,
|
||||
pub tmp_locked: u64,
|
||||
}
|
||||
|
||||
impl From<AccountNanoTokens> for grpc::AccountBalance {
|
||||
fn from(value: AccountNanoTokens) -> Self {
|
||||
impl From<AccountNanoLP> for grpc::AccountBalance {
|
||||
fn from(value: AccountNanoLP) -> Self {
|
||||
grpc::AccountBalance {
|
||||
balance: value.balance,
|
||||
tmp_locked: value.tmp_locked,
|
||||
@ -50,7 +50,7 @@ pub struct Node {
|
||||
pub avail_ipv6: u32,
|
||||
pub avail_ports: u32,
|
||||
pub max_ports_per_vm: u32,
|
||||
// nanotokens per unit per minute
|
||||
// nanoLP per unit per minute
|
||||
pub price: u64,
|
||||
}
|
||||
|
||||
@ -103,7 +103,7 @@ impl Contract {
|
||||
+ (!self.public_ipv4.is_empty() as u64 * 10)
|
||||
}
|
||||
|
||||
// Returns price per minute in nanotokens
|
||||
// Returns price per minute in nanoLP
|
||||
fn price_per_minute(&self) -> u64 {
|
||||
self.total_units() * self.price_per_unit
|
||||
}
|
||||
@ -136,8 +136,8 @@ impl Into<grpc::Contract> for Contract {
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct BrainData {
|
||||
// amount of nanotokens in each account
|
||||
accounts: DashMap<String, AccountNanoTokens>,
|
||||
// amount of nanoLP in each account
|
||||
accounts: DashMap<String, AccountNanoLP>,
|
||||
nodes: RwLock<Vec<Node>>,
|
||||
contracts: RwLock<Vec<Contract>>,
|
||||
tmp_newvm_reqs: DashMap<String, (grpc::NewVmReq, OneshotSender<grpc::NewVmResp>)>,
|
||||
@ -164,11 +164,11 @@ impl BrainData {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_balance(&self, account: &str) -> AccountNanoTokens {
|
||||
pub fn get_balance(&self, account: &str) -> AccountNanoLP {
|
||||
if let Some(account) = self.accounts.get(account) {
|
||||
return account.value().clone();
|
||||
} else {
|
||||
let balance = AccountNanoTokens {
|
||||
let balance = AccountNanoLP {
|
||||
balance: 0,
|
||||
tmp_locked: 0,
|
||||
};
|
||||
@ -180,13 +180,13 @@ impl BrainData {
|
||||
self.add_nano_to_wallet(account, 1000_000000000);
|
||||
}
|
||||
|
||||
fn add_nano_to_wallet(&self, account: &str, nanotokens: u64) {
|
||||
log::debug!("Adding {nanotokens} nanotokens to {account}");
|
||||
fn add_nano_to_wallet(&self, account: &str, nano_lp: u64) {
|
||||
log::debug!("Adding {nano_lp} nanoLP to {account}");
|
||||
self.accounts
|
||||
.entry(account.to_string())
|
||||
.and_modify(|tokens| tokens.balance += nanotokens)
|
||||
.or_insert(AccountNanoTokens {
|
||||
balance: nanotokens,
|
||||
.and_modify(|d| d.balance += nano_lp)
|
||||
.or_insert(AccountNanoLP {
|
||||
balance: nano_lp,
|
||||
tmp_locked: 0,
|
||||
});
|
||||
}
|
||||
@ -205,17 +205,17 @@ impl BrainData {
|
||||
let minutes_to_collect = (Utc::now() - c.collected_at).num_minutes() as u64;
|
||||
c.collected_at = Utc::now();
|
||||
log::debug!("{minutes_to_collect}");
|
||||
let mut nanotokens_to_collect =
|
||||
let mut nanolp_to_collect =
|
||||
c.price_per_minute().saturating_mul(minutes_to_collect);
|
||||
if nanotokens_to_collect > c.locked_nano {
|
||||
nanotokens_to_collect = c.locked_nano;
|
||||
if nanolp_to_collect > c.locked_nano {
|
||||
nanolp_to_collect = c.locked_nano;
|
||||
}
|
||||
log::debug!(
|
||||
"Removing {nanotokens_to_collect} nanotokens from {}",
|
||||
"Removing {nanolp_to_collect} nanoLP from {}",
|
||||
c.uuid
|
||||
);
|
||||
c.locked_nano -= nanotokens_to_collect;
|
||||
self.add_nano_to_wallet(&owner_key, nanotokens_to_collect);
|
||||
c.locked_nano -= nanolp_to_collect;
|
||||
self.add_nano_to_wallet(&owner_key, nanolp_to_collect);
|
||||
if c.locked_nano == 0 {
|
||||
deleted_contracts.push((c.uuid.clone(), c.node_pubkey.clone()));
|
||||
}
|
||||
@ -250,29 +250,29 @@ impl BrainData {
|
||||
nodes.push(node);
|
||||
}
|
||||
|
||||
pub fn lock_nanotockens(&self, account: &str, nanotokens: u64) -> Result<(), Error> {
|
||||
if nanotokens > 100_000_000_000_000 {
|
||||
pub fn lock_nanotockens(&self, account: &str, nano_lp: u64) -> Result<(), Error> {
|
||||
if nano_lp > 100_000_000_000_000 {
|
||||
return Err(Error::TxTooBig);
|
||||
}
|
||||
if let Some(mut account) = self.accounts.get_mut(account) {
|
||||
if nanotokens > account.balance {
|
||||
if nano_lp > account.balance {
|
||||
return Err(Error::InsufficientFunds);
|
||||
}
|
||||
account.balance = account.balance.saturating_sub(nanotokens);
|
||||
account.tmp_locked = account.tmp_locked.saturating_add(nanotokens);
|
||||
account.balance = account.balance.saturating_sub(nano_lp);
|
||||
account.tmp_locked = account.tmp_locked.saturating_add(nano_lp);
|
||||
Ok(())
|
||||
} else {
|
||||
Err(Error::InsufficientFunds)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn unlock_nanotockens(&self, account: &str, nanotokens: u64) -> Result<(), Error> {
|
||||
pub fn unlock_nanotockens(&self, account: &str, nano_lp: u64) -> Result<(), Error> {
|
||||
if let Some(mut account) = self.accounts.get_mut(account) {
|
||||
if nanotokens > account.tmp_locked {
|
||||
if nano_lp > account.tmp_locked {
|
||||
return Err(Error::ImpossibleError);
|
||||
}
|
||||
account.balance = account.balance.saturating_add(nanotokens);
|
||||
account.tmp_locked = account.tmp_locked.saturating_sub(nanotokens);
|
||||
account.balance = account.balance.saturating_add(nano_lp);
|
||||
account.tmp_locked = account.tmp_locked.saturating_sub(nano_lp);
|
||||
Ok(())
|
||||
} else {
|
||||
Err(Error::ImpossibleError)
|
||||
@ -283,9 +283,9 @@ impl BrainData {
|
||||
&self,
|
||||
uuid: &str,
|
||||
account: &str,
|
||||
nanotokens: u64,
|
||||
nano_lp: u64,
|
||||
) -> Result<(), Error> {
|
||||
if nanotokens > 100_000_000_000_000 {
|
||||
if nano_lp > 100_000_000_000_000 {
|
||||
return Err(Error::TxTooBig);
|
||||
}
|
||||
let mut account = match self.accounts.get_mut(account) {
|
||||
@ -300,11 +300,11 @@ impl BrainData {
|
||||
.find(|c| c.uuid == uuid)
|
||||
{
|
||||
Some(contract) => {
|
||||
if account.balance + contract.locked_nano < nanotokens {
|
||||
if account.balance + contract.locked_nano < nano_lp {
|
||||
return Err(Error::InsufficientFunds);
|
||||
}
|
||||
account.balance = account.balance + contract.locked_nano - nanotokens;
|
||||
contract.locked_nano = nanotokens;
|
||||
account.balance = account.balance + contract.locked_nano - nano_lp;
|
||||
contract.locked_nano = nano_lp;
|
||||
Ok(())
|
||||
}
|
||||
None => Err(Error::ContractNotFound(uuid.to_string())),
|
||||
|
Loading…
Reference in New Issue
Block a user