I can send you here what I am doing. I am using the candid file from the ckbtc ledger and the ckbtc minter. Using dfx generate
and using the Actor created in the index file of each one.
Then I use a minterService to connect controller to actor, and use this flow to create Principal and return his Id, then get the balance and then the transfer:
GENERATE CKBTC ADDRESS
const newUuid = uuidv4.v4();
const { uniqueValue, uuid } = req.body;
const encryptedValue = `${uniqueValue}${uuid || newUuid}`;
const address: string = await minterService.getCkBtcAddress(encryptedValue);
MINTER SERVICE FUNCTION
const identity = identityService.createIdentity(uniqueValue);
const principal: Principal = identity.getPrincipal();
const address = principal.toText();
Then I query for balance
GET BALANCE
const decryptedAddress = decrypt(address); //I RETURN IT ENCRYPTED IN THE GET ADDRESS ENDPOINT, THIS WORKS PERFECT
const principal: Principal = Principal.fromText(decryptedAddress);
const satoshiBalance: number = await ledgerService.getBalance({ owner: principal, subaccount: [] });
const ckbtcBalance = satoshiBalance / 100000000;
GET BALANCE FUNCTION
const balance = await ledger.icrc1_balance_of(payload);
this works fine, it returns 2000n what is the amount i sent before from NNS wallet
TRASNFER
const { transferPayload, from } = payload;
const { uuid, uniqueValue } = from; //THIS ARE THE SAME VALUE I GET IN CREATE ADDRESS, AND I USE HERE TO CREATE ANOTHER ACTOR WITH THE IDENTITY FOR THIS ACCOUNT SETTED
const customLedger = this.ledgerCustomAgent(uuid, uniqueValue);
const identity: Identity = identityService.createIdentity(`${uniqueValue}${uuid}`);
const hasBalance = await customLedger.icrc1_balance_of({ owner: identity.getPrincipal(), subaccount: [] });
const transfer = await customLedger.icrc1_transfer(transferPayload)
THE CREATE CUSTOM AGENT:
const canisterId = config.ledger.id;
const identity: Identity = identityService.createIdentity(`${uniqueValue}${uuid}`);
const agentOptions = {
identity,
};
return createActor(canisterId, { agent: undefined, agentOptions });
If you need something else let me know pls. Otherwise, if you want, i can send you a copy of the repository without sensitive information