Skip to content

Commit

Permalink
GQL Ports: Moved over more contract endpoints (#893)
Browse files Browse the repository at this point in the history
  • Loading branch information
ControlCplusControlV authored Jan 7, 2023
1 parent 5b97a28 commit 862a67a
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 41 deletions.
68 changes: 66 additions & 2 deletions crates/fuel-core/src/query/contract.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,36 @@
use crate::database::Database;
use fuel_core_storage::{
tables::ContractsRawCode,
not_found,
tables::{
ContractsAssets,
ContractsInfo,
ContractsRawCode,
},
Result as StorageResult,
StorageAsRef,
};
use fuel_core_types::fuel_types::ContractId;
use fuel_core_types::{
fuel_types::{
AssetId,
ContractId,
},
fuel_vm::Salt,
};

#[cfg_attr(test, mockall::automock)]
/// Trait that specifies all the data required by the output message query.
pub trait ContractQueryData {
fn contract(&self, id: ContractId) -> StorageResult<Option<ContractId>>;

fn contract_bytecode(&self, id: ContractId) -> StorageResult<Vec<u8>>;

fn contract_salt(&self, id: ContractId) -> StorageResult<Salt>;

fn contract_balance(
&self,
contract: ContractId,
asset: AssetId,
) -> StorageResult<(ContractId, u64, AssetId)>;
}

pub struct ContractQueryContext<'a>(pub &'a Database);
Expand All @@ -24,4 +45,47 @@ impl ContractQueryData for ContractQueryContext<'_> {

Ok(Some(id))
}

fn contract_balance(
&self,
contract_id: ContractId,
asset_id: AssetId,
) -> StorageResult<(ContractId, u64, AssetId)> {
let db = self.0;

let result = db
.storage::<ContractsAssets>()
.get(&(&contract_id, &asset_id))?;

let balance = match result {
Some(balance) => balance.into_owned(),
None => return Err(not_found!(ContractsAssets)),
};

Ok((contract_id, balance, asset_id))
}

fn contract_bytecode(&self, id: ContractId) -> StorageResult<Vec<u8>> {
let db = self.0;

let contract = db
.storage::<ContractsRawCode>()
.get(&id)?
.ok_or(not_found!(ContractsRawCode))?
.into_owned();

Ok(contract.into())
}

fn contract_salt(&self, id: ContractId) -> StorageResult<Salt> {
let db = self.0;

let (salt, _) = db
.storage::<ContractsInfo>()
.get(&id)?
.ok_or(not_found!(ContractsInfo))?
.into_owned();

Ok(salt)
}
}
52 changes: 13 additions & 39 deletions crates/fuel-core/src/schema/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use crate::{
U64,
},
};
use anyhow::anyhow;
use async_graphql::{
connection::{
Connection,
Expand All @@ -22,15 +21,6 @@ use async_graphql::{
InputObject,
Object,
};
use fuel_core_storage::{
not_found,
tables::{
ContractsAssets,
ContractsInfo,
ContractsRawCode,
},
StorageAsRef,
};
use fuel_core_types::fuel_types;

pub struct Contract(pub(crate) fuel_types::ContractId);
Expand All @@ -48,27 +38,18 @@ impl Contract {
}

async fn bytecode(&self, ctx: &Context<'_>) -> async_graphql::Result<HexString> {
let db = ctx.data_unchecked::<Database>().clone();
let contract = db
.storage::<ContractsRawCode>()
.get(&self.0)?
.ok_or(not_found!(ContractsRawCode))?
.into_owned();
Ok(HexString(contract.into()))
let context = ContractQueryContext(ctx.data_unchecked());

let bytecode = context.contract_bytecode(self.0)?;

Ok(HexString(bytecode))
}
async fn salt(&self, ctx: &Context<'_>) -> async_graphql::Result<Salt> {
let contract_id = self.0;
let context = ContractQueryContext(ctx.data_unchecked());

let db = ctx.data_unchecked::<Database>().clone();
let (salt, _) = db
.storage::<ContractsInfo>()
.get(&contract_id)?
.ok_or_else(|| anyhow!("Contract does not exist"))?
.into_owned();

let cleaned_salt: Salt = salt.into();
let salt = context.contract_salt(self.0)?;

Ok(cleaned_salt)
Ok(salt.into())
}
}

Expand Down Expand Up @@ -132,21 +113,14 @@ impl ContractBalanceQuery {
contract: ContractId,
asset: AssetId,
) -> async_graphql::Result<ContractBalance> {
let contract_id: fuel_types::ContractId = contract.0;

let db = ctx.data_unchecked::<Database>().clone();

let asset_id: fuel_types::AssetId = asset.into();
let context = ContractQueryContext(ctx.data_unchecked());

let result = db
.storage::<ContractsAssets>()
.get(&(&contract_id, &asset_id))?;
let balance = result.unwrap_or_default().into_owned();
let balance = context.contract_balance(contract.into(), asset.into())?;

Ok(ContractBalance {
contract: contract.into(),
amount: balance,
asset_id,
contract: balance.0,
amount: balance.1,
asset_id: balance.2,
})
}

Expand Down

0 comments on commit 862a67a

Please sign in to comment.