-
Notifications
You must be signed in to change notification settings - Fork 4
/
create_safe.ts
95 lines (73 loc) · 3.13 KB
/
create_safe.ts
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
import type {NextApiRequest, NextApiResponse} from 'next';
import {ethers} from 'ethers'
import Safe, {AddOwnerTxParams, EthersAdapter} from '@safe-global/protocol-kit'
import {SafeFactory} from '@safe-global/protocol-kit'
import {SafeAccountConfig} from '@safe-global/protocol-kit'
import {update} from 'immutable';
//process.env.ALCHEMY_MUMBAI_URL
let acc0 = process.env.ETHEREUM_PRIVATE_KEY as string;
// goerli deploys
let gameGuard = '0x2D00A3F404AEaea97E929766d595A90D8bE0554d';
let moduleAddress = '0xc074Dca6083ccD17872394c8A58Cb79Ac660Ac3d';
const provider = new ethers.providers.JsonRpcProvider(process.env.ALCHEMY_GOERLI_URL)
const dev = new ethers.Wallet(acc0, provider)
async function updateOwners(safeInstance: any, newOwner: string) {
let params: AddOwnerTxParams = {
ownerAddress: newOwner
}
let ret = [];
let safeTX = await safeInstance.createAddOwnerTx(params)
let txResponse = await safeInstance.executeTransaction(safeTX)
await txResponse.transactionResponse?.wait()
console.log('Adding new owner: ', txResponse);
params = {
ownerAddress: await dev.getAddress()
}
safeTX = await safeInstance.createRemoveOwnerTx(params);
txResponse = await safeInstance.executeTransaction(safeTX);
await txResponse.transactionResponse?.wait();
console.log('Removing dev: ', txResponse);
}
export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
try {
const {ethAddress} = req.body; // body only contains ETH address
// if (!ethers.utils.isAddress(ethAddress)) return;
// get signers
const owner1Signer = new ethers.Wallet(acc0, provider);
const ethAdapterOwner1 = new EthersAdapter({
ethers,
signerOrProvider: owner1Signer
});
// create safe config
const safeFactory = await SafeFactory.create({ethAdapter: ethAdapterOwner1});
const safeAccountConfig: SafeAccountConfig = {
owners: [
await owner1Signer.getAddress() // set original owner to be server
],
threshold: 1
};
// create safe for user
const safeInstance = await safeFactory.deploySafe({safeAccountConfig});
const safeAddress = safeInstance.getAddress();
// enable superfluid module
let safeTX = await safeInstance.createEnableModuleTx(moduleAddress)
let txResponse = await safeInstance.executeTransaction(safeTX)
await txResponse.transactionResponse?.wait()
await updateOwners(safeInstance, ethAddress);
// enable guard - final step
// safeTX = await safeInstance.createEnableGuardTx(gameGuard)
// txResponse = await safeInstance.executeTransaction(safeTX)
// await txResponse.transactionResponse?.wait()
const modules = await safeInstance.getModules();
const guardAddress = await safeInstance.getGuard();
console.log(safeAddress, modules, guardAddress);
res.send({success: true});
} catch (error) {
console.error(error)
// @ts-ignore
res.status(500).send({success: false, error: error.message})
}
}