Skip to content

Commit 1975b26

Browse files
committed
feat(call contract example): add new example of CFX Faucet
1 parent 28c0a66 commit 1975b26

File tree

2 files changed

+195
-36
lines changed

2 files changed

+195
-36
lines changed

example/basic-dapp/index.html

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,20 @@
7272
Result: <span id="send_native_token_result">N/A</span>
7373
</footer>
7474
</article>
75+
<article>
76+
<header>
77+
合约调用示例
78+
</header>
79+
<form>
80+
<label>通过Conflux水龙头获取测试CFX</label>
81+
</form>
82+
<button id="get-cfx" disabled=
83+
"true">获取1k CFX
84+
</button>
85+
<footer>
86+
Result: <span id="get_cfx_result">N/A</span>
87+
</footer>
88+
</article>
7589
<article>
7690
<header>
7791
Approve (cUSDT)
@@ -109,14 +123,6 @@
109123
Result: <span id="deploy_contract_result">N/A</span>
110124
</footer>
111125
</article>
112-
<article>
113-
<header>
114-
Add Token
115-
</header>
116-
<button id="add_token" disabled="true">Add
117-
Token
118-
</button>
119-
</article>
120126
<article>
121127
<header>
122128
Add Network

example/basic-dapp/index.js

Lines changed: 181 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
// https://github.com/Conflux-Chain/js-conflux-sdk#readme
44
import { Conflux, Drip } from 'https://cdn.skypack.dev/js-conflux-sdk'
55

