I’ve been trying to implement some functionality similar to OpenChat for another site I have called FootballGod (not OpenFPL) but it requires FPL to play. I need to show the deposit address for the user to send FPL to from the NNS, but I’m not sure how to compute this address.
OpenChat have 1 canister per user, so the address you send to is the canister id which is also the user’s principal id. But FootballGod is just a really small prediction data set, 30 numbers, an entire canister for a user would be overkill. I was thinking the deposit address would be a combination of:
This would create the entry in the OpenFPL SNS Ledger canister using that combination. Then within the FootballGod backend I could get the users balance using the same combination…
However when I send FPL from my local SNS to the principal id within my app and then attempt to get the balance of the account from the above computed FPL SNS ledger entry, it returns 0.
Thanks for getting back to me. So here is the process I’ve been following:
The user logs into FootballGod and gets their principal ID as they would in any other app. They then go to the NNS and send some FPL to that principal ID. So locally I use the sns-testing setup and send some FPL to the principal:
and the caller is converted to a subaccount using the helper function:
public func principalToSubaccount(principal : Principal) : Blob {
let idHash = SHA224.Digest();
idHash.write(Blob.toArray(Principal.toBlob(principal)));
let hashSum = idHash.sum();
let crc32Bytes = beBytes(CRC32.ofArray(hashSum));
let buffer = Buffer.fromArray<Nat8>(crc32Bytes);
for (x in hashSum.vals()) {
buffer.add(x);
};
let blob = Blob.fromArray(Buffer.toArray(buffer));
return blob;
};
Unforuntately I get a zero balance and not the tokens sent from the local NNS.
If the destination address used by the user at the NNS is the same as the address generated by getUserAccountBalance() - where owner is football_god_backend’s canister id, not the frontend’s canister id -, it should be working.
So I was able to get the account balance from just using the caller and null. Really I was confused about who owned the FPL after transferring to FootballGod, just because I use a FootballGod principal ID the user still has the funds and I can’t then use them for things after sending 100FPL like entering them into the competition directly.
So the modified path I’m taking is to send it to the principal id for FootballGod from the NNS but when a user enters the game I will have to do the follow:
From the frontend, have the user approve a payment with @dfinity/ledger, sending the FPL entry fee to the account { owner: football_god_backend_canister_id, subaccount: caller }.
After the successful payment, call the backend play() function, checking for the balance for { owner: football_god_backend_canister_id, subaccount: caller } and transferring it to { owner: football_god_backend_canister_id, subaccount: null } (i.e the prize pot).
After the successful transfer into the prize pot I can then create the users entry record into the game.
I just seem to be hitting an issue with installing the npm package for ledger, it conflicts with @dfinity/agent: