Skip to content

Commit

Permalink
refactor: make delegate public
Browse files Browse the repository at this point in the history
  • Loading branch information
phchan9 committed May 25, 2022
1 parent 8f941a2 commit b0ad5c3
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
1 change: 1 addition & 0 deletions src/contracts/Contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ export class Contracts {
contract: value.contract.connect(signer),
})
})
this.delegateApproval = this.delegateApproval.connect(signer)
}

createIERC20Token(tokenAddress: string) {
Expand Down
1 change: 1 addition & 0 deletions src/core/PerpetualProtocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export interface PerpetualProtocolConnected extends PerpetualProtocolInitialized
positions: Positions
liquidities: Liquidities
vault: Vault
delegateApproval: DelegateApproval
}

/**
Expand Down
29 changes: 19 additions & 10 deletions src/core/clearingHouse/DelegateApproval.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export interface DelegateApprovalConfigs {
export class DelegateApproval extends Channel<DelegateApprovalEventName> {
private readonly _contractReader: ContractReader
private _cache: Map<CacheKey, CacheValue> = new Map()
private _delegate: string
public delegate: string

constructor(
protected readonly _perp: PerpetualProtocol,
Expand All @@ -29,38 +29,47 @@ export class DelegateApproval extends Channel<DelegateApprovalEventName> {
) {
super(_perp.channelRegistry)
this._contractReader = _perp.contractReader
this._delegate = configs ? configs.delegate : _perp.metadata.contracts.LimitOrderBook.address
this.delegate = configs ? configs.delegate : _perp.metadata.contracts.LimitOrderBook.address
}

async approveOpenPosition(delegate: string) {
async approveOpenPosition() {
const openPositionAction = await this._perp.contractReader.getClearingHouseOpenPositionAction()
await this.approve(delegate, openPositionAction)
return this.approve(openPositionAction)
}

async approve(delegate: string, actions: number) {
async revokeOpenPosition() {
const openPositionAction = await this._perp.contractReader.getClearingHouseOpenPositionAction()
return this.revoke(openPositionAction)
}

async approve(actions: number) {
invariant(this._perp.hasConnected(), () => new UnauthorizedError({ functionName: "approve" }))

return getTransaction<ContractDelegateApproval, "approve">({
account: this._perp.wallet.account,
contract: this._perp.contracts.delegateApproval,
contractName: ContractName.DelegateApproval,
contractFunctionName: "approve",
args: [delegate, actions],
args: [this.delegate, actions],
})
}

async revoke(delegate: string, actions: number) {
invariant(this._perp.hasConnected(), () => new UnauthorizedError({ functionName: "approve" }))
async revoke(actions: number) {
invariant(this._perp.hasConnected(), () => new UnauthorizedError({ functionName: "revoke" }))

return getTransaction<ContractDelegateApproval, "revoke">({
account: this._perp.wallet.account,
contract: this._perp.contracts.delegateApproval,
contractName: ContractName.DelegateApproval,
contractFunctionName: "revoke",
args: [delegate, actions],
args: [this.delegate, actions],
})
}

async delegateApprovalForOpenPosition({ cache = true } = {}) {
return await this._fetch("openPosition", { cache })
}

protected _getEventSourceMap() {
const fetchAndEmitDelegateApprovalUpdated = this._createFetchAndEmitDelegateApprovalUpdated()
const delegateApprovalUpdated = new ChannelEventSource({
Expand Down Expand Up @@ -95,7 +104,7 @@ export class DelegateApproval extends Channel<DelegateApprovalEventName> {
let result
switch (key) {
case "openPosition": {
result = await this._contractReader.canOpenPositionFor(this.account, this._delegate)
result = await this._contractReader.canOpenPositionFor(this.account, this.delegate)
break
}
}
Expand Down

0 comments on commit b0ad5c3

Please sign in to comment.