6+
const cusdtAddress = 'cfxtest:acepe88unk7fvs18436178up33hb4zkuf62a9dk1gv'
7+
const confluxFaucetAddress =
8+
'cfxtest:acejjfa80vj06j2jgtz9pngkv423fhkuxj786kjr61'
69
const exampleContract = new Conflux().Contract({
710
abi: [
811
{
@@ -300,8 +303,160 @@ const exampleContract = new Conflux().Contract({
300303
bytecode:
301304
'0x60806040523480156200001157600080fd5b50604051620010ca380380620010ca833981810160405260808110156200003757600080fd5b81019080805160405193929190846401000000008211156200005857600080fd5b838201915060208201858111156200006f57600080fd5b82518660018202830111640100000000821117156200008d57600080fd5b8083526020830192505050908051906020019080838360005b83811015620000c3578082015181840152602081019050620000a6565b50505050905090810190601f168015620000f15780820380516001836020036101000a031916815260200191505b5060405260200180519060200190929190805160405193929190846401000000008211156200011f57600080fd5b838201915060208201858111156200013657600080fd5b82518660018202830111640100000000821117156200015457600080fd5b8083526020830192505050908051906020019080838360005b838110156200018a5780820151818401526020810190506200016d565b50505050905090810190601f168015620001b85780820380516001836020036101000a031916815260200191505b50604052602001805190602001909291905050508360009080519060200190620001e49291906200026e565b5082600160006101000a81548160ff021916908360ff1602179055508160029080519060200190620002189291906200026e565b508060038190555080600460003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002081905550505050506200031d565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f10620002b157805160ff1916838001178555620002e2565b82800160010185558215620002e2579182015b82811115620002e1578251825591602001919060010190620002c4565b5b509050620002f19190620002f5565b5090565b6200031a91905b8082111562000316576000816000905550600101620002fc565b5090565b90565b610d9d806200032d6000396000f3fe608060405234801561001057600080fd5b50600436106100a95760003560e01c8063313ce56711610071578063313ce567146102935780635c658165146102b757806370a082311461032f57806395d89b4114610387578063a9059cbb1461040a578063dd62ed3e14610470576100a9565b806306fdde03146100ae578063095ea7b31461013157806318160ddd1461019757806323b872dd146101b557806327e235e31461023b575b600080fd5b6100b66104e8565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156100f65780820151818401526020810190506100db565b50505050905090810190601f1680156101235780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b61017d6004803603604081101561014757600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff16906020019092919080359060200190929190505050610586565b604051808215151515815260200191505060405180910390f35b61019f610678565b6040518082815260200191505060405180910390f35b610221600480360360608110156101cb57600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190803573ffffffffffffffffffffffffffffffffffffffff1690602001909291908035906020019092919050505061067e565b604051808215151515815260200191505060405180910390f35b61027d6004803603602081101561025157600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff1690602001909291905050506109e9565b6040518082815260200191505060405180910390f35b61029b610a01565b604051808260ff1660ff16815260200191505060405180910390f35b610319600480360360408110156102cd57600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190803573ffffffffffffffffffffffffffffffffffffffff169060200190929190505050610a14565b6040518082815260200191505060405180910390f35b6103716004803603602081101561034557600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190505050610a39565b6040518082815260200191505060405180910390f35b61038f610a82565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156103cf5780820151818401526020810190506103b4565b50505050905090810190601f1680156103fc5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b6104566004803603604081101561042057600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff16906020019092919080359060200190929190505050610b20565b604051808215151515815260200191505060405180910390f35b6104d26004803603604081101561048657600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190803573ffffffffffffffffffffffffffffffffffffffff169060200190929190505050610ce0565b6040518082815260200191505060405180910390f35b60008054600181600116156101000203166002900480601f01602080910402602001604051908101604052809291908181526020018280546001816001161561010002031660029004801561057e5780601f106105535761010080835404028352916020019161057e565b820191906000526020600020905b81548152906001019060200180831161056157829003601f168201915b505050505081565b600081600560003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020819055508273ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff167f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925846040518082815260200191505060405180910390a36001905092915050565b60035481565b600080600560008673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054905082600460008773ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205410156107b5576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260138152602001807f62616c616e636573206e6f7420656e6f7567680000000000000000000000000081525060200191505060405180910390fd5b8281101561082b576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260148152602001807f616c6c6f77616e6365206e6f7420656e6f75676800000000000000000000000081525060200191505060405180910390fd5b82600460008673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000828254019250508190555082600460008773ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600082825403925050819055507fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8110156109785782600560008773ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600082825403925050819055505b8373ffffffffffffffffffffffffffffffffffffffff168573ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef856040518082815260200191505060405180910390a360019150509392505050565b60046020528060005260406000206000915090505481565b600160009054906101000a900460ff1681565b6005602052816000526040600020602052806000526040600020600091509150505481565b6000600460008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020549050919050565b60028054600181600116156101000203166002900480601f016020809104026020016040519081016040528092919081815260200182805460018160011615610100020316600290048015610b185780601f10610aed57610100808354040283529160200191610b18565b820191906000526020600020905b815481529060010190602001808311610afb57829003601f168201915b505050505081565b600081600460003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020541015610bd7576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260138152602001807f62616c616e636573206e6f7420656e6f7567680000000000000000000000000081525060200191505060405180910390fd5b81600460003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000828254039250508190555081600460008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600082825401925050819055508273ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef846040518082815260200191505060405180910390a36001905092915050565b6000600560008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205490509291505056fea2646970667358221220ba3544f95832f9a6f264be220893ae1c951498e955e1908563105ee727d890c064736f6c634300060a0033',
302305
})
303-
304-
const cusdtAddress = 'cfxtest:acepe88unk7fvs18436178up33hb4zkuf62a9dk1gv'
306+
const confluxFaucetContract = new Conflux().Contract({
307+
abi: [
308+
{ inputs: [], stateMutability: 'nonpayable', type: 'constructor' },
309+
{
310+
inputs: [
311+
{
312+
internalType: 'address',
313+
name: 'newManager',
314+
type: 'address',
315+
},
316+
],
317+
name: 'changeManager',
318+
outputs: [],
319+
stateMutability: 'nonpayable',
320+
type: 'function',
321+
},
322+
{
323+
inputs: [],
324+
name: 'claimCfx',
325+
outputs: [],
326+
stateMutability: 'nonpayable',
327+
type: 'function',
328+
},
329+
{
330+
inputs: [],
331+
name: 'claimCfxRegardingSenderBalance',
332+
outputs: [],
333+
stateMutability: 'nonpayable',
334+
type: 'function',
335+
},
336+
{
337+
inputs: [
338+
{
339+
internalType: 'address',
340+
name: 'tokenContractAddress',
341+
type: 'address',
342+
},
343+
],
344+
name: 'claimToken',
345+
outputs: [],
346+
stateMutability: 'nonpayable',
347+
type: 'function',
348+
},
349+
{
350+
inputs: [
351+
{
352+
internalType: 'address',
353+
name: 'tokenContractAddress',
354+
type: 'address',
355+
},
356+
],
357+
name: 'claimTokenRegardingSenderBalance',
358+
outputs: [],
359+
stateMutability: 'nonpayable',
360+
type: 'function',
361+
},
362+
{
363+
inputs: [],
364+
name: 'defaultAmount',
365+
outputs: [{ internalType: 'uint256', name: '', type: 'uint256' }],
366+
stateMutability: 'view',
367+
type: 'function',
368+
},
369+
{
370+
inputs: [],
371+
name: 'defaultInterval',
372+
outputs: [{ internalType: 'uint256', name: '', type: 'uint256' }],
373+
stateMutability: 'view',
374+
type: 'function',
375+
},
376+
{
377+
inputs: [
378+
{
379+
internalType: 'address',
380+
name: 'tokenContractAddress',
381+
type: 'address',
382+
},
383+
],
384+
name: 'getClaimAmount',
385+
outputs: [{ internalType: 'uint256', name: '', type: 'uint256' }],
386+
stateMutability: 'view',
387+
type: 'function',
388+
},
389+
{
390+
inputs: [
391+
{
392+
internalType: 'address',
393+
name: 'tokenContractAddress',
394+
type: 'address',
395+
},
396+
],
397+
name: 'getClaimInterval',
398+
outputs: [{ internalType: 'uint256', name: '', type: 'uint256' }],
399+
stateMutability: 'view',
400+
type: 'function',
401+
},
402+
{
403+
inputs: [
404+
{
405+
internalType: 'address[]',
406+
name: 'tokenList',
407+
type: 'address[]',
408+
},
409+
],
410+
name: 'retrieveToken',
411+
outputs: [],
412+
stateMutability: 'nonpayable',
413+
type: 'function',
414+
},
415+
{
416+
inputs: [
417+
{
418+
internalType: 'address',
419+
name: 'tokenContractAddress',
420+
type: 'address',
421+
},
422+
{ internalType: 'uint256', name: 'interval', type: 'uint256' },
423+
{ internalType: 'uint256', name: 'amount', type: 'uint256' },
424+
],
425+
name: 'setClaimSetting',
426+
outputs: [],
427+
stateMutability: 'nonpayable',
428+
type: 'function',
429+
},
430+
{
431+
inputs: [
432+
{
433+
internalType: 'uint256',
434+
name: 'amount',
435+
type: 'uint256',
436+
},
437+
],
438+
name: 'setDefaultAmount',
439+
outputs: [],
440+
stateMutability: 'nonpayable',
441+
type: 'function',
442+
},
443+
{
444+
inputs: [
445+
{
446+
internalType: 'uint256',
447+
name: 'intervalSeconds',
448+
type: 'uint256',
449+
},
450+
],
451+
name: 'setDefaultIntervalSeconds',
452+
outputs: [],
453+
stateMutability: 'nonpayable',
454+
type: 'function',
455+
},
456+
{ stateMutability: 'payable', type: 'receive' },
457+
],
458+
address: confluxFaucetAddress,
459+
})
305460

306461
// 初始化钱包
307462
window.conflux = new window.AnyWeb.Provider({
@@ -323,6 +478,7 @@ async function walletInitialized() {
323478
const sendNativeTokenButton = getElement('send_native_token')
324479
const approveButton = getElement('approve')
325480
const transferFromButton = getElement('transfer_from')
481+
const getCFXButton = getElement('get-cfx')
326482
const nativeReceiverAddressInput = getElement('native-receiver')
327483
const countInput = getElement('native-count')
328484
const approveAccountInput = getElement('approve-account')
@@ -331,7 +487,6 @@ async function walletInitialized() {
331487

332488
const addNetworkButton = getElement('add_network')
333489
const switchNetworkButton = getElement('switch_network')
334-
const addTokenButton = getElement('add_token')
335490
const deployContract = getElement('deploy_contract')
336491

337492
function authed(address) {
@@ -342,8 +497,8 @@ async function walletInitialized() {
342497
transferFromButton.disabled = false
343498
addNetworkButton.disabled = false
344499
switchNetworkButton.disabled = false
345-
addTokenButton.disabled = false
346500
deployContract.disabled = false
501+
getCFXButton.disabled = false
347502
}
348503

349504
function unAuthed() {
@@ -352,11 +507,9 @@ async function walletInitialized() {
352507
sendNativeTokenButton.disabled = true
353508
approveButton.disabled = true
354509
transferFromButton.disabled = true
355-
personalSignButton.disabled = true
356-
typedSignButton.disabled = true
510+
getCFXButton.disabled = true
357511
addNetworkButton.disabled = true
358512
switchNetworkButton.disabled = true
359-
addTokenButton.disabled = true
360513
deployContract.disabled = true
361514
}
362515

@@ -425,6 +578,26 @@ async function walletInitialized() {
425578
})
426579
.catch((error) => console.error('error', error.message || error))
427580
}
581+
getCFXButton.onclick = async () => {
582+
try {
583+
const [connectedAddress] = await provider.request({
584+
method: 'cfx_accounts',
585+
})
586+
const tx = {
587+
from: connectedAddress,
588+
to: confluxFaucetAddress,
589+
data: confluxFaucetContract.claimCfx().data,
590+
}
591+
console.log('getCFX tx', tx)
592+
provider
593+
.request({ method: 'cfx_sendTransaction', params: [tx] })
594+
.then((result) => {
595+
console.log('result', result)
596+
})
597+
} catch (err) {
598+
console.log('err', err)
599+
}
600+
}
428601
// approve spender
429602
approveButton.onclick = async () => {
430603
try {
@@ -528,33 +701,13 @@ async function walletInitialized() {
528701
provider
529702
.request({ method: 'cfx_sendTransaction', params: [tx] })
530703
.then((result) => {
531-
getElement('deploy_contract_result').innerHTML = result
704+
getElement('get_cfx_result').innerHTML = result
532705
console.log('result', result)
533706
})
534707
} catch (err) {
535708
console.log('err', err)
536709
}
537710
}
538-
539-
// request to add token
540-
addTokenButton.onclick = () => {
541-
provider
542-
.request({
543-
method: 'wallet_watchAsset',
544-
params: {
545-
type: 'ERC20',
546-
options: {
547-
address: 'cfxtest:acepe88unk7fvs18436178up33hb4zkuf62a9dk1gv',
548-
symbol: 'cUSDT',
549-
decimals: 18,
550-
image:
551-
'https://scan-icons.oss-cn-hongkong.aliyuncs.com/testnet/cfxtest%3Aacepe88unk7fvs18436178up33hb4zkuf62a9dk1gv.png',
552-
},
553-
},
554-
})
555-
.then(console.log)
556-
.catch(console.log)
557-
}
558711
}
559712

560713
window.addEventListener('load', async () => {

0 commit comments

Comments
 (0)