1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
//                              Næ§@@@ÑÉ©
//                        æ@@@@@@@@@@@@@@@@@@
//                    Ñ@@@@?.?@@@@@@@@@@@@@@@@@@@N
//                 ¶@@@@@?^%@@.=@@@@@@@@@@@@@@@@@@@@
//               N@@@@@@@?^@@@»^@@@@@@@@@@@@@@@@@@@@@@
//               @@@@@@@@?^@@@».............?@@@@@@@@@É
//              Ñ@@@@@@@@?^@@@@@@@@@@@@@@@@@@'?@@@@@@@@Ñ
//              @@@@@@@@@?^@@@»..............»@@@@@@@@@@
//              @@@@@@@@@?^@@@»^@@@@@@@@@@@@@@@@@@@@@@@@
//              @@@@@@@@@?^ë@@&.@@@@@@@@@@@@@@@@@@@@@@@@
//               @@@@@@@@?^´@@@o.%@@@@@@@@@@@@@@@@@@@@©
//                @@@@@@@?.´@@@@@ë.........*.±@@@@@@@æ
//                 @@@@@@@@?´.I@@@@@@@@@@@@@@.&@@@@@N
//                  N@@@@@@@@@@ë.*=????????=?@@@@@Ñ
//                    @@@@@@@@@@@@@@@@@@@@@@@@@@@¶
//                        É@@@@@@@@@@@@@@@@Ѷ
//                             Næ§@@@ÑÉ©

// Copyright 2020 Chris D'Costa
// This file is part of Totem Live Accounting.
// Authors:
// - Félix Daudré-Vignier   email: felix@totemaccounting.com
// - Chris D'Costa          email: chris.dcosta@totemaccounting.com

// Totem is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Totem is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Totem.  If not, see <http://www.gnu.org/licenses/>.

// Locks prefunded amounts into the runtime.
//
// This module functions as a pseudo-escrow module, holding funds for a specified period of time and or for a specific beneficiary.
// In addition to locking funds until a deadline, this module also updates the accounting ledger showing that the assets have moved.
// There is no automatic release of funds from the locked state so requires that the either the deadline to have past to allow withdrawal
// or the intervention of the permitted party to withdraw the funds.
//
// For the initial use of this prefunding module the intended beneficiary is identified by AccountId.
// In a later version there may be no intended beneficiary (for example for marketplace transactions)
// and therefore the funds may be locked until a cadidate secures the funds.
//
// A further scenario is forseen where a dispute resolution method that relies upon an independent validator
// is required to set the lock-release state.

#![cfg_attr(not(feature = "std"), no_std)]

pub use pallet::*;

#[frame_support::pallet]
mod pallet {

    use frame_support::{
        fail,
        pallet_prelude::*,
        traits::{Currency, ExistenceRequirement, LockIdentifier},
    };
    use frame_system::pallet_prelude::*;

    use sp_runtime::traits::{Convert, Hash};
    use sp_std::prelude::*;

    use totem_common::{StorageMapExt, TryConvert};
    use totem_primitives::{
        accounting::{Indicator, Ledger, Posting, Record as PostingRecord},
        escrow::{EscrowableCurrency, Reason},
        prefunding::*,
        ComparisonAmounts, LedgerBalance,
    };

    type EscrowableBalanceOf<T> = <<<T as Config>::Escrowable as EscrowableCurrency<
        <T as frame_system::Config>::AccountId,
    >>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;

    type CurrencyBalanceOf<T> =
        <<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;

    #[pallet::pallet]
    #[pallet::generate_store(trait Store)]
    pub struct Pallet<T>(_);

    /// Bonsai Storage.
    #[pallet::storage]
    #[pallet::getter(fn prefunding)]
    pub type Prefunding<T: Config> =
        StorageMap<_, Blake2_128Concat, T::Hash, (CurrencyBalanceOf<T>, T::BlockNumber)>;

    /* Hacky workaround for inability of RPC to query transaction by hash */

    /// Maps to current block number allows interrogation of errors.
    #[pallet::storage]
    #[pallet::getter(fn prefunding_hash_owner)]
    pub type PrefundingHashOwner<T: Config> = StorageMap<
        _,
        Blake2_128Concat,
        T::Hash,
        (T::AccountId, LockStatus, T::AccountId, LockStatus),
    >;

    /// Future block number beyond which the Hash should deleted.
    #[pallet::storage]
    #[pallet::getter(fn owner_prefunding_hash_list)]
    pub type OwnerPrefundingHashList<T: Config> =
        StorageMap<_, Blake2_128Concat, T::AccountId, Vec<T::Hash>>;

    /// Tracking to ensure that we can perform housekeeping on finalization of block.
    #[pallet::storage]
    #[pallet::getter(fn reference_status)]
    pub type ReferenceStatus<T: Config> = StorageMap<_, Blake2_128Concat, T::Hash, Status>;

    #[pallet::config]
    pub trait Config: frame_system::Config + pallet_timestamp::Config {
        type Event: From<Event<Self>> + IsType<<Self as frame_system::Config>::Event>;

