diff --git a/CHANGELOG.md b/CHANGELOG.md index 15604eeb..1501adf7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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] diff --git a/src/cross-client.ts b/src/cross-client.ts index dddca6b5..c32a4e40 100644 --- a/src/cross-client.ts +++ b/src/cross-client.ts @@ -710,6 +710,10 @@ export class CrossClient { ): Promise { 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..." @@ -1244,6 +1248,71 @@ export class CrossClient { return txId; } + public async hasReferrerAccounts(): Promise { + // 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 { + 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 { this.delegatedCheck(); diff --git a/src/utils.ts b/src/utils.ts index bc94c8f8..c0a021e9 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -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 { + 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,