Skip to content

Commit

Permalink
Merge pull request #24 from fgiannotti/sepolia-migration
Browse files Browse the repository at this point in the history
fix rinkeby which is not used anymore
  • Loading branch information
fgiannotti authored Oct 13, 2022
2 parents d4df179 + 5ee168d commit c6ccda6
Show file tree
Hide file tree
Showing 8 changed files with 224 additions and 176 deletions.
7 changes: 4 additions & 3 deletions .env
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
ETHEREUM_NETWORK= "rinkeby"
ETHEREUM_NETWORK= "sepolia"
INFURA_PROJECT_ID='5b55ff0d83f7488bb0146a63fb49389c'
SIGNER_PRIVATE_KEY='075d7368c237fee4ac244d91a9df93d520163e5f9e1955a52d49b44b78f93efb'
FORESTOKEN_CONTRACT_ADDRESS='0x512fe9e782cEfEdBDd326984BB68d0626223F778'
FORESTOKEN_OWNER_ADDRESS='0xb08fc792b476f2c493F407863A4d3B7DeAC9332D'
FORESTOKEN_PRIVATE_KEY='075d7368c237fee4ac244d91a9df93d520163e5f9e1955a52d49b44b78f93efb'
FORESTOKEN_CONTRACT_ADDRESS='0x46174d7A2Db240F9aF3Dc84d08E634F3AE586919'
FORESTOKEN_INITIAL_SUPPLY=1000000000
RECEIVER_PRIVATE_KEY='e0fced5ce81698fbf9a980d3f686635d4552627f3bba773b526cbf250a991818'
CLIENT_ID='618062513819-7408serup3f3glq7v23tpomldligsed7.apps.googleusercontent.com'
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
"sqlite3": "^5.0.11",
"theme-ui": "^0.14.7",
"typeorm": "^0.3.7",
"web3": "^1.7.5"
"web3": "^1.8.0"
},
"devDependencies": {
"@nestjs/cli": "^8.0.0",
Expand Down
2 changes: 1 addition & 1 deletion src/server/contracts/call.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ async function main() {
console.log(`Connected to ${network}`);
// Creating a signing account from a private key
const signer = web3.eth.accounts.privateKeyToAccount(
process.env.SIGNER_PRIVATE_KEY
process.env.FORESTOKEN_PRIVATE_KEY
);
web3.eth.accounts.wallet.add(signer);

Expand Down
2 changes: 1 addition & 1 deletion src/server/contracts/deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ async function main() {
);
// Creating a signing account from a private key
const signer = web3.eth.accounts.privateKeyToAccount(
process.env.SIGNER_PRIVATE_KEY
process.env.FORESTOKEN_PRIVATE_KEY
);
web3.eth.accounts.wallet.add(signer);

Expand Down
7 changes: 6 additions & 1 deletion src/server/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@ import {
ValidationError,
ValidationPipe,
} from '@nestjs/common';
declare const module: any;
import fetch from 'node-fetch';
import { abortableFetch } from 'abortcontroller-polyfill/dist/cjs-ponyfill';
// To fix: https://github.com/node-fetch/node-fetch/issues/784
// useful: https://www.npmjs.com/package/node-abort-controller, but I used this: https://lightrun.com/answers/apollographql-apollo-client-expected-signal-to-be-an-instanceof-abortsignal
global.fetch = abortableFetch(fetch).fetch;

declare const module: any;
async function bootstrap() {
const app = await NestFactory.create(AppModule.initialize(), {
logger: ['error', 'warn', 'log', 'debug', 'verbose'],
Expand Down
42 changes: 36 additions & 6 deletions src/server/services/tokens.service.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Logger, Injectable } from '@nestjs/common';
import Web3 from 'web3';
import fs from 'fs';
import path from 'path';
import { AbiItem } from 'web3-utils';
import { Contract } from 'web3-eth-contract';
import * as fs from 'fs';
import path from 'path';

@Injectable()
export class TokensService {
Expand All @@ -15,15 +15,22 @@ export class TokensService {
),
).abi;

private readonly contractJson = JSON.parse(
fs.readFileSync(
path.resolve('src/server/contracts/build/Forestoken.json'),
'utf8',
),
);
private readonly web3Client: Web3 = new Web3(
new Web3.providers.HttpProvider(
`https://${process.env.ETHEREUM_NETWORK}.infura.io/v3/${process.env.INFURA_PROJECT_ID}`,
),
);

private contract: Contract = new this.web3Client.eth.Contract(
this.abi,
this.contractJson.abi,
process.env.FORESTOKEN_CONTRACT_ADDRESS,
{ from: process.env.FORESTOKEN_OWNER_ADDRESS, gasPrice: '2000' },
);

public async mintWithPowr(
Expand Down Expand Up @@ -56,14 +63,37 @@ export class TokensService {
}

public async name(): Promise<string> {
return this.contract.methods.name().call();
const contractJson = JSON.parse(
fs.readFileSync(
path.resolve('src/server/contracts/build/Forestoken.json'),
'utf8',
),
);

const web3Client = new Web3(
new Web3.providers.HttpProvider(
`https://sepolia.infura.io/v3/5b55ff0d83f7488bb0146a63fb49389c`,
),
);

const contract = new web3Client.eth.Contract(
contractJson.abi,
'0x46174d7A2Db240F9aF3Dc84d08E634F3AE586919',
{
from: '0xb08fc792b476f2c493F407863A4d3B7DeAC9332D',
gasPrice: '2000',
},
);

const result = await contract.methods.name().call();
return result;
}

public async balanceOf(id: string): Promise<string> {
//Obtiene el PK desde el user
//const { private_key } = await this.usersService.findOne(id);
const address = this.web3Client.eth.accounts.privateKeyToAccount(
process.env.SIGNER_PRIVATE_KEY,
process.env.FORESTOKEN_PRIVATE_KEY,
).address;
return this.contract.methods.balanceOf(address).call();
}
Expand All @@ -79,7 +109,7 @@ export class TokensService {

// Creating a signing account from a private key
const signer = this.web3Client.eth.accounts.privateKeyToAccount(
process.env.SIGNER_PRIVATE_KEY,
process.env.FORESTOKEN_PRIVATE_KEY,
);
this.web3Client.eth.accounts.wallet.add(signer);

Expand Down
2 changes: 1 addition & 1 deletion tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"extends": "./tsconfig.json",
"exclude": ["node_modules",
"src/test", "dist", "**/*spec.ts"]
"src/test", "dist", "**/*spec.ts"],
}
Loading

0 comments on commit c6ccda6

Please sign in to comment.