Skip to content

Commit

Permalink
NEP141 connector: minor fixes and improvements (#239)
Browse files Browse the repository at this point in the history
* Minor cleanup and improvments

* Update contract build

* Update contract build

* Fix `erc20-connector` ci tests
  • Loading branch information
karim-en authored May 14, 2024
1 parent 59bd76b commit 7f1a948
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 67 deletions.
12 changes: 6 additions & 6 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ on:
name: Tests
jobs:
build:
runs-on: self-hosted
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
Expand Down Expand Up @@ -46,7 +46,7 @@ jobs:
fi
test-erc20-connector:
runs-on: self-hosted
runs-on: ubuntu-latest
name: Test erc20-connector
steps:
- name: Clone the repository
Expand All @@ -58,7 +58,7 @@ jobs:
yarn test
test-erc20-bridge-token:
runs-on: self-hosted
runs-on: ubuntu-latest
name: Test erc20-bridge-token
steps:
- name: Clone the repository
Expand All @@ -70,7 +70,7 @@ jobs:
yarn test
test-bridge-token-factory:
runs-on: self-hosted
runs-on: ubuntu-latest
name: Test bridge-token-factory
steps:
- name: Clone the repository
Expand All @@ -80,7 +80,7 @@ jobs:
make test
test-token-locker:
runs-on: self-hosted
runs-on: ubuntu-latest
name: Test token-locker
steps:
- name: Clone the repository
Expand All @@ -90,7 +90,7 @@ jobs:
make test-token-locker
test-silo-to-silo:
runs-on: self-hosted
runs-on: ubuntu-latest
name: Test silo-to-silo
steps:
- name: Install Foundry
Expand Down
2 changes: 2 additions & 0 deletions bridge-common/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use near_sdk::borsh::{self, BorshDeserialize, BorshSerialize};
use near_sdk::AccountId;

pub mod prover;
pub mod result_types;

#[derive(BorshSerialize, BorshDeserialize)]
pub struct Recipient {
pub target: AccountId,
pub message: Option<String>,
Expand Down
2 changes: 1 addition & 1 deletion erc20-connector/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"dependencies": {
"@openzeppelin/cli": "^2.8.1",
"@openzeppelin/contracts": "^4.7",
"rainbow-bridge-sol": "https://gitpkg.now.sh/aurora-is-near/rainbow-bridge/contracts/eth?c80ff19c74a00ee2d08907668f95f4f99aee8356",
"rainbow-bridge-sol": "https://gitpkg.now.sh/Near-One/rainbow-bridge/contracts/eth?c80ff19c74a00ee2d08907668f95f4f99aee8356",
"rainbow-bridge-lib": "https://github.com/near/rainbow-bridge-lib",
"truffle-assertions": "^0.9.2"
},
Expand Down
2 changes: 1 addition & 1 deletion erc20-connector/scripts/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ if [ "$SOLC_NIGHTLY" = true ]; then
wget -q https://raw.githubusercontent.com/ethereum/solc-bin/gh-pages/bin/soljson-nightly.js -O /tmp/soljson.js && find . -name soljson.js -exec cp /tmp/soljson.js {} \;
fi

truffle version
node_modules/.bin/truffle version

if [ "$SOLIDITY_COVERAGE" = true ]; then
node_modules/.bin/truffle run coverage --network soliditycoverage
Expand Down
4 changes: 2 additions & 2 deletions erc20-connector/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -11412,9 +11412,9 @@ queue-microtask@^1.2.3:
tweetnacl "^1.0.3"
web3 "=1.2.6"

"rainbow-bridge-sol@https://gitpkg.now.sh/aurora-is-near/rainbow-bridge/contracts/eth?c80ff19c74a00ee2d08907668f95f4f99aee8356":
"rainbow-bridge-sol@https://gitpkg.now.sh/Near-One/rainbow-bridge/contracts/eth?c80ff19c74a00ee2d08907668f95f4f99aee8356":
version "2.0.1"
resolved "https://gitpkg.now.sh/aurora-is-near/rainbow-bridge/contracts/eth?c80ff19c74a00ee2d08907668f95f4f99aee8356#806863cccf1e4a35ee22d2ffd18d7b50f894dff7"
resolved "https://gitpkg.now.sh/Near-One/rainbow-bridge/contracts/eth?c80ff19c74a00ee2d08907668f95f4f99aee8356#e4a8e9911f60384ef25fc0328aabc51d4b378a8e"

randomatic@^3.0.0:
version "3.1.1"
Expand Down
Binary file modified res/bridge_token_factory.wasm
Binary file not shown.
Binary file modified res/rainbow_bridge_near_token_locker.wasm
Binary file not shown.
99 changes: 42 additions & 57 deletions token-locker/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use near_plugins::{
access_control, access_control_any, pause, AccessControlRole, AccessControllable, Pausable,
Upgradable,
};
use std::convert::TryInto;

use near_contract_standards::fungible_token::metadata::FungibleTokenMetadata;
use near_contract_standards::storage_management::StorageBalance;
Expand Down Expand Up @@ -82,9 +81,6 @@ pub struct Contract {
pub eth_factory_address: EthAddress,
/// Hashes of the events that were already used.
pub used_events: UnorderedSet<Vec<u8>>,
/// Mask determining all paused functions
#[deprecated]
paused: Mask,
/// Mapping whitelisted tokens to their mode
pub whitelist_tokens: UnorderedMap<AccountId, WhitelistMode>,
/// Mapping whitelisted accounts to their whitelisted tokens by using combined key {token}:{account}
Expand All @@ -109,10 +105,10 @@ pub trait ExtContract {
#[callback]
#[serializer(borsh)]
verification_success: bool,
#[serializer(borsh)] token: String,
#[serializer(borsh)] new_owner_id: String,
#[serializer(borsh)] token: AccountId,
#[serializer(borsh)] recipient: Recipient,
#[serializer(borsh)] amount: Balance,
#[serializer(borsh)] proof: Proof,
#[serializer(borsh)] proof_key: Vec<u8>,
) -> Promise;

#[result_serializer(borsh)]
Expand All @@ -126,8 +122,8 @@ pub trait ExtContract {
&self,
#[callback] storage_balance: Option<StorageBalance>,
#[serializer(borsh)] proof: Proof,
#[serializer(borsh)] token: String,
#[serializer(borsh)] recipient: AccountId,
#[serializer(borsh)] token: AccountId,
#[serializer(borsh)] recipient: Recipient,
#[serializer(borsh)] amount: Balance,
);
}
Expand Down Expand Up @@ -165,7 +161,6 @@ impl Contract {
prover_account,
used_events: UnorderedSet::new(StorageKey::UsedEvents),
eth_factory_address: validate_eth_address(factory_address),
paused: Mask::default(),
whitelist_tokens: UnorderedMap::new(StorageKey::WhitelistTokens),
whitelist_accounts: UnorderedSet::new(StorageKey::WhitelistAccounts),
is_whitelist_mode_enabled: true,
Expand Down Expand Up @@ -218,14 +213,15 @@ impl Contract {
hex::encode(&self.eth_factory_address),
);

let Recipient {
target: recipient_account_id,
message: _,
} = parse_recipient(event.recipient);
let recipient = parse_recipient(event.recipient);
let token: AccountId = event
.token
.parse()
.unwrap_or_else(|_| env::panic_str("Invalid token id"));

ext_token::ext(event.token.clone().try_into().unwrap())
ext_token::ext(token.clone())
.with_static_gas(STORAGE_BALANCE_CALL_GAS)
.storage_balance_of(Some(recipient_account_id.clone()))
.storage_balance_of(Some(recipient.target.clone()))
.then(
ext_self::ext(env::current_account_id())
.with_static_gas(
Expand All @@ -234,12 +230,7 @@ impl Contract {
+ FT_TRANSFER_CALL_GAS,
)
.with_attached_deposit(env::attached_deposit())
.storage_balance_callback(
proof,
event.token,
recipient_account_id,
event.amount,
),
.storage_balance_callback(proof, token, recipient, event.amount),
)
}

Expand All @@ -248,17 +239,17 @@ impl Contract {
&self,
#[callback] storage_balance: Option<StorageBalance>,
#[serializer(borsh)] proof: Proof,
#[serializer(borsh)] token: String,
#[serializer(borsh)] recipient: AccountId,
#[serializer(borsh)] token: AccountId,
#[serializer(borsh)] recipient: Recipient,
#[serializer(borsh)] amount: Balance,
) -> Promise {
assert!(
storage_balance.is_some(),
"The account {} is not registered",
recipient
recipient.target
);

let proof_1 = proof.clone();
let proof_key = proof.get_key();
ext_prover::ext(self.prover_account.clone())
.with_static_gas(VERIFY_LOG_ENTRY_GAS)
.with_attached_deposit(NO_DEPOSIT)
Expand All @@ -275,7 +266,7 @@ impl Contract {
ext_self::ext(env::current_account_id())
.with_attached_deposit(env::attached_deposit())
.with_static_gas(FINISH_WITHDRAW_GAS + FT_TRANSFER_CALL_GAS)
.finish_withdraw(token, recipient.to_string(), amount, proof_1),
.finish_withdraw(token, recipient, amount, proof_key),
)
}

Expand All @@ -297,32 +288,23 @@ impl Contract {
#[callback]
#[serializer(borsh)]
verification_success: bool,
#[serializer(borsh)] token: String,
#[serializer(borsh)] new_owner_id: String,
#[serializer(borsh)] token: AccountId,
#[serializer(borsh)] recipient: Recipient,
#[serializer(borsh)] amount: Balance,
#[serializer(borsh)] proof: Proof,
#[serializer(borsh)] proof_key: Vec<u8>,
) -> Promise {
assert!(verification_success, "Failed to verify the proof");
let required_deposit = self.record_proof(&proof);

let required_deposit = self.record_proof(&proof_key);
assert!(env::attached_deposit() >= required_deposit);

let Recipient { target, message } = parse_recipient(new_owner_id);

env::log_str(
format!(
"Finish deposit. Token:{} Target:{} Message:{:?}",
token, target, message
)
.as_str(),
);
let Recipient { target, message } = recipient;

match message {
Some(message) => ext_token::ext(token.try_into().unwrap())
Some(message) => ext_token::ext(token)
.with_attached_deposit(near_sdk::ONE_YOCTO)
.with_static_gas(FT_TRANSFER_CALL_GAS)
.ft_transfer_call(target, amount.into(), None, message),
None => ext_token::ext(token.try_into().unwrap())
None => ext_token::ext(token)
.with_attached_deposit(near_sdk::ONE_YOCTO)
.with_static_gas(FT_TRANSFER_GAS)
.ft_transfer(target, amount.into(), None),
Expand All @@ -334,14 +316,15 @@ impl Contract {
self.used_events.contains(&proof.get_key())
}

#[access_control_any(roles(Role::DAO))]
pub fn update_factory_address(&mut self, factory_address: String) {
self.eth_factory_address = validate_eth_address(factory_address);
}

/// Record proof to make sure it is not re-used later for anther withdrawal.
#[private]
fn record_proof(&mut self, proof: &Proof) -> Balance {
// TODO: Instead of sending the full proof (clone only relevant parts of the Proof)
// log_index / receipt_index / header_data
fn record_proof(&mut self, proof_key: &Vec<u8>) -> Balance {
let initial_storage = env::storage_usage();

let proof_key = proof.get_key();
assert!(
!self.used_events.contains(&proof_key),
"Event cannot be reused for withdrawing."
Expand All @@ -354,11 +337,6 @@ impl Contract {
env::log_str(&format!("RecordProof:{}", hex::encode(proof_key)));
required_deposit
}

#[access_control_any(roles(Role::DAO))]
pub fn update_factory_address(&mut self, factory_address: String) {
self.eth_factory_address = validate_eth_address(factory_address);
}
}

#[cfg(not(target_arch = "wasm32"))]
Expand All @@ -379,6 +357,13 @@ mod tests {
AccountId::new_unchecked(near_sdk::test_utils::accounts(id).to_string() + ".near")
}

pub fn recipients(id: usize) -> Recipient {
Recipient {
target: accounts(id),
message: None,
}
}

macro_rules! inner_set_env {
($builder:ident) => {
$builder
Expand Down Expand Up @@ -474,9 +459,9 @@ mod tests {
contract.finish_withdraw(
true,
accounts(1).into(),
accounts(2).into(),
recipients(2),
1_000_000,
proof,
proof.get_key(),
);
}

Expand All @@ -503,9 +488,9 @@ mod tests {
contract.finish_withdraw(
true,
accounts(1).into(),
accounts(2).into(),
recipients(2),
1_000_000,
proof,
proof.get_key(),
);
}

Expand Down

0 comments on commit 7f1a948

Please sign in to comment.