Skip to content

Commit

Permalink
feat: Allow asset transfer via frame-babel (#76)
Browse files Browse the repository at this point in the history
* feat: Allow asset transfer via frame-babel

* feat: Adjust weight for babel transfer

* feat(sidecar): Add host setting to config

---------

Co-authored-by: code0xff <[email protected]>
  • Loading branch information
conr2d and code0xff authored Oct 14, 2024
1 parent 4a68bbc commit b4b513a
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 16 deletions.
43 changes: 35 additions & 8 deletions frame/babel/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#![cfg_attr(not(feature = "std"), no_std)]
#![allow(clippy::too_many_arguments)]

extern crate alloc;

#[cfg(feature = "cosmos")]
Expand All @@ -40,7 +41,11 @@ pub mod pallet {
use cosmos_sdk_proto::{cosmos::tx::v1beta1::Tx, traits::Message};
use frame_support::{
pallet_prelude::*,
traits::{fungible::Mutate, tokens::Preservation::Preserve},
traits::{
fungible::Mutate as _,
fungibles::Mutate,
tokens::Preservation::{Expendable, Preserve},
},
};
use frame_system::{ensure_root, pallet_prelude::*};
use pallet_cosmos::{
Expand All @@ -53,11 +58,12 @@ pub mod pallet {
use pallet_multimap::traits::{UniqueMap, UniqueMultimap};
use sp_core::ecdsa;
use sp_runtime::{
traits::{One, Saturating, StaticLookup, UniqueSaturatedInto},
traits::{AtLeast32BitUnsigned, One, Saturating, StaticLookup, UniqueSaturatedInto},
AccountId32,
};

type AccountIdLookupOf<T> = <<T as frame_system::Config>::Lookup as StaticLookup>::Source;
type BalanceOf<T> = <T as Config>::Balance;

#[pallet::config]
pub trait Config:
Expand All @@ -70,6 +76,16 @@ pub mod pallet {
{
type AddressMap: UniqueMultimap<Self::AccountId, VarAddress>;
type AssetMap: UniqueMap<AssetIdOf<Self>, DenomOf<Self>>;
type Balance: Member
+ Parameter
+ AtLeast32BitUnsigned
+ Default
+ Copy
+ MaybeSerializeDeserialize
+ MaxEncodedLen
+ TypeInfo
+ Into<<Self as pallet_balances::Config>::Balance>
+ Into<<Self as pallet_assets::Config>::Balance>;
}

#[pallet::pallet]
Expand Down Expand Up @@ -226,17 +242,17 @@ pub mod pallet {

#[pallet::call_index(3)]
#[pallet::weight({
use pallet_balances::weights::WeightInfo;
use pallet_assets::weights::WeightInfo;
<T as pallet_balances::Config>::WeightInfo::transfer_keep_alive()
<T as pallet_assets::Config>::WeightInfo::transfer()
})]
pub fn transfer(
origin: OriginFor<T>,
id: Option<T::AssetIdParameter>,
dest: VarAddress,
#[pallet::compact] value: <T as pallet_balances::Config>::Balance,
#[pallet::compact] value: BalanceOf<T>,
) -> DispatchResult {
let source = ensure_signed(origin.clone())?;

let who = ensure_signed(origin)?;
let dest: T::AccountId = match dest {
VarAddress::Cosmos(address) =>
<T as pallet_cosmos::Config>::AddressMapping::into_account_id(address.into()),
Expand All @@ -245,7 +261,18 @@ pub mod pallet {
VarAddress::Polkadot(address) => address.into(),
};

pallet_balances::Pallet::<T>::transfer(&source, &dest, value, Preserve).map(|_| ())
match id {
Some(id) => <pallet_assets::Pallet<T> as Mutate<T::AccountId>>::transfer(
id.into(),
&who,
&dest,
value.into(),
Expendable,
)
.map(|_| ()),
None => pallet_balances::Pallet::<T>::transfer(&who, &dest, value.into(), Preserve)
.map(|_| ()),
}
}
}
}
5 changes: 3 additions & 2 deletions sidecar/README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# cosmos-api-sidecar
Cosmos API Sidecar for Noir
# Cosmos API Sidecar

Cosmos API Sidecar
3 changes: 2 additions & 1 deletion sidecar/config/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
},
"server": {
"endpoint": "http://localhost:1317",
"host": "localhost",
"port": 1317,
"logger": false
},
"chain": {
"endpoint": "ws://127.0.0.1:9944",
"endpoint": "ws://localhost:9944",
"denom": "azig"
}
}
5 changes: 3 additions & 2 deletions sidecar/package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{
"name": "@noir/sidecar",
"name": "sidecar",
"version": "0.1.0",
"description": "Cosmos API Sidecar for Noir",
"description": "Cosmos API Sidecar",
"main": "dist/main.js",
"private": true,
"scripts": {
"build": "tsc",
"lint": "npx eslint ./src",
Expand Down
5 changes: 3 additions & 2 deletions sidecar/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,11 @@ export class App {
}

async start() {
const host = this.config.get<string>('server.host');
const port = this.config.get<number>('server.port');
console.debug(`Start to listen on port: ${port}`);
console.debug(`Start to listen on ${host}:${port}`);

await this.server.listen({ port });
await this.server.listen({ host, port });
}

async initDatabase() {
Expand Down
2 changes: 1 addition & 1 deletion sidecar/src/services/abci.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ export class AbciService implements ApiService {
height: Long.fromString(height.toString()),
codespace: '',
};
} catch (e: unknown) {
} catch (e: any) {
const message = e.toString();
const codespace = message.slice(message.indexOf('codespace:') + 'codespace:'.length, message.indexOf('code:')).trim();
const code = message.slice(message.indexOf('code:') + 'code:'.length, message.indexOf('}')).trim();
Expand Down

0 comments on commit b4b513a

Please sign in to comment.