Code Monkey home page Code Monkey logo

openst-payments's Introduction

OpenST Payments - Advance Payment infrastructure on top of the OpenST network

Latest version Travis Downloads per month Gitter: JOIN CHAT

While OpenST 0.9 is available as-is for anyone to use, we caution that this is early stage software and under heavy ongoing development and improvement. Please report bugs and suggested improvements.

Install OpenST Payments

npm install @openstfoundation/openst-payments --save

Init DynamoDB

Following commands need to be run once, only for the first time. DynamoDB initial migrations for shard management are run in it and one shard is created and registered for storing token balance.

. ./mocha_test/scripts/set_env_vars.sh
node tools/dynamo_db_init.js

Run Test Chain

cd mocha_test/scripts/
sh start_test_chain.sh

Set EVN Variables

Setup Initial Setup Variables:

export OST_UTILITY_GETH_RPC_PROVIDER=''
export OST_UTILITY_GETH_WS_PROVIDER=''
export OST_UTILITY_DEPLOYER_ADDR=''
export OST_UTILITY_DEPLOYER_PASSPHRASE=''
export OST_UTILITY_OPS_ADDR=''
export OST_UTILITY_OPS_PASSPHRASE=''

Deploy Branded Token Contract:

node tools/deploy/EIP20TokenMock.js conversionRate symbol name decimals gasPrice

Deploy Workers Contract:

node tools/deploy/workers.js gasPrice chainId

Deploy Airdrop Contract:

node tools/deploy/airdrop.js brandedTokenContractAddress baseCurrency workerContractAddress airdropBudgetHolder gasPrice chainId

Set Caching Engine:

