Refactor mintToken() method

This commit is contained in:
silverpill 2022-01-29 17:26:36 +00:00
parent 44e2fb1624
commit 8aec7dd5c6
4 changed files with 45 additions and 25 deletions

24
src/api/contracts.ts Normal file
View file

@ -0,0 +1,24 @@
import { Contract, Signer } from "ethers"
import { BACKEND_URL } from "@/constants"
import { http } from "./common"
async function getContractAbi(contractName: string): Promise<any> {
// TODO: take artifact URL from instance config
const url = `${BACKEND_URL}/contracts/${contractName}.json`
const response = await http(url, {
method: "GET",
})
const data = await response.json()
return data.abi
}
export async function getContract(
contractName: string,
contractAddress: string,
signer: Signer,
): Promise<Contract> {
const Abi = await getContractAbi(contractName)
const contract = new Contract(contractAddress, Abi, signer)
return contract
}

View file

@ -1,8 +1,10 @@
import { Contract, Signer } from "ethers" import { Signer } from "ethers"
import { TransactionResponse } from "@ethersproject/abstract-provider" import { TransactionResponse } from "@ethersproject/abstract-provider"
import { BACKEND_URL } from "@/constants" import { BACKEND_URL } from "@/constants"
import { Signature } from "@/utils/ethereum"
import { http } from "./common" import { http } from "./common"
import { getContract } from "./contracts"
import { Post } from "./posts" import { Post } from "./posts"
export async function makePermanent( export async function makePermanent(
@ -22,13 +24,7 @@ export async function makePermanent(
} }
} }
interface Signature { export async function getMintingAuthorization(
v: number;
r: string;
s: string;
}
export async function getSignature(
authToken: string, authToken: string,
postId: string, postId: string,
): Promise<Signature> { ): Promise<Signature> {
@ -45,16 +41,6 @@ export async function getSignature(
} }
} }
export async function getContractAbi(contractName: string): Promise<any> {
// TODO: take artifact URL from instance config
const url = `${BACKEND_URL}/contracts/${contractName}.json`
const response = await http(url, {
method: "GET",
})
const data = await response.json()
return data.abi
}
export interface TokenMetadata { export interface TokenMetadata {
name: string; name: string;
description: string; description: string;
@ -65,14 +51,13 @@ export interface TokenMetadata {
export async function mintToken( export async function mintToken(
contractName: string, contractName: string,
contractAddress: string, contractAddress: string,
signer: Signer,
ownerAddress: string, ownerAddress: string,
tokenUri: string, tokenUri: string,
serverSignature: Signature, serverSignature: Signature,
signer: Signer,
): Promise<TransactionResponse> { ): Promise<TransactionResponse> {
const Minter = await getContractAbi(contractName) const adapter = await getContract(contractName, contractAddress, signer)
const minter = new Contract(contractAddress, Minter, signer) const transaction = await adapter.mint(
const transaction = await minter.mint(
ownerAddress, ownerAddress,
tokenUri, tokenUri,
serverSignature.v, serverSignature.v,

View file

@ -201,7 +201,12 @@
import { Options, Vue, setup } from "vue-class-component" import { Options, Vue, setup } from "vue-class-component"
import { Prop } from "vue-property-decorator" import { Prop } from "vue-property-decorator"
import { makePermanent, getSignature, mintToken, onTokenMinted } from "@/api/nft" import {
makePermanent,
getMintingAuthorization,
mintToken,
onTokenMinted,
} from "@/api/nft"
import { import {
VISIBILITY_MAP, VISIBILITY_MAP,
Mention, Mention,
@ -470,7 +475,7 @@ export default class PostComponent extends Vue {
console.info("token URI:", tokenUri) console.info("token URI:", tokenUri)
let signature let signature
try { try {
signature = await getSignature(authToken, this.post.id) signature = await getMintingAuthorization(authToken, this.post.id)
} catch (error) { } catch (error) {
console.log(error) console.log(error)
this.isWaitingForToken = false this.isWaitingForToken = false
@ -485,10 +490,10 @@ export default class PostComponent extends Vue {
const transaction = await mintToken( const transaction = await mintToken(
instance.blockchain_contract_name, instance.blockchain_contract_name,
instance.blockchain_contract_address, instance.blockchain_contract_address,
signer,
currentUser.wallet_address, currentUser.wallet_address,
tokenUri, tokenUri,
signature, signature,
signer,
) )
await onTokenMinted(authToken, this.post.id, transaction.hash) await onTokenMinted(authToken, this.post.id, transaction.hash)
} catch (error) { } catch (error) {

View file

@ -21,3 +21,9 @@ export async function getSigner(): Promise<Signer | null> {
const signer = provider.getSigner() const signer = provider.getSigner()
return signer return signer
} }
export interface Signature {
v: number;
r: string;
s: string;
}