Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix rinkeby which is not used anymore #24

Merged
merged 2 commits into from
Oct 13, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Comment on lines +9 to +15
Copy link
Owner Author

@fgiannotti fgiannotti Oct 10, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Esto arregla un problema mío que me saltaba con un bug que tiene node-fetch. (node-fetch/node-fetch#784)
Algo de CONNECTION ERROR no se que, y por detrás debugeando muuucho encontre otro error escondido que es exactamente el que está en el issue) googleando un toque varios fix este me funcionó.

A todo esto Node-fetch lo solucionó ya. Pero no tenemos esa versión bajada (Y no podemos subirla porque es una dependencia que usa web3, no nosotros directamente)

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
51 changes: 51 additions & 0 deletions testServer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Require express and create an instance of it
const axios = require("axios");
const express = require("express");
const web3 = require("web3");
const fs = require("fs");
const path = require("path");
const app = express();

// on the request to root (localhost:3000/)
app.get("/testInfura", async function(req, res) {
let bodyData = `{
"jsonrpc": "2.0",
"id": 1,
"method": "eth_call",
"params": [
{
"data": "0x06fdde03",
"to": "0x46174d7A2Db240F9aF3Dc84d08E634F3AE586919"
},
"latest"
]
}`;
let result = await axios.post(`https://rinkeby.infura.io/v3/5b55ff0d83f7488bb0146a63fb49389c`, bodyData);
console.log(result);
res.send("<b>My</b> check console log for result");
});

// On localhost:3000/welcome
app.get("/testWeb3", async function(req, res) {
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"
});

const result = await contract.methods.name().call();
console.log(result);
res.send("<b>Hello</b> welcome to my http server made with express");
});

// Change the 404 message modifing the middleware
app.use(function(req, res, next) {
res.status(404).send("Sorry, that route doesn't exist. Have a nice day :)");
});

// start the server in the port 3000 !
app.listen(3001, function() {
console.log("Example app listening on port 3001.");
});
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