|
| 1 | +import { DataSource } from "@pythnetwork/xc-admin-common"; |
| 2 | +import { |
| 3 | + KeyValueConfig, |
| 4 | + PriceFeed, |
| 5 | + PriceFeedContract, |
| 6 | + PrivateKey, |
| 7 | + TxResult, |
| 8 | +} from "../base"; |
| 9 | +import { Chain, NearChain } from "../chains"; |
| 10 | +import * as nearAPI from "near-api-js"; |
| 11 | +import { BN } from "fuels"; |
| 12 | +import { WormholeContract } from "./wormhole"; |
| 13 | + |
| 14 | +export class NearWormholeContract extends WormholeContract { |
| 15 | + static type = "NearWormholeContract"; |
| 16 | + |
| 17 | + constructor(public chain: NearChain, public address: string) { |
| 18 | + super(); |
| 19 | + } |
| 20 | + |
| 21 | + getId(): string { |
| 22 | + return `${this.chain.getId()}__${this.address.replace(/-|\./g, "_")}`; |
| 23 | + } |
| 24 | + |
| 25 | + getChain(): NearChain { |
| 26 | + return this.chain; |
| 27 | + } |
| 28 | + |
| 29 | + getType(): string { |
| 30 | + return NearWormholeContract.type; |
| 31 | + } |
| 32 | + |
| 33 | + static fromJson( |
| 34 | + chain: Chain, |
| 35 | + parsed: { type: string; address: string } |
| 36 | + ): NearWormholeContract { |
| 37 | + if (parsed.type !== NearWormholeContract.type) |
| 38 | + throw new Error("Invalid type"); |
| 39 | + if (!(chain instanceof NearChain)) |
| 40 | + throw new Error(`Wrong chain type ${chain}`); |
| 41 | + return new NearWormholeContract(chain, parsed.address); |
| 42 | + } |
| 43 | + |
| 44 | + toJson(): KeyValueConfig { |
| 45 | + return { |
| 46 | + chain: this.chain.getId(), |
| 47 | + address: this.address, |
| 48 | + type: NearWormholeContract.type, |
| 49 | + }; |
| 50 | + } |
| 51 | + |
| 52 | + async upgradeGuardianSets( |
| 53 | + senderPrivateKey: PrivateKey, |
| 54 | + vaa: Buffer |
| 55 | + ): Promise<TxResult> { |
| 56 | + const senderAddress = await this.chain.getAccountAddress(senderPrivateKey); |
| 57 | + const account = await this.chain.getNearAccount( |
| 58 | + senderAddress, |
| 59 | + senderPrivateKey |
| 60 | + ); |
| 61 | + const outcome = await account.functionCall({ |
| 62 | + contractId: this.address, |
| 63 | + methodName: "submit_vaa", |
| 64 | + args: { vaa: vaa.toString("hex") }, |
| 65 | + gas: new BN(300e12), |
| 66 | + attachedDeposit: new BN(1e12), |
| 67 | + }); |
| 68 | + return { id: outcome.transaction.hash, info: outcome }; |
| 69 | + } |
| 70 | + |
| 71 | + getCurrentGuardianSetIndex(): Promise<number> { |
| 72 | + throw new Error( |
| 73 | + "near wormhole contract doesn't implement getCurrentGuardianSetIndex method" |
| 74 | + ); |
| 75 | + } |
| 76 | + getChainId(): Promise<number> { |
| 77 | + throw new Error( |
| 78 | + "near wormhole contract doesn't implement getChainId method" |
| 79 | + ); |
| 80 | + } |
| 81 | + getGuardianSet(): Promise<string[]> { |
| 82 | + throw new Error( |
| 83 | + "near wormhole contract doesn't implement getGuardianSet method" |
| 84 | + ); |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +export class NearPriceFeedContract extends PriceFeedContract { |
| 89 | + public static type = "NearPriceFeedContract"; |
| 90 | + |
| 91 | + constructor(public chain: NearChain, public address: string) { |
| 92 | + super(); |
| 93 | + } |
| 94 | + |
| 95 | + getId(): string { |
| 96 | + return `${this.chain.getId()}__${this.address.replace(/-|\./g, "_")}`; |
| 97 | + } |
| 98 | + |
| 99 | + getType(): string { |
| 100 | + return NearPriceFeedContract.type; |
| 101 | + } |
| 102 | + |
| 103 | + getChain(): NearChain { |
| 104 | + return this.chain; |
| 105 | + } |
| 106 | + |
| 107 | + toJson(): KeyValueConfig { |
| 108 | + return { |
| 109 | + chain: this.chain.getId(), |
| 110 | + address: this.address, |
| 111 | + type: NearPriceFeedContract.type, |
| 112 | + }; |
| 113 | + } |
| 114 | + |
| 115 | + static fromJson( |
| 116 | + chain: Chain, |
| 117 | + parsed: { type: string; address: string } |
| 118 | + ): NearPriceFeedContract { |
| 119 | + if (parsed.type !== NearPriceFeedContract.type) { |
| 120 | + throw new Error("Invalid type"); |
| 121 | + } |
| 122 | + if (!(chain instanceof NearChain)) { |
| 123 | + throw new Error(`Wrong chain type ${chain}`); |
| 124 | + } |
| 125 | + return new NearPriceFeedContract(chain, parsed.address); |
| 126 | + } |
| 127 | + |
| 128 | + async getContractNearAccount( |
| 129 | + senderPrivateKey?: PrivateKey |
| 130 | + ): Promise<nearAPI.Account> { |
| 131 | + return await this.chain.getNearAccount(this.address, senderPrivateKey); |
| 132 | + } |
| 133 | + |
| 134 | + async getValidTimePeriod(): Promise<number> { |
| 135 | + const account = await this.getContractNearAccount(); |
| 136 | + return account.viewFunction({ |
| 137 | + contractId: this.address, |
| 138 | + methodName: "get_stale_threshold", |
| 139 | + }); |
| 140 | + } |
| 141 | + |
| 142 | + async getDataSources(): Promise<DataSource[]> { |
| 143 | + const account = await this.getContractNearAccount(); |
| 144 | + const outcome: [{ emitter: number[]; chain: number }] = |
| 145 | + await account.viewFunction({ |
| 146 | + contractId: this.address, |
| 147 | + methodName: "get_sources", |
| 148 | + }); |
| 149 | + return outcome.map((item) => { |
| 150 | + return { |
| 151 | + emitterChain: item.chain, |
| 152 | + emitterAddress: Buffer.from(item.emitter).toString("hex"), |
| 153 | + }; |
| 154 | + }); |
| 155 | + } |
| 156 | + |
| 157 | + async getPriceFeed(feedId: string): Promise<PriceFeed | undefined> { |
| 158 | + const account = await this.getContractNearAccount(); |
| 159 | + const price: { |
| 160 | + price: string; |
| 161 | + conf: string; |
| 162 | + expo: number; |
| 163 | + publish_time: number; |
| 164 | + } | null = await account.viewFunction({ |
| 165 | + contractId: this.address, |
| 166 | + methodName: "get_price_unsafe", |
| 167 | + args: { price_identifier: feedId }, |
| 168 | + }); |
| 169 | + const emaPrice: { |
| 170 | + price: string; |
| 171 | + conf: string; |
| 172 | + expo: number; |
| 173 | + publish_time: number; |
| 174 | + } | null = await account.viewFunction({ |
| 175 | + contractId: this.address, |
| 176 | + methodName: "get_ema_price_unsafe", |
| 177 | + args: { price_id: feedId }, |
| 178 | + }); |
| 179 | + if (price === null || emaPrice === null) { |
| 180 | + return undefined; |
| 181 | + } else { |
| 182 | + return { |
| 183 | + price: { |
| 184 | + price: price.price, |
| 185 | + conf: price.conf, |
| 186 | + expo: price.expo.toString(), |
| 187 | + publishTime: price.publish_time.toString(), |
| 188 | + }, |
| 189 | + emaPrice: { |
| 190 | + price: emaPrice.price, |
| 191 | + conf: emaPrice.conf, |
| 192 | + expo: emaPrice.expo.toString(), |
| 193 | + publishTime: emaPrice.publish_time.toString(), |
| 194 | + }, |
| 195 | + }; |
| 196 | + } |
| 197 | + } |
| 198 | + |
| 199 | + async executeUpdatePriceFeed( |
| 200 | + senderPrivateKey: PrivateKey, |
| 201 | + vaas: Buffer[] |
| 202 | + ): Promise<TxResult> { |
| 203 | + if (vaas.length === 0) { |
| 204 | + throw new Error("no vaas specified"); |
| 205 | + } |
| 206 | + const senderAddress = await this.chain.getAccountAddress(senderPrivateKey); |
| 207 | + const account = await this.chain.getNearAccount( |
| 208 | + senderAddress, |
| 209 | + senderPrivateKey |
| 210 | + ); |
| 211 | + const results = []; |
| 212 | + for (const vaa of vaas) { |
| 213 | + const outcome = await account.functionCall({ |
| 214 | + contractId: this.address, |
| 215 | + methodName: "update_price_feeds", |
| 216 | + args: { data: vaa.toString("hex") }, |
| 217 | + gas: new BN(300e12), |
| 218 | + attachedDeposit: new BN(1e12), |
| 219 | + }); |
| 220 | + results.push({ id: outcome.transaction.hash, info: outcome }); |
| 221 | + } |
| 222 | + if (results.length === 1) { |
| 223 | + return results[0]; |
| 224 | + } else { |
| 225 | + return { |
| 226 | + id: results.map((x) => x.id).join(","), |
| 227 | + info: results.map((x) => x.info), |
| 228 | + }; |
| 229 | + } |
| 230 | + } |
| 231 | + |
| 232 | + async executeGovernanceInstruction( |
| 233 | + senderPrivateKey: PrivateKey, |
| 234 | + vaa: Buffer |
| 235 | + ): Promise<TxResult> { |
| 236 | + const senderAddress = await this.chain.getAccountAddress(senderPrivateKey); |
| 237 | + const account = await this.chain.getNearAccount( |
| 238 | + senderAddress, |
| 239 | + senderPrivateKey |
| 240 | + ); |
| 241 | + const outcome = await account.functionCall({ |
| 242 | + contractId: this.address, |
| 243 | + methodName: "execute_governance_instruction", |
| 244 | + args: { vaa: vaa.toString("hex") }, |
| 245 | + gas: new BN(300e12), |
| 246 | + attachedDeposit: new BN(1e12), |
| 247 | + }); |
| 248 | + return { id: outcome.transaction.hash, info: outcome }; |
| 249 | + } |
| 250 | + |
| 251 | + getBaseUpdateFee(): Promise<{ amount: string; denom?: string }> { |
| 252 | + throw new Error("near contract doesn't implement getBaseUpdateFee method"); |
| 253 | + } |
| 254 | + getLastExecutedGovernanceSequence(): Promise<number> { |
| 255 | + throw new Error( |
| 256 | + "near contract doesn't implement getLastExecutedGovernanceSequence method" |
| 257 | + ); |
| 258 | + } |
| 259 | + getGovernanceDataSource(): Promise<DataSource> { |
| 260 | + throw new Error( |
| 261 | + "near contract doesn't implement getGovernanceDataSource method" |
| 262 | + ); |
| 263 | + } |
| 264 | +} |
0 commit comments