Skip to content

Commit

Permalink
Feat: set AccountId as trait parameter (#35)
Browse files Browse the repository at this point in the history
* Added Trait for account init method

* Refactoring ft_transfer_call

* balance_of refactoring

* Eth-connector contract AccountID changes

* Fix: AccountId tratits for Option

* Add references

* Change trait binding definitions
  • Loading branch information
mrLSD authored Jun 30, 2023
1 parent 901a511 commit 478465b
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 54 deletions.
97 changes: 55 additions & 42 deletions eth-connector/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use crate::operation::{
ViewStorageBalanceBounds, ViewStorageBalanceOf,
};
use crate::types::{MigrationInputData, PausedMask, Proof};
use aurora_engine_types::account_id::AccountId;
use aurora_engine_types::types::Address;
use aurora_workspace_utils::{Contract, ContractId};
use near_contract_standards::fungible_token::metadata::FungibleTokenMetadata;
Expand Down Expand Up @@ -42,40 +41,41 @@ impl ContractId for EthConnectorContract {
impl EthConnectorContract {
pub fn init(
&self,
prover_account: AccountId,
prover_account: &impl AsRef<str>,
eth_custodian_address: String,
metadata: FungibleTokenMetadata,
account_with_access_right: &AccountId,
owner_id: AccountId,
account_with_access_right: &impl AsRef<str>,
owner_id: &impl AsRef<str>,
) -> CallNew {
CallNew::call(&self.contract).args_json(json!({
"prover_account": prover_account,
"account_with_access_right": account_with_access_right,
"owner_id": owner_id,
"prover_account": prover_account.as_ref(),
"account_with_access_right": account_with_access_right.as_ref(),
"owner_id": owner_id.as_ref(),
"eth_custodian_address": eth_custodian_address,
"metadata": metadata,
}))
}

pub fn ft_transfer(
&self,
receiver_id: AccountId,
receiver_id: &impl AsRef<str>,
amount: U128,
memo: Option<String>,
) -> CallFtTransfer {
CallFtTransfer::call(&self.contract)
.args_json(json!({ "receiver_id": receiver_id, "amount": amount, "memo": memo }))
CallFtTransfer::call(&self.contract).args_json(
json!({ "receiver_id": receiver_id.as_ref(), "amount": amount, "memo": memo }),
)
}

pub fn ft_transfer_call(
&self,
receiver_id: AccountId,
receiver_id: &impl AsRef<str>,
amount: U128,
memo: Option<String>,
msg: String,
) -> CallFtTransferCall {
CallFtTransferCall::call(&self.contract).args_json(json!({
"receiver_id": receiver_id,
"receiver_id": receiver_id.as_ref(),
"amount": amount,
"memo": memo,
"msg": msg,
Expand All @@ -84,53 +84,57 @@ impl EthConnectorContract {

pub fn engine_ft_transfer(
&self,
sender_id: AccountId,
receiver_id: AccountId,
sender_id: &impl AsRef<str>,
receiver_id: &impl AsRef<str>,
amount: U128,
memo: Option<String>,
) -> CallEngineFtTransfer {
CallEngineFtTransfer::call(&self.contract).args_json(json!({
"sender_id": sender_id,
"receiver_id": receiver_id,
"sender_id": sender_id.as_ref(),
"receiver_id": receiver_id.as_ref(),
"amount": amount,
"memo": memo
}))
}

pub fn engine_ft_transfer_call(
&self,
sender_id: AccountId,
receiver_id: AccountId,
sender_id: &impl AsRef<str>,
receiver_id: &impl AsRef<str>,
amount: U128,
memo: Option<String>,
msg: String,
) -> CallEngineFtTransferCall {
CallEngineFtTransferCall::call(&self.contract).args_json(json!({
"sender_id": sender_id,
"receiver_id": receiver_id,
"sender_id": sender_id.as_ref(),
"receiver_id": receiver_id.as_ref(),
"amount": amount,
"memo": memo,
"msg": msg,
}))
}

pub fn set_engine_account(&self, engine_account: &AccountId) -> CallSetEngineAccount {
pub fn set_engine_account(&self, engine_account: &impl AsRef<str>) -> CallSetEngineAccount {
CallSetEngineAccount::call(&self.contract).args_json(json!({
"engine_account": engine_account,
"engine_account": engine_account.as_ref(),
}))
}

pub fn remove_engine_account(&self, engine_account: &AccountId) -> CallRemoveEngineAccount {
pub fn remove_engine_account(
&self,
engine_account: &impl AsRef<str>,
) -> CallRemoveEngineAccount {
CallRemoveEngineAccount::call(&self.contract).args_json(json!({
"engine_account": engine_account,
"engine_account": engine_account.as_ref(),
}))
}

pub fn storage_deposit(
&self,
account_id: Option<AccountId>,
account_id: Option<&impl AsRef<str>>,
registration_only: Option<bool>,
) -> CallStorageDeposit {
let account_id = account_id.map(|a| a.as_ref());
CallStorageDeposit::call(&self.contract)
.args_json(json!({ "account_id": account_id, "registration_only": registration_only}))
}
Expand All @@ -145,38 +149,39 @@ impl EthConnectorContract {

pub fn engine_storage_deposit(
&self,
sender_id: AccountId,
account_id: Option<AccountId>,
sender_id: &impl AsRef<str>,
account_id: Option<&impl AsRef<str>>,
registration_only: Option<bool>,
) -> CallEngineStorageDeposit {
let account_id = account_id.map(|a| a.as_ref());
CallEngineStorageDeposit::call(&self.contract)
.args_json(json!({ "sender_id": sender_id, "account_id": account_id, "registration_only": registration_only}))
.args_json(json!({ "sender_id": sender_id.as_ref(), "account_id": account_id, "registration_only": registration_only}))
}

pub fn engine_storage_withdraw(
&self,
sender_id: AccountId,
sender_id: &impl AsRef<str>,
amount: Option<U128>,
) -> CallEngineStorageWithdraw {
CallEngineStorageWithdraw::call(&self.contract)
.args_json(json!({ "sender_id": sender_id, "amount": amount }))
.args_json(json!({ "sender_id": sender_id.as_ref(), "amount": amount }))
}

pub fn engine_storage_unregister(
&self,
sender_id: AccountId,
sender_id: &impl AsRef<str>,
force: Option<bool>,
) -> CallEngineStorageUnregister {
CallEngineStorageUnregister::call(&self.contract)
.args_json(json!({ "sender_id": sender_id, "force": force }))
.args_json(json!({ "sender_id": sender_id.as_ref(), "force": force }))
}

pub fn set_paused_flags(&self, paused: PausedMask) -> CallSetPausedFlags {
CallSetPausedFlags::call(&self.contract).args_borsh(paused)
}

pub fn set_access_right(&self, account: AccountId) -> CallSetAccessRight {
CallSetAccessRight::call(&self.contract).args_json((account,))
pub fn set_access_right(&self, account: &impl AsRef<str>) -> CallSetAccessRight {
CallSetAccessRight::call(&self.contract).args_json((account.as_ref(),))
}

pub fn withdraw(&self, recipient_address: Address, amount: Balance) -> CallWithdraw {
Expand All @@ -185,11 +190,15 @@ impl EthConnectorContract {

pub fn engine_withdraw(
&self,
sender_id: AccountId,
sender_id: &impl AsRef<str>,
recipient_address: Address,
amount: Balance,
) -> CallEngineWithdraw {
CallEngineWithdraw::call(&self.contract).args_borsh((sender_id, recipient_address, amount))
CallEngineWithdraw::call(&self.contract).args_borsh((
sender_id.as_ref(),
recipient_address,
amount,
))
}

pub fn deposit(&self, raw_proof: Proof) -> CallDeposit {
Expand Down Expand Up @@ -234,25 +243,29 @@ impl EthConnectorContract {
ViewIsUsedProof::view(&self.contract).args_borsh(proof)
}

pub fn storage_balance_of(&self, account_id: AccountId) -> ViewStorageBalanceOf {
ViewStorageBalanceOf::view(&self.contract).args_json(json!({ "account_id": account_id }))
pub fn storage_balance_of(&self, account_id: &impl AsRef<str>) -> ViewStorageBalanceOf {
ViewStorageBalanceOf::view(&self.contract)
.args_json(json!({ "account_id": account_id.as_ref() }))
}

pub fn storage_balance_bounds(&self) -> ViewStorageBalanceBounds {
ViewStorageBalanceBounds::view(&self.contract)
}

pub fn is_engine_account_exist(&self, engine_account: &AccountId) -> ViewIsEngineAccountExist {
pub fn is_engine_account_exist(
&self,
engine_account: &impl AsRef<str>,
) -> ViewIsEngineAccountExist {
ViewIsEngineAccountExist::view(&self.contract).args_json(json!({
"engine_account": engine_account,
"engine_account": engine_account.as_ref(),
}))
}

pub fn ft_total_supply(&self) -> ViewFtTotalSupply {
ViewFtTotalSupply::view(&self.contract)
}

pub fn ft_balance_of(&self, account_id: AccountId) -> ViewFtBalanceOf {
ViewFtBalanceOf::view(&self.contract).args_json(json!((account_id,)))
pub fn ft_balance_of(&self, account_id: &impl AsRef<str>) -> ViewFtBalanceOf {
ViewFtBalanceOf::view(&self.contract).args_json(json!((account_id.as_ref(),)))
}
}
24 changes: 12 additions & 12 deletions eth-connector/tests/fungible_token_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ async fn deploy_and_init() -> anyhow::Result<EthConnectorContract> {

eth_contract
.init(
prover_account,
&prover_account,
eth_custodian_address,
metadata,
&account_with_access_right,
owner_id,
&owner_id,
)
.transact()
.await?;
Expand All @@ -54,7 +54,7 @@ async fn test_ft_transfer() {
let memo = Some(String::from("some message"));

contract
.ft_transfer(some_acc, amount, memo)
.ft_transfer(&some_acc, amount, memo)
.max_gas()
.deposit(1)
.transact()
Expand All @@ -71,7 +71,7 @@ async fn test_ft_transfer_call() {
let msg = String::from("some msg");

let res: PromiseOrValue<U128> = contract
.ft_transfer_call(some_acc, amount, memo, msg)
.ft_transfer_call(&some_acc, amount, memo, msg)
.max_gas()
.deposit(1)
.transact()
Expand Down Expand Up @@ -102,7 +102,7 @@ async fn test_ft_balance_of() {
let contract = deploy_and_init().await.unwrap();
let account = contract.as_contract().id();
let account_id = AccountId::from_str(account.as_str()).unwrap();
let res = contract.ft_balance_of(account_id).await.unwrap();
let res = contract.ft_balance_of(&account_id).await.unwrap();
let expected = ViewResult {
result: U128::from(200),
logs: vec![],
Expand Down Expand Up @@ -154,7 +154,7 @@ async fn test_storage_deposit() {
let contract = deploy_and_init().await.unwrap();
let account_id = AccountId::from_str("test.near").unwrap();
let res = contract
.storage_deposit(Some(account_id), Some(true))
.storage_deposit(Some(&account_id), Some(true))
.max_gas()
.transact()
.await
Expand Down Expand Up @@ -199,7 +199,7 @@ async fn test_engine_storage_deposit() {
let sender_id = AccountId::from_str("test.near").unwrap();
let account_id = sender_id.clone();
let res = contract
.engine_storage_deposit(sender_id, Some(account_id), Some(true))
.engine_storage_deposit(&sender_id, Some(&account_id), Some(true))
.max_gas()
.transact()
.await
Expand All @@ -215,7 +215,7 @@ async fn test_engine_storage_withdraw() {
let sender_id = AccountId::from_str("test.near").unwrap();
let amount = Some(U128::from(100));
let res = contract
.engine_storage_withdraw(sender_id, amount)
.engine_storage_withdraw(&sender_id, amount)
.max_gas()
.transact()
.await
Expand All @@ -231,7 +231,7 @@ async fn test_engine_storage_unregister() {
let sender_id = AccountId::from_str("test.near").unwrap();
let force = Some(true);
let res = contract
.engine_storage_unregister(sender_id, force)
.engine_storage_unregister(&sender_id, force)
.max_gas()
.transact()
.await
Expand All @@ -244,7 +244,7 @@ async fn test_engine_storage_unregister() {
async fn test_storage_balance_of() {
let contract = deploy_and_init().await.unwrap();
let account_id = AccountId::from_str("test.near").unwrap();
let res = contract.storage_balance_of(account_id).await.unwrap();
let res = contract.storage_balance_of(&account_id).await.unwrap();
let result = res.result;
assert_eq!(result.total, U128::from(10));
assert_eq!(result.available, U128::from(20));
Expand Down Expand Up @@ -274,7 +274,7 @@ async fn test_set_access_right() {
let contract = deploy_and_init().await.unwrap();
let account = AccountId::from_str("test.near").unwrap();
contract
.set_access_right(account)
.set_access_right(&account)
.max_gas()
.transact()
.await
Expand Down Expand Up @@ -306,7 +306,7 @@ async fn test_engine_withdraw() {
let sender_id = AccountId::from_str("test.near").unwrap();
let recipient_address = Address::zero();
let res = contract
.engine_withdraw(sender_id, recipient_address, 10)
.engine_withdraw(&sender_id, recipient_address, 10)
.max_gas()
.transact()
.await
Expand Down

0 comments on commit 478465b

Please sign in to comment.