sui 资源权限问题
#276
Replies: 2 comments
-
不能。Move语言里结构体只能在定义的模块内使用,也就是说你获得 |
Beta Was this translation helpful? Give feedback.
0 replies
-
验证合约内容(合约A) /// Unlike `Owned` objects, `Shared` ones can be accessed by anyone on the
/// network. Extended functionality and accessibility of this kind of objects
/// requires additional effort by securing access if needed.
module testdapp::donuts1 {
use sui::transfer;
use sui::sui::SUI;
use sui::coin::{Self, Coin};
use sui::object::{Self, UID};
use sui::balance::{Self, Balance};
use sui::tx_context::{Self, TxContext};
/// For when Coin balance is too low.
const ENotEnough: u64 = 0;
/// Capability that grants an owner the right to collect profits.
struct ShopOwnerCap has key { id: UID }
/// A purchasable Donut. For simplicity's sake we ignore implementation.
struct Donut has key { id: UID }
/// A shared object. `key` ability is required.
struct DonutShop has key {
id: UID,
price: u64,
balance: Balance<SUI>
}
/// Init function is often ideal place for initializing
/// a shared object as it is called only once.
///
/// To share an object `transfer::share_object` is used.
fun init(ctx: &mut TxContext) {
transfer::transfer(ShopOwnerCap {
id: object::new(ctx)
}, tx_context::sender(ctx));
// Share the object to make it accessible to everyone!
transfer::share_object(DonutShop {
id: object::new(ctx),
price: 1000,
balance: balance::zero()
})
}
/// Entry function available to everyone who owns a Coin.
public entry fun buy_donut(
shop: &mut DonutShop, payment: &mut Coin<SUI>, ctx: &mut TxContext
) {
assert!(coin::value(payment) >= shop.price, ENotEnough);
// Take amount = `shop.price` from Coin<SUI>
let coin_balance = coin::balance_mut(payment);
let paid = balance::split(coin_balance, shop.price);
// Put the coin to the Shop's balance
balance::join(&mut shop.balance, paid);
transfer::transfer(Donut {
id: object::new(ctx)
}, tx_context::sender(ctx))
}
/// Consume donut and get nothing...
public entry fun eat_donut(d: Donut) {
let Donut { id } = d;
object::delete(id);
}
/// Take coin from `DonutShop` and transfer it to tx sender.
/// Requires authorization with `ShopOwnerCap`.
public entry fun collect_profits(
_: &ShopOwnerCap, shop: &mut DonutShop, ctx: &mut TxContext
) {
let amount = balance::value(&shop.balance);
let profits = coin::take(&mut shop.balance, amount, ctx);
transfer::transfer(profits, tx_context::sender(ctx))
}
/// Increment a counter by 1.
public entry fun increment(donut: &mut DonutShop) {
donut.price = donut.price + 100;
}
} 验证过程
sui client call --gas-budget 10000 \
--package 0xb41b3a93814f6f0f6ba1a58ea1963e2620cc2ced \ # bob部署的合约objectid
--module "donuts1" \
--function "increment" \
--args "0x7754d58a67064937533a853ac0528c3041bdf756" # alice创建的DonutShop ObjectId
总结 这个问题 就类似aptos move 其他合约能不能控制这个合约的resource。resource/object的修改,必须通过"对应"的合约方法调用修改。 |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
相关文档:
https://examples.sui.io/basics/shared-object.html
如 文档中所述, 资源可以通过 sui::transfer::share_object 对 所有用户可读,可写。 这种读写是限制在 当前合约层面的么?如果不是,那么,合约间互相调用是否全局可读写。
比如:
示例合约中 init 创建了一个 全局可读写的 DonutShop 资源。 但是,合约只对拥有ShopOwnerCap 提供了 collect_profits 方法。
那么,如果 我写了另一个合约 其中删除了 的限制 ,其中传递了 DonutShop 资源,是否可以提取 DonutShop 中的 coin。
如果不能? 为什么?
Beta Was this translation helpful? Give feedback.
All reactions