        type Currency: Currency<Self::AccountId>;
        type Escrowable: EscrowableCurrency<Self::AccountId, Moment = Self::BlockNumber>;
        type PrefundingConverter: TryConvert<LedgerBalance, u128>
            + TryConvert<CurrencyBalanceOf<Self>, LedgerBalance>
            + Convert<Vec<u8>, LockIdentifier>
            + Convert<u32, Self::BlockNumber>
            + Convert<CurrencyBalanceOf<Self>, u128>
            + TryConvert<LedgerBalance, EscrowableBalanceOf<Self>>;
        type Accounting: Posting<
            Self::AccountId,
            Self::Hash,
            Self::BlockNumber,
            CurrencyBalanceOf<Self>,
        >;
    }

    #[pallet::error]
    pub enum Error<T> {
        /// You are not the owner or the beneficiary
        LockNotAllowed1,
        /// You are not the owner or the beneficiary
        LockNotAllowed2,
        /// You are not the owner or the beneficiary
        LockNotAllowed3,
        /// You are not the owner or the beneficiary
        LockNotAllowed4,
        /// You are not the owner or the beneficiary
        LockNotAllowed5,
        /// You are not the owner or the beneficiary
        LockNotAllowed6,
        /// Not enough funds to prefund
        InsufficientPreFunds,
        /// Cannot set this state
        WrongState1,
        /// Cannot set this state
        WrongState2,
        /// Cannot set this state
        WrongState3,
        /// Cannot set this state
        WrongState4,
        /// Cannot set this state
        WrongState5,
        /// Funds already locked for intended purpose by both parties.
        NotAllowed1,
        /// Not the beneficiary
        NotAllowed2,
        /// Not the owner
        NotAllowed3,
        /// This function should not be used for this state
        NotAllowed4,
        /// Funds locked for intended purpose by both parties.
        NotAllowed5,
        /// Funds locked for beneficiary.
        NotAllowed6,
        /// The demander has not approved the work yet!
        NotApproved,
        /// The demander has not approved the work yet!
        NotApproved2,
        /// Deadline not yet passed. Wait a bit longer!
        DeadlineInPlay,
        /// Funds locked for intended purpose by both parties.
        FundsInPlay,
        /// Funds locked for intended purpose by both parties.
        FundsInPlay2,
        /// You are not the owner of the hash!
        NotOwner,
        /// You are not the owner of the hash!
        NotOwner2,
        /// This hash already exists!
        HashExists,
        /// Hash does not exist
        HashDoesNotExist,
        /// Hash does not exist
        HashDoesNotExist2,
        /// Hash does not exist
        HashDoesNotExist3,
        /// Deadline is too short! Must be at least 48 hours
        ShortDeadline,
        /// Deposit was not taken
        PrefundNotSet,
        /// An error occured posting to accounts - prefunding for...
        InAccounting1,
        /// An error occured posting to accounts - send simple invoice
        InAccounting2,
        /// An error occured posting to accounts - settle invoice
        InAccounting3,
        /// Did not set the status - prefunding for...
        SettingStatus1,
        /// Did not set the status - send simple invoice
        SettingStatus2,
        /// Error getting details from hash
        NoDetails,
        /// Error setting release state
        ReleaseState,
        /// Error unlocking for beneficiary
        Unlocking,
        /// Error cancelling prefunding
        CancellingPrefund,
        /// Error getting prefunding details
        NoPrefunding,
        /// Cancelling prefunding failed for some reason
        CancelFailed,
        /// Cancelling prefunding failed for some reason
        CancelFailed2,
        /// Value overflowed during computation.
        Overflow,
        /// Error while locking the funds.
        LockFailed,
    }

    #[pallet::hooks]
    impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {}

    #[pallet::call]
    impl<T: Config> Pallet<T> {
        /// This function reserves funds from the buyer for a specific vendor account (Closed Order). It is used when an order is created.
        /// Quantity is not relevant.
        /// The prefunded amount remains as an asset of the buyer until the order is accepted.
        /// Updates only the accounts of the buyer.
        #[pallet::weight(0/*TODO*/)]
        pub fn prefund_someone(
            origin: OriginFor<T>,
            beneficiary: T::AccountId,
            amount: CurrencyBalanceOf<T>,
            deadline: T::BlockNumber,
            tx_uid: T::Hash,
        ) -> DispatchResultWithPostInfo {
            let who = ensure_signed(origin)?;
            // check that the beneficiary is not the sender
            ensure!(who != beneficiary, "Beneficiary must be another account");
            let prefunding_hash: T::Hash =
                Self::get_pseudo_random_hash(who.clone(), beneficiary.clone());

            Self::prefunding_for(who, beneficiary, amount, deadline, prefunding_hash, tx_uid)
        }

        /// Creates a single line simple invoice without taxes, tariffs or commissions.
        /// This invoice is associated with a prefunded order - therefore needs to provide the hash reference of the order.
        /// Updates the accounting for the vendor and the customer.
        #[pallet::weight(0/*TODO*/)]
        pub fn invoice_prefunded_order(
            origin: OriginFor<T>,
            payer: T::AccountId,
            amount: i128,
            reference: T::Hash,
            uid: T::Hash,
        ) -> DispatchResultWithPostInfo {
            let who = ensure_signed(origin)?;

            Self::send_simple_invoice(who, payer, amount, reference, uid)
        }

