Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MVP for crypto-fund #356

Open
wants to merge 11 commits into
base: dev
Choose a base branch
from
1,050 changes: 1,038 additions & 12 deletions .pnp.cjs

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,8 @@
"typescript.tsdk": ".yarn/sdks/typescript/lib",
"typescript.enablePromptUseWorkspaceTsdk": true,
"eslint.nodePath": ".yarn/sdks",
"prettier.prettierPath": ".yarn/sdks/prettier/index.js"
"prettier.prettierPath": ".yarn/sdks/prettier/index.js",
"cSpell.words": [
"solana"
]
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
},
"types": "./lib/index.d.ts",
"dependencies": {
"@ardrive/turbo-sdk": "1.9.0",
"ardrive-core-js": "2.0.3",
"arweave": "1.11.4",
"axios": "^0.21.1",
"bn.js": "^5.2.1",
"bs58": "^5.0.0",
"commander": "^8.2.0",
"ipfs-only-hash": "^4.0.0",
"lodash": "^4.17.21",
Expand Down
4 changes: 2 additions & 2 deletions src/CLICommand/parameters_helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
DryRunParameter,
MetaDataFileParameter,
MetaDataGqlTagsParameter,
MetadataJsonParameter,
TokenTypeParameter,
DataGqlTagsParameter,
TurboUrlParameter
} from '../parameter_declarations';
Expand Down Expand Up @@ -347,7 +347,7 @@ export class ParametersHelper {
Object.assign(customMetaData, { dataGqlTags });
}

const metaDataJson = this.getParameterValue<string>(MetadataJsonParameter);
const metaDataJson = this.getParameterValue<string>(TokenTypeParameter);

if (metaDataJson) {
Object.assign(customMetaData, { metaDataJson: this.parseMetaDataJson(metaDataJson) });
Expand Down
85 changes: 85 additions & 0 deletions src/commands/crypto_fund.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { AR, JWKWallet } from 'ardrive-core-js';
import { CLICommand } from '../CLICommand';
import { ParametersHelper } from '../CLICommand';
import { CLIAction } from '../CLICommand/action';
import { SUCCESS_EXIT_CODE } from '../CLICommand/error_codes';
import {
CryptoAmountParameter,
BoostParameter,
DryRunParameter,
GatewayParameter,
TokenTypeParameter,
TransactionIdParameter,
WalletTypeParameters
} from '../parameter_declarations';
import {
TurboFactory,
ARToTokenAmount,
ETHToTokenAmount,
SOLToTokenAmount,
tokenTypes,
TokenType
} from '@ardrive/turbo-sdk';
import bs58 from 'bs58';

function isTokenType(tokenType: string): tokenType is TokenType {
return tokenTypes.includes(tokenType as TokenType);
}

new CLICommand({
name: 'crypto-fund',
parameters: [
CryptoAmountParameter,
BoostParameter,
DryRunParameter,
TokenTypeParameter,
...WalletTypeParameters,
GatewayParameter,
TransactionIdParameter
],
action: new CLIAction(async function action(options) {
const parameters = new ParametersHelper(options);

const tokenType = parameters.getParameterValue(TokenTypeParameter) ?? 'arweave';
if (!isTokenType(tokenType)) {
// TODO: Could be handled in param helper `getTokenType`
throw new Error(`Invalid token type: ${tokenType}`);
}

const transactionId = parameters.getParameterValue(TransactionIdParameter);
if (transactionId) {
const turbo = TurboFactory.unauthenticated({
paymentServiceConfig: { token: tokenType }
});

const res = await turbo.submitFundTransaction({
txId: transactionId
});
console.log('res', JSON.stringify(res, null, 2));
return SUCCESS_EXIT_CODE;
}

const cryptoAmount = parameters.getRequiredParameterValue(CryptoAmountParameter, AR.from);
const jwkWallet = (await parameters.getRequiredWallet()) as JWKWallet;

// TODO: These conversions could be done by convenience in the Turbo SDK
const tokenConversions = {
arweave: { token: ARToTokenAmount, wallet: () => jwkWallet['jwk'] },
ethereum: { token: ETHToTokenAmount, wallet: () => jwkWallet['jwk'] },
solana: { token: SOLToTokenAmount, wallet: () => bs58.encode(jwkWallet['jwk']) }
};

const turbo = TurboFactory.authenticated({
token: tokenType,
privateKey: tokenConversions[tokenType].wallet()
});

const res = await turbo.topUpWithTokens({
tokenAmount: tokenConversions[tokenType].token(cryptoAmount.valueOf())
});

console.log('res', JSON.stringify(res, null, 2));

return SUCCESS_EXIT_CODE;
})
});
1 change: 1 addition & 0 deletions src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import './create_drive';
import './create_folder';
import './create_manifest';
import './create_tx';
import './crypto_fund';
import './download_drive';
import './download_file';
import './download_folder';
Expand Down
22 changes: 18 additions & 4 deletions src/parameter_declarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export const DriveKeyParameter = 'driveKey';
export const AddressParameter = 'address';
export const DriveIdParameter = 'driveId';
export const ArAmountParameter = 'arAmount';
export const CryptoAmountParameter = 'cryptoAmount';
export const RewardParameter = 'reward';
export const LastTxParameter = 'lastTx';
export const TxFilePathParameter = 'txFilePath';
Expand Down Expand Up @@ -49,6 +50,7 @@ export const DataGqlTagsParameter = 'dataGqlTags';
export const MetaDataFileParameter = 'metadataFile';
export const MetaDataGqlTagsParameter = 'metadataGqlTags';
export const MetadataJsonParameter = 'metadataJson';
export const TokenTypeParameter = 'token';

// Aggregates for convenience
export const CustomMetaDataParameters = [
Expand All @@ -65,6 +67,7 @@ export const AllParameters = [
AddressParameter,
AllParameter,
ArAmountParameter,
CryptoAmountParameter,
TurboUrlParameter,
BoostParameter,
ConfirmationsParameter,
Expand Down Expand Up @@ -202,8 +205,14 @@ Parameter.declare({
name: ArAmountParameter,
aliases: ['-a', '--ar-amount'],
description: `amount of AR to send to the --dest-address
\t\t\t\t\t\t\t• does NOT include transaction mining base rewards`,
required: true
\t\t\t\t\t\t\t• does NOT include transaction mining base rewards`
});

Parameter.declare({
name: CryptoAmountParameter,
aliases: ['-a', '--crypto-amount'],
description: `amount of crypto tokens to send in the transaction
\t\t\t\t\t\t\t• does NOT include transaction mining base rewards`
});

Parameter.declare({
Expand Down Expand Up @@ -237,8 +246,7 @@ Parameter.declare({
Parameter.declare({
name: TransactionIdParameter,
aliases: ['-t', '--tx-id'],
description: 'The transaction id to check the status of in the mempool',
required: true
description: 'The transaction id to check the status of in the mempool'
});

Parameter.declare({
Expand Down Expand Up @@ -496,6 +504,12 @@ Parameter.declare({
description: `(OPTIONAL) a 'protocol://host:port' formatted string specifying the connection info for the Arweave gateway server to use`
});

Parameter.declare({
name: TokenTypeParameter,
aliases: ['-t', '--token'],
description: `(OPTIONAL) token type for this command`
});

Parameter.declare({
name: TurboUrlParameter,
aliases: ['--turbo-url'],
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true
"noFallthroughCasesInSwitch": true,
"skipLibCheck": true
},
"include": ["src", "tests"]
}
Loading
Loading