Use new Instance API

This commit is contained in:
silverpill 2022-08-31 20:05:40 +00:00
parent 29b2426f92
commit ac15b55cca
7 changed files with 66 additions and 57 deletions

View file

@ -1,17 +1,25 @@
import { BACKEND_URL } from "@/constants"
import { http } from "./common"
interface ContractFeatures {
interface Features {
minter: boolean;
subscription: boolean;
subscriptions: boolean;
}
interface ChainInfo {
interface ChainMetadata {
chain_name: string;
public_api_url: string;
currency_name: string;
currency_symbol: string;
currency_decimals: string;
currency_decimals: number;
public_api_url: string;
explorer_url: string | null;
}
interface BlockchainInfo {
chain_id: string;
chain_metadata: ChainMetadata | null;
contract_address: string | null;
features: Features;
}
export interface InstanceInfo {
@ -22,11 +30,7 @@ export interface InstanceInfo {
registrations: boolean;
login_message: string;
post_character_limit: number;
blockchain_id: string | null;
blockchain_explorer_url: string | null;
blockchain_contract_address: string | null;
blockchain_features: ContractFeatures | null;
blockchain_info: ChainInfo | null;
blockchains: BlockchainInfo[];
ipfs_gateway_url: string | null;
}

View file

@ -267,6 +267,7 @@ let menuVisible = $ref(false)
let selectedPaymentAddress = $ref<string | null>(null)
let isWaitingForToken = $ref(false)
const blockchain = $computed(() => instance?.blockchains[0])
const author = $computed(() => new ProfileWrapper(props.post.account))
onMounted(() => {
@ -462,8 +463,8 @@ function isTokenized(): boolean {
function canMintToken(): boolean {
return (
Boolean(instance?.ipfs_gateway_url) &&
Boolean(instance?.blockchain_contract_address) &&
Boolean(instance?.blockchain_features?.minter) &&
Boolean(blockchain?.contract_address) &&
Boolean(blockchain?.features.minter) &&
props.post.account.id === currentUser?.id &&
props.post.visibility === "public" &&
author.getVerifiedEthereumAddress() !== null &&
@ -475,7 +476,7 @@ function canMintToken(): boolean {
async function onMintToken() {
if (
!instance ||
!instance.blockchain_contract_address
!blockchain?.contract_address
) {
return
}
@ -509,7 +510,7 @@ async function onMintToken() {
}
try {
const transaction = await mintToken(
instance.blockchain_contract_address,
blockchain.contract_address,
signer,
authorAddress,
tokenUri,

View file

@ -108,7 +108,7 @@
<script setup lang="ts">
import { BigNumber, FixedNumber } from "ethers"
import { onMounted, watch } from "vue"
import { $, $$, $ref } from "vue/macros"
import { $, $$, $computed, $ref } from "vue/macros"
import { searchProfileByEthereumAddress } from "@/api/search"
import { Profile, ProfileWrapper } from "@/api/users"
@ -173,11 +173,12 @@ onMounted(() => {
}
})
const blockchain = $computed(() => instance?.blockchains[0])
function canConnectWallet(): boolean {
return (
Boolean(instance?.blockchain_id) &&
Boolean(instance?.blockchain_contract_address) &&
Boolean(instance?.blockchain_features?.subscription) &&
Boolean(blockchain?.contract_address) &&
Boolean(blockchain?.features.subscriptions) &&
// Only profiles with verified address can have subscription
recipientEthereumAddress !== null &&
walletAddress === null
@ -206,7 +207,7 @@ watch($$(walletAddress), (newValue) => {
async function checkSubscription() {
if (
!instance?.blockchain_contract_address ||
!blockchain?.contract_address ||
!recipientEthereumAddress ||
!walletAddress
) {
@ -226,7 +227,7 @@ async function checkSubscription() {
const signer = getSigner()
isLoading = true
subscriptionConfig = await getSubscriptionConfig(
instance.blockchain_contract_address,
blockchain.contract_address,
signer,
recipientEthereumAddress,
)
@ -238,7 +239,7 @@ async function checkSubscription() {
return
}
subscriptionState = await getSubscriptionState(
instance.blockchain_contract_address,
blockchain.contract_address,
signer,
walletAddress,
recipientEthereumAddress,
@ -282,7 +283,7 @@ async function refreshTokenBalance() {
async function onMakeSubscriptionPayment() {
if (
!instance?.blockchain_contract_address ||
!blockchain?.contract_address ||
!recipientEthereumAddress ||
!walletAddress ||
!subscriptionConfig ||
@ -296,7 +297,7 @@ async function onMakeSubscriptionPayment() {
let transaction
try {
transaction = await makeSubscriptionPayment(
instance.blockchain_contract_address,
blockchain.contract_address,
signer,
recipientEthereumAddress,
amount,
@ -312,7 +313,7 @@ async function onMakeSubscriptionPayment() {
let newSubscriptionState
while (!newSubscriptionState || subscriptionState.senderBalance === newSubscriptionState.senderBalance) {
newSubscriptionState = await getSubscriptionState(
instance.blockchain_contract_address,
blockchain.contract_address,
signer,
walletAddress,
recipientEthereumAddress,
@ -336,7 +337,7 @@ function canCancel(): boolean {
async function onCancelSubscription() {
if (
!instance?.blockchain_contract_address ||
!blockchain?.contract_address ||
!recipientEthereumAddress ||
!walletAddress ||
!subscriptionConfig
@ -348,7 +349,7 @@ async function onCancelSubscription() {
let transaction
try {
transaction = await cancelSubscription(
instance.blockchain_contract_address,
blockchain.contract_address,
signer,
recipientEthereumAddress,
)
@ -359,7 +360,7 @@ async function onCancelSubscription() {
}
await transaction.wait()
subscriptionState = await getSubscriptionState(
instance.blockchain_contract_address,
blockchain.contract_address,
signer,
walletAddress,
recipientEthereumAddress,

View file

@ -68,7 +68,7 @@
<script setup lang="ts">
import { onMounted, watch } from "vue"
import { $, $$, $ref } from "vue/macros"
import { $, $$, $computed, $ref } from "vue/macros"
import { DateTime } from "luxon"
@ -122,11 +122,12 @@ onMounted(() => {
}
})
const blockchain = $computed(() => instance?.blockchains[0])
function canConnectWallet(): boolean {
return (
Boolean(instance?.blockchain_id) &&
Boolean(instance?.blockchain_contract_address) &&
Boolean(instance?.blockchain_features?.subscription) &&
Boolean(blockchain?.contract_address) &&
Boolean(blockchain?.features.subscriptions) &&
// Only profiles with verified address can have subscription
profileEthereumAddress !== null &&
walletAddress === null
@ -156,7 +157,7 @@ watch($$(walletAddress), (newValue) => {
async function checkSubscription() {
if (
!instance?.blockchain_contract_address ||
!blockchain?.contract_address ||
!profileEthereumAddress ||
!walletAddress
) {
@ -170,7 +171,7 @@ async function checkSubscription() {
isLoading = true
const signer = getSigner()
subscriptionConfig = await getSubscriptionConfig(
instance.blockchain_contract_address,
blockchain.contract_address,
signer,
profileEthereumAddress,
)
@ -185,7 +186,7 @@ async function checkSubscription() {
} else {
subscriptionsEnabled = false
subscriptionToken = await getSubscriptionToken(
instance.blockchain_contract_address,
blockchain.contract_address,
signer,
)
}
@ -203,8 +204,7 @@ function canEnableSubscriptions(): boolean {
async function onEnableSubscriptions() {
if (
profileEthereumAddress === null ||
!instance ||
!instance.blockchain_contract_address ||
!blockchain?.contract_address ||
subscriptionToken === null
) {
return
@ -220,7 +220,7 @@ async function onEnableSubscriptions() {
let transaction
try {
transaction = await configureSubscriptions(
instance.blockchain_contract_address,
blockchain.contract_address,
signer,
profileEthereumAddress,
pricePerSec,
@ -234,7 +234,7 @@ async function onEnableSubscriptions() {
await transaction.wait()
subscriptionsEnabled = true
subscriptionConfig = await getSubscriptionConfig(
instance.blockchain_contract_address,
blockchain.contract_address,
signer,
profileEthereumAddress,
)
@ -259,7 +259,7 @@ function onSubscriberSelected(subscription: Subscription) {
async function onCheckSubsciptionState() {
if (
!profileEthereumAddress ||
!instance?.blockchain_contract_address ||
!blockchain?.contract_address ||
!subscriberAddress
) {
return
@ -267,7 +267,7 @@ async function onCheckSubsciptionState() {
isLoading = true
const signer = getSigner()
subscriptionState = await getSubscriptionState(
instance.blockchain_contract_address,
blockchain.contract_address,
signer,
subscriberAddress,
profileEthereumAddress,
@ -277,7 +277,7 @@ async function onCheckSubsciptionState() {
async function onWithdrawReceived() {
if (
!instance?.blockchain_contract_address ||
!blockchain?.contract_address ||
!subscriberAddress
) {
return
@ -285,7 +285,7 @@ async function onWithdrawReceived() {
isLoading = true
const signer = getSigner()
await withdrawReceived(
instance.blockchain_contract_address,
blockchain.contract_address,
signer,
subscriberAddress,
)

View file

@ -18,7 +18,8 @@ function disconnectWallet() {
async function connectWallet(): Promise<void> {
const { instance } = useInstanceInfo()
if (!instance.value?.blockchain_id) {
const blockchain = instance.value?.blockchains[0]
if (!blockchain) {
throw new Error("blockchain integration disabled")
}
let provider
@ -29,7 +30,7 @@ async function connectWallet(): Promise<void> {
return
}
const instanceChainId = parseCAIP2_ChainId(instance.value.blockchain_id)
const instanceChainId = parseCAIP2_ChainId(blockchain.chain_id)
try {
// https://eips.ethereum.org/EIPS/eip-3326
await provider.send(
@ -38,18 +39,18 @@ async function connectWallet(): Promise<void> {
)
} catch (switchError: any) {
// This error code indicates that the chain has not been added to MetaMask
if (switchError.code === 4902 && instance.value.blockchain_info) {
const { blockchain_explorer_url, blockchain_info } = instance.value
if (switchError.code === 4902 && blockchain.chain_metadata) {
const { chain_metadata } = blockchain
// https://eips.ethereum.org/EIPS/eip-3085
const ethereumChainParams = {
chainId: instanceChainId,
chainName: blockchain_info.chain_name,
rpcUrls: [blockchain_info.public_api_url],
blockExplorerUrls: blockchain_explorer_url ? [blockchain_explorer_url] : [],
chainName: chain_metadata.chain_name,
rpcUrls: [chain_metadata.public_api_url],
blockExplorerUrls: chain_metadata.explorer_url ? [chain_metadata.explorer_url] : [],
nativeCurrency: {
name: blockchain_info.currency_name,
symbol: blockchain_info.currency_symbol,
decimals: parseInt(blockchain_info.currency_decimals),
name: chain_metadata.currency_name,
symbol: chain_metadata.currency_symbol,
decimals: chain_metadata.currency_decimals,
},
}
try {

View file

@ -97,7 +97,7 @@ const actorAddress = $computed<string>(() => {
})
const transactionUrl = $computed<string | null>(() => {
const explorerUrl = instance?.blockchain_explorer_url
const explorerUrl = instance?.blockchains[0]?.chain_metadata?.explorer_url
if (!explorerUrl || !post?.token_tx_id) {
return null
}

View file

@ -446,9 +446,10 @@ async function verifyEthereumAddress(): Promise<void> {
function canManageSubscriptions(): boolean {
// Only users with verified address can configure subscription
const blockchain = instance?.blockchains[0]
return (
Boolean(instance?.blockchain_contract_address) &&
Boolean(instance?.blockchain_features?.subscription) &&
Boolean(blockchain?.contract_address) &&
Boolean(blockchain?.features.subscriptions) &&
profile !== null &&
profile.getVerifiedEthereumAddress() !== null &&
isCurrentUser()
@ -456,9 +457,10 @@ function canManageSubscriptions(): boolean {
}
function canSubscribe(): boolean {
const blockchain = instance?.blockchains[0]
return (
Boolean(instance?.blockchain_contract_address) &&
Boolean(instance?.blockchain_features?.subscription) &&
Boolean(blockchain?.contract_address) &&
Boolean(blockchain?.features.subscriptions) &&
profile !== null &&
profile.getVerifiedEthereumAddress() !== null &&
profile.subscription_page_url !== null &&