-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #103 from art-by-city/100-following
Adds Following module
- Loading branch information
Showing
13 changed files
with
365 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
import Arweave from 'arweave' | ||
import { Tag, Warp } from 'warp-contracts' | ||
import { ArweaveSigner } from 'warp-arbundles' | ||
import { InjectedArweaveSigner, isSigner } from 'warp-contracts-plugin-deploy' | ||
|
||
import { ArtByCityConfig } from '../config' | ||
import { getAddressFromSigner } from '../util/crypto' | ||
import { | ||
ArtByCityFollowing, | ||
FollowInteraction, | ||
FollowingCreationOptions, | ||
UnfollowInteraction | ||
} from './' | ||
|
||
export default class AuthenticatedArtByCityFollowing | ||
extends ArtByCityFollowing | ||
{ | ||
constructor( | ||
protected readonly arweave: Arweave, | ||
protected readonly warp: Warp, | ||
protected readonly config: ArtByCityConfig, | ||
private readonly signer: ArweaveSigner | InjectedArweaveSigner | ||
) { | ||
super(arweave, warp, config) | ||
} | ||
|
||
async getOrCreate() { | ||
const owner = await getAddressFromSigner(this.signer) | ||
const contract = await this.getContract(owner) | ||
|
||
if (contract) { | ||
return contract.txId() | ||
} | ||
|
||
return await this.create() | ||
} | ||
|
||
async create(opts?: FollowingCreationOptions) { | ||
if (!opts) { | ||
opts = {} | ||
} | ||
|
||
if (!opts.owner) { | ||
opts.owner = await getAddressFromSigner(this.signer) | ||
} | ||
|
||
const initialState = { | ||
owner: opts.owner, | ||
following: opts.following || [] | ||
} | ||
|
||
const tags = (opts.tags || []).map<Tag>(tag => new Tag(tag.name, tag.value)) | ||
tags.push( | ||
new Tag('Protocol', 'ArtByCity'), | ||
new Tag('Contract-Name', 'Following'), | ||
new Tag('Contract-Version', '0.0.1'), | ||
new Tag('Entity-Type', 'following') | ||
) | ||
|
||
const { contractTxId } = await this.warp.deployFromSourceTx({ | ||
/* @ts-expect-error warp types are spaghetti */ | ||
wallet: this.signer, | ||
srcTxId: this.config.contracts.following, | ||
initState: JSON.stringify(initialState), | ||
tags | ||
/* @ts-expect-error warp types are spaghetti */ | ||
}, !isSigner(this.signer)) | ||
|
||
return contractTxId | ||
} | ||
|
||
async follow(address: string) { | ||
const owner = await getAddressFromSigner(this.signer) | ||
const contract = await this.getContract(owner) | ||
|
||
if (contract) { | ||
const res = await contract | ||
/* @ts-expect-error warp spaghetti */ | ||
.connect(this.signer) | ||
.writeInteraction<FollowInteraction>({ | ||
function: 'follow', | ||
address | ||
}) | ||
|
||
if (!res) { | ||
throw new Error(`Error interacting with Following contract`) | ||
} | ||
|
||
return res | ||
} | ||
|
||
throw new Error(`No Following contract found for ${owner}`) | ||
} | ||
|
||
async unfollow(address: string) { | ||
const owner = await getAddressFromSigner(this.signer) | ||
const contract = await this.getContract(owner) | ||
|
||
if (contract) { | ||
const res = await contract | ||
/* @ts-expect-error warp spaghetti */ | ||
.connect(this.signer) | ||
.writeInteraction<UnfollowInteraction>({ | ||
function: 'unfollow', | ||
address | ||
}) | ||
|
||
if (!res) { | ||
throw new Error(`Error interacting with Following contract`) | ||
} | ||
|
||
return res | ||
} | ||
|
||
throw new Error(`No Following contract found for ${owner}`) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import ArDB from 'ardb' | ||
import ArdbTransaction from 'ardb/lib/models/transaction' | ||
import Arweave from 'arweave' | ||
import { Warp } from 'warp-contracts' | ||
|
||
import TransactionsModule from '../common/transactions' | ||
import { ArtByCityConfig } from '../config' | ||
import { FollowingContractState } from './' | ||
|
||
export default class ArtByCityFollowing { | ||
protected readonly ardb!: ArDB | ||
protected readonly transactions!: TransactionsModule | ||
|
||
constructor( | ||
protected readonly arweave: Arweave, | ||
protected readonly warp: Warp, | ||
protected readonly config: ArtByCityConfig | ||
) { | ||
this.ardb = new ArDB(this.arweave) | ||
this.transactions = new TransactionsModule(arweave) | ||
} | ||
|
||
async getContract(owner: string) { | ||
const txs = await this.ardb | ||
.search('transactions') | ||
.from(owner) | ||
.tag('Contract-Name', 'Following') | ||
.sort('HEIGHT_DESC') | ||
.limit(1) | ||
.find() as ArdbTransaction[] | ||
|
||
const tx = txs.at(0) | ||
|
||
if (tx) { | ||
return this.warp.contract<FollowingContractState>(tx.id) | ||
} | ||
|
||
return null | ||
} | ||
|
||
async following(owner: string) { | ||
const contract = await this.getContract(owner) | ||
|
||
if (contract) { | ||
const { cachedValue: { state } } = await contract.readState() | ||
|
||
return state.following | ||
} | ||
|
||
return [] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
export { default as ArtByCityFollowing } from './following' | ||
export { | ||
default as AuthenticatedArtByCityFollowing | ||
} from './authenticated-following' | ||
|
||
export interface FollowingCreationOptions { | ||
owner?: string | ||
following?: string[] | ||
tags?: { name: string, value: string }[] | ||
slug?: boolean | string | ||
} | ||
|
||
export interface FollowingContractState { | ||
owner: string | ||
following: string[] | ||
} | ||
|
||
export interface FollowInteraction { | ||
function: 'follow' | ||
address: string | ||
} | ||
|
||
export interface UnfollowInteraction { | ||
function: 'unfollow' | ||
address: string | ||
} |
Oops, something went wrong.