TradeFlows
  • TradeFlows
    • What is the Core Protocol?
    • Benefits of using TradeFlows
    • Innovations
    • Flows - Programmed Liquidity
    • Dharma - Collaborative Ratings
    • Smart Contracts
  • Guides
    • Getting Started
      • Assets
        • Minting
        • Agreeing
        • Composing
      • Payment Streams
        • Create
        • Pause
      • Escrows
        • Increase Collateral
        • Decrease Collateral
      • Ratings
      • Withdraw Funds
  • Use Cases
    • Web3
      • DAO Management
    • Web2
      • Building & Construction
      • Gig Economy
      • Small Business
  • Links
    • Website
    • Twitter
    • Discord
Powered by GitBook
On this page
  1. Guides
  2. Getting Started
  3. Payment Streams

Create

Programming the trading liquidity

After agreeing to the trade, buyer opens a set of payment streams as per the payment schedule of the agreement.

When creating a new payment stream, the buyer sets:

  • Total amount to be paid out in aggregate

  • Initial amount the buyer collateralises the stream with

  • Start date the stream is to start flowing

  • Maturity date of the stream, when the Total amount is to have been transferred

Upon creating the stream, the buyer must approve a transfer from their balance to the balance of the escrow contract. The approved amount must be the initial amount.

const target_amount = toUint256WithFelts((BigInt('10,000'.replace(/,/g, ''))).toString())
const initial_amount = toUint256WithFelts((BigInt('5,000'.replace(/,/g, ''))).toString())

await buyer.invoke(
    txFlowContract, 
    'approve', 
    { spender: txFlowContract.address, amount: initial_amount}, 
    { maxFee: FEE}
  )

The following code shows the stream creation process which is executed after the approval above:

const {block_number: block, block_timestamp: time} = await account0.call(txFlowContract, "state")

let block_timestamp = new Date(Number(time) * 1000)

let start_timestamp = new Date(block_timestamp)
let start_unixtime = Math.floor(start_timestamp.getTime() / 1000)

let maturity_timestamp = start_timestamp
maturity_timestamp.setMinutes(start_timestamp.getMinutes() + 35)
let maturity_unixtime = Math.floor(maturity_timestamp.getTime() / 1000)

let tokenId = toUint256WithFelts("0")

let txHash = await buyer.invoke(
      txFlowContract, 
      "addNFTMaturityStream", 
      { 
            beneficiary_address: txTradeContract.address, 
            beneficiary_tokenId: tokenId, 
            target_amount: target_amount, 
            initial_amount: initial_amount, 
            start: start_unixtime, 
            maturity: maturity_unixtime 
      }, 
      { maxFee: FEE}
    )

console.log('Tx: ', txHash)
let txReceipt = await starknet.getTransactionReceipt(txHash)
let flowId = parseInt(txReceipt['events'][0]['data'][0], 16)
console.log('Flow ID: ', flowId)

After the stream has been opened, the flowId in the code above (towards the end of the code block) is the stream identifier used to control it.

PreviousPayment StreamsNextPause

Last updated 2 years ago