Skip to content
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

Update Omni Bridge Factory for compatibility with Rainbow Bridge Tokens. #203

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 77 additions & 12 deletions near/omni-bridge/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,18 @@ use near_sdk::{
PanicOnDefault, Promise, PromiseError, PromiseOrValue, PromiseResult,
};
use omni_types::locker_args::{
BindTokenArgs, ClaimFeeArgs, DeployTokenArgs, FinTransferArgs, StorageDepositAction,
AddDeployedTokenArgs, BindTokenArgs, ClaimFeeArgs, DeployTokenArgs, FinTransferArgs,
StorageDepositAction,
};
use omni_types::mpc_types::SignatureResponse;
use omni_types::near_events::OmniBridgeEvent;
use omni_types::prover_args::VerifyProofArgs;
use omni_types::prover_result::ProverResult;
use omni_types::{
BasicMetadata, ChainKind, Fee, InitTransferMsg, MetadataPayload, Nonce, OmniAddress,
PayloadType, SignRequest, TransferId, TransferMessage, TransferMessagePayload, UpdateFee,
PayloadType, SignRequest, TransferId, TransferMessage, TransferMessagePayload, UpdateFee, H160,
};
use std::str::FromStr;
use storage::{Decimals, TransferMessageStorage, TransferMessageStorageValue, NEP141_DEPOSIT};

mod errors;
Expand All @@ -47,6 +49,7 @@ const BIND_TOKEN_CALLBACK_GAS: Gas = Gas::from_tgas(25);
const BIND_TOKEN_REFUND_GAS: Gas = Gas::from_tgas(5);
const FT_TRANSFER_CALL_GAS: Gas = Gas::from_tgas(125);
const FT_TRANSFER_GAS: Gas = Gas::from_tgas(5);
const UPDATE_CONTROLLER_GAS: Gas = Gas::from_tgas(250);
const WNEAR_WITHDRAW_GAS: Gas = Gas::from_tgas(10);
const STORAGE_BALANCE_OF_GAS: Gas = Gas::from_tgas(3);
const STORAGE_DEPOSIT_GAS: Gas = Gas::from_tgas(3);
Expand Down Expand Up @@ -83,6 +86,7 @@ pub enum Role {
UpgradableCodeDeployer,
MetadataManager,
UnrestrictedRelayer,
TokenControllerUpdater,
}

#[ext_contract(ext_token)]
Expand Down Expand Up @@ -127,6 +131,11 @@ pub trait ExtToken {
);
}

#[ext_contract(ext_bridge_token_facory)]
pub trait ExtBridgeTokenFactory {
fn set_controller_for_tokens(&self, tokens_account_id: Vec<AccountId>);
}

#[ext_contract(ext_signer)]
pub trait ExtSigner {
fn sign(&mut self, request: SignRequest);
Expand Down Expand Up @@ -804,6 +813,47 @@ impl Contract {
Self::refund(predecessor_account_id, refund_amount);
}

#[allow(clippy::needless_pass_by_value)]
pub fn finish_withdraw_v2(
&mut self,
#[serializer(borsh)] sender_id: &AccountId,
#[serializer(borsh)] amount: u128,
#[serializer(borsh)] recipient: String,
) {
let token_id = env::predecessor_account_id();
require!(self.deployed_tokens.contains(&token_id));

self.current_origin_nonce += 1;
let destination_nonce = self.get_next_destination_nonce(ChainKind::Eth);

let transfer_message = TransferMessage {
origin_nonce: self.current_origin_nonce,
token: OmniAddress::Near(token_id.clone()),
amount: U128(amount),
recipient: OmniAddress::Eth(
H160::from_str(&recipient).sdk_expect("Error on recipient parsing"),
),
fee: Fee {
fee: U128(0),
native_fee: U128(0),
},
sender: OmniAddress::Near(sender_id.clone()),
msg: String::new(),
destination_nonce,
};

let required_storage_balance =
self.add_transfer_message(transfer_message.clone(), sender_id.clone());

self.update_storage_balance(
env::current_account_id(),
required_storage_balance,
NearToken::from_yoctonear(0),
);

env::log_str(&OmniBridgeEvent::InitTransferEvent { transfer_message }.to_log_string());
}

pub fn get_token_address(
&self,
chain_kind: ChainKind,
Expand Down Expand Up @@ -863,28 +913,32 @@ impl Contract {

#[access_control_any(roles(Role::DAO))]
#[payable]
pub fn add_deployed_tokens(&mut self, tokens: Vec<(OmniAddress, AccountId)>) {
pub fn add_deployed_tokens(&mut self, tokens: Vec<AddDeployedTokenArgs>) {
require!(
env::attached_deposit() >= NEP141_DEPOSIT.saturating_mul(tokens.len() as u128),
"ERR_NOT_ENOUGH_ATTACHED_DEPOSIT"
);

for (token_address, token_id) in tokens {
self.deployed_tokens.insert(&token_id);
self.token_address_to_id.insert(&token_address, &token_id);
for token_info in tokens {
self.deployed_tokens.insert(&token_info.token_id);
self.token_address_to_id
.insert(&token_info.token_address, &token_info.token_id);
self.token_id_to_address.insert(
&(token_address.get_chain(), token_id.clone()),
&token_address,
&(
token_info.token_address.get_chain(),
token_info.token_id.clone(),
),
&token_info.token_address,
);
self.token_decimals.insert(
&token_address,
&token_info.token_address,
&Decimals {
decimals: 0,
origin_decimals: 0,
decimals: token_info.decimals,
origin_decimals: token_info.decimals,
},
);

ext_token::ext(token_id)
ext_token::ext(token_info.token_id)
.with_static_gas(STORAGE_DEPOSIT_GAS)
.with_attached_deposit(NEP141_DEPOSIT)
.storage_deposit(&env::current_account_id(), Some(true));
Expand Down Expand Up @@ -913,6 +967,17 @@ impl Contract {
pub fn get_mpc_account(&self) -> AccountId {
self.mpc_signer.clone()
}

#[access_control_any(roles(Role::DAO, Role::TokenControllerUpdater))]
pub fn update_tokens_controller(
&self,
factory_account_id: AccountId,
tokens_accounts_id: Vec<AccountId>,
) {
ext_bridge_token_facory::ext(factory_account_id)
.with_static_gas(UPDATE_CONTROLLER_GAS)
.set_controller_for_tokens(tokens_accounts_id);
}
}

impl Contract {
Expand Down
2 changes: 1 addition & 1 deletion near/omni-token/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use omni_ft::{MetadataManagment, MintAndBurn, UpgradeAndMigrate};
use omni_types::{BasicMetadata, OmniAddress};
const OUTER_UPGRADE_GAS: Gas = Gas::from_tgas(15);
const NO_DEPOSIT: NearToken = NearToken::from_yoctonear(0);
const CURRENT_STATE_VERSION: u32 = 1;
const CURRENT_STATE_VERSION: u32 = 3;

pub mod omni_ft;

Expand Down
10 changes: 9 additions & 1 deletion near/omni-types/src/locker_args.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use near_sdk::{near, AccountId};

use crate::ChainKind;
use crate::{ChainKind, OmniAddress};

#[near(serializers = [borsh, json])]
#[derive(Clone)]
Expand Down Expand Up @@ -38,3 +38,11 @@ pub struct DeployTokenArgs {
pub chain_kind: ChainKind,
pub prover_args: Vec<u8>,
}

#[near(serializers = [borsh, json])]
#[derive(Clone)]
pub struct AddDeployedTokenArgs {
pub token_id: AccountId,
pub token_address: OmniAddress,
pub decimals: u8,
}
Loading