Created
April 24, 2025 13:45
-
-
Save goodylili/645bbace6787bae23b0bc4837981eaa9 to your computer and use it in GitHub Desktop.
Airdrop a token to multiple addresses on Sui
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module rendevous::airdrop; | |
use sui::coin::{Self, Coin}; | |
use sui::event; | |
/// Event emitted for every airdrop transfer. | |
public struct AirdropEvent has copy, drop { | |
amount: u64, | |
recipient: address, | |
} | |
const E_LENGTH_MISMATCH: u64 = 0; | |
/// Send tokens to multiple addresses from a single coin and emit event for each. | |
public entry fun send_by_allocation<T: store>( | |
coin: &mut Coin<T>, | |
recipients: vector<address>, | |
amounts: vector<u64>, | |
ctx: &mut TxContext | |
) { | |
let num_recipients = vector::length(&recipients); | |
let num_amounts = vector::length(&amounts); | |
assert!(num_recipients == num_amounts, E_LENGTH_MISMATCH); | |
let mut i = 0; | |
while (i < num_recipients) { | |
let amount = *vector::borrow(&amounts, i); | |
let recipient = *vector::borrow(&recipients, i); | |
let portion = coin::split(coin, amount, ctx); | |
transfer::public_transfer(portion, recipient); | |
// Emit an event for each transfer | |
event::emit(AirdropEvent { | |
amount, | |
recipient, | |
}); | |
i = i + 1; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment