-
Notifications
You must be signed in to change notification settings - Fork 261
Expand file tree
/
Copy pathdeploy-framework.js
More file actions
1369 lines (1242 loc) · 53.6 KB
/
deploy-framework.js
File metadata and controls
1369 lines (1242 loc) · 53.6 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
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
const fs = require("fs");
const util = require("util");
const { execSync } = require('child_process');
const getConfig = require("./libs/getConfig");
const SuperfluidSDK = require("@superfluid-finance/js-sdk");
const {web3tx} = require("@decentral.ee/web3-helpers");
const deployERC1820 = require("../ops-scripts/deploy-erc1820");
const {
getScriptRunnerFactory: S,
ZERO_ADDRESS,
hasCode,
codeChanged,
isProxiable,
extractWeb3Options,
builtTruffleContractLoader,
sendGovernanceAction,
setResolver,
versionStringToPseudoAddress,
pseudoAddressToVersionString,
getGasConfig,
} = require("./libs/common");
let resetSuperfluidFramework;
let resolver;
let sfObjForGovAndResolver;
/// @param deployFunc must return a contract object
/// @returns the newly deployed or existing loaded contract
async function deployAndRegisterContractIf(
Contract,
resolverKey,
cond,
deployFunc
) {
let contractDeployed;
const contractName = Contract.contractName;
const contractAddress = await resolver.get.call(resolverKey);
console.log(`${resolverKey} address`, contractAddress);
if (resetSuperfluidFramework || (await cond(contractAddress))) {
console.log(`${contractName} needs new deployment.`);
contractDeployed = await deployFunc();
console.log(`${resolverKey} deployed to`, contractDeployed.address);
await setResolver(sfObjForGovAndResolver, resolverKey, contractDeployed.address);
} else {
console.log(`${contractName} does not need new deployment.`);
contractDeployed = await Contract.at(contractAddress);
}
return contractDeployed;
}
/// @param deployFunc must return a contract address
/// @returns the address of the newly deployed contract or ZERO_ADDRESS if not deployed
async function deployContractIf(web3, Contract, cond, deployFunc) {
const contractName = Contract.contractName;
if (await cond()) {
console.log(`${contractName} logic code has changed`);
const newCodeAddress = await deployFunc();
console.log(`${contractName} new logic code address ${newCodeAddress}`);
return newCodeAddress;
} else {
console.log(
`${contractName} has the same logic code, no deployment needed.`
);
return ZERO_ADDRESS;
}
}
/// @param deployFunc must return a contract address
/// @returns the address of the newly deployed contract or ZERO_ADDRESS if not deployed
async function deployContractIfCodeChanged(
web3,
Contract,
codeAddress,
deployFunc,
codeReplacements,
debug = false
) {
return deployContractIf(
web3,
Contract,
async () =>
await codeChanged(web3, Contract, codeAddress, codeReplacements, debug),
deployFunc
);
}
// helper function: encode an address as word
function ap(addr) {
return addr.toLowerCase().slice(2).padStart(64, "0");
}
/**
* @dev Deploy the superfluid framework
* @param {boolean} options.isTruffle Whether the script is used within native truffle framework
* @param {Web3} options.web3 Injected web3 instance
* @param {Address} options.from Address to deploy contracts from
* @param {boolean} options.newTestResolver Force to create a new resolver (overriding env: CREATE_NEW_RESOLVER)
* @param {boolean} options.useMocks Use mock contracts instead (overriding env: USE_MOCKS)
* @param {boolean} options.nonUpgradable Deploy contracts configured to be non-upgradable
* (overriding env: NON_UPGRADABLE)
* @param {boolean} options.appWhiteListing Deploy contracts configured to require app white listing
* (overriding env: ENABLE_APP_WHITELISTING)
* @param {boolean} options.resetSuperfluidFramework Reset the superfluid framework deployment
* (overriding env: RESET_SUPERFLUID_FRAMEWORK)
* @param {string} options.protocolReleaseVersion Specify the protocol release version to be used
* (overriding env: RELEASE_VERSION)
* @param {string} options.outputFile Name of file where to log addresses of newly deployed contracts
* (overriding env: OUTPUT_FILE)
* @param {boolean} options.newSuperfluidLoader Deploy a new superfluid loader contract
* (overriding env: NEW_SUPERFLUID_LOADER)
*
* Usage: npx truffle exec ops-scripts/deploy-framework.js
*/
module.exports = eval(`(${S.toString()})({skipArgv: true})`)(async function (
args,
options = {}
) {
console.log("======== Deploying superfluid framework ========");
let {
newTestResolver,
useMocks,
nonUpgradable,
appWhiteListing,
appCallbackGasLimit,
protocolReleaseVersion,
outputFile,
newSuperfluidLoader,
} = options;
resetSuperfluidFramework = options.resetSuperfluidFramework;
resetSuperfluidFramework =
resetSuperfluidFramework || !!process.env.RESET_SUPERFLUID_FRAMEWORK;
console.log("reset superfluid framework: ", resetSuperfluidFramework);
outputFile = outputFile || process.env.OUTPUT_FILE;
if (outputFile !== undefined) {
console.log("output file: ", outputFile);
}
// string to build a list of newly deployed contracts, written to a file if "outputFile" option set
let output = "";
const networkType = await web3.eth.net.getNetworkType();
const networkId = await web3.eth.net.getId();
const chainId = await web3.eth.getChainId();
const deployerAddr = (await web3.eth.getAccounts())[0];
console.log("network Type: ", networkType);
console.log("network ID: ", networkId);
console.log("chain ID: ", chainId);
console.log("deployer: ", deployerAddr);
const config = getConfig(chainId);
if (config.isTestnet) {
output += "IS_TESTNET=1\n";
}
output += `NETWORK_ID=${networkId}\n`;
const gitRevision = execSync('git rev-parse HEAD').toString().slice(0,16).trim();
const packageVersion = require('../package.json').version;
const versionString = `${packageVersion}-${gitRevision}`;
const deployerInitialBalance = await web3.eth.getBalance(deployerAddr);
const CFAv1_TYPE = web3.utils.sha3(
"org.superfluid-finance.agreements.ConstantFlowAgreement.v1"
);
const IDAv1_TYPE = web3.utils.sha3(
"org.superfluid-finance.agreements.InstantDistributionAgreement.v1"
);
const GDAv1_TYPE = web3.utils.sha3(
"org.superfluid-finance.agreements.GeneralDistributionAgreement.v1"
);
newTestResolver = newTestResolver || !!process.env.CREATE_NEW_RESOLVER;
useMocks = useMocks || !!process.env.USE_MOCKS;
nonUpgradable = nonUpgradable || !!process.env.NON_UPGRADABLE;
appWhiteListing =
appWhiteListing ||
config.gov_enableAppWhiteListing ||
!!process.env.ENABLE_APP_WHITELISTING;
appCallbackGasLimit =
appCallbackGasLimit ||
config.appCallbackGasLimit ||
!!process.env.APP_CALLBACK_GAS_LIMIT;
newSuperfluidLoader = newSuperfluidLoader || !!process.env.NEW_SUPERFLUID_LOADER;
console.log("app whitelisting enabled:", appWhiteListing);
if (newTestResolver) {
console.log("**** !ATTN! CREATING NEW RESOLVER ****");
}
if (useMocks) {
console.log("**** !ATTN! USING MOCKS CONTRACTS ****");
}
if (nonUpgradable) {
console.log("**** !ATTN! DISABLED UPGRADABILITY ****");
}
if (newSuperfluidLoader) {
console.log("**** !ATTN! DEPLOYING NEW SUPERFLUID LOADER ****");
}
await deployERC1820((err) => {
if (err) throw err;
}, options);
const contracts = [
"Ownable",
"CFAv1Forwarder",
"GDAv1Forwarder",
"IMultiSigWallet",
"ISafe",
"SuperfluidGovernanceBase",
"Resolver",
"SuperfluidLoader",
"Superfluid",
"SuperTokenFactory",
"SuperToken",
"TestGovernance",
"ISuperfluidGovernance",
"UUPSProxy",
"UUPSProxiable",
"SlotsBitmapLibrary",
"ConstantFlowAgreementV1",
"InstantDistributionAgreementV1",
"GeneralDistributionAgreementV1",
"SuperfluidUpgradeableBeacon",
"SuperfluidPool",
"SuperfluidPoolPlaceholder",
"SuperfluidPoolDeployerLibrary",
"BeaconProxy",
"PoolAdminNFT",
"PoolMemberNFT",
"IAccessControlEnumerable",
"SimpleForwarder",
"ERC2771Forwarder",
];
const mockContracts = [
"SuperfluidMock",
"SuperTokenFactoryMock",
"SuperTokenMock",
];
const {
Ownable,
IMultiSigWallet,
ISafe,
CFAv1Forwarder,
GDAv1Forwarder,
SuperfluidGovernanceBase,
Resolver,
SuperfluidLoader,
Superfluid,
SuperfluidMock,
SuperTokenFactory,
SuperTokenFactoryMock,
SuperToken,
SuperTokenMock,
TestGovernance,
ISuperfluidGovernance,
UUPSProxy,
UUPSProxiable,
SlotsBitmapLibrary,
ConstantFlowAgreementV1,
InstantDistributionAgreementV1,
GeneralDistributionAgreementV1,
SuperfluidUpgradeableBeacon,
SuperfluidPool,
SuperfluidPoolPlaceholder,
SuperfluidPoolDeployerLibrary,
BeaconProxy,
PoolAdminNFT,
PoolMemberNFT,
IAccessControlEnumerable,
SimpleForwarder,
ERC2771Forwarder,
} = await SuperfluidSDK.loadContracts({
...extractWeb3Options(options),
additionalContracts: contracts.concat(useMocks ? mockContracts : []),
contractLoader: builtTruffleContractLoader,
networkId,
gasConfig: getGasConfig(networkId),
});
if (!newTestResolver && config.resolverAddress) {
resolver = await Resolver.at(config.resolverAddress);
} else {
resolver = await web3tx(Resolver.new, "Resolver.new")();
// make it available for the sdk for testing purpose
process.env.RESOLVER_ADDRESS = resolver.address;
}
console.log("Resolver address", resolver.address);
sfObjForGovAndResolver = {
contracts: {
Resolver,
Ownable,
IMultiSigWallet,
ISafe,
IAccessControlEnumerable,
SuperfluidGovernanceBase
},
resolver: {
address: resolver.address
}
};
const previousVersionString = pseudoAddressToVersionString(
await resolver.get(`versionString.${protocolReleaseVersion}`)
);
console.log(`previous versionString: ${previousVersionString}`);
console.log(`new versionString: ${versionString}`);
// =========== BOOTSTRAPPING (initial deployment) ===========
// deploy superfluid loader
await deployAndRegisterContractIf(
SuperfluidLoader,
"SuperfluidLoader-v1",
async (contractAddress) => newSuperfluidLoader === true || contractAddress === ZERO_ADDRESS,
async () => {
const c = await web3tx(
SuperfluidLoader.new,
"SuperfluidLoader.new"
)(resolver.address);
output += `SUPERFLUID_LOADER=${c.address}\n`;
return c;
}
);
// deploy new TestGovernance contract
// (only on testnets, devnets and initial mainnet deployment)
let testGovernanceInitRequired = false;
let governance;
if (!config.disableTestGovernance && !process.env.NO_NEW_GOVERNANCE) {
const prevGovAddr = await resolver.get.call(`TestGovernance.${protocolReleaseVersion}`);
if (resetSuperfluidFramework || await codeChanged(web3, TestGovernance, prevGovAddr)) {
console.log(`TestGovernance needs new deployment.`);
const c = await web3tx(TestGovernance.new,"TestGovernance.new")();
governance = await TestGovernance.at(c.address);
testGovernanceInitRequired = true;
output += `SUPERFLUID_GOVERNANCE=${c.address}\n`;
} else {
governance = await TestGovernance.at(prevGovAddr);
}
// defer resolver update to after the initialization
// this avoids testnet bricking in case script execution is interrupted
}
// deploy new superfluid host contract
const SuperfluidLogic = useMocks ? SuperfluidMock : Superfluid;
const superfluid = await deployAndRegisterContractIf(
SuperfluidLogic,
`Superfluid.${protocolReleaseVersion}`,
async (contractAddress) => !(await hasCode(web3, contractAddress)),
async () => {
const simpleForwarder = await web3tx(SimpleForwarder.new, "SimpleForwarder.new")();
console.log("SimpleForwarder address:", simpleForwarder.address);
output += `SIMPLE_FORWARDER=${simpleForwarder.address}\n`;
const erc2771Forwarder = await web3tx(ERC2771Forwarder.new, "ERC2771Forwarder.new")();
console.log("ERC2771Forwarder address:", erc2771Forwarder.address);
output += `ERC2771_FORWARDER=${erc2771Forwarder.address}\n`;
let superfluidAddress;
const superfluidLogic = await web3tx(
SuperfluidLogic.new,
"SuperfluidLogic.new"
)(nonUpgradable, appWhiteListing, appCallbackGasLimit, simpleForwarder.address, erc2771Forwarder.address);
console.log(
`Superfluid new code address ${superfluidLogic.address}`
);
output += `SUPERFLUID_HOST_LOGIC=${superfluidLogic.address}\n`;
if (!nonUpgradable) {
const proxy = await web3tx(
UUPSProxy.new,
"Create Superfluid proxy"
)();
output += `SUPERFLUID_HOST_PROXY=${proxy.address}\n`;
await web3tx(
proxy.initializeProxy,
"proxy.initializeProxy"
)(superfluidLogic.address);
superfluidAddress = proxy.address;
} else {
superfluidAddress = superfluidLogic.address;
}
const superfluid = await Superfluid.at(superfluidAddress);
await web3tx(
simpleForwarder.transferOwnership,
"simpleForwarder.transferOwnership"
)(superfluid.address);
await web3tx(
erc2771Forwarder.transferOwnership,
"erc2771Forwarder.transferOwnership"
)(superfluid.address);
await web3tx(
superfluid.initialize,
"Superfluid.initialize"
)(governance.address);
return superfluid;
}
);
// helper objects needed later on
const superfluidConstructorParam = superfluid.address
.toLowerCase()
.slice(2)
.padStart(64, "0");
sfObjForGovAndResolver.host = superfluid;
// load existing governance if needed
if (!governance) {
governance = await ISuperfluidGovernance.at(
await superfluid.getGovernance.call()
);
console.log("Governance address", governance.address);
}
// initialize the new TestGovernance
if (testGovernanceInitRequired) {
const accounts = await web3.eth.getAccounts();
const trustedForwarders = [];
if (config.trustedForwarders) {
trustedForwarders.push(...config.trustedForwarders);
}
if (config.cfaFwd) {
trustedForwarders.push(config.cfaFwd);
}
if (config.gdaFwd) {
trustedForwarders.push(config.gdaFwd);
}
if (config.macroFwd) {
trustedForwarders.push(config.macroFwd);
}
console.log(`initializing TestGovernance with config: ${JSON.stringify({
liquidationPeriod: config.liquidationPeriod,
patricianPeriod: config.patricityPeriod,
trustedForwarders
}, null, 2)}`);
await web3tx(governance.initialize, "governance.initialize")(
superfluid.address,
// let rewardAddress the first account
accounts[0],
// liquidationPeriod
config.liquidationPeriod,
// patricianPeriod
config.patricianPeriod,
// trustedForwarders
trustedForwarders
);
// update the resolver
await setResolver(
sfObjForGovAndResolver,
`TestGovernance.${protocolReleaseVersion}`,
governance.address
);
}
// replace with new governance
if ((await superfluid.getGovernance.call()) !== governance.address) {
const currentGovernance = await ISuperfluidGovernance.at(
await superfluid.getGovernance.call()
);
await web3tx(
currentGovernance.replaceGovernance,
"governance.replaceGovernance"
)(superfluid.address, governance.address);
}
// list CFA v1
const deployCFAv1 = async () => {
const agreement = await web3tx(
ConstantFlowAgreementV1.new,
"ConstantFlowAgreementV1.new"
)(superfluid.address);
console.log("New ConstantFlowAgreementV1 address", agreement.address);
output += `CFA_LOGIC=${agreement.address}\n`;
return agreement;
};
if (!(await superfluid.isAgreementTypeListed.call(CFAv1_TYPE))) {
const cfa = await deployCFAv1();
await web3tx(
governance.registerAgreementClass,
"Governance registers CFA"
)(superfluid.address, cfa.address);
}
/**
* This function:
* deploys an external library
* links a contract artifact to the deployed external library (in two ways depending on if hardhat or truffle env)
* returns the deployed external library
* @param {*} externalLibraryArtifact artifact of the external library
* @param {*} externalLibraryName name of the external library
* @param {*} outputName the output name
* @param {*} contract the contract artifact to link to the external library
* @returns
*/
const deployExternalLibraryAndLink = async (
externalLibraryArtifact,
externalLibraryName,
outputName,
contract,
allowFailure = false
) => {
try {
const externalLibrary = await web3tx(
externalLibraryArtifact.new,
`${externalLibraryName}.new`
)();
output += `${outputName}=${externalLibrary.address}\n`;
if (process.env.IS_HARDHAT) {
contract.link(externalLibrary);
} else {
contract.link(externalLibraryName, externalLibrary.address);
}
console.log(externalLibraryName, "address", externalLibrary.address);
return externalLibrary;
} catch (err) {
console.warn("Error: ", err);
if (!allowFailure) {
throw err;
}
}
};
let slotsBitmapLibraryAddress = ZERO_ADDRESS;
// list IDA v1
const deployIDAv1 = async () => {
// small inefficiency: this may be re-deployed even if not changed
// deploySlotsBitmapLibrary
const slotsBitmapLibrary = await deployExternalLibraryAndLink(
SlotsBitmapLibrary,
"SlotsBitmapLibrary",
"SLOTS_BITMAP_LIBRARY",
InstantDistributionAgreementV1
);
slotsBitmapLibraryAddress = slotsBitmapLibrary.address;
const agreement = await web3tx(
InstantDistributionAgreementV1.new,
"InstantDistributionAgreementV1.new"
)(superfluid.address);
console.log(
"New InstantDistributionAgreementV1 address",
agreement.address
);
output += `IDA_LOGIC=${agreement.address}\n`;
return agreement;
};
if (!(await superfluid.isAgreementTypeListed.call(IDAv1_TYPE))) {
const ida = await deployIDAv1();
await web3tx(
governance.registerAgreementClass,
"Governance registers IDA"
)(superfluid.address, ida.address);
} else {
// NOTE that we are reusing the existing deployed external library
// here as an optimization, this assumes that we do not change the
// library code.
// link library in order to avoid spurious code change detections
try {
const IDAv1 = await InstantDistributionAgreementV1.at(
await superfluid.getAgreementClass.call(IDAv1_TYPE)
);
slotsBitmapLibraryAddress =
await IDAv1.SLOTS_BITMAP_LIBRARY_ADDRESS.call();
if (process.env.IS_HARDHAT) {
if (slotsBitmapLibraryAddress !== ZERO_ADDRESS) {
const lib = await SlotsBitmapLibrary.at(
slotsBitmapLibraryAddress
);
InstantDistributionAgreementV1.link(lib);
}
} else {
InstantDistributionAgreementV1.link(
"SlotsBitmapLibrary",
slotsBitmapLibraryAddress
);
}
} catch (e) {
console.warn("Cannot get slotsBitmapLibrary address", e.toString());
}
}
let gdaIsLinked = false;
const deployGDAv1 = async (superfluidPoolBeaconAddr) => {
if (!gdaIsLinked) {
await deployExternalLibraryAndLink(
SuperfluidPoolDeployerLibrary,
"SuperfluidPoolDeployerLibrary",
"SUPERFLUID_POOL_DEPLOYER_LIBRARY",
GeneralDistributionAgreementV1,
protocolReleaseVersion === "test" ? true : false
);
// deploy a dummy BeaconProxy for verification
const beaconProxy = await web3tx(
BeaconProxy.new,
"BeaconProxy.new"
)(superfluidPoolBeaconAddr, "0x");
console.log("Dummy BeaconProxy address", beaconProxy.address);
output += `DUMMY_BEACON_PROXY=${beaconProxy.address}\n`;
if (process.env.IS_HARDHAT) {
// this fails in test case deployment.test.js:ops-scripts/deploy-super-token.js
// where deploy-framework is invoked twice, the second time failing because
// hardhat claims the library is already linked. Thus we try/catch here.
try {
if (slotsBitmapLibraryAddress !== ZERO_ADDRESS) {
const lib = await SlotsBitmapLibrary.at(
slotsBitmapLibraryAddress
);
GeneralDistributionAgreementV1.link(lib);
}
} catch (e) {
console.warn("!!! Cannot link slotsBitmapLibrary", e.toString());
if (protocolReleaseVersion !== "test") {
throw e;
}
}
} else {
GeneralDistributionAgreementV1.link(
"SlotsBitmapLibrary",
slotsBitmapLibraryAddress
);
}
gdaIsLinked = true;
}
const agreement = await web3tx(
GeneralDistributionAgreementV1.new,
"GeneralDistributionAgreementV1.new"
)(superfluid.address, superfluidPoolBeaconAddr);
console.log(
"New GeneralDistributionAgreementV1 address",
agreement.address
);
output += `GDA_LOGIC=${agreement.address}\n`;
return agreement;
};
// initial GDA deployment (GDA bootstrapping)
if (!(await superfluid.isAgreementTypeListed.call(GDAv1_TYPE))) {
// first we deploy a SuperfluidPoolBeacon
// ... with the placeholder logic (just enough to allow later update)
const superfluidPoolPlaceholderLogic = await web3tx(
SuperfluidPoolPlaceholder.new,
"SuperfluidPoolPlaceholder.new"
)();
const superfluidPoolBeaconContract = await web3tx(
SuperfluidUpgradeableBeacon.new,
"SuperfluidUpgradeableBeacon.new"
)(superfluidPoolPlaceholderLogic.address);
console.log(
"New SuperfluidPoolBeacon address",
superfluidPoolBeaconContract.address
);
output += `SUPERFLUID_POOL_BEACON=${superfluidPoolBeaconContract.address}\n`;
const gda = await deployGDAv1(superfluidPoolBeaconContract.address);
/*
// now that we have a GDA, we can deploy the actual SuperfluidPool...
// "narrator: no, we cannot, needs the proxy address"
const superfluidPoolLogic = await web3tx(
SuperfluidPool.new,
"SuperfluidPool.new"
)(gda.address);
await superfluidPoolLogic.castrate();
console.log("New SuperfluidPoolLogic address", superfluidPoolLogic.address);
output += `SUPERFLUID_POOL_LOGIC=${superfluidPoolLogic.address}\n`;
// ...update the beacon to it...
console.log("Upgrading beacon to the actual SuperfluidPool logic...");
await superfluidPoolBeaconContract.upgradeTo(superfluidPoolLogic.address);
*/
// ...and transfer ownership of the beacon
console.log("Transferring ownership of beacon contract to Superfluid Host...");
await superfluidPoolBeaconContract.transferOwnership(superfluid.address);
// finally, register the GDA with a gov action.
// its pending state changes don't affect the remaining actions
await sendGovernanceAction(
sfObjForGovAndResolver,
(gov) => gov.registerAgreementClass(superfluid.address, gda.address)
);
// assumption: testnets don't require async gov action execution, so can continue
// while for mainnets with async gov action, we need to exit here.
if (!config.isTestnet) {
console.log("info for verification:");
console.log(output);
console.log("##### STEP1 of GDA DEPLOYMENT DONE #####");
console.log("Now go execute the gov action, then run this script again");
process.exit();
}
} else {
// NOTE that we are reusing the existing deployed external library
// here as an optimization, this assumes that we do not change the
// library code.
// link library in order to avoid spurious code change detections
try {
const GDAv1 = await GeneralDistributionAgreementV1.at(
await superfluid.getAgreementClass.call(GDAv1_TYPE)
);
console.log("GDAv1 proxy address", GDAv1.address);
slotsBitmapLibraryAddress =
await GDAv1.SLOTS_BITMAP_LIBRARY_ADDRESS.call();
let superfluidPoolDeployerLibraryAddress =
await GDAv1.SUPERFLUID_POOL_DEPLOYER_ADDRESS.call();
if (process.env.IS_HARDHAT) {
if (slotsBitmapLibraryAddress !== ZERO_ADDRESS) {
const lib = await SlotsBitmapLibrary.at(
slotsBitmapLibraryAddress
);
GeneralDistributionAgreementV1.link(lib);
}
if (superfluidPoolDeployerLibraryAddress !== ZERO_ADDRESS) {
const lib = await SuperfluidPoolDeployerLibrary.at(
superfluidPoolDeployerLibraryAddress
);
GeneralDistributionAgreementV1.link(lib);
}
} else {
GeneralDistributionAgreementV1.link(
"SlotsBitmapLibrary",
slotsBitmapLibraryAddress
);
GeneralDistributionAgreementV1.link(
"SuperfluidPoolDeployerLibrary",
superfluidPoolDeployerLibraryAddress
);
}
} catch (e) {
console.warn("Cannot get slotsBitmapLibrary address", e.toString());
}
}
if (protocolReleaseVersion === "test") {
// deploy CFAv1Forwarder for test deployments
// for other (permanent) deployments, it's not handled by this script
await deployAndRegisterContractIf(
CFAv1Forwarder,
"CFAv1Forwarder",
async (contractAddress) => contractAddress === ZERO_ADDRESS,
async () => {
const forwarder = await CFAv1Forwarder.new(superfluid.address);
output += `CFA_V1_FORWARDER=${forwarder.address}\n`;
await web3tx(
governance.enableTrustedForwarder,
`Governance set CFAv1Forwarder`
)(superfluid.address, ZERO_ADDRESS, forwarder.address);
return forwarder;
}
);
// deploy GDAv1Forwarder for test deployments
// for other (permanent) deployments, it's not handled by this script
await deployAndRegisterContractIf(
GDAv1Forwarder,
"GDAv1Forwarder",
async (contractAddress) => contractAddress === ZERO_ADDRESS,
async () => {
const forwarder = await GDAv1Forwarder.new(superfluid.address);
output += `GDA_V1_FORWARDER=${forwarder.address}\n`;
await web3tx(
governance.enableTrustedForwarder,
`Governance set GDAv1Forwarder`
)(superfluid.address, ZERO_ADDRESS, forwarder.address);
return forwarder;
}
);
}
// =========== UPGRADE changed contracts ===========
console.log("===== STARTING UPGRADE PHASE ======");
let superfluidNewLogicAddress = ZERO_ADDRESS;
const agreementsToUpdate = [];
if (!nonUpgradable) {
if (await superfluid.NON_UPGRADABLE_DEPLOYMENT.call()) {
throw new Error("Superfluid is not upgradable");
}
async function getOrDeployForwarder(
superfluid,
ForwarderContract,
getPrevAddrFn,
outputKey
) {
let prevAddr = await getPrevAddrFn().catch(_err => {
console.error(`### Error getting ${ForwarderContract.contractName} address, likely not yet deployed`);
return ZERO_ADDRESS;
});
if (prevAddr !== ZERO_ADDRESS) {
// TEMPORARY FIX - can be removed after applied
// we found a previous deployment. Now verify it has the host as owner.
// the first mainnet deployment didn't have this for SimpleForwarder, thus needs a redeployment.
const ownerAddr = await (await Ownable.at(prevAddr)).owner();
if (ownerAddr != superfluid.address) {
console.log(` !!! ${outputKey} has wrong owner, needs re-deployment`);
prevAddr = ZERO_ADDRESS; // by setting zero, we force a re-deployment
}
}
const newAddress = await deployContractIfCodeChanged(
web3,
ForwarderContract,
prevAddr,
async () => {
const forwarder = await web3tx(ForwarderContract.new, `${ForwarderContract.contractName}.new`)();
await web3tx(
forwarder.transferOwnership,
"forwarder.transferOwnership"
)(superfluid.address);
output += `${outputKey}=${forwarder.address}\n`;
return forwarder.address;
}
);
return newAddress !== ZERO_ADDRESS ? newAddress : prevAddr;
}
const simpleForwarderAddress = await getOrDeployForwarder(
superfluid,
SimpleForwarder,
() => superfluid.SIMPLE_FORWARDER(),
"SIMPLE_FORWARDER"
);
const erc2771ForwarderAddress = await getOrDeployForwarder(
superfluid,
ERC2771Forwarder,
() => superfluid.getERC2771Forwarder(),
"ERC2771_FORWARDER"
);
// get previous callback gas limit, make sure we don't decrease it
const prevCallbackGasLimit = await superfluid.CALLBACK_GAS_LIMIT();
if (prevCallbackGasLimit.toNumber() > appCallbackGasLimit) {
throw new Error("Cannot decrease app callback gas limit");
} else if (prevCallbackGasLimit.toNumber() !== appCallbackGasLimit) {
console.log(` !!! CHANGING APP CALLBACK GAS LIMIT FROM ${prevCallbackGasLimit} to ${appCallbackGasLimit} !!!`);
}
// deploy new superfluid host logic
superfluidNewLogicAddress = await deployContractIfCodeChanged(
web3,
SuperfluidLogic,
await superfluid.getCodeAddress(),
async () => {
if (!(await isProxiable(UUPSProxiable, superfluid.address))) {
throw new Error("Superfluid is non-upgradable");
}
const superfluidLogic = await web3tx(
SuperfluidLogic.new,
"SuperfluidLogic.new"
)(nonUpgradable, appWhiteListing, appCallbackGasLimit, simpleForwarderAddress, erc2771ForwarderAddress);
output += `SUPERFLUID_HOST_LOGIC=${superfluidLogic.address}\n`;
return superfluidLogic.address;
},
[
ap(erc2771ForwarderAddress),
ap(simpleForwarderAddress),
appCallbackGasLimit.toString(16).padStart(64, "0")
],
);
// deploy new CFA logic
const cfaNewLogicAddress = await deployContractIfCodeChanged(
web3,
ConstantFlowAgreementV1,
await (
await UUPSProxiable.at(
await superfluid.getAgreementClass.call(CFAv1_TYPE)
)
).getCodeAddress(),
async () => (await deployCFAv1()).address,
[ superfluidConstructorParam ]
);
if (cfaNewLogicAddress !== ZERO_ADDRESS) {
agreementsToUpdate.push(cfaNewLogicAddress);
}
// deploy new IDA logic
const idaNewLogicAddress = await deployContractIfCodeChanged(
web3,
InstantDistributionAgreementV1,
await (
await UUPSProxiable.at(
await superfluid.getAgreementClass.call(IDAv1_TYPE)
)
).getCodeAddress(),
async () => (await deployIDAv1()).address,
[ superfluidConstructorParam ]
);
if (idaNewLogicAddress !== ZERO_ADDRESS) {
agreementsToUpdate.push(idaNewLogicAddress);
}
// deploy new GDA logic
const gdaProxyAddr = await superfluid.getAgreementClass.call(GDAv1_TYPE);
const gdaLogicAddr = await (await UUPSProxiable.at(gdaProxyAddr)).getCodeAddress();
const superfluidPoolBeaconAddr = await (
await GeneralDistributionAgreementV1.at(gdaProxyAddr)
).superfluidPoolBeacon.call();
const gdaNewLogicAddress = await deployContractIfCodeChanged(
web3,
GeneralDistributionAgreementV1,
gdaLogicAddr,
async () => (await deployGDAv1(superfluidPoolBeaconAddr)).address,
[
superfluidConstructorParam,
ap(superfluidPoolBeaconAddr)
]
);
if (gdaNewLogicAddress !== ZERO_ADDRESS) {
agreementsToUpdate.push(gdaNewLogicAddress);
}
}
// deploy new super token factory logic (depends on SuperToken logic, which links to nft deployer library)
const SuperTokenFactoryLogic = useMocks
? SuperTokenFactoryMock
: SuperTokenFactory;
const SuperTokenLogic = useMocks ? SuperTokenMock : SuperToken;
const factoryAddress = await superfluid.getSuperTokenFactory.call();
let poolAdminNFTLogicChanged = false;
let poolMemberNFTLogicChanged = false;
const deployNFTContract = async (artifact, nftType, nftTypeCaps, args) => {
const nftLogic = await web3tx(artifact.new, `${nftType}.new`)(...args);
console.log(`${nftType} Logic address`, nftLogic.address);
output += `${nftTypeCaps}=${nftLogic.address}\n`;
await nftLogic.castrate();
return nftLogic;
};
const superTokenFactoryNewLogicAddress = await deployContractIf(
web3,
SuperTokenFactoryLogic,
async () => {
console.log(
"checking if SuperTokenFactory needs to be redeployed..."
);
// check if super token factory or super token logic changed
if (factoryAddress === ZERO_ADDRESS) return true;
const factory = await SuperTokenFactoryLogic.at(factoryAddress);
const superTokenLogicAddress = await factory.getSuperTokenLogic.call();
const superTokenLogic = await SuperTokenLogic.at(superTokenLogicAddress);
const gdaPAddr = await superfluid.getAgreementClass.call(GDAv1_TYPE);
const cofNFTPAddr = await superTokenLogic.CONSTANT_OUTFLOW_NFT();
const cifNFTPAddr = await superTokenLogic.CONSTANT_INFLOW_NFT();
let cofNFTLAddr = ZERO_ADDRESS;
let cifNFTLAddr = ZERO_ADDRESS;
if (cofNFTPAddr !== ZERO_ADDRESS) {
const cofNFTContract = await UUPSProxiable.at(cofNFTPAddr);
cofNFTLAddr = await cofNFTContract.getCodeAddress();
}
if (cifNFTPAddr !== ZERO_ADDRESS) {
const cifNFTContract = await UUPSProxiable.at(cifNFTPAddr);
cifNFTLAddr = await cifNFTContract.getCodeAddress();
}
// TODO: remove from try block once all networks have a PoolNFT aware supertoken logic deployed
try {
const poolAdminNFTPAddr = await superTokenLogic.POOL_ADMIN_NFT();
const poolMemberNFTPAddr = await superTokenLogic.POOL_MEMBER_NFT();
const poolAdminNFTContract = await PoolAdminNFT.at(poolAdminNFTPAddr);
const poolMemberNFTContract = await PoolMemberNFT.at(poolMemberNFTPAddr);
const poolAdminNFTLAddr = await poolAdminNFTContract.getCodeAddress();
const poolMemberNFTLAddr = await poolMemberNFTContract.getCodeAddress();
// TODO: check only if non-zero address
// don't do in try block, otherwise we may accidentally re-deploy the NFT proxies