Skip to content

Commit

Permalink
feat(pol): add support for matic/pol token uploads and top ups PE-6721
Browse files Browse the repository at this point in the history
  • Loading branch information
fedellen committed Sep 13, 2024
1 parent 37c11c0 commit 62ff2c8
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 5 deletions.
5 changes: 5 additions & 0 deletions src/cli/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,15 @@ export async function topUp(options: TopUpOptions) {
throw new Error('Must provide a wallet to top up');
}

console.log('privateKey', privateKey);
const turbo = TurboFactory.authenticated({
...config,
privateKey,
});
console.log(
'await turbo.signer.getNativeAddress()',
await turbo.signer.getNativeAddress(),
);
return turbo.createCheckoutSession({
amount,
owner: await turbo.signer.getNativeAddress(),
Expand Down
3 changes: 2 additions & 1 deletion src/cli/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,8 @@ const tokenToDevGatewayMap: Record<TokenType, string> = {
solana: 'https://api.devnet.solana.com',
ethereum: 'https://ethereum-holesky-rpc.publicnode.com',
kyve: 'https://api.korellia.kyve.network',
// matic: 'https://rpc-amoy.polygon.technology',
matic: 'https://rpc-amoy.polygon.technology',
pol: 'https://rpc-amoy.polygon.technology',
};

export function configFromOptions(
Expand Down
2 changes: 2 additions & 0 deletions src/common/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ export abstract class TurboBaseFactory {
uploadServiceConfig = {},
token,
}: TurboUnauthenticatedConfiguration = {}) {
token = token === 'pol' ? 'matic' : token;

const paymentService = new TurboUnauthenticatedPaymentService({
...paymentServiceConfig,
logger: this.logger,
Expand Down
2 changes: 2 additions & 0 deletions src/common/payment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,8 @@ export class TurboUnauthenticatedPaymentService
): Promise<TurboCheckoutSessionResponse> {
const { amount: paymentAmount, type: currencyType } = amount;

console.log('owner', owner);
console.log('this.token', this.token);
const endpoint = `/top-up/checkout-session/${owner}/${currencyType}/${paymentAmount}?uiMode=${uiMode}${
promoCodes.length > 0
? `&${this.appendPromoCodesToQuery(promoCodes)}`
Expand Down
2 changes: 2 additions & 0 deletions src/common/signer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export abstract class TurboDataItemAbstractSigner
return bs58.encode(fromB64Url(owner));

case 'ethereum':
case 'matic':
return computeAddress(computePublicKey(fromB64Url(owner)));

case 'kyve':
Expand Down Expand Up @@ -112,6 +113,7 @@ export abstract class TurboDataItemAbstractSigner
}

public async getNativeAddress(): Promise<NativeAddress> {
console.log('this.token', this.token);
return this.ownerToNativeAddress(
toB64Url(await this.getPublicKey()),
this.token,
Expand Down
7 changes: 6 additions & 1 deletion src/common/token/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,16 @@ export const defaultTokenMap: TokenFactory = {
kyve: (config: TokenConfig) => new KyveToken(config),
} as const;

export const tokenToBaseMap = {
export const tokenToBaseMap: Record<
TokenType,
(a: BigNumber.Value) => BigNumber.Value
> = {
arweave: (a: BigNumber.Value) => ARToTokenAmount(a),
solana: (a: BigNumber.Value) => SOLToTokenAmount(a),
ethereum: (a: BigNumber.Value) => ETHToTokenAmount(a),
kyve: (a: BigNumber.Value) => KYVEToTokenAmount(a),
matic: (a: BigNumber.Value) => ETHToTokenAmount(a),
pol: (a: BigNumber.Value) => ETHToTokenAmount(a),
} as const;

export function isTokenType(token: string): token is TokenType {
Expand Down
2 changes: 2 additions & 0 deletions src/node/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ export class TurboFactory extends TurboBaseFactory {
gatewayUrl,
tokenTools,
}: TurboAuthenticatedConfiguration) {
token = token === 'pol' ? 'matic' : token;

if (!token) {
if (providedSigner) {
// Derive token from signer if not provided
Expand Down
11 changes: 9 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,14 @@ export function isCurrency(currency: string): currency is Currency {

export type Country = 'United States' | 'United Kingdom' | 'Canada'; // TODO: add full list

export const tokenTypes = ['arweave', 'solana', 'ethereum', 'kyve'] as const;
export const tokenTypes = [
'arweave',
'solana',
'ethereum',
'kyve',
'matic',
'pol',
] as const;
export type TokenType = (typeof tokenTypes)[number];

export type Adjustment = {
Expand Down Expand Up @@ -332,7 +339,7 @@ export type TurboAuthenticatedPaymentServiceConfiguration =
export type TurboUnauthenticatedConfiguration = {
paymentServiceConfig?: TurboUnauthenticatedPaymentServiceConfiguration;
uploadServiceConfig?: TurboUnauthenticatedUploadServiceConfiguration;
token?: TokenType;
token?: TokenType | 'pol';
gatewayUrl?: string;
};

Expand Down
1 change: 1 addition & 0 deletions src/utils/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export function createTurboSigner({
case 'solana':
return new HexSolanaSigner(clientProvidedPrivateKey);
case 'ethereum':
case 'matic':
if (!isEthPrivateKey(clientProvidedPrivateKey)) {
throw new Error(
'A valid Ethereum private key must be provided for EthereumSigner.',
Expand Down
2 changes: 2 additions & 0 deletions src/web/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ export class TurboFactory extends TurboBaseFactory {
tokenMap,
tokenTools,
}: TurboAuthenticatedConfiguration) {
token = token === 'pol' ? 'matic' : token;

if (!token) {
if (providedSigner) {
// Derive token from signer if not provided
Expand Down
4 changes: 3 additions & 1 deletion tests/turbo.node.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,13 @@ describe('Node environment', () => {
});

describe('TurboDataItemSigner', () => {
const signers: Record<TokenType, [TurboSigner, NativeAddress]> = {
const signers: Record<string, [TurboSigner, NativeAddress]> = {
arweave: [new ArweaveSigner(testJwk), testArweaveNativeB64Address],
ethereum: [new EthereumSigner(testEthWallet), testEthNativeAddress],
solana: [new HexSolanaSigner(testSolWallet), testSolNativeAddress],
kyve: [new EthereumSigner(testKyvePrivatekey), testKyveNativeAddress],
matic: [new EthereumSigner(testEthWallet), testEthNativeAddress],
pol: [new EthereumSigner(testEthWallet), testEthNativeAddress],
};

for (const [token, [signer, expectedNativeAddress]] of Object.entries(
Expand Down

0 comments on commit 62ff2c8

Please sign in to comment.