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

sui预估交易 #873

Open
klren0312 opened this issue Nov 26, 2024 · 1 comment
Open

sui预估交易 #873

klren0312 opened this issue Nov 26, 2024 · 1 comment
Labels
Blockchain 区块链相关问题

Comments

@klren0312
Copy link
Owner

klren0312 commented Nov 26, 2024

代码

const txb = new Transaction()
txb.moveCall({
  target: `${packageId}::gdNft::add_count`,
  arguments: [
    txb.object(recordObj.data.objectId),
    txb.pure.u64(originNum + num),
    txb.pure.string('当前功德:' + (originNum + num))
  ]
})
// 序列化交易前,需要指定sender
txb.setSender(account.address)
// 序列化交易
const serializedTx = await txb.build({
  client: client,
})
// 获取预交易结果
const dryRunResult = await client.dryRunTransactionBlock({
  transactionBlock: serializedTx
})
// 计算交易的gas
const {computationCost, storageCost, storageRebate} = dryRunResult.effects.gasUsed
const totalGas = BigInt(computationCost) + BigInt(storageCost) - BigInt(storageRebate)
console.log(totalGas.toString())

结果

{
    "effects": {
        "messageVersion": "v1",
        "status": {
            "status": "success"
        },
        "executedEpoch": "565",
        "gasUsed": {
            "computationCost": "1000000",
            "storageCost": "3845600",
            "storageRebate": "3807144",
            "nonRefundableStorageFee": "38456"
        },
        "modifiedAtVersions": [
            {
                "objectId": "0x076a77585915d5d7c17dc172e597b46d26a4852e056bf66a5025100cf0ff6a59",
                "sequenceNumber": "252796841"
            },
            {
                "objectId": "0x38bd1c22f812201b354a5d675b166e9791832a8544dd7ae4147a65fece0247e8",
                "sequenceNumber": "246710197"
            }
        ],
        "transactionDigest": "3L1FoThGfCpiqNqBnKvaHuEKLxiPMsRf5f3GSfcQeBiR",
        "mutated": [
            {
                "owner": {
                    "AddressOwner": "0x6db319f04b2c601f5fdf1e400bf7e2262a7a1338d867e6b63f360c7af3d3c989"
                },
                "reference": {
                    "objectId": "0x076a77585915d5d7c17dc172e597b46d26a4852e056bf66a5025100cf0ff6a59",
                    "version": 252796842,
                    "digest": "8bm5fTnSv6ejje4viY8GMNksPLpRTodvptXCfTtD2Ajz"
                }
            },
            {
                "owner": {
                    "AddressOwner": "0x6db319f04b2c601f5fdf1e400bf7e2262a7a1338d867e6b63f360c7af3d3c989"
                },
                "reference": {
                    "objectId": "0x38bd1c22f812201b354a5d675b166e9791832a8544dd7ae4147a65fece0247e8",
                    "version": 252796842,
                    "digest": "GRonkMyJdfw3XQ3JFNaFUYAzyL54i2TpPLnrvCpqT63s"
                }
            }
        ],
        "gasObject": {
            "owner": {
                "AddressOwner": "0x6db319f04b2c601f5fdf1e400bf7e2262a7a1338d867e6b63f360c7af3d3c989"
            },
            "reference": {
                "objectId": "0x076a77585915d5d7c17dc172e597b46d26a4852e056bf66a5025100cf0ff6a59",
                "version": 252796842,
                "digest": "8bm5fTnSv6ejje4viY8GMNksPLpRTodvptXCfTtD2Ajz"
            }
        },
        "dependencies": [
            "4Syfa81ob1WXf69hApbhE4JAAUCHNq4UwXDZvYw9S9ra",
            "C7Uc96SatJDw6dz8a3TQP7hqr6eYWEXdc2dcYtbPSDUK",
            "EDBYJXfBDfyFiHMbLXhXtDzWtZdhHeHYjSv1bhBCmK89"
        ]
    },
    "events": [],
    "objectChanges": [
        {
            "type": "mutated",
            "sender": "0x6db319f04b2c601f5fdf1e400bf7e2262a7a1338d867e6b63f360c7af3d3c989",
            "owner": {
                "AddressOwner": "0x6db319f04b2c601f5fdf1e400bf7e2262a7a1338d867e6b63f360c7af3d3c989"
            },
            "objectType": "0x2::coin::Coin<0x2::sui::SUI>",
            "objectId": "0x076a77585915d5d7c17dc172e597b46d26a4852e056bf66a5025100cf0ff6a59",
            "version": "252796842",
            "previousVersion": "252796841",
            "digest": "8bm5fTnSv6ejje4viY8GMNksPLpRTodvptXCfTtD2Ajz"
        },
        {
            "type": "mutated",
            "sender": "0x6db319f04b2c601f5fdf1e400bf7e2262a7a1338d867e6b63f360c7af3d3c989",
            "owner": {
                "AddressOwner": "0x6db319f04b2c601f5fdf1e400bf7e2262a7a1338d867e6b63f360c7af3d3c989"
            },
            "objectType": "0x544f19419ede80e69bf24f297effefcf3549710ffeee4b3a07aae34fa42588ba::gdNft::Gd",
            "objectId": "0x38bd1c22f812201b354a5d675b166e9791832a8544dd7ae4147a65fece0247e8",
            "version": "252796842",
            "previousVersion": "246710197",
            "digest": "GRonkMyJdfw3XQ3JFNaFUYAzyL54i2TpPLnrvCpqT63s"
        }
    ],
    "balanceChanges": [
        {
            "owner": {
                "AddressOwner": "0x6db319f04b2c601f5fdf1e400bf7e2262a7a1338d867e6b63f360c7af3d3c989"
            },
            "coinType": "0x2::sui::SUI",
            "amount": "-1038456"
        }
    ],
    "input": {
        "messageVersion": "v1",
        "transaction": {
            "kind": "ProgrammableTransaction",
            "inputs": [
                {
                    "type": "object",
                    "objectType": "immOrOwnedObject",
                    "objectId": "0x38bd1c22f812201b354a5d675b166e9791832a8544dd7ae4147a65fece0247e8",
                    "version": "246710197",
                    "digest": "CWjRsk5DSVBaPtpi5A7rZGxcRxUcoKEKRv5NoED8JTpu"
                },
                {
                    "type": "pure",
                    "valueType": "u64",
                    "value": "58"
                },
                {
                    "type": "pure",
                    "valueType": "0x1::string::String",
                    "value": "当前功德:58"
                }
            ],
            "transactions": [
                {
                    "MoveCall": {
                        "package": "0x544f19419ede80e69bf24f297effefcf3549710ffeee4b3a07aae34fa42588ba",
                        "module": "gdNft",
                        "function": "add_count",
                        "arguments": [
                            {
                                "Input": 0
                            },
                            {
                                "Input": 1
                            },
                            {
                                "Input": 2
                            }
                        ]
                    }
                }
            ]
        },
        "sender": "0x6db319f04b2c601f5fdf1e400bf7e2262a7a1338d867e6b63f360c7af3d3c989",
        "gasData": {
            "payment": [
                {
                    "objectId": "0x076a77585915d5d7c17dc172e597b46d26a4852e056bf66a5025100cf0ff6a59",
                    "version": 252796841,
                    "digest": "6edRwq4CdbJcSqT59K5FCYPxPTizH1zSfgbNuiywG3e6"
                }
            ],
            "owner": "0x6db319f04b2c601f5fdf1e400bf7e2262a7a1338d867e6b63f360c7af3d3c989",
            "price": "1000",
            "budget": "3016576"
        }
    }
}
@klren0312
Copy link
Owner Author