export OST_CACHING_ENGINE='none'
For using redis/memcache as cache engine refer - [OpenSTFoundation/ost-price-oracle](https://github.com/OpenSTFoundation/ost-price-oracle)

Set DB Details For Payments/Airdrop:

export OP_MYSQL_HOST=''
export OP_MYSQL_USER=''
export OP_MYSQL_PASSWORD=''
export OP_MYSQL_DATABASE=''
export OP_MYSQL_CONNECTION_POOL_SIZE='5'

Create Airdrop Tables:

node migrations/create_tables.js 

Deploy Service Examples:

const OpenSTPayment = require('@openstfoundation/openst-payments')
  , Deploy = OpenSTPayment.services.deploy
;  
  // Deploy Workers
  const deployWorkerObject = new Deploy.workers({
    gas_price: gasPrice,
    options: {returnType: 'txHash'}
  });
  deployWorkerObject.perform();
  
  // Deploy Airdrop
  const deployAirdropObject = new Deploy.airdrop({
    branded_token_contract_address: brandedTokenAddress,
    base_currency: baseCurrency,
    worker_contract_address: workerContractAddress,
    airdrop_budget_holder: airdropBudgetHolder,
    gas_price: gasPrice,
    options: {returnType: 'txHash'}
  });
  deployAirdropObject.perform();
  

OpsManaged Service Examples

const OpenSTPayment = require('@openstfoundation/openst-payments')
  , OpsManaged = OpenSTPayment.services.opsManaged
;  
  // Set Ops Address
  const setOpsObject = new OpsManaged.setOps({
    contract_address: contractAddress,
    gas_price: gasPrice,
    chain_id: chainId,
    deployer_address: deployerAddress,
    deployer_passphrase: deployerPassphrase,
    ops_address: opsAddress,
    options: {returnType: 'txHash'}
  });
  setOpsObject.perform();
    
  // Get Ops Address
  const getOpsObject = new OpsManaged.getOps({
    contract_address: contractAddress,
    gas_price: gasPrice,
    chain_id: chainId
  });
  getOpsObject.perform();

Workers Service Examples

const OpenSTPayment = require('@openstfoundation/openst-payments')
  , Workers = OpenSTPayment.services.workers
;  
  // Set Worker
  const setWorkerObject = new Workers.setWorker({
      workers_contract_address: constants.workersContractAddress,
      sender_address: constants.ops,
      sender_passphrase: constants.opsPassphrase,
      worker_address: workerAddress,
      deactivation_height: deactivationHeight.toString(10),
      gas_price: gasPrice,
      chain_id: chainId,
      options: {returnType: 'txHash'}
  });
  setWorkerObject.perform();
  
  // Is Worker
  const isWorkerObject = new Workers.isWorker({
      workers_contract_address: workersContractAddress,
      worker_address: workerAddress,
      chain_id: chainId
  });
  isWorkerObject.perform();

Airdrop Management Service Examples:

const OpenSTPayment = require('@openstfoundation/openst-payments')
  , AirdropManager = OpenSTPayment.services.airdropManager
;  
  // Register Airdrop
  const registerObject = new AirdropManager.register({
    airdrop_contract_address: airdropContractAddress,
    chain_id: chainId
  });
  registerObject.perform();
  
  // Set Price Oracle
  const setPriceOracleObject = new AirdropManager.setPriceOracle({
      airdrop_contract_address: airdropContractAddress,
      chain_id: chainId,
      sender_address: senderAddress,
      sender_passphrase: senderPassphrase,
      currency: currency,
      price_oracle_contract_address: priceOracleContractAddress,
      gas_price: gasPrice,
      options: {tag: 'airdrop.setPriceOracle', returnType: 'txHash'}
  });
  setPriceOracleObject.perform();
  
  // Set Accepted Margin
  const setAcceptedMarginObject = new AirdropManager.setAcceptedMargin({
    airdrop_contract_address: airdropContractAddress,
    chain_id: chainId,
    sender_address: senderAddress,
    sender_passphrase: senderPassphrase,
    currency: currency,
    accepted_margin: acceptedMargin,
    gas_price: gasPrice,
    options: {tag: 'airdrop.setAcceptedMargin', returnType: 'txHash'}
  });
  setAcceptedMarginObject.perform();
  
  // Transfer Amount to airdrop budget holder
  const transferObject = new AirdropManager.transfer({
    sender_address: senderAddress,
    sender_passphrase: senderPassphrase,
    airdrop_contract_address: airdropContractAddress,
    amount: airdropBudgetAmountInWei,
    gas_price: gasPrice,
    chain_id: chainId,
    options: {tag: 'airdrop.transfer', returnType: 'txHash'}
  });
  transferObject.perform();
  
  // Approve airdrop budget holder
  const approveObject = new AirdropManager.approve({
    airdrop_contract_address: airdropContractAddress,
    airdrop_budget_holder_passphrase: airdropBudgetHolderPassphrase,
    gas_price: gasPrice,
    chain_id: chainId,
    options: {tag: 'airdrop.approve', returnType: 'txHash'}
  });
  approveObject.perform();
  
  // Allocate airdrop amount to users in batch
  const batchAllocatorObject = new AirdropManager.batchAllocator({
    airdrop_contract_address: airdropContractAddress,
    transaction_hash: transactionHash,
    airdrop_users: {userAddress1: {airdropAmount: amountInWei, expiryTimestamp: 0}, userAddress2: {airdropAmount: amountInWei, expiryTimestamp: 0}},
    chain_id: chainId
  });
  batchAllocatorObject.perform();
  
  // Get Users Airdrop Balance
  const userBalanceObject = new AirdropManager.userBalance({
    airdrop_contract_address: airdropContractAddress,
    chain_id: chainId,
    user_addresses: [user1, user2]
  });
  userBalanceObject.perform();
  
  // Call Pay method
  const payObject = new AirdropManager.pay({
    airdrop_contract_address: airdropContractAddress,
    chain_id: chainId,
    sender_worker_address: workerAddress,
    sender_worker_passphrase: workerPassphrase,
    beneficiary_address: beneficiary,
    transfer_amount: transferAmount.toString(10),
    commission_beneficiary_address: commissionBeneficiary,
    commission_amount: commissionAmount.toString(10),
    currency: currency,
    intended_price_point: intendedPricePoint,
    spender: spenderAddress,
    gas_price: gasPrice,
    options: {tag:'airdrop.pay', returnType: 'txHash'}
  });
  payObject.perform()
  

For further implementation details, please refer to the API documentation.

openst-payments's People

Contributors

benjaminbollen avatar bitsnacker avatar jasonklein avatar kedarchandrayan avatar puneet-khushwani-eth avatar sunilkhedar avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

openst-payments's Issues

Setup - Contracts

Add basic (or expected) elements smart contract-related elements for this repo:

  • license for contracts
  • interfaces
  • Truffle/test utilities

Relates to #1.

Introduce workers in Pricer:pay

Introduce workers in Pricer:pay

workers were introduced for Airdrop as a necessity, but they form a crucial building block to start horizontally scaling the number of geth nodes as workers can then be sharded over the geth nodes running;

this aligns the interaction layer for pay and airdropPay with the workers paradigm

Update notification data.

Update notification data.

  • use transaction_error instead if error (in catch)
  • add publisher in notification data
  • change notification topic to payments.workers.* ( add 's' in worker)
  • change notification topic to payments.. ( add 's' in payment)

Move Pricer:CalculateAmounts to the interaction layer

  1. Move Pricer:CalculateAmounts to the interaction layer
  2. get Price point can be achieved from getPricePoint and this function can be extended to return both the PricePoint of OST/curr and the (constant BrandedTokenConversionRate)

Airdrop/Pricer - Refactor, rebase, and harden

Refactor Pricer to minimize code duplication for Airdrop, rebase airdrop work on develop resolve conflicts, and harden the logic.

Changes:

  • consider adding a remove to Airdrop
  • leave/do not overwrite Pricer.pay
  • airdropBudget is not a contract, but an address controlled by a key (presumably the reserve address)

Pricer - specification

Pricer - specification

  • write and review specification in docs/pricer.md (2hr)
  • Pricer.sol interface (2hr)
  • write PriceOracle interface (2hr)
  • write PriceOracleMock.sol (2hr)
  • write tests for Pricer.sol (12hr)
  • implement Pricer.sol (6hr)
  • implement interaction layer in openst-platform (6hr)
  • code review (4hr)

Estimate: 36hr

PriceOracleInterface - corrections

  • Correct license to reflect that OpenST holds the copyright
  • move up one level from contracts/ost-price-oracle to /contracts from /contracts/ost-price-oracle
  • make other attendant changes

Workers - Refactor Truffle tests

Review and refactor Truffle tests to remove unnecessary duplication/testing:

Ex., in is_worker.js, the before hook includes:

assert.equal(await workers.isWorker.call(worker1Address), true);
...
assert.equal(await workers.isWorker.call(worker2Address), true);

And the actual tests include:

it('returns true when worker was set and deactivationHeight is not reached', async () => {

    assert.equal(await workers.isWorker.call(worker1Address), true);
    assert.equal(await workers.isWorker.call(worker2Address), true);

});

Airdrop Manager

  • setup airdrop
  • Transfer to Budget Holder
  • Approve to budget Holder
  • allocate airdrop amount to users

Pricer - add decimals, conversionRate, baseCurrency

Add private state vars pricerDecimals, pricerConversionRate, pricerBaseCurrency and:

  • add _baseCurrency to and set pricerBaseCurrency in constructor
  • set pricerDecimals, pricerConversionRate, and pricerBaseCurrency in the constructor (obtain them from calling the functions on branded token).
  • add public view getters decimals, conversionRate, and baseCurrency
  • update functions that call the branded token for conversion rate to use the state var instead.

Update interface accordingly.

Add status check in trasaction receipt in perform send

Contract interact helper perform send right now just checks if the transaction receipt is received. For the failed transactions we get the receipt with status 0. For such case the respose should be with error. Right now it returns as a success.

PriceOracle - Specification

PriceOracle - specification

  • setup OST.PriceOracle (4hr)
  • write and review specification in docs/priceOracle.md (2hr)
  • write tests for PriceOracle.sol (12hr)
  • implement PriceOracle.sol (imports PriceOracle.sol interface) (6hr)
  • implement interaction layer (6hr)
  • code review (4hr)

Estimate: 34hr

Setup openst-payments repository

Setup openst-payments repository:

  • root structure
  • readme
  • dual license : apache v2.0 for contracts, LGPLv3.0 for npm modules
  • CI

Estimate: 4hr

Payment Caching and Dependencies

  • brandedToken method cache
  • airdropBudgetHolder cache
  • airdrop amount caching in airdrop.pay method
  • Update user_airdrop_details.airdrop_used column based on when airdrop amount is used

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.