hi,
I built a simple game that can players discover the manipulation in the buying or selling process and I made a simple transaction for every process but I don’t use ledger here it is just a simple object my question is how I can test these transactions and if my simplicity in building transaction, not the right way please suggest or guide me the right way to save these transactions
You can see how you can test the ICP ledger locally using this tutorial.
thanks @jennifertran I read this tutorial before it is still confusing I need a real example
Can you walk me through what you are doing on the game? Do you allow players to transact with ICP? If so, how did you build that functionality?
the game is simple it is all about how players can discover the deceiving in the game if the player sells a product with misleading information the buyer player should find it so it is required that the transactions in the game should be visible but I don’t know how can I reach it, I read about ledgers, and cycles in ICP but I couldn’t figure out how can I handle this specially I am newbie in this technology so I did a simple object that represent the transaction and save it in an array. so that it thanks for your help.
Is the goal to allow players to sell the product with ICP? How would seeing the transactions of a product help with confirming if it is fraudulent?
You would need the ICP ledger to allow players to transact with ICP. This video tutorial might be able to provide better clarity.
thanks, @jennifertran I used the ICP ledger locally but I need to test these transactions in a test network my main problem is when I deploy that simple project it is not enough to deploy on the internet I don’t have enough ICP to deploy the project though I claimed the free ICP from the dfinity discord so I wonder am I deploy the project wrong or just the amount of ICP doesn’t enough? so I will ask this question in a separate post.
I will watch the video thanks again @jennifertran
Thank you for the clarification. You can test these transactions locally.
There is not a “test” network on ICP.
You should have enough to deploy the simple application. Do you know how many cycles you have now?
dfx cycles --network ic balance
now i have another problem when i deployed my project locally the icp_ledger failed and I don’t know how can I fix it?
What is the error that you are getting?
thanks @jennifertran
I fixed it.
I have another question is there any way to capture the specific transaction happening on a specific product and here I am talking about my simple project?
You should be able to call the query_blocks
function in the local canister to see the latest transactions. You can view the older transactions in the archive canisters.
You can learn more about the concepts here.
thanks @jennifertran
so, I can’t make a relationship between transactions and any other data in my case the product itself?
my mission is to store all transactions that happened in a specific product therefore I can show it to the admin when clicking the product.
if I used the transfer method which returns the block index I could make a relationship between the product and that block index and then I used the query_blocks
method to fetch from that index as the start but that also has a problem which there are a lot of transactions happened in other products so how can I filter that?
To confirm, you are trying to tie a transaction specific to a product. Let’s say Customer A buys Product A from Customer B using ICP. You would like to tie this transaction?
You can do the following:
- When a user calls a
transfer
function, it should return the block number specific to the transaction such as:
(variant { Ok = 2 : nat })
- You can create a Hashmap (if using Motoko) to store the block number with the product ID. For example:
import HashMap "mo:base/HashMap";
import Text "mo:base/Text";
let map = HashMap.HashMap<Text, Nat>(5, Text.equal, Text.hash);
map.put("productA", blockNumber);
- Putting this together, I imagine you can something like this (Please test this):
import Icrc1Ledger "canister:icrc1_ledger_canister";
import Debug "mo:base/Debug";
import Result "mo:base/Result";
import Error "mo:base/Error";
import HashMap "mo:base/HashMap";
actor {
// Define the ProductName type
type ProductName = Text;
type TransferArgs = {
amount : Nat;
toAccount : Icrc1Ledger.Account;
productName : ProductName;
};
let map = HashMap.HashMap<Text, Nat>(5, Text.equal, Text.hash);
public shared func transfer(args : TransferArgs) : async Result.Result<Icrc1Ledger.BlockIndex, Text> {
Debug.print(
"Transferring "
# debug_show (args.amount)
# " tokens to account "
# debug_show (args.toAccount)
# " for product "
# debug_show (args.productName)
);
let transferArgs : Icrc1Ledger.TransferArg = {
// can be used to distinguish between transactions
memo = null;
// the amount we want to transfer
amount = args.amount;
// we want to transfer tokens from the default subaccount of the canister
from_subaccount = null;
// if not specified, the default fee for the canister is used
fee = null;
// the account we want to transfer tokens to
to = args.toAccount;
// a timestamp indicating when the transaction was created by the caller; if it is not specified by the caller then this is set to the current ICP time
created_at_time = null;
};
try {
// initiate the transfer
let transferResult = await Icrc1Ledger.icrc1_transfer(transferArgs);
// check if the transfer was successful
switch (transferResult) {
case (#Err(transferError)) {
return #err("Couldn't transfer funds:\n" # debug_show (transferError));
};
case (#Ok(blockIndex)) {
map.put(args.productName, blockIndex); // Use args.productName
return #ok blockIndex;
};
};
} catch (error : Error) {
// catch any errors that might occur during the transfer
return #err("Reject message: " # Error.message(error));
};
};
};
Awesome @jennifertran
that is what I thought but I couldn’t figure out how I store the transactions themselves in a map so is there any way to store them only for the product item I know that there is a method called get_blocks
that takes a start index and length but I am not sure if it can give me only the transactions belongs to the specific product or not?
Do you mean query_blocks
? query_blocks
would give you the blocks of the ICP ledger which would only include the transactions.
If you want to log the transactions by product, you should create your own map logging the transactions and product info.
Please let me know if you need more information.
thanks @jennifertran
yes, please I want to log the transactions by product how can I do that?
I recommend avoid doing that within the ledger.
Do this as mentioned: