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(),
}
}