Skip to content

Commit

Permalink
Add payer option to lazy dist
Browse files Browse the repository at this point in the history
  • Loading branch information
ChewingGlass committed Dec 8, 2023
1 parent 09ffa05 commit c99cd68
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 29 deletions.
73 changes: 44 additions & 29 deletions packages/distributor-oracle/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,14 @@ export async function formBulkTransactions({
program: lazyDistributorProgram,
rewardsOracleProgram,
heliumEntityManagerProgram,
skipOracleSign,
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,
Expand All @@ -194,14 +195,19 @@ export async function formBulkTransactions({
wallet?: PublicKey;
assetEndpoint?: string;
skipOracleSign?: boolean;
getAssetBatchFn?: (url: string, assetIds: PublicKey[]) => Promise<Asset[] | undefined>;
payer?: PublicKey;

getAssetBatchFn?: (
url: string,
assetIds: PublicKey[]
) => Promise<Asset[] | undefined>;
getAssetProofBatchFn?: (
url: string,
assetIds: PublicKey[]
) => Promise<Record<string, AssetProof> | 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;

Expand All @@ -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) => ({
Expand All @@ -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) {
Expand All @@ -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(),
];
Expand All @@ -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<HeliumEntityManager>["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<HeliumEntityManager>["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,
Expand Down Expand Up @@ -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],
Expand Down Expand Up @@ -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;
});
Expand Down Expand Up @@ -396,6 +407,7 @@ export async function formTransaction({
hotspot,
lazyDistributor,
wallet = provider.wallet.publicKey,
payer = provider.wallet.publicKey,
skipOracleSign = false,
assetEndpoint,
getAssetFn = getAsset,
Expand All @@ -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<Asset | undefined>;
Expand Down Expand Up @@ -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,
})
Expand Down Expand Up @@ -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 (
Expand All @@ -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);
Expand All @@ -523,6 +537,7 @@ export async function formTransaction({
.accounts({
// @ts-ignore
common: {
payer,
recipient,
lazyDistributor,
rewardsMint,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export async function distributeCompressionRewards<IDL extends Idl>({
assetId,
lazyDistributor,
rewardsMint,
payer,
...rest
}: {
program: Program<LazyDistributor>;
Expand All @@ -16,6 +17,7 @@ export async function distributeCompressionRewards<IDL extends Idl>({
assetEndpoint?: string;
lazyDistributor: PublicKey;
owner?: PublicKey;
payer?: PublicKey;
getAssetFn?: (url: string, assetId: PublicKey) => Promise<Asset | undefined>;
getAssetProofFn?: (
url: string,
Expand All @@ -42,6 +44,7 @@ export async function distributeCompressionRewards<IDL extends Idl>({
.accounts({
...accounts,
common: {
payer,
lazyDistributor,
rewardsMint,
owner,
Expand Down

0 comments on commit c99cd68

Please sign in to comment.