0%

Crypto-pancakeswap_receipt细节

swap token:

测试用BNB去购买VRT

代码步骤:

执行一段完整的交易,代码需要进行几个步骤:

  1. swapExactETHForTokens 传参,构造好函数

1
func = router_contract.functions.swapExactETHForTokens(min_output_tokens, [self.addr.wbnb, token_address], self.wallet, self.deadline(60))
  1. buildTransaction 构造好交易

1
transaction = func.buildTransaction(tx_params)
  1. sign_transaction 私钥进行签名

1
signed_tx = self.w3.eth.account.sign_transaction(transaction, private_key=self.secret_key)
  1. send_raw_transaction 广播交易

1
self.w3.eth.send_raw_transaction(signed_tx.rawTransaction)
  1. wait_for_transaction_receipt 等待交易完成,获取收据

1
self.w3.eth.wait_for_transaction_receipt(tx, timeout=60)
  1. processReceipt 可以解析获取Transfer事件的内容

1
logs = self.get_token_contract(token_address=token_address).events.Transfer().processReceipt(receipt)

receipt log 交易收据log:

2B776BF8-05AF-41A0-ADE5-402D51A5771E

tx记录

example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
{
'blockHash': HexBytes('0x9ba33f85ec6edccfee637ae9a3eb8303df74aeb81cbc106676c1af44f7291cb3'),
'blockNumber': 11864838,
'contractAddress': None,
'cumulativeGasUsed': 34932511,
'from': '0x302c98e6d6A65Bf15255b81972f9EaA1F45438C8',
'gasUsed': 130517,
'logs': [AttributeDict({
'address': '0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c',
'topics': [HexBytes('0xe1fffcc4923d04b559f4d29a8bfc6cda04eb5b0d3c460751c2402c5c5cc9109c'), HexBytes('0x00000000000000000000000010ed43c718714eb63d5aa57b78b54704e256024e')],
'data': '0x00000000000000000000000000000000000000000000000000038d7ea4c68000',
'blockNumber': 11864838,
'transactionHash': HexBytes('0xa1215da3b2f9fba0dc4ce90318fd42e947d596e91c6582d8b450612f6a472abf'),
'transactionIndex': 215,
'blockHash': HexBytes('0x9ba33f85ec6edccfee637ae9a3eb8303df74aeb81cbc106676c1af44f7291cb3'),
'logIndex': 752,
'removed': False
}), AttributeDict({
'address': '0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c',
'topics': [HexBytes('0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef'), HexBytes('0x00000000000000000000000010ed43c718714eb63d5aa57b78b54704e256024e'), HexBytes('0x000000000000000000000000ef5212ada83ec2cc105c409df10b8806d20e3b35')],
'data': '0x00000000000000000000000000000000000000000000000000038d7ea4c68000',
'blockNumber': 11864838,
'transactionHash': HexBytes('0xa1215da3b2f9fba0dc4ce90318fd42e947d596e91c6582d8b450612f6a472abf'),
'transactionIndex': 215,
'blockHash': HexBytes('0x9ba33f85ec6edccfee637ae9a3eb8303df74aeb81cbc106676c1af44f7291cb3'),
'logIndex': 753,
'removed': False
}), AttributeDict({
'address': '0x5F84ce30DC3cF7909101C69086c50De191895883',
'topics': [HexBytes('0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef'), HexBytes('0x000000000000000000000000ef5212ada83ec2cc105c409df10b8806d20e3b35'), HexBytes('0x000000000000000000000000302c98e6d6a65bf15255b81972f9eaa1f45438c8')],
'data': '0x000000000000000000000000000000000000000000000015e7daca2ce9a452a0',
'blockNumber': 11864838,
'transactionHash': HexBytes('0xa1215da3b2f9fba0dc4ce90318fd42e947d596e91c6582d8b450612f6a472abf'),
'transactionIndex': 215,
'blockHash': HexBytes('0x9ba33f85ec6edccfee637ae9a3eb8303df74aeb81cbc106676c1af44f7291cb3'),
'logIndex': 754,
'removed': False
}), AttributeDict({
'address': '0xEf5212aDa83EC2cc105C409DF10b8806D20E3b35',
'topics': [HexBytes('0x1c411e9a96e071241c2f21f7726b17ae89e3cab4c78be50e062b03a9fffbbad1')],
'data': '0x0000000000000000000000000000000000000000007f1919c0dd1f6e07bbbe220000000000000000000000000000000000000000000000148fc351f07c27c38d',
'blockNumber': 11864838,
'transactionHash': HexBytes('0xa1215da3b2f9fba0dc4ce90318fd42e947d596e91c6582d8b450612f6a472abf'),
'transactionIndex': 215,
'blockHash': HexBytes('0x9ba33f85ec6edccfee637ae9a3eb8303df74aeb81cbc106676c1af44f7291cb3'),
'logIndex': 755,
'removed': False
}), AttributeDict({
'address': '0xEf5212aDa83EC2cc105C409DF10b8806D20E3b35',
'topics': [HexBytes('0xd78ad95fa46c994b6551d0da85fc275fe613ce37657fb8d5e3d130840159d822'), HexBytes('0x00000000000000000000000010ed43c718714eb63d5aa57b78b54704e256024e'), HexBytes('0x000000000000000000000000302c98e6d6a65bf15255b81972f9eaa1f45438c8')],
'data': '0x000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000038d7ea4c68000000000000000000000000000000000000000000000000015e7daca2ce9a452a00000000000000000000000000000000000000000000000000000000000000000',
'blockNumber': 11864838,
'transactionHash': HexBytes('0xa1215da3b2f9fba0dc4ce90318fd42e947d596e91c6582d8b450612f6a472abf'),
'transactionIndex': 215,
'blockHash': HexBytes('0x9ba33f85ec6edccfee637ae9a3eb8303df74aeb81cbc106676c1af44f7291cb3'),
'logIndex': 756,
'removed': False
})],
'logsBloom': HexBytes('0x00200200000000000000000080000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000000000008000000200200000000000000000400008080000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000240001000400080000004000000000000000000000000000000400020000400000000000000000000000000000000200000002000004080000000000000000000000000000001800000000000080000000000000000000000000800000000000000000000000400000000000000000'),
'status': 1,
'to': '0x10ED43C718714eb63d5aA57B78B54704E256024E',
'transactionHash': HexBytes('0xa1215da3b2f9fba0dc4ce90318fd42e947d596e91c6582d8b450612f6a472abf'),
'transactionIndex': 215,
'type': '0x0'
}

Event:

  1. Wbnb Deposit()

  2. Wbnb Transfer()

  3. VRT Transfer()

  4. PancakeSwap V2: VRT 5 Sync()

  5. PancakeSwap V2: VRT 5 Swap()

Events:

交易后可以获取到收据对象,收据对象里有log,记录了每个调用函数的 入参

  1. Wbnb——Deposit()

1
2
3
4
5
6
7
8
9
10
11
12
13
AttributeDict({
'args': AttributeDict({
'dst': '0x10ED43C718714eb63d5aA57B78B54704E256024E', # router_v2
'wad': 1000000000000000 # 买入时 输入0.001BNB
}),
'event': 'Deposit',
'logIndex': 752,
'transactionIndex': 215,
'transactionHash': HexBytes('0xa1215da3b2f9fba0dc4ce90318fd42e947d596e91c6582d8b450612f6a472abf'),
'address': '0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c',
'blockHash': HexBytes('0x9ba33f85ec6edccfee637ae9a3eb8303df74aeb81cbc106676c1af44f7291cb3'),
'blockNumber': 11864838
})

router_v2质押了0.001 BNB

  1. Wbnb——Transfer()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
AttributeDict({
'args': AttributeDict({
'from': '0x10ED43C718714eb63d5aA57B78B54704E256024E', # router_v2
'to': '0xEf5212aDa83EC2cc105C409DF10b8806D20E3b35', # PancakeSwap V2: VRT 5
'value': 1000000000000000 # 买入时 输入0.001BNB
}),
'event': 'Transfer',
'logIndex': 753,
'transactionIndex': 215,
'transactionHash': HexBytes('0xa1215da3b2f9fba0dc4ce90318fd42e947d596e91c6582d8b450612f6a472abf'),
'address': '0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c',
'blockHash': HexBytes('0x9ba33f85ec6edccfee637ae9a3eb8303df74aeb81cbc106676c1af44f7291cb3'),
'blockNumber': 11864838
})