klren0312 commented Nov 26, 2024

在这个交易结果中,gas的消耗可以通过以下几个字段综合分析得出:

1. gasUsed 部分的字段:

computationCost: 表示执行计算所需的gas量(1000000)。
storageCost: 表示存储相关操作所需的gas量(3845600)。
storageRebate: 表示存储操作返还的gas量(3807144)。
nonRefundableStorageFee: 表示不可退还的存储费(38456)。
这些字段表示交易执行过程中与计算和存储相关的gas消耗情况。

2. 计算实际的消耗量:

实际消耗的gas可以通过公式计算:

实际消耗的 gas = computationCost + storageCost − storageRebate

3. 带入数据:

实际消耗的 gas=1000000+3845600−3807144=1038456

4. 确认费用与预算:

gasData中的price为每单位gas的价格(1000)。
budget为最大可用gas预算(3016576)。

实际费用为gas消耗量×price:

实际费用 = 1038456 × 1000 = 1038456000

因为实际费用小于预算,交易可以成功。

5. 交易中balanceChanges的验证:

balanceChanges中显示 SUI 余额减少了 -1038456,和实际消耗一致。

总结

本次交易总消耗为 1038456 gas,费用已在balanceChanges中体现,并从账户余额中扣除。

@klren0312 klren0312 added the Blockchain 区块链相关问题 label Nov 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Blockchain 区块链相关问题
Projects
None yet
Development

No branches or pull requests

1 participant