axelar-cgp-sui

Module 0xa2::gas_service_v0

use 0x1::ascii;
use 0x1::type_name;
use 0x2::address;
use 0x2::bag;
use 0x2::balance;
use 0x2::coin;
use 0x2::hash;
use 0x2::transfer;
use 0x2::tx_context;
use 0xa1::message_ticket;
use 0xa2::events;
use 0xb0::version_control;

Struct GasService_v0

struct GasService_v0 has store
Fields
balances: bag::Bag
version_control: version_control::VersionControl

Function new

public(friend) fun new(version_control: version_control::VersionControl, ctx: &mut tx_context::TxContext): gas_service_v0::GasService_v0
Implementation
public(package) fun new(version_control: VersionControl, ctx: &mut TxContext): GasService_v0 {
    GasService_v0 {
        balances: bag::new(ctx),
        version_control,
    }
}

Function version_control

public(friend) fun version_control(self: &gas_service_v0::GasService_v0): &version_control::VersionControl
Implementation
public(package) fun version_control(self: &GasService_v0): &VersionControl {
    &self.version_control
}

Function pay_gas

public(friend) fun pay_gas<T>(self: &mut gas_service_v0::GasService_v0, message_ticket: &message_ticket::MessageTicket, coin: coin::Coin<T>, refund_address: address, params: vector<u8>)
Implementation
public(package) fun pay_gas<T>(
    self: &mut GasService_v0,
    message_ticket: &MessageTicket,
    coin: Coin<T>,
    refund_address: address,
    params: vector<u8>,
) {
    let coin_value = coin.value();
    self.put(coin);

    let payload_hash = address::from_bytes(
        keccak256(&message_ticket.payload()),
    );

    events::gas_paid<T>(
        message_ticket.source_id(),
        message_ticket.destination_chain(),
        message_ticket.destination_address(),
        payload_hash,
        coin_value,
        refund_address,
        params,
    );
}

Function add_gas

public(friend) fun add_gas<T>(self: &mut gas_service_v0::GasService_v0, coin: coin::Coin<T>, message_id: ascii::String, refund_address: address, params: vector<u8>)
Implementation
public(package) fun add_gas<T>(
    self: &mut GasService_v0,
    coin: Coin<T>,
    message_id: String,
    refund_address: address,
    params: vector<u8>,
) {
    let coin_value = coin.value();
    self.put(coin);

    events::gas_added<T>(
        message_id,
        coin_value,
        refund_address,
        params,
    );
}

Function collect_gas

public(friend) fun collect_gas<T>(self: &mut gas_service_v0::GasService_v0, receiver: address, amount: u64, ctx: &mut tx_context::TxContext)
Implementation
public(package) fun collect_gas<T>(self: &mut GasService_v0, receiver: address, amount: u64, ctx: &mut TxContext) {
    transfer::public_transfer(
        self.take<T>(amount, ctx),
        receiver,
    );

    events::gas_collected<T>(
        receiver,
        amount,
    );
}

Function refund

public(friend) fun refund<T>(self: &mut gas_service_v0::GasService_v0, message_id: ascii::String, receiver: address, amount: u64, ctx: &mut tx_context::TxContext)
Implementation
public(package) fun refund<T>(self: &mut GasService_v0, message_id: String, receiver: address, amount: u64, ctx: &mut TxContext) {
    transfer::public_transfer(
        self.take<T>(amount, ctx),
        receiver,
    );

    events::refunded<T>(
        message_id,
        amount,
        receiver,
    );
}

Function allow_function

public(friend) fun allow_function(self: &mut gas_service_v0::GasService_v0, version: u64, function_name: ascii::String)
Implementation
public(package) fun allow_function(self: &mut GasService_v0, version: u64, function_name: String) {
    self.version_control.allow_function(version, function_name);
}

Function disallow_function

public(friend) fun disallow_function(self: &mut gas_service_v0::GasService_v0, version: u64, function_name: ascii::String)
Implementation
public(package) fun disallow_function(self: &mut GasService_v0, version: u64, function_name: String) {
    self.version_control.disallow_function(version, function_name);
}

Function balance

public(friend) fun balance<T>(self: &gas_service_v0::GasService_v0): &balance::Balance<T>
Implementation
public(package) fun balance<T>(self: &GasService_v0): &Balance<T> {
    self.balances.borrow<TypeName, Balance<T>>(type_name::get<T>())
}

Function put

fun put<T>(self: &mut gas_service_v0::GasService_v0, coin: coin::Coin<T>)
Implementation
fun put<T>(self: &mut GasService_v0, coin: Coin<T>) {
    coin::put(self.balance_mut<T>(), coin);
}

Function take

fun take<T>(self: &mut gas_service_v0::GasService_v0, amount: u64, ctx: &mut tx_context::TxContext): coin::Coin<T>
Implementation
fun take<T>(self: &mut GasService_v0, amount: u64, ctx: &mut TxContext): Coin<T> {
    coin::take(self.balance_mut<T>(), amount, ctx)
}

Function balance_mut

fun balance_mut<T>(self: &mut gas_service_v0::GasService_v0): &mut balance::Balance<T>
Implementation
fun balance_mut<T>(self: &mut GasService_v0): &mut Balance<T> {
    let key = type_name::get<T>();

    if (!self.balances.contains(key)) {
        self.balances.add(key, balance::zero<T>());
    };

    self.balances.borrow_mut<TypeName, Balance<T>>(key)
}