From 5a81decce5367367db5766078a0af45d799334ec Mon Sep 17 00:00:00 2001 From: Chewing Glass Date: Fri, 8 Dec 2023 13:33:45 -0600 Subject: [PATCH] Add payer option to lazy dist --- packages/distributor-oracle/src/client.ts | 73 +++++++++++-------- .../functions/distributeCompressionRewards.ts | 3 + 2 files changed, 47 insertions(+), 29 deletions(-) diff --git a/packages/distributor-oracle/src/client.ts b/packages/distributor-oracle/src/client.ts index 36c00d76f..8317eef09 100644 --- a/packages/distributor-oracle/src/client.ts +++ b/packages/distributor-oracle/src/client.ts @@ -172,13 +172,14 @@ export async function formBulkTransactions({ program: lazyDistributorProgram, rewardsOracleProgram, heliumEntityManagerProgram, + skipOracleSign = false, rewards, assets, compressionAssetAccs, lazyDistributor, lazyDistributorAcc, wallet = (lazyDistributorProgram.provider as AnchorProvider).wallet.publicKey, - skipOracleSign = false, + payer = (lazyDistributorProgram.provider as AnchorProvider).wallet.publicKey, assetEndpoint, getAssetBatchFn = getAssetBatch, getAssetProofBatchFn = getAssetProofBatch, @@ -194,14 +195,19 @@ export async function formBulkTransactions({ wallet?: PublicKey; assetEndpoint?: string; skipOracleSign?: boolean; - getAssetBatchFn?: (url: string, assetIds: PublicKey[]) => Promise; + payer?: PublicKey; + + getAssetBatchFn?: ( + url: string, + assetIds: PublicKey[] + ) => Promise; getAssetProofBatchFn?: ( url: string, assetIds: PublicKey[] ) => Promise | undefined>; }) { if (assets.length > 100) { - throw new Error('Too many assets, max 100'); + throw new Error("Too many assets, max 100"); } const provider = lazyDistributorProgram.provider as AnchorProvider; @@ -222,17 +228,17 @@ export async function formBulkTransactions({ compressionAssetAccs = await getAssetBatchFn( assetEndpoint || provider.connection.rpcEndpoint, assets - ) + ); } if (compressionAssetAccs?.length != assets.length) { - throw new Error('Assets not the same length as compressionAssetAccs'); + throw new Error("Assets not the same length as compressionAssetAccs"); } let recipientKeys = assets.map( (asset) => recipientKey(lazyDistributor, asset)[0] ); const cache = await getSingleton( - heliumEntityManagerProgram.provider.connection + heliumEntityManagerProgram!.provider.connection ); const recipientAccs = ( await cache.searchMultiple(recipientKeys, (pubkey, account) => ({ @@ -246,7 +252,7 @@ export async function formBulkTransactions({ const assetProofsById = await getAssetProofBatchFn( assetEndpoint || provider.connection.rpcEndpoint, assets - ) + ); let ixsPerAsset = await Promise.all( recipientAccs.map(async (recipientAcc, idx) => { if (!recipientAcc) { @@ -258,9 +264,14 @@ export async function formBulkTransactions({ lazyDistributor, assetEndpoint, owner: wallet, - payer: wallet, + payer, getAssetFn: () => Promise.resolve(compressionAssetAccs![idx]), // cache result so we don't hit again - getAssetProofFn: assetProofsById ? () => Promise.resolve(assetProofsById[compressionAssetAccs![idx].id.toBase58()]) : undefined, + getAssetProofFn: assetProofsById + ? () => + Promise.resolve( + assetProofsById[compressionAssetAccs![idx].id.toBase58()] + ) + : undefined, }) ).instruction(), ]; @@ -270,27 +281,27 @@ export async function formBulkTransactions({ ); const keyToAssetKs = compressionAssetAccs.map((assetAcc, idx) => { - return keyToAssetForAsset(assetAcc) - }) - const keyToAssets = await cache.searchMultiple( - keyToAssetKs, - (pubkey, account) => ({ - pubkey, - account, - info: heliumEntityManagerProgram!.coder.accounts.decode< - IdlAccounts["keyToAssetV0"] - >("KeyToAssetV0", account.data), - }), - true, - false - ); + return keyToAssetForAsset(assetAcc); + }); + const keyToAssets = await cache.searchMultiple( + keyToAssetKs, + (pubkey, account) => ({ + pubkey, + account, + info: heliumEntityManagerProgram!.coder.accounts.decode< + IdlAccounts["keyToAssetV0"] + >("KeyToAssetV0", account.data), + }), + true, + false + ); // construct the set and distribute ixs let setAndDistributeIxs = await Promise.all( compressionAssetAccs.map(async (assetAcc, idx) => { const keyToAssetK = keyToAssets[idx]?.pubkey; - const keyToAsset = keyToAssets[idx]?.info + const keyToAsset = keyToAssets[idx]?.info; if (!keyToAsset || !keyToAssetK) { - return [] + return []; } const entityKey = decodeEntityKey( keyToAsset.entityKey, @@ -320,7 +331,7 @@ export async function formBulkTransactions({ if (setRewardIxs.length == 0) { return []; } - const distributeIx = await( + const distributeIx = await ( await distributeCompressionRewards({ program: lazyDistributorProgram, assetId: assets![idx], @@ -355,7 +366,7 @@ export async function formBulkTransactions({ let initialTxs = ixsPerAsset.map((ixs) => { const tx = new Transaction(); tx.recentBlockhash = blockhash; - tx.feePayer = wallet; + tx.feePayer = payer; tx.add(...ixs); return tx; }); @@ -396,6 +407,7 @@ export async function formTransaction({ hotspot, lazyDistributor, wallet = provider.wallet.publicKey, + payer = provider.wallet.publicKey, skipOracleSign = false, assetEndpoint, getAssetFn = getAsset, @@ -410,6 +422,7 @@ export async function formTransaction({ hotspot?: PublicKey; lazyDistributor: PublicKey; wallet?: PublicKey; + payer?: PublicKey; assetEndpoint?: string; skipOracleSign?: boolean; getAssetFn?: (url: string, assetId: PublicKey) => Promise; @@ -464,7 +477,7 @@ export async function formTransaction({ lazyDistributor, assetEndpoint, owner: wallet, - payer: wallet, + payer, getAssetFn: () => Promise.resolve(assetAcc), // cache result so we don't hit again getAssetProofFn, }) @@ -503,7 +516,7 @@ export async function formTransaction({ await provider.connection.getLatestBlockhash() ).blockhash; - tx.feePayer = wallet ? wallet : provider.wallet.publicKey; + tx.feePayer = payer ? payer : provider.wallet.publicKey; if (assetAcc.compression.compressed) { const distributeIx = await ( @@ -514,6 +527,7 @@ export async function formTransaction({ getAssetFn: () => Promise.resolve(assetAcc), // cache result so we don't hit again getAssetProofFn, assetEndpoint, + payer, }) ).instruction(); tx.add(distributeIx); @@ -523,6 +537,7 @@ export async function formTransaction({ .accounts({ // @ts-ignore common: { + payer, recipient, lazyDistributor, rewardsMint, diff --git a/packages/lazy-distributor-sdk/src/functions/distributeCompressionRewards.ts b/packages/lazy-distributor-sdk/src/functions/distributeCompressionRewards.ts index e3da9c2a0..b87f7accc 100644 --- a/packages/lazy-distributor-sdk/src/functions/distributeCompressionRewards.ts +++ b/packages/lazy-distributor-sdk/src/functions/distributeCompressionRewards.ts @@ -8,6 +8,7 @@ export async function distributeCompressionRewards({ assetId, lazyDistributor, rewardsMint, + payer, ...rest }: { program: Program; @@ -16,6 +17,7 @@ export async function distributeCompressionRewards({ assetEndpoint?: string; lazyDistributor: PublicKey; owner?: PublicKey; + payer?: PublicKey; getAssetFn?: (url: string, assetId: PublicKey) => Promise; getAssetProofFn?: ( url: string, @@ -42,6 +44,7 @@ export async function distributeCompressionRewards({ .accounts({ ...accounts, common: { + payer, lazyDistributor, rewardsMint, owner,