Skip to content

Commit

Permalink
isAffiliateCodeAvailable() (#382)
Browse files Browse the repository at this point in the history
* new util

* changelog and package

* add util
  • Loading branch information
filipzeta authored Apr 15, 2024
1 parent 1a81047 commit d1e0d82
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Version changes are pinned to SDK releases.
## [1.26.4]

- Respect Exchange.skipRpcConfirmation everywhere. ([#384](https://github.com/zetamarkets/sdk/pull/384))
- New util isAffiliateCodeAvailable(). ([#382](https://github.com/zetamarkets/sdk/pull/382))

## [1.26.3]

Expand Down
69 changes: 69 additions & 0 deletions src/cross-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,10 @@ export class CrossClient {
): Promise<TransactionSignature> {
let tx = new Transaction();

if ((await this.hasReferrerAccounts()) == true) {
throw Error("User already has a referrer account!");
}

if (this._accountManager === null) {
console.log(
"User has no cross margin account manager. Creating account manager..."
Expand Down Expand Up @@ -1244,6 +1248,71 @@ export class CrossClient {
return txId;
}

public async hasReferrerAccounts(): Promise<boolean> {
// Firstly, close all referrer accounts
let referrerPubkeyAccountAddress = utils.getReferrerPubkeyAccount(
Exchange.programId,
this.publicKey
)[0];
let referrerPubkeyAccount =
await Exchange.program.account.referrerPubkeyAccount.fetchNullable(
referrerPubkeyAccountAddress
);
return referrerPubkeyAccount != null;
}

public async remakeReferrerAccounts(
id: string
): Promise<TransactionSignature> {
this.delegatedCheck();

let tx = new Transaction();

// Firstly, close all referrer accounts
if ((await this.hasReferrerAccounts()) == true) {
let referrerPubkeyAccountAddress = utils.getReferrerPubkeyAccount(
Exchange.programId,
this.publicKey
)[0];
let referrerPubkeyAccount =
await Exchange.program.account.referrerPubkeyAccount.fetch(
referrerPubkeyAccountAddress
);
let id = Buffer.from(referrerPubkeyAccount.referrerId).toString();
let referrerIdAccountAddress = utils.getReferrerIdAccount(
Exchange.programId,
id
)[0];

tx.add(
instructions.closeReferrerAccountsIx(
this.publicKey,
referrerIdAccountAddress,
referrerPubkeyAccountAddress
)
);
}

// Secondly, make new ones with the provided ID
tx.add(
instructions.initializeReferrerAccountsIx(
id,
this.publicKey,
utils.getReferrerIdAccount(Exchange.programId, id)[0],
utils.getReferrerPubkeyAccount(Exchange.programId, this.publicKey)[0]
)
);

return await utils.processTransaction(
this._provider,
tx,
undefined,
undefined,
undefined,
this._useVersionedTxs ? utils.getZetaLutArr() : undefined
);
}

public async closeReferrerAccounts(): Promise<TransactionSignature> {
this.delegatedCheck();

Expand Down
10 changes: 10 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1964,6 +1964,16 @@ export function median(arr: number[]): number | undefined {
return s.length % 2 === 0 ? (s[mid - 1] + s[mid]) / 2 : s[mid];
}

export async function isAffiliateCodeAvailable(code: string): Promise<boolean> {
let referrerIdAddress = getReferrerIdAccount(Exchange.programId, code)[0];
let referrerIdAccount =
await Exchange.program.account.referrerIdAccount.fetchNullable(
referrerIdAddress
);

return referrerIdAccount == null;
}

export const checkLiquidity = (
size: number,
asset: Asset,
Expand Down

0 comments on commit d1e0d82

Please sign in to comment.