solana糖果机-是否可以知道用户是否在交易中点击批准



我正在使用solana糖果机铸造nft

当我在javascript 中调用智能合约函数时

import * as anchor from "@project-serum/anchor";
let program = new anchor.Program(idl, programId, provider);
let result = await program.rpc.someFunc(); //here is the smart contract function

浏览器将显示弹出窗口,供您批准交易。有没有办法知道用户是点击取消还是批准?

在以太坊中,它有如下内容:

.on("transactionHash", function(hash) {
…
})
.on("error", function(error, receipt) {
…
});

可以在糖果机里做吗?我想在用户点击交易批准后做点什么

是的,您可以实现类似的功能,但直接从糖果机上实现效果不佳。

可能有多种方法,但我发现其中一种是使用血清中的connection.getSignatureStatuses()

代码看起来像这样,它与您共享的内容类似:

// Impor the connection dependencies
import * as anchor from '@project-serum/anchor';
// Loop using waiting for a positive status / or timeout
while (...) {
// Get the status using the transaction ID
const statusResponse = await connection.getSignatureStatuses([
txId,
]);
const status = statusResponse?.value[0];
if (status.err) {
// ...
}
}

一旦你得到了事务的状态,包括用户拒绝它,你就可以在前端触发行为。

最新更新