Skip to content

Commit

Permalink
Merge pull request #58 from SugarFunge/feature/polkadot-v1.6.0
Browse files Browse the repository at this point in the history
Feature/polkadot v1.6.0
  • Loading branch information
MauricinhoMorales authored Apr 8, 2024
2 parents ffb50bf + 9c51b51 commit 6cfc5d9
Show file tree
Hide file tree
Showing 18 changed files with 3,446 additions and 1,143 deletions.
3,956 changes: 2,946 additions & 1,010 deletions Cargo.lock

Large diffs are not rendered by default.

33 changes: 17 additions & 16 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ std = ["codec/std", "scale-info/std"]
[dependencies]
sugarfunge-api-types = { path = "crates/sugarfunge-api-types" }
env_logger = "0.10.0"
clap = { version = "4.3.0", features = ["derive"] }
clap = { version = "4.2.5", features = ["derive"] }
rand = "0.8.5"
hex = "0.4.3"
futures = "0.3"
Expand All @@ -23,40 +23,41 @@ actix-web = "4.3"
actix-web-actors = "4.2"
actix-cors = "0.6.4"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
serde_json = "1.0.96"
jsonrpsee-types = "0.18.2"
url = { version = "2", features = ["serde"] }
derive_more = { version = "0.99.17", default-features = false }
codec = { package = "parity-scale-codec", version = "3.2.2", default-features = false, features = ["derive", "full", "bit-vec"] }
codec = { package = "parity-scale-codec", version = "3.6.1", default-features = false, features = ["derive", "full", "bit-vec"] }
scale-info = { version = "2.5.0", features = ["bit-vec"] }

[dependencies.subxt]
git = "https://github.com/SugarFunge/subxt.git"
branch = "feature/polkadot-v0.9.43"
git = "https://github.com/paritytech/subxt.git"
branch = "master"
features = ["substrate-compat"]

[dependencies.sp-keyring]
git = "https://github.com/paritytech/substrate.git"
branch = "polkadot-v0.9.43"
git = "https://github.com/paritytech/polkadot-sdk.git"
branch = "release-polkadot-v1.6.0"
package = "sp-keyring"

[dependencies.sp-std]
git = "https://github.com/paritytech/substrate.git"
branch = "polkadot-v0.9.43"
git = "https://github.com/paritytech/polkadot-sdk.git"
branch = "release-polkadot-v1.6.0"
package = "sp-std"

[dependencies.sp-io]
git = "https://github.com/paritytech/substrate.git"
branch = "polkadot-v0.9.43"
git = "https://github.com/paritytech/polkadot-sdk.git"
branch = "release-polkadot-v1.6.0"
package = "sp-io"
features = []

[dependencies.sp-core]
git = "https://github.com/paritytech/substrate.git"
branch = "polkadot-v0.9.43"
git = "https://github.com/paritytech/polkadot-sdk.git"
branch = "release-polkadot-v1.6.0"
package = "sp-core"
features = ["full_crypto"]

[dependencies.sp-runtime]
git = "https://github.com/paritytech/substrate.git"
branch = "polkadot-v0.9.43"
package = "sp-runtime"
git = "https://github.com/paritytech/polkadot-sdk.git"
branch = "release-polkadot-v1.6.0"
package = "sp-runtime"
13 changes: 7 additions & 6 deletions crates/sugarfunge-api-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,17 @@ scale-info = { version = "2.0.0", features = ["bit-vec"] }
bevy_derive = "0.11"

[dependencies.subxt]
git = "https://github.com/SugarFunge/subxt.git"
branch = "feature/polkadot-v0.9.43"
git = "https://github.com/paritytech/subxt.git"
branch = "master"
features = ["substrate-compat"]

[dependencies.sp-core]
git = "https://github.com/paritytech/substrate.git"
branch = "polkadot-v0.9.43"
git = "https://github.com/paritytech/polkadot-sdk.git"
branch = "release-polkadot-v1.6.0"
package = "sp-core"
features = ["full_crypto"]

[dependencies.sp-runtime]
git = "https://github.com/paritytech/substrate.git"
branch = "polkadot-v0.9.43"
git = "https://github.com/paritytech/polkadot-sdk.git"
branch = "release-polkadot-v1.6.0"
package = "sp-runtime"
13 changes: 13 additions & 0 deletions crates/sugarfunge-api-types/src/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,16 @@ pub struct SeededAccountOutput {
pub seed: Seed,
pub account: Account,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct SetBalanceInput {
pub seed: Seed,
pub to: Account,
pub amount: Balance,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct SetBalanceOutput {
pub account: Account,
pub amount: Balance,
}
1 change: 1 addition & 0 deletions crates/sugarfunge-api-types/src/asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ pub struct AssetBalanceOutput {
pub struct AssetBalancesInput {
pub account: Account,
pub class_id: Option<ClassId>,
pub asset_id: Option<AssetId>,
}

#[derive(Serialize, Deserialize, Debug)]
Expand Down
27 changes: 27 additions & 0 deletions crates/sugarfunge-api-types/src/bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,30 @@ pub struct BurnBundleOutput {
pub bundle_id: BundleId,
pub amount: Balance,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct GetBundles {
pub bundles: Vec<BundleItem>,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct BundleItem {
pub class_id: ClassId,
pub asset_id: AssetId,
pub bundle_id: BundleId,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct GetBundlesData {
pub bundles: Vec<BundleDataItem>,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct BundleDataItem {
pub bundle_id: BundleId,
pub creator: Account,
pub class_id: ClassId,
pub asset_id: AssetId,
pub metadata: serde_json::Value,
pub schema: BundleSchema,
}
7 changes: 5 additions & 2 deletions crates/sugarfunge-api-types/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
#[subxt::subxt(
runtime_metadata_path = "sugarfunge_metadata.scale",
derive_for_type(path = "frame_support::traits::tokens::misc::BalanceStatus", derive = "serde::Serialize"),
derive_for_type(
path = "frame_support::traits::tokens::misc::BalanceStatus",
derive = "serde::Serialize"
),
derive_for_type(path = "pallet_balances::pallet::Event", derive = "serde::Serialize"),
derive_for_type(path = "sugarfunge_asset::pallet::Event", derive = "serde::Serialize"),
derive_for_type(path = "sugarfunge_bag::pallet::Event", derive = "serde::Serialize"),
derive_for_type(path = "sugarfunge_bag::pallet::Event", derive = "serde::Serialize")
)]
pub mod sugarfunge {}
pub mod account;
Expand Down
13 changes: 13 additions & 0 deletions crates/sugarfunge-api-types/src/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,16 @@ pub struct RemoveValidatorInput {
pub struct RemoveValidatorOutput {
pub validator_id: ValidatorId,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct SetKeysInput {
pub seed: Seed,
pub aura: String,
pub grandpa: String,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct SetKeysOutput {
pub aura: String,
pub grandpa: String,
}
Binary file modified crates/sugarfunge-api-types/sugarfunge_metadata.scale
Binary file not shown.
60 changes: 53 additions & 7 deletions src/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ use crate::util::*;
use actix_web::{error, web, HttpRequest, HttpResponse};
use rand::prelude::*;
use serde_json::json;
use sp_core::Pair;
use sp_runtime::traits::IdentifyAccount;
use subxt::ext::sp_core::sr25519::Public as SubxtPublic;
use subxt::ext::sp_core::Pair;
use subxt::ext::sp_runtime::traits::IdentifyAccount;
use subxt::tx::PairSigner;
use sugarfunge_api_types::account::*;
use sugarfunge_api_types::primitives::*;
Expand All @@ -17,7 +18,7 @@ pub async fn create(_req: HttpRequest) -> error::Result<HttpResponse> {
let seed = format!("//{}", seed);
let seed = Seed::from(seed);
let pair = get_pair_from_seed(&seed)?;
let account: sp_core::sr25519::Public = pair.public();
let account: SubxtPublic = pair.public();
let account = account.into_account();
Ok(HttpResponse::Ok().json(CreateAccountOutput {
seed,
Expand All @@ -40,15 +41,16 @@ pub async fn fund(
data: web::Data<AppState>,
req: web::Json<FundAccountInput>,
) -> error::Result<HttpResponse> {
let pair = get_pair_from_seed(&req.seed)?;
//let signer = sp_core::sr25519::Pair::try_from(pair).unwrap();
let pair = get_pair_from_seed(&req.seed).unwrap(); // Assuming get_pair_from_seed now returns the correct type
let signer = PairSigner::new(pair);
let account = subxt::utils::AccountId32::try_from(&req.to).map_err(map_account_err)?;
let account = subxt::utils::MultiAddress::Id(account);
let amount_input = req.amount;
let api = &data.api;

let call = sugarfunge::tx().balances().transfer(account, amount_input.into());
let call = sugarfunge::tx()
.balances()
.transfer_keep_alive(account, amount_input.into());

let result = api
.tx()
Expand Down Expand Up @@ -95,7 +97,7 @@ pub async fn balance(
message: json!("Failed to find sugarfunge::balances::events::balance"),
description: "Error in account::balance".to_string(),
})),
}
}
}

/// Check if account exists and is active
Expand All @@ -122,3 +124,47 @@ pub async fn exists(
})),
}
}

/// Fund a given account with amount
pub async fn set_balance(
data: web::Data<AppState>,
req: web::Json<SetBalanceInput>,
) -> error::Result<HttpResponse> {
let pair = get_pair_from_seed(&req.seed).unwrap(); // Assuming get_pair_from_seed now returns the correct type
let signer = PairSigner::new(pair);
let account = subxt::utils::AccountId32::try_from(&req.to).map_err(map_account_err)?;
let account = subxt::utils::MultiAddress::Id(account);
let amount_input = req.amount;
let api = &data.api;

let call = sugarfunge::runtime_types::pallet_balances::pallet::Call::force_set_balance {
who: account,
new_free: amount_input.into(),
};

let call = sugarfunge::runtime_types::sugarfunge_runtime::RuntimeCall::Balances(call);

let sudo_call = sugarfunge::tx().sudo().sudo(call);

let result = api
.tx()
.sign_and_submit_then_watch(&sudo_call, &signer, Default::default())
.await
.map_err(map_subxt_err)?
.wait_for_finalized_success()
.await
.map_err(map_sf_err)?;
let result = result
.find_first::<sugarfunge::balances::events::BalanceSet>()
.map_err(map_subxt_err)?;
match result {
Some(event) => Ok(HttpResponse::Ok().json(SetBalanceOutput {
account: event.who.into(),
amount: event.free.into(),
})),
None => Ok(HttpResponse::BadRequest().json(RequestError {
message: json!("Failed to find sugarfunge::balances::events::BalanceSet"),
description: "Error in account::set_balance".to_string(),
})),
}
}
Loading

0 comments on commit 6cfc5d9

Please sign in to comment.