-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCreate.tsx
251 lines (241 loc) · 8.73 KB
/
Create.tsx
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import { useState } from 'react';
import {
encodeAbiParameters,
encodeFunctionData,
encodePacked,
parseUnits,
zeroAddress,
} from 'viem';
import { currChainId, uiConfig } from '../utils/constants';
import { lensHubAbi } from '../utils/abis/lensHubAbi';
import { useWalletClient } from 'wagmi';
import { Input } from '@/components/ui/input';
import { Button } from '@/components/ui/button';
import {
ChainId,
getCommonSignatureFromString,
getCommonSignature,
CommonNftType,
} from '@decent.xyz/box-common';
import { useDecentOA } from '@/context/DecentOAContext';
import { publicClient } from '@/main';
export const Create = () => {
const { address, profileId, refresh } = useDecentOA();
const { data: walletClient } = useWalletClient();
const [createState, setCreateState] = useState<string | undefined>();
const [freeCollect, setFreeCollect] = useState<boolean>(false);
const [txHash, setTxHash] = useState<string | undefined>();
const [uri, setURI] = useState<string>(
'https://upload.wikimedia.org/wikipedia/commons/1/17/University_of_Virginia_seal.svg'
);
const [nftType, setNftType] = useState<string>('Decent');
const [dstChainId, setDstChainId] = useState<number>(ChainId.POLYGON);
const [nftAddress, setNftAddress] = useState<string>(
uiConfig.nfts.polygonNft0_1
);
const [cost, setCost] = useState<string>('0.1');
// other example nft
// const [nftType, setNftType] = useState<string>('Decent');
// const [dstChainId, setDstChainId] = useState<number>(ChainId.ARBITRUM);
// const [nftAddress, setNftAddress] = useState<string>(
// uiConfig.nfts.arbitrumNft0_00005
// );
// const [cost, setCost] = useState<string>('0.00005');
const createPost = async () => {
// this can be done in a handful of ways either by calling
// getCommonSignature: and selecting the CommonNftType enum from dropdown in the frontend or
// getCommonSignatureFromString: this is more flexible for ppl who want to pass
// in arbitrary function signatures / no dropdown
const nftSignature: string = getCommonSignatureFromString(nftType);
if (!profileId) return;
// our encoded abi parameters
const InitData = [
{
name: 'data',
type: 'tuple',
internalType: 'struct InitializedAction',
components: [
{ name: 'targetContract', type: 'address', internalType: 'address' },
{ name: 'tokenId', type: 'uint256', internalType: 'uint256' },
{ name: 'paymentToken', type: 'address', internalType: 'address' },
{ name: 'chainId', type: 'uint256', internalType: 'uint256' },
{ name: 'cost', type: 'uint256', internalType: 'uint256' },
{
name: 'publishingClientProfileId',
type: 'uint256',
internalType: 'uint256',
},
{ name: 'signature', type: 'bytes', internalType: 'bytes' },
{ name: 'platformName', type: 'bytes', internalType: 'bytes' },
],
},
] as const;
const encodedInitData = encodeAbiParameters(InitData, [
{
targetContract: nftAddress as `0x${string}`,
tokenId: 0n,
paymentToken: zeroAddress,
chainId: BigInt(dstChainId),
cost: parseUnits(cost, 18),
publishingClientProfileId: BigInt(profileId),
signature: encodePacked(['string'], [nftSignature]),
platformName: encodePacked(['string'], ['ExampleRepo']),
},
]);
const actionModulesInitDatas = [encodedInitData];
// get the decent open action
const actionModules = [uiConfig.decentOpenActionContractAddress];
if (freeCollect) {
const baseFeeCollectModuleTypes = [
{ type: 'uint160' },
{ type: 'uint96' },
{ type: 'address' },
{ type: 'uint16' },
{ type: 'bool' },
{ type: 'uint72' },
{ type: 'address' },
];
const encodedBaseFeeCollectModuleInitData = encodeAbiParameters(
baseFeeCollectModuleTypes,
[0, 0, zeroAddress, 0, false, 0, zeroAddress]
);
const encodedCollectActionInitData = encodeAbiParameters(
[{ type: 'address' }, { type: 'bytes' }],
[
uiConfig.simpleCollectModuleContractAddress,
encodedBaseFeeCollectModuleInitData,
]
);
actionModulesInitDatas.push(encodedCollectActionInitData);
actionModules.push(uiConfig.collectActionContractAddress);
}
// Post parameters
const args = {
profileId: BigInt(profileId!),
contentURI: uri,
actionModules,
actionModulesInitDatas,
referenceModule:
'0x0000000000000000000000000000000000000000' as `0x${string}`,
referenceModuleInitData: '0x01' as `0x${string}`,
};
const calldata = encodeFunctionData({
abi: lensHubAbi,
functionName: 'post',
args: [args],
});
setCreateState('PENDING IN WALLET');
try {
const hash = await walletClient!.sendTransaction({
to: uiConfig.lensHubProxyAddress,
account: address,
data: calldata,
});
setCreateState('PENDING IN MEMPOOL');
setTxHash(hash);
const result = await publicClient({
chainId: currChainId,
}).waitForTransactionReceipt({ hash });
if (result.status === 'success') {
setCreateState('SUCCESS');
refresh();
} else {
setCreateState('CREATE TXN REVERTED');
}
} catch (e) {
setCreateState(`ERROR: ${e instanceof Error ? e.message : String(e)}`);
}
};
return (
<>
<div className="pb-4">
{address && profileId && (
<div className="flex flex-1 flex-col">
<div className="flex flex-1 flex-col">
<p className="my-2">Content URI (link to content for the post)</p>
<Input
type="text"
value={uri}
placeholder="URI"
onChange={(e) => setURI(e.target.value)}
/>
<p className="my-2">Initialize contract address</p>
<Input
placeholder="Types: Zora, Sound, or Manifold"
type="string"
value={nftAddress}
onChange={(e) => setNftAddress(e.target.value)}
/>
<p className="my-2">Chain</p>
<select
name="chain"
id="chainSelect"
onChange={(e) => {
setDstChainId(parseInt(e.target.value));
console.log(parseInt(e.target.value));
}}
className="border p-2"
>
<option value={ChainId.POLYGON}>Polygon</option>
<option value={ChainId.ARBITRUM}>Arbitrum</option>
<option value={ChainId.ETHEREUM}>Ethereum</option>
<option value={ChainId.OPTIMISM}>Optimism</option>
<option value={ChainId.BASE}>Base</option>
<option value={ChainId.POLYGON_TESTNET}>Mumbai</option>
<option value={ChainId.GOERLI}>Goerli</option>
</select>
<p className="my-2">Cost</p>
<Input
placeholder="Types: Zora, Sound, or Manifold"
type="string"
value={cost}
onChange={(e) => setCost(e.target.value)}
/>
<p className="my-2">Contract Type</p>
<Input
placeholder="Types: Zora, Sound, or Manifold"
type="string"
value={nftType}
onChange={(e) => setNftType(e.target.value)}
/>
<div className="my-3 mx-auto">
<input
type="checkbox"
id="filterCheckbox"
className="mr-3 cursor-pointer"
checked={freeCollect}
onChange={(e) => setFreeCollect(e.target.checked)}
/>
<label htmlFor="filterCheckbox">Enable free collects</label>
</div>
<Button className="mt-3" onClick={createPost}>
Create
</Button>
</div>
{createState && <p className="create-state-text">{createState}</p>}
{txHash && (
<a
href={`${uiConfig.blockExplorerLink}${txHash}`}
className="block-explorer-link"
target="_blank"
>
Block Explorer Link
</a>
)}
<Button
variant={'outline'}
className="my-3"
onClick={() => {
setTxHash(undefined);
setURI('');
setCreateState(undefined);
}}
>
Clear
</Button>
</div>
)}
</div>
</>
);
};