0xa2::gas_service
GasService
GasCollectorCap
GasPaid
GasAdded
Refunded
GasCollected
init
pay_gas
add_gas
collect_gas
refund
version_control
use 0x1::ascii;
use 0x1::vector;
use 0x2::address;
use 0x2::coin;
use 0x2::event;
use 0x2::hash;
use 0x2::object;
use 0x2::sui;
use 0x2::transfer;
use 0x2::tx_context;
use 0x2::versioned;
use 0xa2::gas_service_v0;
use 0xb0::version_control;
GasService
struct GasService has store, key
id: object::UID
inner: versioned::Versioned
GasCollectorCap
struct GasCollectorCap has store, key
id: object::UID
GasPaid
struct GasPaid<T> has copy, drop
sender: address
destination_chain: ascii::String
destination_address: ascii::String
payload_hash: address
value: u64
refund_address: address
params: vector<u8>
GasAdded
struct GasAdded<T> has copy, drop
message_id: ascii::String
value: u64
refund_address: address
params: vector<u8>
Refunded
struct Refunded<T> has copy, drop
message_id: ascii::String
value: u64
refund_address: address
GasCollected
struct GasCollected<T> has copy, drop
receiver: address
value: u64
const VERSION: u64 = 0;
init
fun init(ctx: &mut tx_context::TxContext)
fun init(ctx: &mut TxContext) {
transfer::share_object(GasService {
id: object::new(ctx),
inner: versioned::create(
VERSION,
gas_service_v0::new(
version_control(),
),
ctx,
),
});
transfer::public_transfer(
GasCollectorCap {
id: object::new(ctx),
},
ctx.sender(),
);
}
pay_gas
Pay gas for a contract call. This function is called by the channel that wants to pay gas for a contract call. It can also be called by the user to pay gas for a contract call, while setting the sender as the channel ID.
public fun pay_gas(self: &mut gas_service::GasService, coin: coin::Coin<sui::SUI>, sender: address, destination_chain: ascii::String, destination_address: ascii::String, payload: vector<u8>, refund_address: address, params: vector<u8>)
public fun pay_gas(
self: &mut GasService,
coin: Coin<SUI>,
sender: address,
destination_chain: String,
destination_address: String,
payload: vector<u8>,
refund_address: address,
params: vector<u8>,
) {
let coin_value = coin.value();
self.value_mut!(b"pay_gas").put(coin);
let payload_hash = address::from_bytes(keccak256(&payload));
event::emit(GasPaid<SUI> {
sender,
destination_chain,
destination_address,
payload_hash,
value: coin_value,
refund_address,
params,
})
}
add_gas
Add gas for an existing cross-chain contract call. This function can be called by a user who wants to add gas for a contract call with insufficient gas.
public fun add_gas(self: &mut gas_service::GasService, coin: coin::Coin<sui::SUI>, message_id: ascii::String, refund_address: address, params: vector<u8>)
public fun add_gas(
self: &mut GasService,
coin: Coin<SUI>,
message_id: String,
refund_address: address,
params: vector<u8>,
) {
let coin_value = coin.value();
self.value_mut!(b"add_gas").put(coin);
event::emit(GasAdded<SUI> {
message_id,
value: coin_value,
refund_address,
params,
});
}
collect_gas
public fun collect_gas(self: &mut gas_service::GasService, _: &gas_service::GasCollectorCap, receiver: address, amount: u64, ctx: &mut tx_context::TxContext)
public fun collect_gas(
self: &mut GasService,
_: &GasCollectorCap,
receiver: address,
amount: u64,
ctx: &mut TxContext,
) {
transfer::public_transfer(
self.value_mut!(b"collect_gas").take(amount, ctx),
receiver,
);
event::emit(GasCollected<SUI> {
receiver,
value: amount,
});
}
refund
public fun refund(self: &mut gas_service::GasService, _: &gas_service::GasCollectorCap, message_id: ascii::String, receiver: address, amount: u64, ctx: &mut tx_context::TxContext)
public fun refund(
self: &mut GasService,
_: &GasCollectorCap,
message_id: String,
receiver: address,
amount: u64,
ctx: &mut TxContext,
) {
transfer::public_transfer(
self.value_mut!(b"refund").take(amount, ctx),
receiver,
);
event::emit(Refunded<SUI> {
message_id,
value: amount,
refund_address: receiver,
});
}
version_control
fun version_control(): version_control::VersionControl
fun version_control(): VersionControl {
version_control::new(
vector[
vector[b"pay_gas", b"add_gas", b"collect_gas", b"refund"].map!(
|function_name| function_name.to_ascii_string(),
),
],
)
}