        /// Buyer pays a prefunded order. Needs to supply the correct hash reference.
        /// Updates bother the buyer and the vendor accounts.
        #[pallet::weight(0/*TODO*/)]
        pub fn pay_prefunded_invoice(
            origin: OriginFor<T>,
            reference: T::Hash,
            uid: T::Hash,
        ) -> DispatchResultWithPostInfo {
            let who = ensure_signed(origin)?;

            Self::settle_prefunded_invoice(who, reference, uid)
        }

        /// Is used by the buyer to recover funds if the vendor does not accept the order by the deadline.
        #[pallet::weight(0/*TODO*/)]
        pub fn cancel_prefunded_closed_order(
            origin: OriginFor<T>,
            reference: T::Hash,
            uid: T::Hash,
        ) -> DispatchResultWithPostInfo {
            let who = ensure_signed(origin)?;

            Self::unlock_funds_for_owner(who, reference, uid)
        }
    }

    #[pallet::event]
    #[pallet::generate_deposit(pub(super) fn deposit_event)]
    pub enum Event<T: Config> {
        PrefundingCancelled(T::AccountId, T::Hash),
        PrefundingLockSet(T::Hash),
        PrefundingCompleted(T::Hash),
        InvoiceIssued(T::Hash),
        InvoiceSettled(T::Hash),
    }

    impl<T: Config> Pallet<T> {
        /// Reserve the prefunding deposit.
        fn set_prefunding(
            s: T::AccountId,
            c: LedgerBalance,
            d: T::BlockNumber,
            h: T::Hash,
            _u: T::Hash,
        ) -> DispatchResultWithPostInfo {
            // Prepare make sure we are not taking the deposit again
            if ReferenceStatus::<T>::contains_key(&h) {
                fail!(Error::<T>::HashExists);
            }

            // You cannot prefund any amount unless you have at least at balance of 1618 units + the amount you want to prefund
            // Ensure that the funds can be subtracted from sender's balance without causing the account to be destroyed by the existential deposit
            let min_balance: ComparisonAmounts = 1618_u128;
            let current_balance: ComparisonAmounts =
                T::PrefundingConverter::convert(T::Currency::free_balance(&s));
            let prefund_amount: ComparisonAmounts =
                T::PrefundingConverter::try_convert(c.clone()).ok_or(Error::<T>::Overflow)?;
            let minimum_amount = min_balance + prefund_amount;

            if current_balance >= minimum_amount {
                let amount = T::PrefundingConverter::try_convert(c).ok_or(Error::<T>::Overflow)?;
                // Lock the amount from the sender and set deadline
                T::Escrowable::set_lock(
                    Self::get_prefunding_id(h),
                    &s,
                    amount,
                    d,
                    Reason::Escrowing,
                )
                .or(Err(Error::<T>::LockFailed))?;
            } else {
                fail!(Error::<T>::InsufficientPreFunds);
            }

            Ok(().into())
        }

        /// Generate a Prefund Id from a hash.
        fn get_prefunding_id(hash: T::Hash) -> LockIdentifier {
            // Convert Hash to ID using first 8 bytes of hash
            T::PrefundingConverter::convert(hash.encode())
        }

        /// Generate a reference from a hash.
        fn get_pseudo_random_hash(sender: T::AccountId, recipient: T::AccountId) -> T::Hash {
            let tuple = (sender, recipient);
            let input = (
                tuple,
                pallet_timestamp::Pallet::<T>::get(),
                sp_io::offchain::random_seed(),
                frame_system::Pallet::<T>::extrinsic_index(),
                frame_system::Pallet::<T>::block_number(),
            );

            T::Hashing::hash(input.encode().as_slice()) // default hash BlakeTwo256
        }

        /// Check a hash exists and is valid.
        fn reference_valid(h: T::Hash) -> bool {
            match ReferenceStatus::<T>::get(&h) {
                Some(0) | Some(1) | Some(100) | Some(200) | Some(300) | Some(400) => true,
                _ => false,
            }
        }

        /// Prefunding deadline passed?
        fn prefund_deadline_passed(h: T::Hash) -> bool {
            match Self::prefunding(&h) {
                Some((_, deadline)) if deadline < frame_system::Pallet::<T>::block_number() => true,
                _ => false,
            }
        }

        /// Gets the state of the locked funds.
        /// The hash needs to be prequalified before passing in as no checks performed here.
        fn get_release_state(h: T::Hash) -> (LockStatus, LockStatus) {
            let owners = Self::prefunding_hash_owner(&h).unwrap(); //TODO

            (owners.1, owners.3)
        }

        /// Cancels lock for owner.
        fn cancel_prefunding_lock(
            o: T::AccountId,
            h: T::Hash,
            s: Status,
        ) -> DispatchResultWithPostInfo {
            // funds can be unlocked for the owner
            // convert hash to lock identifyer
            let prefunding_id = Self::get_prefunding_id(h);
            // unlock the funds
            T::Escrowable::remove_lock(prefunding_id, &o).or(Err(Error::<T>::LockFailed))?;
            // perform cleanup removing all reference hashes. No accounting posting have been made, so no cleanup needed there
            Prefunding::<T>::remove(&h);
            PrefundingHashOwner::<T>::remove(&h);
            ReferenceStatus::<T>::insert(&h, s); // This sets the status but does not remove the hash
            OwnerPrefundingHashList::<T>::mutate_or_err(&o, |owner_prefunding_hash_list| {
                owner_prefunding_hash_list.retain(|e| e != &h)
            })?;

            // Issue event
            Self::deposit_event(Event::PrefundingCancelled(o, h));

            Ok(().into())
        }

