Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get transaction hash? #30

Closed
7JUMPER7 opened this issue Mar 4, 2024 · 13 comments
Closed

How to get transaction hash? #30

7JUMPER7 opened this issue Mar 4, 2024 · 13 comments

Comments

@7JUMPER7
Copy link

7JUMPER7 commented Mar 4, 2024

How to get transaction hash after I create it with:

const transfer = walletContract.createTransfer({
                seqno: seqno,
                secretKey: keypair.secretKey,
                messages: [internalMessage],
            });

and sent with:
await this.client.sendExternalMessage(walletContract, transfer);

When I call transfer.hash().toString("hex"), it gives me hash which is different from what I see on tonviewer.

@mahnunchik
Copy link

+1

@wbelhomsi
Copy link

Any updates on that? Having the same issue

@7JUMPER7
Copy link
Author

Any updates on that? Having the same issue

Nothing :(

@CursedMun
Copy link

transfer.hash().toString("base64") gives you what shows tonviewer

@7JUMPER7
Copy link
Author

7JUMPER7 commented May 3, 2024

transfer.hash().toString("base64") gives you what shows tonviewer

No. There is my hashes from transfer.hash():
base64: 5/VSswqg7BTCcdfBYXYKdosyU6U5SHZD9DZ575m01kI=
hex: e7f552b30aa0ec14c271d7c161760a768b3253a539487643f43679ef99b4d642

When I enter base64 to TonViewer, I have hex hash in domain string and 404 message on the page.
image

@CursedMun
Copy link

CursedMun commented May 4, 2024

transfer.hash().toString("base64") gives you what shows tonviewer

No. There is my hashes from transfer.hash(): base64: 5/VSswqg7BTCcdfBYXYKdosyU6U5SHZD9DZ575m01kI= hex: e7f552b30aa0ec14c271d7c161760a768b3253a539487643f43679ef99b4d642

When I enter base64 to TonViewer, I have hex hash in domain string and 404 message on the page. image

Can you provide the wallet of that transaction?
P.S. Can't see the image

@7JUMPER7
Copy link
Author

7JUMPER7 commented May 4, 2024

transfer.hash().toString("base64") gives you what shows tonviewer

No. There is my hashes from transfer.hash(): base64: 5/VSswqg7BTCcdfBYXYKdosyU6U5SHZD9DZ575m01kI= hex: e7f552b30aa0ec14c271d7c161760a768b3253a539487643f43679ef99b4d642
When I enter base64 to TonViewer, I have hex hash in domain string and 404 message on the page. image

Can you provide the wallet of that transaction? P.S. Can't see the image

This is mainnet, wallet: UQDSFFpQzCNjL3uOhwpGoEkLa67Tsjlq346VuMvtCM3bGFcu
There is the image
Screenshot 2024-05-04 at 12 27 34

@7JUMPER7
Copy link
Author

7JUMPER7 commented May 4, 2024

transfer.hash().toString("base64") gives you what shows tonviewer

No. There is my hashes from transfer.hash(): base64: 5/VSswqg7BTCcdfBYXYKdosyU6U5SHZD9DZ575m01kI= hex: e7f552b30aa0ec14c271d7c161760a768b3253a539487643f43679ef99b4d642
When I enter base64 to TonViewer, I have hex hash in domain string and 404 message on the page. image

Can you provide the wallet of that transaction? P.S. Can't see the image

This is mainnet, wallet: UQDSFFpQzCNjL3uOhwpGoEkLa67Tsjlq346VuMvtCM3bGFcu There is the image Screenshot 2024-05-04 at 12 27 34

Send the transaction too please, or it can be any?

There it is: https://tonviewer.com/transaction/72468f5599057f3cc36a75bbddbddba5d3317abc449c117db3b698ed94ba3982

@Inffix
Copy link

Inffix commented May 12, 2024

I have found a solution
The problem is that the final BoC is formed immediately before sending the transaction, and there is there is no built-in way to get its hash
So, it can be retrieved and returned in the functions external or sendMessage of the file @ton/ton/dist/client/TonClient.js
I rewrote the end of the function external (line 358) as follows

let final_cell = (0, core_1.beginCell)().store((0, core_1.storeMessage)(ext)).endCell()
let boc = final_cell.toBoc()
await client.sendFile(boc)
return final_cell.hash().toString('hex')

And function sendMessage (line 168) that way

async sendMessage(src) {
        let final_cell = (0, core_1.beginCell)().store((0, core_1.storeMessage)(src)).endCell()
        const boc = final_cell.toBoc();
        await __classPrivateFieldGet(this, _TonClient_api, "f").sendBoc(boc);
        return final_cell.hash().toString('hex')
}

Next, you need to track calls to this function up the tree and pass the hash value

For example

File @ton/ton/dist/client/TonClient.js (line 195)

 async sendExternalMessage(contract, src) {
        if (await this.isContractDeployed(contract.address) || !contract.init) {
            const message = (0, core_1.external)({
                to: contract.address,
                body: src
            });
            return await this.sendMessage(message);
        }
        else {
            const message = (0, core_1.external)({
                to: contract.address,
                init: { code: contract.init.code, data: contract.init.data },
                body: src
            });
            return await this.sendMessage(message);
        }
    }

File @ton/ton/dist/wallets/WalletContractV3R2.js (line 60)

async send(provider, message) {
    let hash = await provider.external(message)
    return hash
}

File @ton/ton/dist/client/TonClient.js (line 407)

let hash = await via.send({ to: address, value, bounce, sendMode: message.sendMode, init: neededInit, body})
return hash

If you're using DeDust SDK
File @dedust/sdk/dist/contracts/jettons/JettonWallet.js (line 24)
return await provider.internal(via, {...

File @dedust/sdk/dist/contracts/dex/vault/VaultNative.js (line 62)

let hash = await provider.internal(via, {sendMode: core_1.SendMode.PAY_GAS_SEPARATELY, body: cell, value: amount + (gasAmount ?? (0, core_1.toNano)('0.2'))})
return hash

@farshidbahmani
Copy link

const client = new TonClient({ 
		endpoint: 'https://testnet.toncenter.com/api/v2/jsonRPC',
		apiKey: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
	});
async transfer(req) {
    let mnemonics = req.body.mnemonics.split(" ")
    let keyPair = await mnemonicToPrivateKey(mnemonics);
    let wallet = WalletContractV4.create({ workchain: 0, publicKey: keyPair.publicKey });
    let contract = client.open(wallet);
    let seqno  = await contract.getSeqno();
    let tx = await contract.createTransfer({
        seqno,
        secretKey: keyPair.secretKey,
        messages: [internal({
            value: fromNano(req.body.amount),
            to: req.body.toAddress,
            body: req.body.tag,
            bounce: false,
        })],
    })
    
    let hash = await this.send(tx, wallet.address)

}

async send(message, address) {
let neededInit = null;
// if (init && !await client.isContractDeployed(address)) {
//     neededInit = init;
// }
const ext = (0, external)({
    to: address,
    init: neededInit,
    body: message
});
let boc = (0, beginCell)()
    .store((0, storeMessage)(ext))
    .endCell();
await client.sendFile(boc.toBoc());
return boc.hash().toString('hex')

}

@h1rdr3v2
Copy link

I have found a solution The problem is that the final BoC is formed immediately before sending the transaction, and there is there is no built-in way to get its hash So, it can be retrieved and returned in the functions external or sendMessage of the file @ton/ton/dist/client/TonClient.js I rewrote the end of the function external (line 358) as follows

let final_cell = (0, core_1.beginCell)().store((0, core_1.storeMessage)(ext)).endCell()
let boc = final_cell.toBoc()
await client.sendFile(boc)
return final_cell.hash().toString('hex')

And function sendMessage (line 168) that way

async sendMessage(src) {
        let final_cell = (0, core_1.beginCell)().store((0, core_1.storeMessage)(src)).endCell()
        const boc = final_cell.toBoc();
        await __classPrivateFieldGet(this, _TonClient_api, "f").sendBoc(boc);
        return final_cell.hash().toString('hex')
}

Next, you need to track calls to this function up the tree and pass the hash value

For example

File @ton/ton/dist/client/TonClient.js (line 195)

 async sendExternalMessage(contract, src) {
        if (await this.isContractDeployed(contract.address) || !contract.init) {
            const message = (0, core_1.external)({
                to: contract.address,
                body: src
            });
            return await this.sendMessage(message);
        }
        else {
            const message = (0, core_1.external)({
                to: contract.address,
                init: { code: contract.init.code, data: contract.init.data },
                body: src
            });
            return await this.sendMessage(message);
        }
    }

File @ton/ton/dist/wallets/WalletContractV3R2.js (line 60)

async send(provider, message) {
    let hash = await provider.external(message)
    return hash
}

File @ton/ton/dist/client/TonClient.js (line 407)

let hash = await via.send({ to: address, value, bounce, sendMode: message.sendMode, init: neededInit, body})
return hash

If you're using DeDust SDK File @dedust/sdk/dist/contracts/jettons/JettonWallet.js (line 24) return await provider.internal(via, {...

File @dedust/sdk/dist/contracts/dex/vault/VaultNative.js (line 62)

let hash = await provider.internal(via, {sendMode: core_1.SendMode.PAY_GAS_SEPARATELY, body: cell, value: amount + (gasAmount ?? (0, core_1.toNano)('0.2'))})
return hash

I followed your method and i got something back, but one issue the transaction hash differs from the one on the explorer.
previously without your code i do not get a reply at all

const hash = await contract.sendTransfer({ secretKey: KEYPAIR.secretKey, seqno, messages: [ internal({ value: amount, to: toAddress, }), ], sendMode: 3, });

in creating my wallet instance i use
WalletContractV4.create({ endpoint })

Do you think there would be a better update in the future, because the lib encounters an error it stops my code from running even when i have a try..catch block

@cuonghx-ngen
Copy link

+1

@Jobians
Copy link

Jobians commented Jul 29, 2024

const client = new TonClient({ 
		endpoint: 'https://testnet.toncenter.com/api/v2/jsonRPC',
		apiKey: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
	});
async transfer(req) {
    let mnemonics = req.body.mnemonics.split(" ")
    let keyPair = await mnemonicToPrivateKey(mnemonics);
    let wallet = WalletContractV4.create({ workchain: 0, publicKey: keyPair.publicKey });
    let contract = client.open(wallet);
    let seqno  = await contract.getSeqno();
    let tx = await contract.createTransfer({
        seqno,
        secretKey: keyPair.secretKey,
        messages: [internal({
            value: fromNano(req.body.amount),
            to: req.body.toAddress,
            body: req.body.tag,
            bounce: false,
        })],
    })
    
    let hash = await this.send(tx, wallet.address)

}

async send(message, address) {
let neededInit = null;
// if (init && !await client.isContractDeployed(address)) {
//     neededInit = init;
// }
const ext = (0, external)({
    to: address,
    init: neededInit,
    body: message
});
let boc = (0, beginCell)()
    .store((0, storeMessage)(ext))
    .endCell();
await client.sendFile(boc.toBoc());
return boc.hash().toString('hex')

}

Bro your solution works, thanks man

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants