I am working on an auction smart contract. I have a create_auction
function, and when I try to send CIS2 tokens to the contract, it interrupts the contract execution, tries to call the CIS2 contract and fails without returning an error:
#[receive(contract = “auction”, name = “create_auction”, parameter = “NewAuctionParameter”, enable_logger, mutable)]
pub fn create_auction(
ctx: &impl HasReceiveContext,
host: &mut Host,
logger: &mut impl HasLogger,
) → Result<(), ()> {
let parameter: NewAuctionParameter = ctx.parameter_cursor().get().map_err(|_| ())?;
let owner = match ctx.sender() {
Address::Account(account_address) => account_address,
_ => return Err(()), // Only accounts can create auctions
};
// Transfer CIS-2 tokens from the auction creator to the contract
let transfer = Transfer {
token_id: parameter.token_id,
amount: parameter.token_amount,
from: Address::Account(owner),
to: Receiver::from_contract(ctx.self_address(), OwnedEntrypointName::new_unchecked("onReceivingCIS2".to_string())),
data: AdditionalData::empty(),
};
let client = Cis2Client::new(ContractAddress::new(parameter.token_contract.index, parameter.token_contract.subindex));
let result: Result<bool, Cis2ClientError<()>> = client.transfer(host, transfer);
if let Err(err) = &result {
logger.log(&format!("Transfer failed: {:?}", err)).map_err(|_| ())?;
}
logger.log(&format!("{:?}", result.is_ok())).map_err(|_| ())?;
let auction = Auction {
auction_state: AuctionState::NotSoldYet,
highest_bidder: None,
initial_price: parameter.initial_price,
highest_bid: Amount::zero(),
item: parameter.item,
end: parameter.end,
owner,
token_contract: parameter.token_contract,
token_id: parameter.token_id,
token_amount: parameter.token_amount,
};
// Add the new auction to the array
let state = host.state_mut();
state.auctions.push(auction);
// Return the ID of the newly created auction
let id = (state.auctions.len() - 1) as u32;
logger.log(&AuctionEvent::Register(AuctionEventData { auction_id: id })).map_err(|_| ())?;
Ok(())
}
Maybe someone has an idea how to check what the error is and how to fix it?
Here is the params.json:
{
“end”: “2025-05-02T12:00:00Z”,
“initial_price”: 10,
“item”: “Item”,
“token_amount”: “1000”,
“token_contract”: {
“index”: 11287,
“subindex”: 0
},
“token_id”: “01”
}