-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathsign-and-submit-tx.jsx
95 lines (88 loc) · 3.46 KB
/
sign-and-submit-tx.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import { identity } from "@deso-core/identity";
import { useContext } from "react";
import { UserContext } from "../contexts";
// FIXME: Import this from @deso-core/identity instead of re-defining it.
const NO_MONEY_ERROR = 'User does not have sufficient funds in their wallet to complete the transaction';
export const SignAndSubmitTx = () => {
const { currentUser: user } = useContext(UserContext);
const usernameOrPubkey =
user?.ProfileEntryResponse?.Username ?? user?.PublicKeyBase58Check;
let hasPostingPermissions = identity.hasPermissions({
TransactionCountLimitMap: {
SUBMIT_POST: 1,
},
})
if (!usernameOrPubkey || !user.BalanceNanos || !hasPostingPermissions) {
return (
<button onClick={() => {
identity.login({
getFreeDeso: true,
}).then((res)=>{
alert('Login success!')
}).catch((err)=>{
if (err?.toString().indexOf(NO_MONEY_ERROR) >= 0) {
alert('You need DESO in order to post!')
} else {
alert(err)
}
})
}}>Login to create a post</button>
);
} else {
return (
<>
<h1>Submit a signed post transaction</h1>
<form
onSubmit={(e) => {
e.preventDefault();
// check if the user can make a post
if (!hasPostingPermissions) {
// if the user doesn't have permissions, request them
identity.requestPermissions({
GlobalDESOLimit: 10000000, // 0.01 DESO
TransactionCountLimitMap: {
SUBMIT_POST: 3,
},
}).then((res)=>{
console.log(res)
debugger;
});
return;
}
const body = e.target[0].value;
const createTx = () =>
fetch("https://node.deso.org/api/v0/submit-post", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
UpdaterPublicKeyBase58Check: user?.PublicKeyBase58Check,
BodyObj: {
Body: body,
ImageURLs: [],
VideoURLs: [],
},
MinFeeRateNanosPerKB: 1000,
}),
}).then((res) => res.json());
identity
.signAndSubmitTx(createTx)
.then(() => alert("Post submitted successfully!"))
.catch((e) =>
alert(`Error submitting post. Please try again. ${e.toString()}`)
);
}}
>
<textarea
name="post-textarea"
cols={30}
rows={10}
style={{ border: "1px solid black" }}
></textarea>
<div>
<button>Post</button>
</div>
</form>
</>
);
}
};