public func transferFunds(){
let now = Time.now();
let res = Ledger.transfer({
amount = { e8s = 2_000_000_000 };
created_at_time = ?{ timestamp_nanos = Nat64.fromNat(Int.abs(now)) };
fee = { e8s = 10_000 };
from_subaccount = null;
memo = 42;
to = "22oak-uehh2-5fc2j-rdhlb-zypwb-hwgyq-kgrfl-tvait-nwqof-hssiv-3qe";
});
};
Well I write this function to transfer ICP from the caller account to the other account having principal id = 22oak-uehh2-5fc2j-rdhlb-zypwb-hwgyq-kgrfl-tvait-nwqof-hssiv-3qe… The function execute without any error, but what funny is neither the caller account balance got debited, nor the receiver account got credited.
What am I doing wrong, and what is the current approach?
You can only send ICPs between accounts.
Do not use the principal as sender or receiver. You need to convert the principal to the main account identifier.
Take a look a this section : The ICP Ledger | Internet Computer Home
You can compute the (main) account programmatically or just take a look on ICScan that does it automatically for you.
public func transferFunds(){
let now = Time.now();
var main_account_principal = Principal.fromText("22oak-uehh2-5fc2j-rdhlb-zypwb-hwgyq-kgrfl-tvait-nwqof-hssiv-3qe");
let res = Ledger.transfer({
amount = { e8s = 2_000_000_000 };
created_at_time = ?{ timestamp_nanos = Nat64.fromNat(Int.abs(now)) };
fee = { e8s = 10_000 };
from_subaccount = null;
memo = 42;
to = Account.accountIdentifier(main_account_principal, Account.defaultSubaccount());
});
};
Well I have changed my code, but the problem remain the same, the accounts are not getting updated after the function call.
Note: I am doing in my local environment.
Can you inspect the value of res? Maybe you can just return it? It should either contain confirmation that everything worked or it should contain an error of some sort. In my experience with the ledger, the error messages were always good explanations what went wrong.
Wild guess since you’re developing locally: does your source account even have enough tokens to transfer?
The dfx command to check the ICP balance is dfx ledger balance <account id> (maybe with --network ic).
To figure out the account identifier, use dfx ledger account-id to produce the identifier. Use dfx ledger account-id --help to see how to use the command.
dfx wallet balance shows you the (cycles) balance of your cycles wallet.
first it figures out the ‘most prolific author’ by the number of posts created
then it checks the most prolific autor
if it’s empty, do nothing (no posts created yet, so noone to tranfer tokens to)
otherwise: transfer tokens to that person
The transfer happens like this:
// If there is a winner, transfer 1 Token to the winner.
let res = await Ledger.transfer({
memo = Nat64.fromNat(maxPosts);
from_subaccount = null;
to = Account.accountIdentifier(principal, Account.defaultSubaccount());
amount = { e8s = 100_000_000 };
fee = { e8s = 10_000 };
created_at_time = ?{ timestamp_nanos = Nat64.fromNat(Int.abs(now)) };
})
and it returns the principal of the most prolific author.
Side note: the principal 2vxsx-fae is the so-called anonymous identity, which everyone is allowed to use. If you do dfx identity use anonymous your requests will be sent using that principal.
To figure out the account that got credited, look at the above code block: to = Account.accountIdentifier(principal, Account.defaultSubaccount());
principal = 2vxsx-fae (as returned by the function in the end)
subaccount = default subaccount = empty string
ledger account = dfx ledger account-id --of-principal "2vxsx-fae" = 1c7a48ba6a562aa9eaa2481a9049cdf0433b9738c992d698c31d8abf89cadc79. This is where the tokens get sent to.