Skip to content
This repository has been archived by the owner on Sep 1, 2021. It is now read-only.

Commit

Permalink
feature/otp-via-sdk (#20)
Browse files Browse the repository at this point in the history
* Remove axios and querystring & import EIP712 from module

* Refactor axiosify

* Update integration with serverside Node.js apps

* feature/otp-via-sdk

* implement API methods
  • Loading branch information
lopezjurip committed Nov 7, 2019
1 parent f9b0eee commit 7ea0eb4
Show file tree
Hide file tree
Showing 15 changed files with 2,123 additions and 1,736 deletions.
8 changes: 6 additions & 2 deletions README.md
Expand Up @@ -2,10 +2,10 @@

## Install

> You may also require some extra dependencies like `axios`, `web3`, `eventemitter3`, and `querystring`.
> You may also require some extra dependencies like `web3`, and `eventemitter3`
```nocode
yarn add @daisypayments/daisy-sdk axios eventemitter3 querystring web3@1.0.0-beta.37
yarn add @daisypayments/daisy-sdk eventemitter3 web3@1.0.0-beta.37
```

## MetaMask helper
Expand Down Expand Up @@ -36,13 +36,17 @@ It is extremely important to keep `DAISY_SECRET_KEY` **private**.

```js
const { ServiceSubscriptions } = require("@daisypayments/daisy-sdk/private");
const fetch = require("node-fetch");

ServiceSubscriptions.fetch = fetch;
const subscriptionService = new ServiceSubscriptions({
identifier: process.env.DAISY_ID,
secretKey: process.env.DAISY_SECRET_KEY,
});
```

> Server instances requires an assignation of `fetch`. We recommend [node-fetch](https://www.npmjs.com/package/node-fetch).
Create and endpoint to retrieve information from your servers back to the frontend.
Here is an example using Express.js:

Expand Down
83 changes: 83 additions & 0 deletions browser/DaisyPayments.js
@@ -0,0 +1,83 @@
import ClientPayments from "../common/ClientPayments";

import ERC20 from "../contracts/lite/ERC20.json";

export default class DaisyPayments {
get web3() {
return this.withGlobals.web3 || window.web3;
}

constructor({ manager, override, withGlobals }) {
this.manager = manager;
this.withGlobals = withGlobals;
this.override = override;
this.client = ClientPayments.init(manager, override, withGlobals);
}

with(withGlobals) {
return new DaisyPayments({
manager: this.manager,
override: this.override,
withGlobals: { ...this.withGlobals, ...withGlobals },
});
}

sync() {
return this.client
.request({
method: "get",
url: "/payments",
})
.then(({ data: body }) => {
this.manager = { ...this.manager, ...body["data"] };
return this;
});
}

loadToken({ symbol, address } = {}) {
if (address) {
return new this.web3.eth.Contract(ERC20["abi"], address);
} else if (symbol) {
throw new Error("Not implemented yet");
} else {
return new this.web3.eth.Contract(
ERC20["abi"],
this.manager["tokenAddress"]
);
}
}

prepareToken(token) {
return new DaisyPaymentsOnToken({ manager: this.manager, token });
}
}

export class DaisyPaymentsOnToken {
constructor({ manager, token }) {
this.manager = manager;
this.token = token;
}

balanceOf(sendArgs) {
if (!sendArgs.tokenOwner) {
throw new Error(`balanceOf() was called without a tokenOwner specified. Be sure to call balanceOf() like:
daisy
.prepareToken(token)
.balanceOf({ tokenOwner: account })
`);
}
return this.token.methods["balanceOf"](sendArgs.tokenOwner).call();
}

/**
* Transfer token to an address "predicted" by create2.
* @async
*/
pay(invoice, sendArgs) {
const price = invoice["invoicedPrice"];
const address = invoice["address"];
return this.token.methods["transfer"](address, price).send(sendArgs);
}
}

0 comments on commit 7ea0eb4

Please sign in to comment.