Skip to content

Commit

Permalink
updated core service
Browse files Browse the repository at this point in the history
  • Loading branch information
leohhhn committed Apr 15, 2023
1 parent cdac15c commit 2946e86
Show file tree
Hide file tree
Showing 15 changed files with 4,021 additions and 50 deletions.
3 changes: 3 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ETHEREUM_PRIVATE_KEY=bd75a7b8a79f94102f4fcb9cc282660ec644798620980cd830a82454262bceca
ALCHEMY_MUMBAI_URL=https://polygon-mumbai.g.alchemy.com/v2/gab24G6d4XSf3EA2dMLTEsEZqfQ4gJPB
ALCHEMY_GOERLI_URL=https://eth-goerli.g.alchemy.com/v2/jQRXBq2vttfq5xNT1SCgli1Q9Kl_B4Bi
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ yarn-error.log*

# local env files
.env*.local
.env

# vercel
.vercel
Expand Down
5 changes: 5 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions .idea/superplay.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Open [http://localhost:3000](http://localhost:3000) with your browser to see the

You can start editing the page by modifying `pages/index.tsx`. The page auto-updates as you edit the file.

[API routes](https://nextjs.org/docs/api-routes/introduction) can be accessed on [http://localhost:3000/api/hello](http://localhost:3000/api/hello). This endpoint can be edited in `pages/api/hello.ts`.
[API routes](https://nextjs.org/docs/api-routes/introduction) can be accessed on [http://localhost:3000/api/hello](http://localhost:3000/api/hello). This endpoint can be edited in `pages/api/create_safe.ts`.

The `pages/api` directory is mapped to `/api/*`. Files in this directory are treated as [API routes](https://nextjs.org/docs/api-routes/introduction) instead of React pages.

Expand Down
4 changes: 4 additions & 0 deletions hardhat.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/** @type import('hardhat/config').HardhatUserConfig */
module.exports = {
solidity: "0.8.18",
};
8 changes: 8 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,22 @@
"lint": "next lint"
},
"dependencies": {
"@safe-global/api-kit": "^1.0.1",
"@safe-global/protocol-kit": "^0.1.1",
"@safe-global/safe-core-sdk-types": "^1.10.1",
"@types/node": "18.15.11",
"@types/react": "18.0.35",
"@types/react-dom": "18.0.11",
"dotenv": "^16.0.3",
"eslint": "8.38.0",
"eslint-config-next": "13.3.0",
"ethers": "5.7.2",
"next": "13.3.0",
"react": "18.2.0",
"react-dom": "18.2.0",
"typescript": "5.0.4"
},
"devDependencies": {
"hardhat": "^2.14.0"
}
}
95 changes: 95 additions & 0 deletions pages/api/create_safe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,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})
}

}
15 changes: 15 additions & 0 deletions pages/api/gameAction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import {NextApiRequest, NextApiResponse} from 'next';

export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
try {
// implement any game action
res.send({success: true});
} catch (error) {
console.error(error)
// @ts-ignore
res.status(500).send({success: false, error: error.message})
}
}
13 changes: 0 additions & 13 deletions pages/api/hello.ts

This file was deleted.

15 changes: 15 additions & 0 deletions pages/api/start_subscription.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import {NextApiRequest, NextApiResponse} from 'next';

export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
try {
// start superfluid subscription
res.send({success: true});
} catch (error) {
console.error(error)
// @ts-ignore
res.status(500).send({success: false, error: error.message})
}
}

0 comments on commit 2946e86

Please sign in to comment.