-
Notifications
You must be signed in to change notification settings - Fork 374
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
120 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
var remote = "http://gno.land:36657", | ||
chainId = "testchain", | ||
walletFn = {keplr: {}}; | ||
|
||
walletFn.createVmCallMsg = function (sender, pkgPath, funcName, args) { | ||
return { | ||
type: "/vm.m_call", | ||
value: { | ||
caller: sender, | ||
send: "", | ||
pkg_path: pkgPath, | ||
func: funcName, | ||
args: args, | ||
} | ||
}; | ||
}; | ||
|
||
walletFn.createSignDoc = function(account, msg, chainId, gas) { | ||
return { | ||
msgs: [msg], | ||
fee: { amount: [{ | ||
amount: "1", | ||
denom: "gnot" | ||
}], gas: gas }, | ||
chain_id: chainId, | ||
memo: "", | ||
account_number: account.account_number, | ||
sequence: account.sequence, | ||
}; | ||
}; | ||
|
||
walletFn.keplr.signAndBroadcast = function(sender, msg) { | ||
return window.keplr.enable(chainId).then(function () { | ||
return walletFn.getAccount(sender); | ||
}) | ||
.then(function(account) { | ||
const signDoc = walletFn.createSignDoc(account, msg, chainId, "2000000"); | ||
return window.keplr.signAmino(chainId, sender, signDoc, { | ||
// use app fee (1gnot fixed fee) | ||
preferNoSetFee: true, | ||
}); | ||
}) | ||
.then(function (signature) { | ||
const tx = walletFn.makeStdTx(signature.signed, signature.signature); | ||
return walletFn.broadcastTx(tx); | ||
}); | ||
}; | ||
|
||
walletFn.makeStdTx = function(content, signature) { | ||
const feeAmount = content.fee.amount; | ||
|
||
return { | ||
msg: content.msgs.map(function (msg) { | ||
return { | ||
"@type": msg.type, | ||
...msg.value | ||
}; | ||
}), | ||
fee: { | ||
gas_wanted: content.fee.gas, | ||
gas_fee: feeAmount.length ? `${content.fee.amount[0].amount}${content.fee.amount[0].denom}`: "", | ||
}, | ||
signatures: [{ | ||
pub_key: { | ||
"@type": signature.pub_key.type, | ||
value: signature.pub_key.value, | ||
}, | ||
signature: signature.signature, | ||
}], | ||
memo: content.memo, | ||
}; | ||
}; | ||
|
||
walletFn.getAccount = function(address) { | ||
return walletFn.abciQuery(`auth/accounts/${address}`) | ||
.then(function (data) { | ||
const response = data.result.response.ResponseBase; | ||
if (response.Error) { | ||
throw new Error(response.Log); | ||
} | ||
|
||
const account = JSON.parse(atob(response.Data)); | ||
if (!account) { | ||
throw new Error("Account not found"); | ||
} | ||
|
||
return account.BaseAccount; | ||
}); | ||
}; | ||
|
||
walletFn.abciQuery = function(path) { | ||
return fetch(`${remote}/abci_query?path="${path}"`).then(function(response) { | ||
return response.json(); | ||
}); | ||
}; | ||
|
||
walletFn.broadcastTx = function(tx) { | ||
return fetch("/txs", { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json" | ||
}, | ||
body: JSON.stringify(tx) | ||
}).then(function (response) { | ||
return response.json(); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters