Created
January 29, 2022 08:48
-
-
Save ethanfrey/13033611a0b5ac5f319e0ab18964b86b to your computer and use it in GitHub Desktop.
CW20 IBC transfer
This file contains hidden or 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
#!/usr/bin/env node | |
/*jshint esversion: 8 */ | |
/* eslint-disable @typescript-eslint/naming-convention */ | |
const { toBase64, toUtf8 } = require('@cosmjs/encoding'); | |
const axios = require('axios'); | |
const { SigningCosmWasmClient } = require('@cosmjs/cosmwasm-stargate'); | |
const { GasPrice } = require('@cosmjs/stargate'); | |
const { DirectSecp256k1HdWallet } = require('@cosmjs/proto-signing'); | |
const config = { | |
endpoint: 'https://rpc-juno.itastakers.com:443', | |
bech32prefix: 'juno', | |
feeDenom: 'ujuno', | |
gasPrice: GasPrice.fromString('0.003ujuno'), | |
mnemonic: 'PUT YOUR MNEMONIC HERE', | |
}; | |
async function setup() { | |
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(config.mnemonic, { | |
prefix: config.bech32prefix, | |
}); | |
const { address } = (await wallet.getAccounts())[0]; | |
const options = { | |
prefix: config.bech32prefix, | |
gasPrice: config.gasPrice, | |
}; | |
const client = await SigningCosmWasmClient.connectWithSigner( | |
config.endpoint, | |
wallet, | |
options | |
); | |
// now ensure there is a balance | |
console.log(`Querying balance of ${address}`); | |
const {denom, amount} = await client.getBalance(address, config.feeDenom); | |
console.log(`Got ${amount} ${denom}`); | |
if (!amount || amount === "0") { | |
console.log('Please add tokens to your account before uploading') | |
} | |
return { address, client }; | |
} | |
const tokenAddr = "juno1y0y8rkzytel0htm86uknczrjeyzqzznuyqgs64fvk9k2zfnpw64qe7vmgj"; | |
const ics20Addr = "juno1v4887y83d6g28puzvt8cl0f3cdhd3y6y9mpysnsp3k8krdm7l6jqgm0rkn"; | |
const ibcPortId = "wasm.juno1v4887y83d6g28puzvt8cl0f3cdhd3y6y9mpysnsp3k8krdm7l6jqgm0rkn"; | |
const junoChannel = "channel-47"; | |
// TODO: put your osmosis address here | |
const osmosisAddr = "osmo1x95keq9l9y9p7hjdt6xd8wnknrpnjsqrtlxdss"; | |
async function main() { | |
const { address, client } = await setup(); | |
// send some native token over the channel | |
console.log(`IBC over ${ibcPortId} / ${junoChannel}`); | |
console.log(`Sending 0.017771 JUNO to ${osmosisAddr}`); | |
const receiveMsg = { | |
channel: junoChannel, | |
remote_address: osmosisAddr, | |
// give it 10 hours | |
timeout: 36000, | |
}; | |
await client.execute( | |
address, | |
ics20Addr, | |
{ transfer: receiveMsg }, | |
'auto', | |
'Send Native token via contract', | |
[{amount: "17771", denom: "ujuno"}], | |
); | |
// send some cw20 over the channel | |
console.log(`Sending 456.789 TEST to ${osmosisAddr}`); | |
const encoded = toBase64(toUtf8(JSON.stringify(receiveMsg))); | |
const sendMsg = { | |
send: { | |
contract: ics20Addr, | |
amount: '456789000', // leaving 123000.0000000 tokens | |
msg: encoded, | |
}, | |
}; | |
await client.execute( | |
address, | |
tokenAddr, | |
sendMsg, | |
'auto', | |
'Send CW20 tokens via ICS20' | |
); | |
} | |
main().then( | |
() => { | |
console.info('All done, let the coins flow.'); | |
process.exit(0); | |
}, | |
(error) => { | |
console.error(error); | |
process.exit(1); | |
} | |
); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment