Skip to content

Commit

Permalink
make contract commit to a close method
Browse files Browse the repository at this point in the history
  • Loading branch information
zoedberg committed Jan 14, 2025
1 parent 2c40b9c commit 6655618
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 54 deletions.
72 changes: 34 additions & 38 deletions src/rgb20/issuer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

use std::str::FromStr;

use bp::dbc::Method;
use bp::seals::txout::CloseMethod;
use rgbstd::containers::ValidContract;
use rgbstd::interface::{BuilderError, ContractBuilder, IfaceClass, TxOutpoint};
use rgbstd::invoice::{Amount, Precision};
Expand Down Expand Up @@ -62,35 +62,47 @@ pub struct PrimaryIssue {

impl PrimaryIssue {
pub fn testnet_with(
close_method: CloseMethod,
issuer: SchemaIssuer<Rgb20>,
by: &str,
ticker: &str,
name: &str,
details: Option<&str>,
precision: Precision,
) -> Result<Self, InvalidRString> {
Self::testnet_int(issuer, by, ticker, name, details, precision, false)
Self::testnet_int(close_method, issuer, by, ticker, name, details, precision, false)
}

pub fn testnet<C: IssuerWrapper<IssuingIface = Rgb20>>(
close_method: CloseMethod,
by: &str,
ticker: &str,
name: &str,
details: Option<&str>,
precision: Precision,
) -> Result<Self, InvalidRString> {
Self::testnet_int(C::issuer(), by, ticker, name, details, precision, false)
Self::testnet_int(close_method, C::issuer(), by, ticker, name, details, precision, false)
}

pub fn testnet_det<C: IssuerWrapper<IssuingIface = Rgb20>>(
close_method: CloseMethod,
by: &str,
ticker: &str,
name: &str,
details: Option<&str>,
precision: Precision,
asset_tag: AssetTag,
) -> Result<Self, InvalidRString> {
let mut me = Self::testnet_int(C::issuer(), by, ticker, name, details, precision, true)?;
let mut me = Self::testnet_int(
close_method,
C::issuer(),
by,
ticker,
name,
details,
precision,
true,
)?;
me.builder = me
.builder
.add_asset_tag("assetOwner", asset_tag)
Expand All @@ -99,6 +111,7 @@ impl PrimaryIssue {
}

fn testnet_int(
close_method: CloseMethod,
issuer: SchemaIssuer<Rgb20>,
by: &str,
ticker: &str,
Expand All @@ -116,6 +129,7 @@ impl PrimaryIssue {
let (schema, main_iface_impl, types, scripts, features) = issuer.into_split();
let mut builder = match deterministic {
false => ContractBuilder::with(
close_method,
Identity::from_str(by).expect("invalid issuer identity string"),
features.iface(),
schema,
Expand All @@ -125,6 +139,7 @@ impl PrimaryIssue {
Layer1::Bitcoin,
),
true => ContractBuilder::deterministic(
close_method,
Identity::from_str(by).expect("invalid issuer identity string"),
features.iface(),
schema,
Expand Down Expand Up @@ -163,14 +178,12 @@ impl PrimaryIssue {

pub fn allocate<O: TxOutpoint>(
mut self,
method: Method,
beneficiary: O,
amount: impl Into<Amount>,
) -> Result<Self, IssuerError> {
let amount = amount.into();
let beneficiary = beneficiary.map_to_xchain(|outpoint| {
GenesisSeal::new_random(method, outpoint.txid, outpoint.vout)
});
let beneficiary = beneficiary
.map_to_xchain(|outpoint| GenesisSeal::new_random(outpoint.txid, outpoint.vout));
self.issued
.checked_add_assign(amount)
.ok_or(IssuerError::AmountOverflow)?;
Expand All @@ -182,27 +195,25 @@ impl PrimaryIssue {

pub fn allocate_all<O: TxOutpoint>(
mut self,
method: Method,
allocations: impl IntoIterator<Item = (O, impl Into<Amount>)>,
) -> Result<Self, IssuerError> {
for (beneficiary, amount) in allocations {
self = self.allocate(method, beneficiary, amount)?;
self = self.allocate(beneficiary, amount)?;
}
Ok(self)
}

/// Add asset allocation in a deterministic way.
pub fn allocate_det<O: TxOutpoint>(
mut self,
method: Method,
beneficiary: O,
seal_blinding: u64,
amount: impl Into<Amount>,
amount_blinding: BlindingFactor,
) -> Result<Self, IssuerError> {
let amount = amount.into();
let beneficiary = beneficiary.map_to_xchain(|outpoint| {
GenesisSeal::with_blinding(method, outpoint.txid, outpoint.vout, seal_blinding)
GenesisSeal::with_blinding(outpoint.txid, outpoint.vout, seal_blinding)
});
self.issued
.checked_add_assign(amount)
Expand All @@ -218,14 +229,12 @@ impl PrimaryIssue {

pub fn allow_inflation<O: TxOutpoint>(
mut self,
method: Method,
controller: O,
supply: impl Into<Amount>,
) -> Result<Self, IssuerError> {
let supply = supply.into();
let controller = controller.map_to_xchain(|outpoint| {
GenesisSeal::new_random(method, outpoint.txid, outpoint.vout)
});
let controller = controller
.map_to_xchain(|outpoint| GenesisSeal::new_random(outpoint.txid, outpoint.vout));
self = self.update_max_supply(supply)?;
self.builder =
self.builder
Expand All @@ -236,15 +245,14 @@ impl PrimaryIssue {
/// Add asset allocation in a deterministic way.
pub fn allow_inflation_det<O: TxOutpoint>(
mut self,
method: Method,
beneficiary: O,
seal_blinding: u64,
supply: impl Into<Amount>,
supply_blinding: BlindingFactor,
) -> Result<Self, IssuerError> {
let supply = supply.into();
let beneficiary = beneficiary.map_to_xchain(|outpoint| {
GenesisSeal::with_blinding(method, outpoint.txid, outpoint.vout, seal_blinding)
GenesisSeal::with_blinding(outpoint.txid, outpoint.vout, seal_blinding)
});
self = self.update_max_supply(supply)?;
self.builder = self.builder.add_fungible_state_det(
Expand Down Expand Up @@ -276,51 +284,39 @@ impl PrimaryIssue {
Ok(self)
}

pub fn allow_burn<O: TxOutpoint>(
mut self,
method: Method,
controller: O,
) -> Result<Self, IssuerError> {
let controller = controller.map_to_xchain(|outpoint| {
GenesisSeal::new_random(method, outpoint.txid, outpoint.vout)
});
pub fn allow_burn<O: TxOutpoint>(mut self, controller: O) -> Result<Self, IssuerError> {
let controller = controller
.map_to_xchain(|outpoint| GenesisSeal::new_random(outpoint.txid, outpoint.vout));
self.builder = self.builder.add_rights("burnRight", controller)?;
Ok(self)
}

pub fn allow_burn_det<O: TxOutpoint>(
mut self,
method: Method,
controller: O,
seal_blinding: u64,
) -> Result<Self, IssuerError> {
let controller = controller.map_to_xchain(|outpoint| {
GenesisSeal::with_blinding(method, outpoint.txid, outpoint.vout, seal_blinding)
GenesisSeal::with_blinding(outpoint.txid, outpoint.vout, seal_blinding)
});
self.builder = self.builder.add_rights("burnRight", controller)?;
Ok(self)
}

pub fn allow_replace<O: TxOutpoint>(
mut self,
method: Method,
controller: O,
) -> Result<Self, IssuerError> {
let controller = controller.map_to_xchain(|outpoint| {
GenesisSeal::new_random(method, outpoint.txid, outpoint.vout)
});
pub fn allow_replace<O: TxOutpoint>(mut self, controller: O) -> Result<Self, IssuerError> {
let controller = controller
.map_to_xchain(|outpoint| GenesisSeal::new_random(outpoint.txid, outpoint.vout));
self.builder = self.builder.add_rights("replaceRight", controller)?;
Ok(self)
}

pub fn allow_replace_det<O: TxOutpoint>(
mut self,
method: Method,
controller: O,
seal_blinding: u64,
) -> Result<Self, IssuerError> {
let controller = controller.map_to_xchain(|outpoint| {
GenesisSeal::with_blinding(method, outpoint.txid, outpoint.vout, seal_blinding)
GenesisSeal::with_blinding(outpoint.txid, outpoint.vout, seal_blinding)
});
self.builder = self.builder.add_rights("replaceRight", controller)?;
Ok(self)
Expand Down
15 changes: 13 additions & 2 deletions src/rgb20/wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use bp::seals::txout::CloseMethod;
use rgbstd::interface::{
AssignmentsFilter, ContractIface, ContractOp, FungibleAllocation, IfaceClass, IfaceId,
IfaceWrapper, RightsAllocation,
Expand Down Expand Up @@ -118,24 +119,34 @@ impl<S: ContractStateRead> IfaceWrapper<S> for Rgb20Wrapper<S> {

impl<S: ContractStateRead> Rgb20Wrapper<S> {
pub fn testnet<C: IssuerWrapper<IssuingIface = Rgb20>>(
close_method: CloseMethod,
issuer: &str,
ticker: &str,
name: &str,
details: Option<&str>,
precision: Precision,
) -> Result<PrimaryIssue, InvalidRString> {
PrimaryIssue::testnet::<C>(issuer, ticker, name, details, precision)
PrimaryIssue::testnet::<C>(close_method, issuer, ticker, name, details, precision)
}

pub fn testnet_det<C: IssuerWrapper<IssuingIface = Rgb20>>(
close_method: CloseMethod,
issuer: &str,
ticker: &str,
name: &str,
details: Option<&str>,
precision: Precision,
asset_tag: AssetTag,
) -> Result<PrimaryIssue, InvalidRString> {
PrimaryIssue::testnet_det::<C>(issuer, ticker, name, details, precision, asset_tag)
PrimaryIssue::testnet_det::<C>(
close_method,
issuer,
ticker,
name,
details,
precision,
asset_tag,
)
}

pub fn features(&self) -> Rgb20 {
Expand Down
25 changes: 13 additions & 12 deletions src/rgb25/issuer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

use std::str::FromStr;

use bp::dbc::Method;
use bp::seals::txout::CloseMethod;
use rgbstd::containers::ValidContract;
use rgbstd::interface::{BuilderError, ContractBuilder, IfaceClass, TxOutpoint};
use rgbstd::invoice::{Amount, Precision};
Expand All @@ -44,6 +44,7 @@ pub struct Issue {

impl Issue {
fn testnet_int(
close_method: CloseMethod,
issuer: SchemaIssuer<Rgb25>,
by: &str,
name: &str,
Expand All @@ -56,6 +57,7 @@ impl Issue {

let (schema, main_iface_impl, types, scripts, features) = issuer.into_split();
let builder = ContractBuilder::with(
close_method,
Identity::from_str(by).expect("invalid issuer identity string"),
features.iface(),
schema,
Expand All @@ -78,29 +80,32 @@ impl Issue {
}

pub fn testnet<C: IssuerWrapper<IssuingIface = Rgb25>>(
close_method: CloseMethod,
by: &str,
name: &str,
precision: Precision,
) -> Result<Self, InvalidRString> {
Self::testnet_int(C::issuer(), by, name, precision)
Self::testnet_int(close_method, C::issuer(), by, name, precision)
}

pub fn testnet_with(
close_method: CloseMethod,
issuer: SchemaIssuer<Rgb25>,
by: &str,
name: &str,
precision: Precision,
) -> Result<Self, InvalidRString> {
Self::testnet_int(issuer, by, name, precision)
Self::testnet_int(close_method, issuer, by, name, precision)
}

pub fn testnet_det<C: IssuerWrapper<IssuingIface = Rgb25>>(
close_method: CloseMethod,
by: &str,
name: &str,
precision: Precision,
asset_tag: AssetTag,
) -> Result<Self, InvalidRString> {
let mut me = Self::testnet_int(C::issuer(), by, name, precision)?;
let mut me = Self::testnet_int(close_method, C::issuer(), by, name, precision)?;
me.builder = me
.builder
.add_asset_tag("assetOwner", asset_tag)
Expand Down Expand Up @@ -129,7 +134,6 @@ impl Issue {

pub fn allocate<O: TxOutpoint>(
mut self,
method: Method,
beneficiary: O,
amount: Amount,
) -> Result<Self, IssuerError> {
Expand All @@ -138,9 +142,8 @@ impl Issue {
"for creating deterministic contracts please use allocate_det method"
);

let beneficiary = beneficiary.map_to_xchain(|outpoint| {
GenesisSeal::new_random(method, outpoint.txid, outpoint.vout)
});
let beneficiary = beneficiary
.map_to_xchain(|outpoint| GenesisSeal::new_random(outpoint.txid, outpoint.vout));
self.issued
.checked_add_assign(amount)
.ok_or(IssuerError::AmountOverflow)?;
Expand All @@ -152,19 +155,17 @@ impl Issue {

pub fn allocate_all<O: TxOutpoint>(
mut self,
method: Method,
allocations: impl IntoIterator<Item = (O, Amount)>,
) -> Result<Self, IssuerError> {
for (beneficiary, amount) in allocations {
self = self.allocate(method, beneficiary, amount)?;
self = self.allocate(beneficiary, amount)?;
}
Ok(self)
}

/// Add asset allocation in a deterministic way.
pub fn allocate_det<O: TxOutpoint>(
mut self,
method: Method,
beneficiary: O,
seal_blinding: u64,
amount: Amount,
Expand All @@ -181,7 +182,7 @@ impl Issue {
.asset_tag("assetOwner")
.expect("internal library error: asset tag is unassigned");
let beneficiary = beneficiary.map_to_xchain(|outpoint| {
GenesisSeal::with_blinding(method, outpoint.txid, outpoint.vout, seal_blinding)
GenesisSeal::with_blinding(outpoint.txid, outpoint.vout, seal_blinding)
});
self.issued
.checked_add_assign(amount)
Expand Down
Loading

0 comments on commit 6655618

Please sign in to comment.