Refactor mintToken() method
This commit is contained in:
parent
44e2fb1624
commit
8aec7dd5c6
4 changed files with 45 additions and 25 deletions
24
src/api/contracts.ts
Normal file
24
src/api/contracts.ts
Normal 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
|
||||
}
|
|
@ -1,8 +1,10 @@
|
|||
import { Contract, Signer } from "ethers"
|
||||
import { Signer } from "ethers"
|
||||
import { TransactionResponse } from "@ethersproject/abstract-provider"
|
||||
|
||||
import { BACKEND_URL } from "@/constants"
|
||||
import { Signature } from "@/utils/ethereum"
|
||||
import { http } from "./common"
|
||||
import { getContract } from "./contracts"
|
||||
import { Post } from "./posts"
|
||||
|
||||
export async function makePermanent(
|
||||
|
@ -22,13 +24,7 @@ export async function makePermanent(
|
|||
}
|
||||
}
|
||||
|
||||
interface Signature {
|
||||
v: number;
|
||||
r: string;
|
||||
s: string;
|
||||
}
|
||||
|
||||
export async function getSignature(
|
||||
export async function getMintingAuthorization(
|
||||
authToken: string,
|
||||
postId: string,
|
||||
): 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 {
|
||||
name: string;
|
||||
description: string;
|
||||
|
@ -65,14 +51,13 @@ export interface TokenMetadata {
|
|||
export async function mintToken(
|
||||
contractName: string,
|
||||
contractAddress: string,
|
||||
signer: Signer,
|
||||
ownerAddress: string,
|
||||
tokenUri: string,
|
||||
serverSignature: Signature,
|
||||
signer: Signer,
|
||||
): Promise<TransactionResponse> {
|
||||
const Minter = await getContractAbi(contractName)
|
||||
const minter = new Contract(contractAddress, Minter, signer)
|
||||
const transaction = await minter.mint(
|
||||
const adapter = await getContract(contractName, contractAddress, signer)
|
||||
const transaction = await adapter.mint(
|
||||
ownerAddress,
|
||||
tokenUri,
|
||||
serverSignature.v,
|
||||
|
|
|
@ -201,7 +201,12 @@
|
|||
import { Options, Vue, setup } from "vue-class-component"
|
||||
import { Prop } from "vue-property-decorator"
|
||||
|
||||
import { makePermanent, getSignature, mintToken, onTokenMinted } from "@/api/nft"
|
||||
import {
|
||||
makePermanent,
|
||||
getMintingAuthorization,
|
||||
mintToken,
|
||||
onTokenMinted,
|
||||
} from "@/api/nft"
|
||||
import {
|
||||
VISIBILITY_MAP,
|
||||
Mention,
|
||||
|
@ -470,7 +475,7 @@ export default class PostComponent extends Vue {
|
|||
console.info("token URI:", tokenUri)
|
||||
let signature
|
||||
try {
|
||||
signature = await getSignature(authToken, this.post.id)
|
||||
signature = await getMintingAuthorization(authToken, this.post.id)
|
||||
} catch (error) {
|
||||
console.log(error)
|
||||
this.isWaitingForToken = false
|
||||
|
@ -485,10 +490,10 @@ export default class PostComponent extends Vue {
|
|||
const transaction = await mintToken(
|
||||
instance.blockchain_contract_name,
|
||||
instance.blockchain_contract_address,
|
||||
signer,
|
||||
currentUser.wallet_address,
|
||||
tokenUri,
|
||||
signature,
|
||||
signer,
|
||||
)
|
||||
await onTokenMinted(authToken, this.post.id, transaction.hash)
|
||||
} catch (error) {
|
||||
|
|
|
@ -21,3 +21,9 @@ export async function getSigner(): Promise<Signer | null> {
|
|||
const signer = provider.getSigner()
|
||||
return signer
|
||||
}
|
||||
|
||||
export interface Signature {
|
||||
v: number;
|
||||
r: string;
|
||||
s: string;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue