|
| 1 | +import type { RadiusWalletInterface } from '@radiustechsystems/ai-agent-wallet'; |
| 2 | +import { ContractError, TransactionError } from '@radiustechsystems/ai-agent-wallet'; |
| 3 | +import { dataAccessABI } from './abi'; |
| 4 | +import type { AccessTier } from './types'; |
| 5 | + |
| 6 | +/** |
| 7 | + * Interface representing a tier returned from the DataAccess contract |
| 8 | + */ |
| 9 | +interface ContractTier { |
| 10 | + id: string | number; |
| 11 | + name: string; |
| 12 | + description: string; |
| 13 | + domains: string[]; |
| 14 | + price: string | number | bigint; |
| 15 | + ttl: string | number | bigint; |
| 16 | + active: boolean; |
| 17 | +} |
| 18 | + |
| 19 | +/** |
| 20 | + * DataAccessContract provides a wrapper around the DataAccess smart contract |
| 21 | + * It handles all the direct blockchain interactions required by the plugin |
| 22 | + */ |
| 23 | +export class DataAccessContract { |
| 24 | + private contractAddress: string; |
| 25 | + private projectId: string; |
| 26 | + private wallet: RadiusWalletInterface; |
| 27 | + |
| 28 | + constructor(wallet: RadiusWalletInterface, contractAddress: string, projectId: string) { |
| 29 | + this.wallet = wallet; |
| 30 | + this.contractAddress = contractAddress; |
| 31 | + this.projectId = projectId; |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * Check if address has valid access to a tier |
| 36 | + */ |
| 37 | + async isValid(address: string, tierId: number): Promise<boolean> { |
| 38 | + try { |
| 39 | + const result = await this.wallet.read({ |
| 40 | + address: this.contractAddress, |
| 41 | + abi: dataAccessABI, |
| 42 | + functionName: 'isValid', |
| 43 | + args: [address, tierId], |
| 44 | + }); |
| 45 | + |
| 46 | + return result.value as boolean; |
| 47 | + } catch (error) { |
| 48 | + throw new ContractError( |
| 49 | + `Failed to check access validity: ${error instanceof Error ? error.message : String(error)}`, |
| 50 | + this.contractAddress, |
| 51 | + 'isValid', |
| 52 | + ); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Get token balance for an address and tier |
| 58 | + */ |
| 59 | + async balanceOf(address: string, tierId: number): Promise<number> { |
| 60 | + try { |
| 61 | + const result = await this.wallet.read({ |
| 62 | + address: this.contractAddress, |
| 63 | + abi: dataAccessABI, |
| 64 | + functionName: 'balanceOf', |
| 65 | + args: [address, tierId], |
| 66 | + }); |
| 67 | + |
| 68 | + return Number(result.value); |
| 69 | + } catch (error) { |
| 70 | + throw new ContractError( |
| 71 | + `Failed to get token balance: ${error instanceof Error ? error.message : String(error)}`, |
| 72 | + this.contractAddress, |
| 73 | + 'balanceOf', |
| 74 | + ); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Get available access tiers |
| 80 | + */ |
| 81 | + async tiers(): Promise<AccessTier[]> { |
| 82 | + try { |
| 83 | + const result = await this.wallet.read({ |
| 84 | + address: this.contractAddress, |
| 85 | + abi: dataAccessABI, |
| 86 | + functionName: 'getTiers', |
| 87 | + args: [this.projectId], |
| 88 | + }); |
| 89 | + |
| 90 | + // Convert contract response to AccessTier format |
| 91 | + // Use proper typing with the ContractTier interface |
| 92 | + return ((result.value as ContractTier[]) || []).map((tier) => ({ |
| 93 | + id: Number(tier.id), |
| 94 | + name: tier.name, |
| 95 | + description: tier.description, |
| 96 | + domains: tier.domains, |
| 97 | + price: BigInt(tier.price.toString()), |
| 98 | + ttl: BigInt(tier.ttl.toString()), |
| 99 | + active: tier.active, |
| 100 | + })); |
| 101 | + } catch (error) { |
| 102 | + console.error(`Error getting tiers: ${error}`); |
| 103 | + // Return mock tiers for fallback during development |
| 104 | + // In production, we should throw the error |
| 105 | + return [ |
| 106 | + { |
| 107 | + id: 1, |
| 108 | + name: 'Breaking News', |
| 109 | + description: 'Grants access to content for 1 hour', |
| 110 | + domains: ['http://localhost:3000/content'], |
| 111 | + price: BigInt('12500000000000000'), // 0.0125 ETH |
| 112 | + ttl: BigInt(3600), |
| 113 | + active: true, |
| 114 | + }, |
| 115 | + { |
| 116 | + id: 2, |
| 117 | + name: 'Daily News', |
| 118 | + description: 'Grants access to content for 24 hours', |
| 119 | + domains: ['http://localhost:3000/content'], |
| 120 | + price: BigInt('125000000000000000'), // 0.125 ETH |
| 121 | + ttl: BigInt(86400), |
| 122 | + active: true, |
| 123 | + }, |
| 124 | + { |
| 125 | + id: 3, |
| 126 | + name: 'Weekly News', |
| 127 | + description: 'Grants access to content for 7 days', |
| 128 | + domains: ['http://localhost:3000/content'], |
| 129 | + price: BigInt('750000000000000000'), // 0.75 ETH |
| 130 | + ttl: BigInt(604800), |
| 131 | + active: true, |
| 132 | + }, |
| 133 | + ]; |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Purchase access to a tier |
| 139 | + */ |
| 140 | + async purchase(tierId: number, amount = 1): Promise<{ txHash: string }> { |
| 141 | + try { |
| 142 | + // Get tier to determine price |
| 143 | + const allTiers = await this.tiers(); |
| 144 | + const tier = allTiers.find((t) => t.id === tierId); |
| 145 | + |
| 146 | + if (!tier) { |
| 147 | + throw new Error(`Tier ${tierId} not found`); |
| 148 | + } |
| 149 | + |
| 150 | + // Execute contract transaction |
| 151 | + const hash = await this.wallet.sendTransaction({ |
| 152 | + to: this.contractAddress, |
| 153 | + abi: dataAccessABI, |
| 154 | + functionName: 'purchase', |
| 155 | + args: [this.projectId, tierId, amount], |
| 156 | + value: tier.price, // Send the price with the transaction |
| 157 | + }); |
| 158 | + |
| 159 | + return { txHash: hash.hash }; |
| 160 | + } catch (error) { |
| 161 | + throw new TransactionError( |
| 162 | + `Failed to purchase access tier: ${error instanceof Error ? error.message : String(error)}`, |
| 163 | + ); |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + /** |
| 168 | + * Verify a challenge signature |
| 169 | + */ |
| 170 | + async verify(tierId: number, challenge: string, signature: string): Promise<boolean> { |
| 171 | + try { |
| 172 | + const result = await this.wallet.read({ |
| 173 | + address: this.contractAddress, |
| 174 | + abi: dataAccessABI, |
| 175 | + functionName: 'verifyChallenge', |
| 176 | + args: [this.projectId, tierId, challenge, signature], |
| 177 | + }); |
| 178 | + |
| 179 | + return result.value as boolean; |
| 180 | + } catch (error) { |
| 181 | + throw new ContractError( |
| 182 | + `Failed to verify challenge: ${error instanceof Error ? error.message : String(error)}`, |
| 183 | + this.contractAddress, |
| 184 | + 'verifyChallenge', |
| 185 | + ); |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + /** |
| 190 | + * Get expiration time for a token |
| 191 | + */ |
| 192 | + async expiresAt(walletAddress: string, tierId: number): Promise<number> { |
| 193 | + try { |
| 194 | + const result = await this.wallet.read({ |
| 195 | + address: this.contractAddress, |
| 196 | + abi: dataAccessABI, |
| 197 | + functionName: 'expiresAt', |
| 198 | + args: [walletAddress, this.projectId, tierId], |
| 199 | + }); |
| 200 | + |
| 201 | + return Number(result.value); |
| 202 | + } catch (error) { |
| 203 | + throw new ContractError( |
| 204 | + `Failed to get token expiration: ${error instanceof Error ? error.message : String(error)}`, |
| 205 | + this.contractAddress, |
| 206 | + 'expiresAt', |
| 207 | + ); |
| 208 | + } |
| 209 | + } |
| 210 | +} |
0 commit comments