axelar-cgp-sui

Module 0xa2::gas_service

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;

Resource GasService

struct GasService has store, key
Fields
id: object::UID
inner: versioned::Versioned

Resource GasCollectorCap

struct GasCollectorCap has store, key
Fields
id: object::UID

Struct GasPaid

struct GasPaid<T> has copy, drop
Fields
sender: address
destination_chain: ascii::String
destination_address: ascii::String
payload_hash: address
value: u64
refund_address: address
params: vector<u8>

Struct GasAdded

struct GasAdded<T> has copy, drop
Fields
message_id: ascii::String
value: u64
refund_address: address
params: vector<u8>

Struct Refunded

struct Refunded<T> has copy, drop
Fields
message_id: ascii::String
value: u64
refund_address: address

Struct GasCollected

struct GasCollected<T> has copy, drop
Fields
receiver: address
value: u64

Constants

const VERSION: u64 = 0;

Function init

fun init(ctx: &mut tx_context::TxContext)
Implementation
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(),
    );
}

Function 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>)
Implementation
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,
    })
}

Function 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>)
Implementation
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,
    });
}

Function collect_gas

public fun collect_gas(self: &mut gas_service::GasService, _: &gas_service::GasCollectorCap, receiver: address, amount: u64, ctx: &mut tx_context::TxContext)
Implementation
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,
    });
}

Function 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)
Implementation
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,
    });
}

Function version_control

fun version_control(): version_control::VersionControl
Implementation
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(),
            ),
        ],
    )
}