Send SNS token to a users sub account

So I took payment from people using this code:

    public func payEuro2024EntryFee(defaultAccount : Principal, caller : Principal) : async FPLLedger.TransferResult {
      return await ledger.icrc1_transfer({
        memo = ?Text.encodeUtf8("0");
        from_subaccount = ?principalToSubaccount(caller);
        to = { owner = defaultAccount; subaccount = ?Account.defaultSubaccount()};
        amount = entry_fee - fpl_fee - fpl_fee;
        fee = ?fpl_fee;
        created_at_time =?Nat64.fromNat(Int.abs(Time.now()));
      });
    };

I was thinking to send a payment back it would be something like this




    public func transferWinnings(defaultAccount : Principal, user : Principal, amount : Float) : async () {
      let e8Amount = Nat64.toNat(Int64.toNat64(Float.toInt64(amount * 1e8)));
      let _ = await ledger.icrc1_transfer({
        memo = ?Text.encodeUtf8("0");
        from_subaccount = ?Account.defaultSubaccount();
        to = {owner = defaultAccount; subaccount = ?principalToSubaccount(user)};
        amount = e8Amount - fpl_fee ;
        fee = ?fpl_fee;
        created_at_time =?Nat64.fromNat(Int.abs(Time.now()));
      });
    };

I can just send via NNS just would be nice to send automatically.

1 Like

Could you please describe in more detail what exactly you would like the to do using the NNS dapp?

1 Like

I didn’t mention using the nns app for the code specified.

I’m not sure I can be clearer, I ran the first code and move people’s FPL from a canisters sub account related to their principal into that canisters default account. I want to reverse it essentially, moving from the canisters default account into the sub account related to their principal.

I’ll tag you on twitter about it

I’m not sure I understand what you are asking. If they have deposited funds in this account on your canister and that is where your dapp looks for their balance then I don’t see why your code wouldn’t work.

It would be good to know what your defaultAccount and principalToAccount functions look like. Typically the default account is just null…but perhaps you are doing something different?

Which error did you get when you run the code?

type TransferError = variant {
    BadFee : record { expected_fee : Tokens };
    BadBurn : record { min_burn_amount : Tokens };
    InsufficientFunds : record { balance : Tokens };
    TooOld;
    CreatedInFuture : record { ledger_time : Timestamp };
    TemporarilyUnavailable;
    Duplicate : record { duplicate_of : BlockIndex };
    GenericError : record { error_code : nat; message : text };
};

Yeah I was just being an idiot. Basically, I took the payment and sent to the backend with the users principal as the default account and then tried to send it back… but that account was just a temp account while I then moved into a combined ‘pot’ account, the backend’s default account.

So I’ve sent the FPL to the principal of the user, and not the backend and their sub account id which works.

Thanks