forked from cowri/ocean
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathdeployFractionalizer.js
More file actions
146 lines (118 loc) · 4.98 KB
/
Copy pathdeployFractionalizer.js
File metadata and controls
146 lines (118 loc) · 4.98 KB
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
const hre = require("hardhat");
const shell = require("../utils-js");
const delay = ms => new Promise(resolve => setTimeout(resolve, ms))
const deployFactory = async (signer) => {
const factoryContract = await ethers.getContractFactory("FractionalizerFactory", signer);
const factory = await factoryContract.deploy();
await factory.deployed();
delay(1000);
console.log('Deployed Fractionalizer Factory')
console.log('Fractionalizer Factory address:', factory.address)
await hre.run("verify:verify", {
address: factory.address,
constructorArguments: [],
});
return factory
}
const deployFractionalizer = async (factory, oceanAddress, collectionAddress, exchangeRate, is721) => {
await factory.deploy(oceanAddress, collectionAddress, exchangeRate, is721)
const fractionalizerAddress = await factory.getFractionalizer(collectionAddress)
console.log('Deployed fractionalizer for', collectionAddress)
console.log('Fractionalizer address:', fractionalizerAddress)
return fractionalizerAddress
}
const fractionalize721 = async(signer, oceanAddress, nftAddress, fractionalizerAddress, nftID) => {
const ocean = await hre.ethers.getContractAt("Ocean", oceanAddress)
const fractionalizer = await hre.ethers.getContractAt("Fractionalizer721", fractionalizerAddress);
const fungibleTokenID = await fractionalizer.fungibleTokenId();
const interactions = [
shell.interactions.wrapERC721({
address: nftAddress,
id: nftID
}),
shell.interactions.computeOutputAmount({
address: fractionalizerAddress,
inputToken: shell.utils.calculateWrappedTokenId({address: nftAddress, id: nftID}),
outputToken: fungibleTokenID,
specifiedAmount: 1,
metadata: ethers.utils.hexZeroPad(nftID, 32)
})
]
await shell.executeInteractions({
ocean,
signer,
interactions: interactions
});
console.log("Fungible token amount", await ocean.balanceOf(signer.address, fungibleTokenID))
await hre.run("verify:verify", {
address: fractionalizerAddress,
constructorArguments: [
oceanAddress,
nftAddress,
exchangeRate
],
});
}
const fractionalize1155 = async(signer, oceanAddress, nftAddress, fractionalizerAddress, nftID, nftAmount) => {
const ocean = await hre.ethers.getContractAt("Ocean", oceanAddress)
const fractionalizer = await hre.ethers.getContractAt("Fractionalizer1155", fractionalizerAddress);
let fungibleTokenID = await fractionalizer.fungibleTokenIds(shell.utils.calculateWrappedTokenId({address: nftAddress, id: nftID}));
if(fungibleTokenID == 0){
const nonce = await fractionalizer.registeredTokenNonce();
fungibleTokenID = shell.utils.calculateWrappedTokenId({address: fractionalizerAddress, id: nonce})
}
const nftContract = await hre.ethers.getContractAt("ERC1155", nftAddress);
const approvalStatus = await nftContract.isApprovedForAll(signer.address, oceanAddress)
if(!approvalStatus){
await nftContract.setApprovalForAll(oceanAddress, true)
}
const interactions = [
shell.interactions.wrapERC1155({
address: nftAddress,
id: nftID,
amount: nftAmount
}),
shell.interactions.computeOutputAmount({
address: fractionalizerAddress,
inputToken: shell.utils.calculateWrappedTokenId({address: nftAddress, id: nftID}),
outputToken: fungibleTokenID,
specifiedAmount: nftAmount,
metadata: ethers.utils.hexZeroPad(nftID, 32)
})
]
await shell.executeInteractions({
ocean,
signer,
interactions: interactions
});
console.log("Fungible token amount", await ocean.balanceOf(signer.address, fungibleTokenID))
await hre.run("verify:verify", {
address: fractionalizerAddress,
constructorArguments: [
oceanAddress,
nftAddress,
exchangeRate
],
});
}
async function main() {
const signer = await ethers.getSigner();
delay(1000)
console.log('Deploying from', signer.address)
console.log('Deployer ETH balance', ethers.utils.formatEther(await ethers.provider.getBalance(signer.address)))
const factory = await deployFactory(signer)
// const factory = await hre.ethers.getContractAt("FractionalizerFactory", "FACTORY_ADDRESS_HERE")
const oceanAddress = ''
const nftAddress = ''
const exchangeRate = hre.ethers.utils.parseUnits('100')
const is721 = true
const fractionalizerAddress = await deployFractionalizer(factory, oceanAddress, nftAddress, exchangeRate, is721)
await fractionalize721(signer, oceanAddress, nftAddress, fractionalizerAddress, 0);
// await fractionalize1155(signer, oceanAddress, nftAddress, fractionalizerAddress, 0, 1)
}
main()
.then(() => process.exit(0))
.catch((e) => {
console.error(e);
process.exit(1);
});