        /// Unlocks & pays beneficiary with funds transfer and account updates (settlement of invoice).
        fn unlock_funds_for_beneficiary(
            o: T::AccountId,
            h: T::Hash,
            _u: T::Hash,
        ) -> DispatchResultWithPostInfo {
            use LockStatus::*;

            if Self::reference_valid(h) == false {
                fail!(Error::<T>::HashDoesNotExist);
            }

            if Self::check_ref_beneficiary(o.clone(), h) == false {
                fail!(Error::<T>::NotOwner);
            }

            // TODO this should return the details otherwise there is second read later in the process
            match Self::get_release_state(h) {
                // submitted, but not yet accepted
                (Locked, Unlocked) => fail!(Error::<T>::NotApproved),
                (Locked, Locked) => fail!(Error::<T>::FundsInPlay),
                // Owner has approved now get status of hash. Only allow if invoiced.
                // Note handling the account posting is done outside of this function
                (Unlocked, Locked) => {
                    match ReferenceStatus::<T>::get(&h) {
                        Some(400) => {
                            // get details of lock
                            let details =
                                Self::prefunding_hash_owner(&h).ok_or("Error fetching details")?;
                            // get details of prefunding
                            let prefunding =
                                Self::prefunding(&h).ok_or("Error getting prefunding details")?;
                            // Cancel prefunding lock
                            let status: Status = 500; // Settled
                            Self::cancel_prefunding_lock(details.0.clone(), h, status)?;
                            // transfer to beneficiary.
                            // TODO when currency conversion is implemnted the payment should be at the current rate for the currency
                            if let Err(_) = T::Currency::transfer(
                                &details.0,
                                &o,
                                prefunding.0,
                                ExistenceRequirement::KeepAlive,
                            ) {
                                fail!("Error during transfer")
                            }
                        }
                        _ => fail!("Only allowed when status is Invoiced"),
                    }
                }
                // Owner has been given permission by beneficiary to release funds
                (Unlocked, Unlocked) => fail!(Error::<T>::NotAllowed1),
            }

            Ok(().into())
        }

        /// Set the status for the prefunding.
        fn set_ref_status(h: T::Hash, s: Status) -> DispatchResultWithPostInfo {
            ReferenceStatus::<T>::insert(&h, s);

            Ok(().into())
        }

        // TODO Check should be made for available balances, and if the amount submitted is more than the invoice amount.
        /// Settles invoice by updates to various relevant accounts and transfer of funds.
        #[allow(dead_code)/*TODO use it */]
        fn settle_unfunded_invoice() -> DispatchResultWithPostInfo {
            fail!("TODO")
        }

        /// Return a pair of:
        /// - The amount given as a parameter, but signed.
        /// - The opposite of that amount.
        fn increase_decrease_amounts(
            amount: CurrencyBalanceOf<T>,
        ) -> Result<(LedgerBalance, LedgerBalance), Error<T>> {
            let increase_amount: LedgerBalance =
                T::PrefundingConverter::try_convert(amount).ok_or(Error::<T>::Overflow)?;
            let decrease_amount = increase_amount.checked_neg().ok_or(Error::<T>::Overflow)?;

            Ok((increase_amount, decrease_amount))
        }
    }

    impl<T: Config> Encumbrance<T::AccountId, T::Hash, T::BlockNumber, CurrencyBalanceOf<T>>
        for Pallet<T>
    {
        fn prefunding_for(
            who: T::AccountId,
            recipient: T::AccountId,
            amount: CurrencyBalanceOf<T>, //todo rename amount
            deadline: T::BlockNumber,
            ref_hash: T::Hash,
            uid: T::Hash,
        ) -> DispatchResultWithPostInfo {
            // As amount will always be positive, convert for use in accounting
            let (increase_amount, decrease_amount) = Self::increase_decrease_amounts(amount)?;
            let current_block = <frame_system::Pallet<T>>::block_number();
            // Prefunding is always recorded in the same block. It cannot be posted to another period
            let current_block_dupe = <frame_system::Pallet<T>>::block_number();
            let prefunding_hash: T::Hash = ref_hash;
            // NEED TO CHECK THAT THE DEADLINE IS SENSIBLE!!!!
            // 48 hours is the minimum deadline. This is the minimum amountof time before the money can be reclaimed
            let minimum_deadline: T::BlockNumber = current_block
                + <T::PrefundingConverter as Convert<u32, T::BlockNumber>>::convert(11520_u32);
            if deadline < minimum_deadline {
                fail!(Error::<T>::ShortDeadline);
            }
            let prefunded = (amount, deadline.clone());
            let owners = (who.clone(), true, recipient, false);
            // manage the deposit
            if let Err(_) =
                Self::set_prefunding(who.clone(), increase_amount, deadline, prefunding_hash, uid)
            {
                fail!(Error::<T>::PrefundNotSet);
            }

            // Deposit taken at this point. Note that if an error occurs beyond here we need to remove the locked funds.
            let keys = [
                PostingRecord {
                    primary_party: who.clone(),
                    counterparty: who.clone(),
                    ledger: Ledger::B1010005_0000000D, // debit  increase 110100050000000 Prefunding Account
                    amount: increase_amount,
                    debit_credit: Indicator::Credit,
                    reference_hash: prefunding_hash,
                    changed_on_blocknumber: current_block,
                    applicable_period_blocknumber: current_block_dupe,
                },
                PostingRecord {
                    primary_party: who.clone(),
                    counterparty: who.clone(),
                    ledger: Ledger::B1010004_0000000D, // credit decrease 110100040000000 XTX Balance
                    amount: decrease_amount,
                    debit_credit: Indicator::Debit,
                    reference_hash: prefunding_hash,
                    changed_on_blocknumber: current_block,
                    applicable_period_blocknumber: current_block_dupe,
                },
                PostingRecord {
                    primary_party: who.clone(),
                    counterparty: who.clone(),
                    ledger: Ledger::C6060002_0000000D, // debit  increase 360600020000000 Runtime Ledger by Module
                    amount: increase_amount,
                    debit_credit: Indicator::Credit,
                    reference_hash: prefunding_hash,
                    changed_on_blocknumber: current_block,
                    applicable_period_blocknumber: current_block_dupe,
                },
                PostingRecord {
                    primary_party: who.clone(),
                    counterparty: who.clone(),
                    ledger: Ledger::C6060006_0000000D, // debit  increase 360600060000000 Runtime Ledger Control
                    amount: increase_amount,
                    debit_credit: Indicator::Credit,
                    reference_hash: prefunding_hash,
                    changed_on_blocknumber: current_block,
                    applicable_period_blocknumber: current_block_dupe,
                },
            ];

            if let Err(_) = T::Accounting::handle_multiposting_amounts(&keys) {
                fail!(Error::<T>::InAccounting1);
            }

            // Record Prefunding ownership and status
            PrefundingHashOwner::<T>::insert(&prefunding_hash, owners);
            Prefunding::<T>::insert(&prefunding_hash, prefunded);

            // Add reference hash to list of hashes
            OwnerPrefundingHashList::<T>::mutate_or_err(&who, |owner_prefunding_hash_list| {
                owner_prefunding_hash_list.push(prefunding_hash)
            })?;

            // Submitted, Locked by sender.
            if let Err(_) = Self::set_ref_status(prefunding_hash, 1) {
                fail!(Error::<T>::SettingStatus1);
            }

            Self::deposit_event(Event::PrefundingCompleted(uid));

            Ok(().into())
        }

        /// Simple invoice. Does not include tax jurisdiction, tax amounts, freight, commissions,
        /// tariffs, discounts and other extended line item values.
        /// Must include a connection to the originating reference.
        /// Invoices cannot be made to parties that haven't asked for something identified by a valid hash.
        fn send_simple_invoice(
            who: T::AccountId,
            recipient: T::AccountId,
            amount: LedgerBalance,
            ref_hash: T::Hash,
            uid: T::Hash,
        ) -> DispatchResultWithPostInfo {
            // Validate that the hash is indeed assigned to the seller
            if Self::check_ref_beneficiary(who.clone(), ref_hash) == false {
                fail!(Error::<T>::NotAllowed2);
            }

            // Amount CAN be negative - this is therefore not an Invoice but a Credit Note!
            // The account postings are identical to an invoice, however we must also handle the refund immediately if possible.
            // In order to proceed with a credit note, validate that the vendor has sufficient funds.
            // If they do not have sufficient funds, the credit note can still be issued, but will remain outstanding until it is settled.
            // As amount will always be positive, convert for use in accounting
            let current_block = frame_system::Pallet::<T>::block_number();
            let current_block_dupe = frame_system::Pallet::<T>::block_number();

            // Keys for posting
            let keys = [
                // Seller
                PostingRecord {
                    primary_party: who.clone(),
                    counterparty: who.clone(),
                    ledger: Ledger::B1010008_0000000D, // Debit  increase 110100080000000	Accounts receivable (Sales Control Account or Trade Debtor's Account)
                    amount,
                    debit_credit: Indicator::Credit,
                    reference_hash: ref_hash,
                    changed_on_blocknumber: current_block,
                    applicable_period_blocknumber: current_block_dupe,
                },
                PostingRecord {
                    primary_party: who.clone(),
                    counterparty: who.clone(),
                    ledger: Ledger::P4040001_0000000C, // Credit increase 240400010000000	Product or Service Sales
                    amount,
                    debit_credit: Indicator::Debit,
                    reference_hash: ref_hash,
                    changed_on_blocknumber: current_block,
                    applicable_period_blocknumber: current_block_dupe,
                },
                PostingRecord {
                    primary_party: who.clone(),
                    counterparty: who.clone(),
                    ledger: Ledger::C6060001_0000000D, // Debit  increase 360600010000000	Sales Ledger by Payer
                    amount,
                    debit_credit: Indicator::Credit,
                    reference_hash: ref_hash,
                    changed_on_blocknumber: current_block,
                    applicable_period_blocknumber: current_block_dupe,
                },
                PostingRecord {
                    primary_party: who.clone(),
                    counterparty: who.clone(),
                    ledger: Ledger::C6060005_0000000D, // Debit  increase 360600050000000	Sales Ledger Control
                    amount,
                    debit_credit: Indicator::Credit,
                    reference_hash: ref_hash,
                    changed_on_blocknumber: current_block,
                    applicable_period_blocknumber: current_block_dupe,
                },
                // Buyer
                PostingRecord {
                    primary_party: recipient.clone(),
                    counterparty: recipient.clone(),
                    ledger: Ledger::B2020003_0000000C, // Credit increase 120200030000000	Accounts payable
                    amount,
                    debit_credit: Indicator::Debit,
                    reference_hash: ref_hash,
                    changed_on_blocknumber: current_block,
                    applicable_period_blocknumber: current_block_dupe,
                },
                PostingRecord {
                    primary_party: recipient.clone(),
                    counterparty: recipient.clone(),
                    ledger: Ledger::P5050012_0000013D, // Debit  increase 250500120000013	Labour
                    amount,
                    debit_credit: Indicator::Credit,
                    reference_hash: ref_hash,
                    changed_on_blocknumber: current_block,
                    applicable_period_blocknumber: current_block_dupe,
                },
                PostingRecord {
                    primary_party: recipient.clone(),
                    counterparty: recipient.clone(),
                    ledger: Ledger::C6060003_0000000D, // Debit  increase 360600030000000	Purchase Ledger by Vendor
                    amount,
                    debit_credit: Indicator::Credit,
                    reference_hash: ref_hash,
                    changed_on_blocknumber: current_block,
                    applicable_period_blocknumber: current_block_dupe,
                },
                PostingRecord {
                    primary_party: recipient.clone(),
                    counterparty: recipient.clone(),
                    ledger: Ledger::C6060007_0000000D, // Debit  increase 360600070000000	Purchase Ledger Control
                    amount,
                    debit_credit: Indicator::Credit,
                    reference_hash: ref_hash,
                    changed_on_blocknumber: current_block,
                    applicable_period_blocknumber: current_block_dupe,
                },
            ];

            if let Err(_) = T::Accounting::handle_multiposting_amounts(&keys) {
                fail!(Error::<T>::InAccounting2);
            }

            // Add status processing
            let new_status: Status = 400; // invoiced(400), can no longer be accepted,
            if let Err(_) = Self::set_ref_status(ref_hash, new_status) {
                fail!(Error::<T>::SettingStatus2);
            }

            Self::deposit_event(Event::InvoiceIssued(uid));

            Ok(().into())
        }

        /// Settles invoice by unlocking funds and updates various relevant accounts and pays prefunded amount.
        fn settle_prefunded_invoice(
            who: T::AccountId,
            ref_hash: T::Hash,
            uid: T::Hash,
        ) -> DispatchResultWithPostInfo {
            use LockStatus::*;

            // release state must be 11
            // sender must be owner
            // accounts updated before payment, because if there is an error then the accounting can be rolled back
            let (payer, beneficiary) = match Self::get_release_state(ref_hash) {
                // submitted, but not yet accepted
                (Locked, Unlocked) => fail!(Error::<T>::NotApproved2),
                (Locked, Locked) => {
                    // Validate that the hash is indeed owned by the buyer
                    if Self::check_ref_owner(who.clone(), ref_hash) == false {
                        fail!(Error::<T>::NotAllowed3);
                    }

                    // get beneficiary from hash
                    let (_, _, details /*TODO better name*/, _) =
                        Self::prefunding_hash_owner(&ref_hash).ok_or(Error::<T>::NoDetails)?;
                    // get prefunding amount for posting to accounts
                    let (prefunded_amount, _) =
                        Self::prefunding(&ref_hash).ok_or(Error::<T>::NoPrefunding)?;
                    // convert to Account Balance type
                    let (increase_amount, decrease_amount) =
                        Self::increase_decrease_amounts(prefunded_amount)?;
                    let current_block = frame_system::Pallet::<T>::block_number();
                    let current_block_dupe = frame_system::Pallet::<T>::block_number();

                    // Keys for posting
                    let keys = [
                        // Buyer
                        PostingRecord {
                            primary_party: who.clone(),
                            counterparty: who.clone(),
                            ledger: Ledger::B2020003_0000000C, // 120200030000000	Debit  decrease Accounts payable
                            amount: decrease_amount,
                            debit_credit: Indicator::Credit,
                            reference_hash: ref_hash,
                            changed_on_blocknumber: current_block,
                            applicable_period_blocknumber: current_block_dupe,
                        },
                        PostingRecord {
                            primary_party: who.clone(),
                            counterparty: who.clone(),
                            ledger: Ledger::B1010005_0000000D, // 110100050000000	Credit decrease Totem Runtime Deposit (Escrow)
                            amount: decrease_amount,
                            debit_credit: Indicator::Debit,
                            reference_hash: ref_hash,
                            changed_on_blocknumber: current_block,
                            applicable_period_blocknumber: current_block_dupe,
                        },
                        PostingRecord {
                            primary_party: who.clone(),
                            counterparty: who.clone(),
                            ledger: Ledger::C6060002_0000000D, // 360600020000000	Credit decrease Runtime Ledger by Module
                            amount: decrease_amount,
                            debit_credit: Indicator::Debit,
                            reference_hash: ref_hash,
                            changed_on_blocknumber: current_block,
                            applicable_period_blocknumber: current_block_dupe,
                        },
                        PostingRecord {
                            primary_party: who.clone(),
                            counterparty: who.clone(),
                            ledger: Ledger::C6060006_0000000D, // 360600060000000	Credit decrease Runtime Ledger Control
                            amount: decrease_amount,
                            debit_credit: Indicator::Debit,
                            reference_hash: ref_hash,
                            changed_on_blocknumber: current_block,
                            applicable_period_blocknumber: current_block_dupe,
                        },
                        PostingRecord {
                            primary_party: who.clone(),
                            counterparty: who.clone(),
                            ledger: Ledger::C6060003_0000000D, // 360600030000000	Credit decrease Purchase Ledger by Vendor
                            amount: decrease_amount,
                            debit_credit: Indicator::Debit,
                            reference_hash: ref_hash,
                            changed_on_blocknumber: current_block,
                            applicable_period_blocknumber: current_block_dupe,
                        },
                        PostingRecord {
                            primary_party: who.clone(),
                            counterparty: who.clone(),
                            ledger: Ledger::C6060007_0000000D, // 360600070000000	Credit decrease Purchase Ledger Control
                            amount: decrease_amount,
                            debit_credit: Indicator::Debit,
                            reference_hash: ref_hash,
                            changed_on_blocknumber: current_block,
                            applicable_period_blocknumber: current_block_dupe,
                        },
                        // Seller
                        PostingRecord {
                            primary_party: details.clone(),
                            counterparty: details.clone(),
                            ledger: Ledger::B1010004_0000000D, // 110100040000000	Debit  increase XTX Balance
                            amount: increase_amount,
                            debit_credit: Indicator::Credit,
                            reference_hash: ref_hash,
                            changed_on_blocknumber: current_block,
                            applicable_period_blocknumber: current_block_dupe,
                        },
                        PostingRecord {
                            primary_party: details.clone(),
                            counterparty: details.clone(),
                            ledger: Ledger::B1010008_0000000D, // 110100080000000	Credit decrease Accounts receivable (Sales Control Account or Trade Debtor's Account)
                            amount: decrease_amount,
                            debit_credit: Indicator::Debit,
                            reference_hash: ref_hash,
                            changed_on_blocknumber: current_block,
                            applicable_period_blocknumber: current_block_dupe,
                        },
                        PostingRecord {
                            primary_party: details.clone(),
                            counterparty: details.clone(),
                            ledger: Ledger::C6060001_0000000D, // 360600010000000	Credit decrease Sales Ledger by Payer
                            amount: decrease_amount,
                            debit_credit: Indicator::Debit,
                            reference_hash: ref_hash,
                            changed_on_blocknumber: current_block,
                            applicable_period_blocknumber: current_block_dupe,
                        },
                        PostingRecord {
                            primary_party: details.clone(),
                            counterparty: details.clone(),
                            ledger: Ledger::C6060005_0000000D, // 360600050000000	Credit decrease Sales Ledger Control
                            amount: decrease_amount,
                            debit_credit: Indicator::Debit,
                            reference_hash: ref_hash,
                            changed_on_blocknumber: current_block,
                            applicable_period_blocknumber: current_block_dupe,
                        },
                    ];

                    if let Err(_) = T::Accounting::handle_multiposting_amounts(&keys) {
                        fail!(Error::<T>::InAccounting3);
                    }

                    // export details for final payment steps
                    (who, details)
                }
                // This state is not allowed for this functions
                (Unlocked, Locked) => fail!(Error::<T>::NotAllowed4),
                // Owner has been given permission by beneficiary to release funds
                (Unlocked, Unlocked) => fail!(Error::<T>::NotAllowed5),
            };

            // Set release lock "buyer who has approved invoice"
            // this may have been set independently, but is required for next step
            if let Err(_) = Self::set_release_state(payer, Unlocked, ref_hash, uid) {
                fail!(Error::<T>::ReleaseState);
            }

            // Unlock, tansfer funds and mark hash as settled in full
            if let Err(_) = Self::unlock_funds_for_beneficiary(beneficiary, ref_hash, uid) {
                fail!(Error::<T>::Unlocking);
            }

            Self::deposit_event(Event::InvoiceSettled(uid));

            Ok(().into())
        }

        /// Sets the release state by the owner or the beneficiary is only called when something already exists.
        fn set_release_state(
            who: T::AccountId,
            o_lock: LockStatus,
            ref_hash: T::Hash,
            uid: T::Hash,
        ) -> DispatchResultWithPostInfo {
            use LockStatus::*;

            // 0= false, 1=true
            // 10, sender can take after deadline (initial state)
            // 11, accepted by recipient. (funds locked, nobody can take)
            // 01, sender approves (recipient can take, or refund)
            // 00, only the recipient authorises sender to retake funds regardless of deadline.
            // Initialise new tuple with some dummy values
            let mut change = (who.clone(), Unlocked, who.clone(), Unlocked);

            match Self::prefunding_hash_owner(&ref_hash) {
                Some(state_lock) => {
                    let locks = (state_lock.1, state_lock.3);
                    change.0 = state_lock.0.clone();
                    change.2 = state_lock.2.clone();
                    let commander = state_lock.0.clone();
                    let fulfiller = state_lock.2.clone();
                    match locks {
                        // In this state the commander has created the lock, but it has not been accepted.
                        // The commander can withdraw the lock (set to false) if the deadline has passed, or
                        // the fulfiller can accept the order (set to true)
                        (Locked, Unlocked) => {
                            match o_lock {
                                Locked => {
                                    if who == commander {
                                        fail!(Error::<T>::WrongState1);
                                    } else if who == fulfiller {
                                        change.1 = state_lock.1;
                                        change.3 = o_lock;
                                    } else {
                                        fail!(Error::<T>::LockNotAllowed1);
                                    };
                                }
                                Unlocked => {
                                    // We do care if the deadline has passed IF this is the commander calling directly
                                    // but that must be handled outside of this function
                                    if who == commander {
                                        change.1 = o_lock;
                                        change.3 = state_lock.3;
                                    } else if who == fulfiller {
                                        fail!(Error::<T>::WrongState2);
                                    } else {
                                        fail!(Error::<T>::LockNotAllowed2);
                                    };
                                }
                            }
                        }
                        // In this state the commander can change the lock, and they can only change it to false
                        // In this state the fulfiller can change the lock, and they can only change it to false
                        (Locked, Locked) => match o_lock {
                            Locked => fail!(Error::<T>::WrongState3),
                            Unlocked => {
                                if who == commander {
                                    change.1 = o_lock;
                                    change.3 = state_lock.3;
                                } else if who == fulfiller {
                                    change.1 = state_lock.1;
                                    change.3 = o_lock;
                                } else {
                                    fail!(Error::<T>::LockNotAllowed3);
                                }
                            }
                        },
                        // In this state the commander cannot change the lock
                        // In this state the fulfiller can change the lock, and they can only change it to false
                        (Unlocked, Locked) => match o_lock {
                            Locked => fail!(Error::<T>::LockNotAllowed4),
                            Unlocked => {
                                if who == commander {
                                    fail!(Error::<T>::WrongState5);
                                } else if who == fulfiller {
                                    change.1 = state_lock.1;
                                    change.3 = o_lock;
                                } else {
                                    fail!(Error::<T>::LockNotAllowed5);
                                };
                            }
                        },
                        // This state should technically make the funds refundable to the buyer.
                        // Even if the buy wanted to set this state they cannot. Meaning they must create a new order.
                        (Unlocked, Unlocked) => fail!(Error::<T>::LockNotAllowed5),
                    }
                }
                None => fail!(Error::<T>::HashDoesNotExist2),
            };
            PrefundingHashOwner::<T>::insert(&ref_hash, change);
            // Issue event
            Self::deposit_event(Event::PrefundingLockSet(uid));

            Ok(().into())
        }

        /// Unlocks for owner.
        fn unlock_funds_for_owner(
            who: T::AccountId,
            ref_hash: T::Hash,
            _uid: T::Hash,
        ) -> DispatchResultWithPostInfo {
            use LockStatus::*;

            if Self::reference_valid(ref_hash) == false {
                fail!(Error::<T>::HashDoesNotExist3);
            }

            if Self::check_ref_owner(who.clone(), ref_hash) == false {
                fail!(Error::<T>::NotOwner2);
            }

            match Self::get_release_state(ref_hash) {
                // submitted, but not yet accepted
                // Check if the dealine has passed. If not funds cannot be release
                (Locked, Unlocked) => {
                    if Self::prefund_deadline_passed(ref_hash) {
                        let status: Status = 50; // Abandoned or Cancelled
                        if let Err(_) = Self::cancel_prefunding_lock(who, ref_hash, status) {
                            fail!(Error::<T>::CancelFailed2);
                        }
                    } else {
                        fail!(Error::<T>::DeadlineInPlay);
                    }
                }
                (Locked, Locked) => fail!(Error::<T>::FundsInPlay2),
                (Unlocked, Locked) => fail!(Error::<T>::NotAllowed6),
                (Unlocked, Unlocked) => {
                    // Owner has been  given permission by beneficiary to release funds
                    let status: Status = 50; // Abandoned or cancelled
                    if let Err(_) = Self::cancel_prefunding_lock(who, ref_hash, status) {
                        fail!(Error::<T>::CancellingPrefund);
                    }
                }
            }

            Ok(().into())
        }

        /// Checks owner (of hash) - if anything fails then returns false.
        fn check_ref_owner(who: T::AccountId, ref_hash: T::Hash) -> bool {
            match Self::prefunding_hash_owner(&ref_hash) {
                Some(owners) if owners.0 == who => true,
                _ => false,
            }
        }

        /// Checks beneficiary (of hash reference).
        fn check_ref_beneficiary(who: T::AccountId, ref_hash: T::Hash) -> bool {
            match Self::prefunding_hash_owner(&ref_hash) {
                Some(owners) if owners.2 == who => true,
                _ => false,
            }
        }
    }
}