In an order for the prosecution of an undertaking given for the appearance of a person accused of contempt, the court, whenever it thinks proper to do so, may, or whenever no party is aggrieved by the misconduct of the person, shall, direct a prosecution to be made in the name of this state by the attorney general or by the state's attorney of the county in which the undertaking was given. In an action brought pursuant to such direction, the state is entitled to recover the entire sum specified in the undertaking. Out of the money collected, the court which directed the prosecution must order the person at whose instance a warrant was issued to be paid such a sum as it thinks proper to satisfy the costs and expenses incurred by the person and to compensate the person for the loss or injury sustained by reason of the misconduct. The residue of the money must be paid into the treasury of this state to the credit of the school fund.
N.D.C.C. § 27-10-20