Skip to content

Commit

Permalink
Added Create Stream Endpoint (#139)
Browse files Browse the repository at this point in the history
* added new endpoint for creating Stream on supertokens
  • Loading branch information
vignesha22 authored Jul 4, 2022
1 parent 83d6041 commit f11df09
Showing 11 changed files with 113 additions and 51 deletions.
3 changes: 1 addition & 2 deletions examples/package.json
Original file line number Diff line number Diff line change
@@ -26,7 +26,6 @@
"18-cross-chain-quote": "../node_modules/.bin/ts-node ./src/18-cross-chain-quote.ts"
},
"dependencies": {
"dotenv": "^16.0.1",
"@superfluid-finance/sdk-core": "0.4.2"
"dotenv": "16.0.1"
}
}
51 changes: 8 additions & 43 deletions examples/src/17-superFluids-createStream.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Framework } from "@superfluid-finance/sdk-core";
import { ethers } from "ethers";
import { utils } from "ethers";
import { NetworkNames, Sdk, EnvNames } from "../../src";
import { logger } from "./common";

@@ -11,51 +10,19 @@ async function main(): Promise<void> {

try {

const provider = new ethers.providers.JsonRpcProvider('https://api.avax-test.network/ext/bc/C/rpc')
const sdk = new Sdk(
SENDER_PRIVATE_KEY,
{ networkName: NetworkNames.Fuji, env: EnvNames.TestNets },
); // sender wallet sdk instance

await sdk.computeContractAccount();
const senderAddress = sdk.state.accountAddress;
await sdk.computeContractAccount();

/*
** Initialise SuperFluid SDK with chainid and provider
** Make sure that the chainid is supported by SuperFluid
** You can verify the supported networks available in https://docs.superfluid.finance/superfluid/developers/networks
*/
const sf = await Framework.create({
chainId: 43113,
provider
});

// Get balance of Supertoken before streaming
const balance = await sf.query.listUserInteractedSuperTokens({account: sdk.state.accountAddress, token: superTokenAddress});
logger.log("balance: ", balance);

// Get Stream info
const flowInfo = await sf.cfaV1.getFlow({
sender: senderAddress,
const txnData = await sdk.createStreamTransactionPayload({
tokenAddress: superTokenAddress,
receiver: receiverAddress,
superToken: superTokenAddress,
providerOrSigner: provider
});
logger.log("flowInfo", flowInfo);

// Check if there are any existing stream present from sender to receiver on the desired superToken
if(flowInfo && flowInfo.flowRate === '0') {
// Create Stream using SuperFluid SDK
const createFlowOperation = sf.cfaV1.createFlow({
sender: senderAddress,
receiver: receiverAddress,
superToken: superTokenAddress,
flowRate: "100000000000" // Amount of tokens streamed per month(in wei)
});

// Get Transaction data using SuperFluid SDK
const txnData = await createFlowOperation.getPopulatedTransactionRequest(provider.getSigner(sdk.state.accountAddress));

amount: utils.parseEther('0.1')
})
if (!txnData.error && txnData.data && txnData.to) {
// Submit the Transaction data using Etherspot SDK
await sdk.clearGatewayBatch();
await sdk.batchExecuteAccountTransaction({
@@ -65,10 +32,8 @@ async function main(): Promise<void> {
logger.log('estimated batch', await sdk.estimateGatewayBatch());
logger.log('submitted batch', await sdk.submitGatewayBatch());
} else {
logger.log("Cannot create stream as the stream is already active", null);
logger.log('Error:', txnData.error);
}


} catch (Err){
logger.log("Caught Error: ", Err);
}
2 changes: 1 addition & 1 deletion examples/src/18-cross-chain-quote.ts
Original file line number Diff line number Diff line change
@@ -98,4 +98,4 @@ async function main(): Promise<void> {

main()
.catch(logger.error)
.finally(() => process.exit());
.finally(() => process.exit());
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "etherspot",
"version": "1.33.8",
"version": "1.33.9",
"description": "Etherspot SDK",
"keywords": [
"ether",
19 changes: 19 additions & 0 deletions src/sdk/dto/create-stream-payload.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { IsOptional } from 'class-validator';
import { BigNumber } from 'ethers';
import { IsAddress, IsBigNumberish } from './validators';

export class CreateStreamTransactionPayloadDto {
@IsOptional()
@IsAddress()
account?: string = null;

@IsAddress()
receiver: string;

@IsAddress()
tokenAddress: string;

@IsBigNumberish()
amount: BigNumber;

}
1 change: 1 addition & 0 deletions src/sdk/dto/index.ts
Original file line number Diff line number Diff line change
@@ -67,3 +67,4 @@ export * from './update-project.dto';
export * from './utils';
export * from './withdraw-p2p-payment-deposit.dto';
export * from './get-exchange-cross-chain-quote.dto';
export * from './create-stream-payload.dto';
33 changes: 32 additions & 1 deletion src/sdk/sdk.ts
Original file line number Diff line number Diff line change
@@ -99,6 +99,7 @@ import {
IsEligibleForAirdropDto,
GetCrossChainBridgeTokenListDto,
GetP2PPaymentChannelsAdminDto,
CreateStreamTransactionPayloadDto,
} from './dto';
import { ENSNode, ENSNodeStates, ENSRootNode, ENSService, parseENSName } from './ens';
import { Env, EnvNames } from './env';
@@ -145,7 +146,7 @@ import {
} from './payments';
import { CurrentProject, Project, Projects, ProjectService } from './project';
import { Session, SessionService } from './session';
import { Transactions, Transaction, TransactionsService, NftList } from './transactions';
import { Transactions, Transaction, TransactionsService, NftList, StreamTransactionPayload } from './transactions';
import { State, StateService } from './state';
import { WalletService, isWalletProvider, WalletProviderLike } from './wallet';

@@ -2123,6 +2124,36 @@ export class Sdk {
);
}

/**
* returns transaction payload for creating stream of supertoken
* @param dto
* @return Promise<StreamTransactionPayload>
*/

async createStreamTransactionPayload(dto: CreateStreamTransactionPayloadDto): Promise<StreamTransactionPayload> {
const { tokenAddress, receiver, amount, account } = await validateDto(
dto,
CreateStreamTransactionPayloadDto,
{
addressKeys: ['tokenAddress', 'receiver', 'account'],
},
);

await this.require({
session: true,
wallet: !account,
contractAccount: true,
});

return this.services.transactionsService.createStreamTransactionPayload(
this.prepareAccountAddress(account),
receiver,
BigNumber.from(amount),
tokenAddress
);

}

// utils

/**
1 change: 1 addition & 0 deletions src/sdk/transactions/classes/index.ts
Original file line number Diff line number Diff line change
@@ -10,3 +10,4 @@ export * from './transaction-data';
export * from './transaction-log';
export * from './transaction-log-decoded';
export * from './transactions';
export * from './stream-transaction-payload';
10 changes: 10 additions & 0 deletions src/sdk/transactions/classes/stream-transaction-payload.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { BaseClass } from '../../common';

export class StreamTransactionPayload extends BaseClass<StreamTransactionPayload> {
error: string;

data: string;

to: string;

}
38 changes: 37 additions & 1 deletion src/sdk/transactions/transactions.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { gql } from '@apollo/client/core';
import { BigNumber } from 'ethers';
import { Service } from '../common';
import { NftList, Transaction, Transactions } from './classes';
import { NftList, StreamTransactionPayload, Transaction, Transactions } from './classes';

export class TransactionsService extends Service {
async getTransaction(hash: string): Promise<Transaction> {
@@ -140,4 +141,39 @@ export class TransactionsService extends Service {

return result;
}

async createStreamTransactionPayload(
account: string,
receiver: string,
amount: BigNumber,
tokenAddress: string,
): Promise<StreamTransactionPayload> {
const { apiService } = this.services;

const { result } = await apiService.query<{
result: StreamTransactionPayload;
}>(
gql`
query($account: String!, $receiver: String!, $amount: BigNumber!, $tokenAddress: String!, $chainId: Int!) {
result: streamTransactionPayload(account: $account, receiver: $receiver, amount: $amount, tokenAddress: $tokenAddress, chainId: $chainId) {
data
to
error
}
}
`,
{
variables: {
account,
receiver,
amount,
tokenAddress
},
models: {
result: StreamTransactionPayload,
},
},
);
return result;
}
}

0 comments on commit f11df09

Please sign in to comment.