0%

solidity | 合约领取空投

利用合约低成本大量领取空投。


参考资料


更高级的操作


合约


我修改了 RND 的合约,然后迁移到了 bsc 的测试网中,其具体内容如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// SPDX-License-Identifier: MIT
pragma solidity ^0.7.0;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";

contract Rnd is ERC20 {
uint256 public _totalSupply = uint256(37800000000000 ether);
mapping(address => bool) public is_claim;

constructor() ERC20("random", "RND"){
}


function claim() external {
if (is_claim[msg.sender] == false) {
is_claim[msg.sender] = true;
_mint(msg.sender, 100 wei);
}
}
}

感兴趣的可以使用这个合约来撸空投,由于没有很多限制,所以,这个合约可以无限量撸下去。

该合约的 abi claim 如下:

1
2
3
4
5
6
7
8
9
[
{
"inputs": [],
"name": "claim",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
}
]

批量撸空投


我将使用两个方法

  • 模拟多个账号分别 claim
  • 使用一个账号进行合约创建撸

多个账号撸

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
import time
from web3 import Web3

abi = """[
{
"inputs": [],
"name": "claim",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
}
]
"""

w3 = Web3(Web3.HTTPProvider("https://data-seed-prebsc-1-s1.binance.org:8545"))
pancakeswap = "0x986f089bdc9C50b9c94C78687Ff518dc89354D39"

contractObj = w3.eth.contract(
address=Web3.toChecksumAddress(pancakeswap), abi=abi)

tx_dic = contractObj.functions.claim(
). \
buildTransaction(
{
'gas': 500000,
}
)

def send_transaction(tx_dic, wallet_address, wallet_private):
nonce = w3.eth.getTransactionCount(wallet_address)
tx_dic["nonce"] = nonce
tx_dic['gasPrice'] = w3.toWei(20, 'gwei')
# print(nonce)
sign_tx = w3.eth.account.signTransaction(tx_dic, private_key=wallet_private)
txn_hash = w3.eth.sendRawTransaction(sign_tx.rawTransaction)
return Web3.toHex(txn_hash)

tx = send_transaction(tx_dic, address, private)
print(tx)

一笔交易大概使用 0.19U

如果是 10 笔交易,算上 2 次转账「转 BNB、转 RND,每次大概 0.17U 左右。」,那么一共大概是 5.3U

利用合约批量撸

ps: 这个合约算是比较高级的用法了,因为这样可以突破 CA 限制。

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
pragma solidity ^0.7.0;

interface airdrop {
function transfer(address recipient, uint256 amount) external;

function balanceOf(address account) external view returns (uint256);

function claim() external;
}

contract RndGet {
//RND的合约地址
address constant contra = address(0x986f089bdc9C50b9c94C78687Ff518dc89354D39);

//创建子合约
function call(uint256 times) public {
for (uint i = 0; i < times; ++i) {
new claimer(contra);
}
}

function callnodest(uint256 times) public {
for (uint i = 0; i < times; ++i) {
new claimernodest(contra);
}
}
}

contract claimer {
constructor(address contra){
//领取空投
airdrop(contra).claim();
//获得领取的空投数量
uint256 balance = airdrop(contra).balanceOf(address(this));
//把空投的币转回主钱包
airdrop(contra).transfer(address(tx.origin), balance);
//销毁子合约
selfdestruct(payable(address(msg.sender)));
}
}

contract claimernodest {
constructor(address contra){
//领取空投
airdrop(contra).claim();
//获得领取的空投数量
uint256 balance = airdrop(contra).balanceOf(address(this));
//把空投的币转回主钱包
airdrop(contra).transfer(address(tx.origin), balance);
}
}

上面一个有销毁合约,一个没有销毁合约。

销毁合约会返回一些 gas

我部署的合约如下

使用 python 调用如下。

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
66
67
from web3 import Web3

abi = """[
{
"inputs": [
{
"internalType": "uint256",
"name": "times",
"type": "uint256"
}
],
"name": "call",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint256",
"name": "times",
"type": "uint256"
}
],
"name": "callnodest",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
}
]
"""

w3 = Web3(Web3.HTTPProvider("https://data-seed-prebsc-1-s1.binance.org:8545"))
contract = "0x10373DA73C47b09E3d35199175A9cec7d7Cbc585"

contractObj = w3.eth.contract(
address=Web3.toChecksumAddress(contract), abi=abi)

tx_dic1 = contractObj.functions.call(10). \
buildTransaction(
{
'gas': 15000000,
}
)

tx_dic2 = contractObj.functions.callnodest(10). \
buildTransaction(
{
'gas': 15000000,
}
)


def send_transaction(tx_dic, wallet_address, wallet_private):
nonce = w3.eth.getTransactionCount(wallet_address)
tx_dic["nonce"] = nonce
tx_dic['gasPrice'] = w3.toWei(20, 'gwei')
sign_tx = w3.eth.account.signTransaction(tx_dic, private_key=wallet_private)
txn_hash = w3.eth.sendRawTransaction(sign_tx.rawTransaction)
return Web3.toHex(txn_hash)


# tx1 = send_transaction(tx_dic1, "address",
# "private")

tx2 = send_transaction(tx_dic1, "address",
"private")

第一种,合约销毁,最后花费的价钱是 4.88 U

第二种,合约不销毁,最后花费的价钱是 7.64 U

请我喝杯咖啡吧~