-
Notifications
You must be signed in to change notification settings - Fork 4
/
nft.php
81 lines (71 loc) · 2.52 KB
/
nft.php
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
require('./exampleBase.php');
require('./utils.php');
use Web3\Utils;
use Web3\Contract;
use Web3p\EthereumTx\Transaction;
$contract = new Contract($web3->provider, $erc721Json->abi);
$ownAccount = $testAddress;
$ownBalance = getBalance($eth, $ownAccount);
echo 'Start to deploy erc721 contract' . PHP_EOL;
// deploy erc721 token
$data = $contract->bytecode($erc721Json->bytecode)->getData("Web3Identity", "W3");
$nonce = getNonce($eth, $ownAccount);
$gasPrice = '0x' . Utils::toWei('5', 'gwei')->toHex();
$transaction = new Transaction([
'nonce' => '0x' . $nonce->toHex(),
'gas' => 3000000,
'gasPrice' => $gasPrice,
'data' => '0x' . $data,
'chainId' => $chainId
]);
$transaction->sign($testPrivateKey);
$txHash = '';
$eth->sendRawTransaction('0x' . $transaction->serialize(), function ($err, $transaction) use ($eth, $ownAccount, &$txHash) {
if ($err !== null) {
echo 'Error: ' . $err->getMessage();
return;
}
echo 'Deploy tx hash: ' . $transaction . PHP_EOL;
$txHash = $transaction;
});
$transaction = confirmTx($eth, $txHash);
if (!$transaction) {
throw new Error('Transaction was not confirmed.');
}
$contractAddress = $transaction->contractAddress;
echo "\nContract has been created:) block number: " . $transaction->blockNumber . PHP_EOL . "Contract address: " . $contractAddress . PHP_EOL;
$contract = $contract->at($contractAddress);
$contract->at($contractAddress)->estimateGas('mint', $ownAccount, 1, 'https://avatars.githubusercontent.com/u/34670362?s=400', [
'from' => $ownAccount,
], function ($err, $result) use (&$estimatedGas) {
if ($err !== null) {
throw $err;
}
$estimatedGas = '0x' . $result->toHex();
});
$data = $contract->getData('mint', $ownAccount, 1, 'https://avatars.githubusercontent.com/u/34670362?s=400');
$nonce = $nonce->add(Utils::toBn(1));
$transaction = new Transaction([
'nonce' => '0x' . $nonce->toHex(),
'to' => $contractAddress,
'gas' => $estimatedGas,
'gasPrice' => $gasPrice,
'data' => '0x' . $data,
'chainId' => $chainId
]);
$transaction->sign($testPrivateKey);
$txHash = '';
$eth->sendRawTransaction('0x' . $transaction->serialize(), function ($err, $transaction) use ($eth, $ownAccount, &$txHash) {
if ($err !== null) {
echo 'Error: ' . $err->getMessage();
return;
}
echo 'Mint tx hash: ' . $transaction . PHP_EOL;
$txHash = $transaction;
});
$transaction = confirmTx($eth, $txHash);
if (!$transaction) {
throw new Error('Transaction was not confirmed.');
}
echo "Minted!!!" . PHP_EOL;