How to provide ETH/base-asset to a script?

I’ve got a Sway script that transfers assets, the script works fine when using most assets, but fails if I try to use the base asset. I assume this issue has something to do with the UTXO attached for gas payment.

My code is using a copy of the code from the example AMM repo:


pub async fn get_transaction_inputs_outputs(
  wallet: &WalletUnlocked,
  assets: &Vec<AssetId>,
  amounts: &Vec<u64>,
) -> (Vec<Input>, Vec<Output>) {
    let provider = wallet.get_provider().unwrap();
    let mut input_coins: Vec<Input> = vec![]; // capacity depends on wallet resources
    let mut output_variables: Vec<Output> = Vec::with_capacity(assets.len());

    for (asset_index, asset) in assets.iter().enumerate() {
        input_coins.extend(
            transaction_input_coin(
                provider,
                wallet.address(),
                *asset,
                *amounts.get(asset_index).unwrap(),
            )
            .await,
        );
        output_variables.push(transaction_output_variable());
    }

    (input_coins, output_variables)
}

async fn transaction_input_coin(
    provider: &Provider,
    from: &Bech32Address,
    asset_id: AssetId,
    amount: u64,
) -> Vec<Input> {
    let coins = &provider
        .get_spendable_resources(from, asset_id, amount)
        .await
        .unwrap();

    let input_coins: Vec<Input> = coins
        .iter()
        .map(|coin| {
            let (coin_utxo_id, coin_amount) = match coin {
                Resource::Coin(coin) => (coin.utxo_id, coin.amount),
                _ => panic!("Resource type does not match"),
            };
            Input::CoinSigned {
                utxo_id: coin_utxo_id,
                owner: Address::from(from),
                amount: coin_amount,
                asset_id,
                tx_pointer: TxPointer::default(),
                witness_index: 0,
                maturity: 0,
            }
        })
        .collect();

    input_coins
}

fn transaction_output_variable() -> Output {
    Output::Variable {
        amount: 0,
        to: Address::zeroed(),
        asset_id: AssetId::default(),
    }
}

I’ve updated my script to use Output::Change instead of Output::Variable, but now I’m getting this error:

ValidationError(TransactionOutputChangeAssetIdNotFound(0000000000000000000000000000000000000000000000000000000000000000))

Which is strange, because ETH is being included as an input.

[
  CoinSigned { utxo_id: UtxoId { tx_id: 75e9ca7591407c0927c5465134de7bf48950e27e8db85658c0e5330294a1d988, output_index: 1 }, owner: 09c0b2d1a486c439a87bcba6b46a7a1a23f3897cc83a94521a96da5c23bc58db, amount: 10000000000000000000, asset_id: 0000000000000000000000000000000000000000000000000000000000000000, tx_pointer: TxPointer { block_height: 0, tx_index: 0 }, witness_index: 0, maturity: 0 },
  CoinSigned { utxo_id: UtxoId { tx_id: 6c1452d8a12987ef6a2ed4def2743dd7ea2d26c798abf5ef873c871b10e2ea1b, output_index: 0 }, owner: 09c0b2d1a486c439a87bcba6b46a7a1a23f3897cc83a94521a96da5c23bc58db, amount: 10000000000000000000, asset_id: 0202020202020202020202020202020202020202020202020202020202020202, tx_pointer: TxPointer { block_height: 0, tx_index: 0 }, witness_index: 0, maturity: 0 }
]
1 Like