-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathcontract-advanced.html
117 lines (102 loc) · 4.57 KB
/
contract-advanced.html
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Call Contract Methods using NEAR</title>
<script src="/lib/near-api-js.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lib/bn.min.js"></script>
</head>
<body>
<nav>
<div><a href="index.html">home</a></div>
<div><a href="login.html">login</a></div>
<div><a href="contract.html">contract</a></div>
</nav>
<hr />
<button id="hello">helloWorld</button>
<br />
<button id="read">read</button> <input type="text" id="read-key" placeholder="key" />
<br />
<button id="write">write</button> <input type="text" id="write-key" placeholder="key" /> <input type="text" id="write-value" placeholder="value" />
<script>
(async () => {
const { connect, keyStores, WalletConnection } = nearApi;
const CONTRACT_ID = 'dev-1634098284641-40067785396400';
// const gas = new BN('1000'); // uncomment this line to see an error re: "Exceeded the prepaid gas"
const near = await connect(config());
const wallet = new WalletConnection(near, 'ncd-ii');
const dom = setupDOMBindings();
const contract = {
helloWorld: async () => await view(CONTRACT_ID, 'helloWorld'),
readKey: async () => await view(CONTRACT_ID, 'read', { key: dom.txtReadKey.value }),
writeKey: async () => await change(CONTRACT_ID, 'write', { key: dom.txtWriteKey.value, value: dom.txtWriteValue.value }, typeof gas !== 'undefined' ? gas : null)
};
if (wallet.isSignedIn()) {
const accountId = wallet.getAccountId();
dom.btnHello.addEventListener('click', contract.helloWorld);
dom.btnRead.addEventListener('click', contract.readKey);
dom.btnWrite.addEventListener('click', contract.writeKey);
}
async function change(contract, method, args, gas, amount) {
console.log('attempting to send signed transaction ...');
const response = await wallet.account().functionCall(contract, method, args, gas, amount);
console.log('response received.');
const { transaction_outcome: txo, status } = response;
console.log(`gas burned: ${txo.outcome.gas_burnt}`);
console.log(`tokens burned: ${txo.outcome.tokens_burnt}`);
console.log(`logs: \n${txo.outcome.logs}`);
const { SuccessValue: value } = status;
console.log(b64DecodeUnicode(value)); // see helper functions below
}
async function view(contract, method, args) {
const response = await wallet.account().viewFunction(contract, method, args);
console.log(response);
}
function config() {
return {
networkId: 'testnet',
keyStore: new keyStores.BrowserLocalStorageKeyStore(),
nodeUrl: 'https://rpc.testnet.near.org',
walletUrl: 'https://wallet.testnet.near.org',
helperUrl: 'https://helper.testnet.near.org',
explorerUrl: 'https://explorer.testnet.near.org'
};
}
function setupDOMBindings() {
return {
btnHello: document.querySelector('#hello'),
btnRead: document.querySelector('#read'),
txtReadKey: document.querySelector('#read-key'),
btnWrite: document.querySelector('#write'),
txtWriteKey: document.querySelector('#write-key'),
txtWriteValue: document.querySelector('#write-value')
};
}
// ------------------------
// see here for decoding simple ASCII response https://stackoverflow.com/q/33854103/2836874
// see here for decoding UTF-8 data in response (eg. if you want emoji support) https://stackoverflow.com/a/30106551/2836874
// ------------------------
// Encoding UTF8 ⇢ base64
function b64EncodeUnicode(str) {
return btoa(
encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, function (match, p1) {
return String.fromCharCode(parseInt(p1, 16));
})
);
}
// Decoding base64 ⇢ UTF8
function b64DecodeUnicode(str) {
return decodeURIComponent(
Array.prototype.map
.call(atob(str), function (c) {
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
})
.join('')
);
}
})();
</script>
</body>
</html>