Skip to content

Commit

Permalink
Week16 update
Browse files Browse the repository at this point in the history
  • Loading branch information
SkyYap committed May 18, 2023
1 parent 3ae631e commit 5e17d0f
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ function signMessage(signer, message) {

}

function ApiInfo() {
function Profile() {
const [data, setData] = useState(null);
const [isLoading, setLoading] = useState(false);

Expand All @@ -97,4 +97,37 @@ function ApiInfo() {
<p>{data.email}</p>
</div>
);
}

function RequestTokens() {
const { data: signer } = useSigner();
const [txData, setTxData] = useState(null);
const [isLoading, setLoading] = useState(false);

if (txData) return (
<div>
<p>Transaction completed</p>
<a></a>
</div>
)

if (isLoading) return (
<div>
<button onClick={() => requestTokens(signer, "signature", setLoading, setTxData)}>Request Tokens</button>
</div>
)
}

function requestTokens(signer, signature, setLoading, setTxData) {
const requestOptions = {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ address: signer._address, signature: signature })
};
fetch('http://localhost:3001/request-tokens', requestOptions)
.then(response => response.json())
.then((data) => {
setTxData(data);
setLoading(true);
});
}
9 changes: 7 additions & 2 deletions Week14/project/src/app.controller.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Controller, Get, Param, Query } from '@nestjs/common';
import { Controller, Get, Param, Query, Post, Body } from '@nestjs/common';
import { AppService } from './app.service';
import { ethers } from 'ethers';
import { RequestTokensDto } from './dtos/requestTokens.dto';

@Controller()
export class AppController {
Expand Down Expand Up @@ -35,5 +36,9 @@ export class AppController {
async getTransactionReceipt(@Query('hash') hash: string) {
return await this.appService.getTransactionReceipt(hash);
}


@Post('request-tokens')
requestTokens(@Body() body: RequestTokensDto) {
return this.appService.requetTokens(body.address, body.signature);
}
}
11 changes: 10 additions & 1 deletion Week14/project/src/app.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export class AppService {
contract: ethers.Contract;

constructor(private configService: ConfigService) {
const apiKey = this.configService.get<string>('INFURA_API_KEY')
this.provider = new ethers.providers.InfuraProvider('sepolia', apiKey)
this.provider = ethers.getDefaultProvider('sepolia')
this.contract = new ethers.Contract(this.getAddress(), tokenJson.abi, this.provider)
}
Expand All @@ -21,7 +23,7 @@ export class AppService {
}

getAddress() {
const tokenAddress = this.configService.get<string>('TokenAddress');
const tokenAddress = this.configService.get<string>('TOKEN_ADDRESS');
return '0xaaa84599036017e4f44b506510f4847d5b46471d';
}

Expand All @@ -42,4 +44,11 @@ export class AppService {
async getReceipt(tx: ethers.providers.TransactionResponse) {
return await tx.wait();
}

async requetTokens(address: string, signature: string) {
const pKey = this.configService.get<string>('PRIVATE_KEY');
const wallet = new ethers.Wallet(pKey)
const signer = wallet.connect(this.provider)
return this.contract.connect(signer).mint(address, ethers.utils.parseUnits("1"))
}
}
8 changes: 8 additions & 0 deletions Week14/project/src/dtos/requestTokens.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { ApiProperty } from "@nestjs/swagger";

export class RequestTokensDto {
@ApiProperty()
readonly address: string;
@ApiProperty()
readonly signature: string;
}
4 changes: 3 additions & 1 deletion Week14/project/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@ import { AppModule } from './app.module';

async function bootstrap() {
const app = await NestFactory.create(AppModule);


const config = new DocumentBuilder()
.setTitle('API example')
.setDescription('The API description')
.setVersion('1.0')
.build();
const document = SwaggerModule.createDocument(app, config);
app.enableCors();
SwaggerModule.setup('api', app, document);

await app.listen(3000);
await app.listen(3001);
}
bootstrap();

0 comments on commit 5e17d0f

Please sign in to comment.