router_v2PancakeSwap V2: VRT 5转了0.001BNB

  1. VRT——Transfer()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
AttributeDict({
'args': AttributeDict({
'from': '0xEf5212aDa83EC2cc105C409DF10b8806D20E3b35', # PancakeSwap V2: VRT 5
'to': '0x302c98e6d6A65Bf15255b81972f9EaA1F45438C8', # My Address
'value': 404088513609832092320 # 得到404.08851360983209232 vrt
}),
'event': 'Transfer',
'logIndex': 754,
'transactionIndex': 215,
'transactionHash': HexBytes('0xa1215da3b2f9fba0dc4ce90318fd42e947d596e91c6582d8b450612f6a472abf'),
'address': '0x5F84ce30DC3cF7909101C69086c50De191895883',
'blockHash': HexBytes('0x9ba33f85ec6edccfee637ae9a3eb8303df74aeb81cbc106676c1af44f7291cb3'),
'blockNumber': 11864838
})

PancakeSwap V2: VRT 5My Address转了404.08851360983209232 vrt

  1. PancakeSwap V2: VRT 5——Sync()

1
2
3
4
5
6
7
8
9
10
11
12
13
AttributeDict({
'args': AttributeDict({
'reserve0': 153652113319030071628643874,
'reserve1': 379294095135390286733
}),
'event': 'Sync',
'logIndex': 755,
'transactionIndex': 215,
'transactionHash': HexBytes('0xa1215da3b2f9fba0dc4ce90318fd42e947d596e91c6582d8b450612f6a472abf'),
'address': '0xEf5212aDa83EC2cc105C409DF10b8806D20E3b35',
'blockHash': HexBytes('0x9ba33f85ec6edccfee637ae9a3eb8303df74aeb81cbc106676c1af44f7291cb3'),
'blockNumber': 11864838
})
1
2
3
4
5
6
7
(uint112 _reserve0, uint112 _reserve1,) = getReserves(); // gas savings
// 返回用于交易定价和分配流动性的token0和token1储备

// force reserves to match balances
function sync() external lock {
_update(IERC20(token0).balanceOf(address(this)), IERC20(token1).balanceOf(address(this)), reserve0, reserve1);
}

看了下solidity源码,这步应该是用于同步 交易定价和分配流动性的token0和token1储备

  1. PancakeSwap V2: VRT 5——Swap()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
AttributeDict({
'args': AttributeDict({
'sender': '0x10ED43C718714eb63d5aA57B78B54704E256024E', # router_v2
'to': '0x302c98e6d6A65Bf15255b81972f9EaA1F45438C8', # My Address
'amount0In': 0,
'amount1In': 1000000000000000, # 0.001BNB
'amount0Out': 404088513609832092320, # 404.08851360983209232 vrt
'amount1Out': 0
}),
'event': 'Swap',
'logIndex': 756,
'transactionIndex': 215,
'transactionHash': HexBytes('0xa1215da3b2f9fba0dc4ce90318fd42e947d596e91c6582d8b450612f6a472abf'),
'address': '0xEf5212aDa83EC2cc105C409DF10b8806D20E3b35',
'blockHash': HexBytes('0x9ba33f85ec6edccfee637ae9a3eb8303df74aeb81cbc106676c1af44f7291cb3'),
'blockNumber': 11864838
})

这步和第3步有什么区别?


Question:

swap比转账要消耗更多的gas?

我觉得是,测试过程中,swap的话消耗 0.000652585 BNB,转账的话消耗 0.0001071 BNB

20211018163457

swap要比转账需要更多的执行步骤, 所以需要消耗更多的gas

Summary:

交易收据里log列表里有事件event
event里的内容,用对应合约去解析一下,就可以看到记录里调用哪个合约的哪个函数以及哪些入参

感觉可以写个简单页面玩玩,类似debank里的交易历史
1A6352FF-E581-4644-88F2-A7994A20AD46

后面还要看看预言机的相关内容

Refs: