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

feature: withdraw functionality #32

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"ngrok": "3.1.1"
},
"devDependencies": {
"@types/coinstring": "^2.3.0",
iourisorokine marked this conversation as resolved.
Show resolved Hide resolved
"@types/dotenv": "6.1.0",
"husky": "1.3.1",
"lint-staged": "8.0.3",
Expand Down
2 changes: 1 addition & 1 deletion src/commands/help.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const help = async (context: Context) => {

- If you want to know your balance, send a DM to @cooldoge with "balance" as message
- If you want to know your deposit more dogecoin, send a DM to @cooldoge with "deposit" as message
- If you want to know your withdraw address to withdraw your dogecoin to your private wallet, send a DM to @cooldoge with "withdraw" as message
- If you want to withdraw your dogecoin to your private wallet, send a DM to @cooldoge with "withdraw address amount" as message

For more nerdiness here:
https://github.com/cool-bots/doge-cool`
Expand Down
65 changes: 61 additions & 4 deletions src/commands/withdraw.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,66 @@
import help from './help';
import * as utils from '../lib/utils';
import { getAddressByLabel } from '../integrations/blockIo';
import { Context } from '../types/bottender';

const withdraw = async (context: Context) => {
// TODO get user address
// TODO withdraw to address
await context.sendText('Ehehehe no way!!');
export const withdraw = async (context: Context, blockIo: any) => {
const fromLabel = context.session.user.id;
const fromAddress = await getAddressByLabel(fromLabel);
const availableBalance = Number(fromAddress.data.available_balance);

const [, toAddress, amount] = context.event.text.split(' ');
if (!toAddress || !amount || isNaN(+amount)) {
help(context);
return;
}

if (availableBalance < +amount) {
iourisorokine marked this conversation as resolved.
Show resolved Hide resolved
await context.sendText(
`Looks like this amount is above your balance - you only have ${availableBalance} doge!`
);
return;
}

if (+amount < 2) {
await context.sendText(
`Such low amount! Much too low to cover fees! ${utils.generateWow()}`
);
return;
}

const isValid = /[AD9]{1}[5-9A-HJ-NP-U]{1}[1-9A-HJ-NP-Za-km-z]{32}/.test(
toAddress
);
if (!isValid) {
await context.sendText(
`Oh no, much typo in your address. It can't be ${toAddress} ?!`
);
return;
}

try {
await new Promise((resolve, reject) => {
blockIo.withdraw_from_labels({
from_labels: fromLabel,
to_addresses: toAddress,
amounts: amount,
pin: process.env.BLOCK_IO_SECRET_PIN,
});
(error: any, res: any) => {
if (error) {
return reject(error);
}
context.sendText('Successful withdrawal');
SarahHouben marked this conversation as resolved.
Show resolved Hide resolved
resolve();
};
});
await context.sendText(
`${utils.generateCongrats()} Such rich! Much withdrawal!`
);
return;
} catch (error) {
await context.sendText(error.message);
}
};

export default withdraw;
1 change: 0 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ const createAddresses = CreateAddresses(slackClient, blockIo);
bot.onEvent(async (context: Context) => {
let command;
let commandText;

// Public / Private channels
if (context.event.isChannelsMessage || context.event.isGroupsMessage) {
// Unless @cooldoge is mentioned, don't react
Expand Down
46 changes: 46 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@
url-join "^4.0.0"
ws "^4.1.0"

"@types/coinstring@^2.3.0":
version "2.3.0"
resolved "https://registry.yarnpkg.com/@types/coinstring/-/coinstring-2.3.0.tgz#1c3e71aa8f98520d8528e1021348db793e8b451f"
integrity sha1-HD5xqo+YUg2FKOECE0jbeT6LRR8=
dependencies:
"@types/node" "*"

"@types/delay@^2.0.1":
version "2.0.1"
resolved "https://registry.yarnpkg.com/@types/delay/-/delay-2.0.1.tgz#61bcf318a74b61e79d1658fbf054f984c90ef901"
Expand Down Expand Up @@ -553,6 +560,11 @@ bcrypt-pbkdf@^1.0.0:
dependencies:
tweetnacl "^0.14.3"

[email protected]:
version "0.2.0"
resolved "https://registry.yarnpkg.com/bigi/-/bigi-0.2.0.tgz#8bee26348b99c4ae2ed20481fb12384c32792f74"
integrity sha1-i+4mNIuZxK4u0gSB+xI4TDJ5L3Q=

bigi@=1.4.2, bigi@^1.1.0, bigi@^1.4.0:
version "1.4.2"
resolved "https://registry.yarnpkg.com/bigi/-/bigi-1.4.2.tgz#9c665a95f88b8b08fc05cfd731f561859d725825"
Expand All @@ -569,6 +581,11 @@ binary@^0.3.0:
buffers "~0.1.1"
chainsaw "~0.1.0"

[email protected], binstring@~0.2.0:
version "0.2.1"
resolved "https://registry.yarnpkg.com/binstring/-/binstring-0.2.1.tgz#8a174d301f6d54efda550dd98bb4cb524eacd75d"
integrity sha1-ihdNMB9tVO/aVQ3Zi7TLUk6s110=

bitcoinjs-lib@=1.5.6:
version "1.5.6"
resolved "https://registry.yarnpkg.com/bitcoinjs-lib/-/bitcoinjs-lib-1.5.6.tgz#c628826441a1e58298b7b50ac4d4996231286209"
Expand Down Expand Up @@ -705,6 +722,14 @@ braces@^2.3.1, braces@^2.3.2:
split-string "^3.0.2"
to-regex "^3.0.1"

[email protected]:
version "0.3.0"
resolved "https://registry.yarnpkg.com/bs58/-/bs58-0.3.0.tgz#cb48107bf446727d3e17b21102da73ca89109588"
integrity sha1-y0gQe/RGcn0+F7IRAtpzyokQlYg=
dependencies:
bigi "0.2.0"
binstring "~0.2.0"

bs58@^3.1.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/bs58/-/bs58-3.1.0.tgz#d4c26388bf4804cac714141b1945aa47e5eb248e"
Expand Down Expand Up @@ -999,6 +1024,14 @@ code-point-at@^1.0.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/code-point-at/-/code-point-at-1.1.0.tgz#0d070b4d043a5bea33a2f1a40e2edb3d9a4ccf77"

coinstring@^0.2.0:
version "0.2.0"
resolved "https://registry.yarnpkg.com/coinstring/-/coinstring-0.2.0.tgz#fa2820497bb9e35b7cfa116f048219ca6f3f348f"
integrity sha1-+iggSXu541t8+hFvBIIZym8/NI8=
dependencies:
bs58 "0.3.x"
crypto-hashing "~0.3.0"

collection-visit@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/collection-visit/-/collection-visit-1.0.0.tgz#4bc0373c164bc3291b4d368c829cf1a80a59dca0"
Expand Down Expand Up @@ -1156,6 +1189,14 @@ cross-spawn@^6.0.0, cross-spawn@^6.0.3:
shebang-command "^1.2.0"
which "^1.2.9"

crypto-hashing@~0.3.0:
version "0.3.1"
resolved "https://registry.yarnpkg.com/crypto-hashing/-/crypto-hashing-0.3.1.tgz#0195548db8bdef50aa9d526514cc546e1e62fbce"
integrity sha1-AZVUjbi971CqnVJlFMxUbh5i+84=
dependencies:
binstring "0.2.x"
ripemd160 "~0.2.0"

crypto-random-string@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/crypto-random-string/-/crypto-random-string-1.0.0.tgz#a230f64f568310e1498009940790ec99545bca7e"
Expand Down Expand Up @@ -4235,6 +4276,11 @@ ripemd160@^2.0.0, ripemd160@^2.0.1:
hash-base "^3.0.0"
inherits "^2.0.1"

ripemd160@~0.2.0:
version "0.2.1"
resolved "https://registry.yarnpkg.com/ripemd160/-/ripemd160-0.2.1.tgz#dee19248a3e1c815ff9aea39e753a337f56a243d"
integrity sha1-3uGSSKPhyBX/muo551OjN/VqJD0=

run-async@^2.2.0:
version "2.3.0"
resolved "https://registry.yarnpkg.com/run-async/-/run-async-2.3.0.tgz#0371ab4ae0bdd720d4166d7dfda64ff7a445a6c0"
Expand Down