Skip to content

Commit

Permalink
feat: Emit WithdrawEvent (#36)
Browse files Browse the repository at this point in the history
* Add WithdrawEvent

* Move emit to outer call
  • Loading branch information
swift1337 authored Feb 21, 2025
1 parent 39742e2 commit 8db5ea4
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
17 changes: 15 additions & 2 deletions localtest/tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"github.com/stretchr/testify/require"
)

const suiCoin = "0000000000000000000000000000000000000000000000000000000000000002::sui::SUI"

func TestDeposit(ts *TestSuite) {
// ARRANGE
// Request some SUI from the faucet
Expand All @@ -24,7 +26,7 @@ func TestDeposit(ts *TestSuite) {
PackageObjectId: ts.PackageID,
Module: "gateway",
Function: "deposit",
TypeArguments: []any{"0x2::sui::SUI"},
TypeArguments: []any{suiCoin},
Arguments: []any{ts.GatewayObjectID, coinObjectId, zetaEthAddress},
GasBudget: "5000000000",
})
Expand Down Expand Up @@ -110,7 +112,7 @@ func TestWithdrawal(ts *TestSuite) {
PackageObjectId: ts.PackageID,
Module: "gateway",
Function: "withdraw",
TypeArguments: []any{"0x2::sui::SUI"},
TypeArguments: []any{suiCoin},
Arguments: []any{ts.GatewayObjectID, amt, nonce, bob, withdrawCapID},
GasBudget: "5000000000",
})
Expand Down Expand Up @@ -140,4 +142,15 @@ func TestWithdrawal(ts *TestSuite) {
require.Equal(ts, amt, change.Amount)
}
}

require.Equal(ts, 1, len(resp.Events))
withdrawEvent := resp.Events[0]

// Check event
require.Equal(ts, fmt.Sprintf("%s::gateway::WithdrawEvent", ts.PackageID), withdrawEvent.Type)
require.Equal(ts, suiCoin, withdrawEvent.ParsedJson["coin_type"])
require.Equal(ts, amt, withdrawEvent.ParsedJson["amount"])
require.Equal(ts, nonce, withdrawEvent.ParsedJson["nonce"])
require.Equal(ts, bob, withdrawEvent.ParsedJson["receiver"])
require.Equal(ts, ts.TSS.Address(), withdrawEvent.ParsedJson["sender"])
}
19 changes: 19 additions & 0 deletions sources/gateway.move
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@ public struct DepositAndCallEvent has copy, drop {
payload: vector<u8>,
}

public struct WithdrawEvent has copy, drop {
coin_type: String,
amount: u64,
sender: address,
receiver: address,
nonce: u64,
}

// === Initialization ===

fun init(ctx: &mut TxContext) {
Expand Down Expand Up @@ -123,7 +131,17 @@ entry fun withdraw<T>(
ctx: &mut TxContext,
) {
let coin = withdraw_impl<T>(gateway, amount, nonce, cap, ctx);

transfer::public_transfer(coin, receiver);

// Emit event
event::emit(WithdrawEvent {
coin_type: coin_name<T>(),
amount: amount,
sender: tx_context::sender(ctx),
receiver: receiver,
nonce: nonce,
});
}

// whitelist whitelists a new coin by creating a new vault for the coin type
Expand Down Expand Up @@ -235,6 +253,7 @@ public fun withdraw_impl<T>(
let coin_name = coin_name<T>();
let vault = bag::borrow_mut<String, Vault<T>>(&mut gateway.vaults, coin_name);
let coin_out = coin::take(&mut vault.balance, amount, ctx);

coin_out
}

Expand Down

0 comments on commit 8db5ea4

Please sign in to comment.