-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.ts
91 lines (71 loc) · 2.36 KB
/
handler.ts
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
import { ethers, providers, utils, Wallet } from "ethers";
import { BadgeOfAssembly__factory } from "./badge-of-assembly-types/typechain";
import debug from 'debug'
const isTestnet = !!process.env.TESTNET
const TESTNET_BOA = "0x881256ada5dD7CcB2457226C4bC978B067daF70B";
const MAINNET_BOA = "0x2C6FD25071Fd516947682f710f6e9F5eD610207F";
export const BOA_ADDRESS = isTestnet ? TESTNET_BOA : MAINNET_BOA
const log = debug('faucet')
debug.enable('*')
if (!process.env.env_delphsPrivateKey) {
throw new Error("must have a DELPHS private key")
}
const rpcUrl = isTestnet ? 'https://staging-v2.skalenodes.com/v1/rapping-zuben-elakrab' : 'https://mainnet.skalenodes.com/v1/haunting-devoted-deneb'
const schainProvider = new ethers.providers.StaticJsonRpcProvider(rpcUrl)
const schainSigner = new Wallet(process.env.env_delphsPrivateKey).connect(schainProvider)
const badgeOfAssembly = BadgeOfAssembly__factory.connect(BOA_ADDRESS, schainSigner)
const highWaterForSFuel = utils.parseEther('1')
export async function handle(event:any, context:any, callback:any) {
const address = JSON.parse(event.body).address
// first get the balances
const [sfuelBalance, badgeTokens] = await Promise.all([
schainProvider.getBalance(address),
badgeOfAssembly.userTokens(address)
])
log(address, 'sfuel: ', utils.formatEther(sfuelBalance), 'badges: ', badgeTokens.length)
if (badgeTokens.length === 0) {
log(address, 'no badge')
return callback(null, {
statusCode: 400,
body: JSON.stringify({
message: 'requires badge',
input: event,
}),
})
}
if (sfuelBalance.gte(highWaterForSFuel)) {
log(address, 'balance is high enough not to mint')
return callback(null, {
statusCode: 200,
body: JSON.stringify({
message: 'you have enough',
input: event,
}),
})
}
let tx:providers.TransactionResponse
log('sending sfuel')
try {
tx = await schainSigner.sendTransaction({
to: address,
value: highWaterForSFuel,
})
} catch (err) {
console.error('error: ', err)
return callback(null, {
statusCode: 500,
body: JSON.stringify({
message: err.toString(),
input: event,
}),
})
}
log(address, 'tx submitted: ', tx.hash)
return callback(null, {
statusCode: 201,
body: JSON.stringify({
message: 'ok',
transactionId: tx.hash,
}),
})
}