Skip to content

Commit

Permalink
chore: export erc20Amount
Browse files Browse the repository at this point in the history
  • Loading branch information
krzysu committed Jan 19, 2024
1 parent c2b05bd commit a7c8e1b
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 9 deletions.
8 changes: 8 additions & 0 deletions .changeset/modern-taxis-share.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@lens-protocol/api-bindings": patch
"@lens-protocol/react": patch
"@lens-protocol/react-native": patch
"@lens-protocol/react-web": patch
---

feat: Expose `erc20Amount` helper to make working with API Amounts easier
5 changes: 4 additions & 1 deletion examples/web/src/revenue/components/RevenueCard.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { RevenueAggregate } from '@lens-protocol/react-web';
import { RevenueAggregate, erc20Amount } from '@lens-protocol/react-web';

type RevenueCardProps = {
revenue: RevenueAggregate;
Expand All @@ -10,6 +10,9 @@ export function RevenueCard({ revenue }: RevenueCardProps) {
<p>{`Currency: ${revenue.total.asset.name}`}</p>
<p>{`Symbol: ${revenue.total.asset.symbol}`}</p>
<p>{`Amount: ${revenue.total.value}`}</p>
<p>{`Dollar value: ${erc20Amount(revenue.total)
.mul(revenue.total.rate?.value || 1)
.toSignificantDigits(2)} ${revenue.total.rate?.asset.symbol}`}</p>
</div>
);
}
2 changes: 1 addition & 1 deletion packages/api-bindings/src/lens/FollowModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export function resolveFollowPolicy({ followModule }: Profile): FollowPolicy {
case 'FeeFollowModuleSettings':
return {
type: FollowPolicyType.CHARGE,
amount: erc20Amount({ from: followModule.amount }),
amount: erc20Amount(followModule.amount),
recipient: followModule.recipient,
contractAddress: followModule.contract.address,
chainId: followModule.contract.chainId,
Expand Down
9 changes: 8 additions & 1 deletion packages/api-bindings/src/lens/utils/amount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@ export function chainType(chainId: number): ChainType {
}
}

export function erc20Amount({ from }: { from: ApiAmount }): Erc20Amount {
/**
* Convert an Amount returned from the Lens API to an Erc20Amount
*
* @param from - Amount returned from the Lens API
* @returns Erc20Amount instance
*/
export function erc20Amount(from: ApiAmount): Erc20Amount {
const { asset, value } = from;

const erc20Asset = erc20({
Expand All @@ -25,5 +31,6 @@ export function erc20Amount({ from }: { from: ApiAmount }): Erc20Amount {
address: asset.contract.address,
chainType: chainType(asset.contract.chainId),
});

return Amount.erc20(erc20Asset, value);
}
6 changes: 3 additions & 3 deletions packages/api-bindings/src/lens/utils/open-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ function resolveCollectRequestFor(
publicationId: collectable.id,
referrer: publication !== collectable ? publication.id : undefined,
fee: {
amount: erc20Amount({ from: settings.amount }),
amount: erc20Amount(settings.amount),
contractAddress: settings.contract.address,
},
public: false,
Expand All @@ -117,7 +117,7 @@ function resolveCollectRequestFor(
};

case 'SimpleCollectOpenActionSettings':
const amount = erc20Amount({ from: settings.amount });
const amount = erc20Amount(settings.amount);

return {
kind: TransactionKind.ACT_ON_PUBLICATION,
Expand All @@ -144,7 +144,7 @@ function resolveCollectRequestFor(
referrers:
context.action.referrers ?? (publication !== collectable ? [publication.id] : undefined),
fee: {
amount: erc20Amount({ from: settings.amount }),
amount: erc20Amount(settings.amount),
contractAddress: settings.contract.address,
},
public: context.public,
Expand Down
4 changes: 2 additions & 2 deletions packages/api-bindings/src/lens/utils/token-allowance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export function resolveTokenAllowanceRequestForCollect(
case 'SimpleCollectOpenActionSettings':
return {
kind: TransactionKind.APPROVE_MODULE,
amount: erc20Amount({ from: module.amount }),
amount: erc20Amount(module.amount),
limit,
spender: module.contract.address,
};
Expand All @@ -51,7 +51,7 @@ export function resolveTokenAllowanceRequestForFollow(
case 'FeeFollowModuleSettings':
return {
kind: TransactionKind.APPROVE_MODULE,
amount: erc20Amount({ from: profile.followModule.amount }),
amount: erc20Amount(profile.followModule.amount),
limit,
spender: profile.followModule.contract.address,
};
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,4 @@ export { UnspecifiedError } from '@lens-protocol/api-bindings';
*/
export * from './utils';
export { useSharedDependencies } from './shared';
export { isValidHandle, resolveFollowPolicy } from '@lens-protocol/api-bindings';
export { isValidHandle, resolveFollowPolicy, erc20Amount } from '@lens-protocol/api-bindings';

0 comments on commit a7c8e1b

Please sign in to comment.