Skip to content

Commit

Permalink
Support Terra 2.0 (#285)
Browse files Browse the repository at this point in the history
* build: bump version to 3.1.0

Co-authored-by: Geoff Lee <geoff@terra.money>
Co-authored-by: sim <sim@terra.money>
Co-authored-by: JSHan94 <saw1515@postech.ac.kr>
Co-authored-by: Jungsu Han <saw151515@gmail.com>
  • Loading branch information
5 people committed May 26, 2022
1 parent 2eac366 commit 870e74d
Show file tree
Hide file tree
Showing 216 changed files with 38,285 additions and 6,299 deletions.
32 changes: 19 additions & 13 deletions README.md
Expand Up @@ -54,14 +54,22 @@ npm install @terra-money/terra.js
Terra.js can be used in Node.js, as well as inside the browser. Please check the [docs](https://docs.terra.money/docs/develop/sdks/terra-js/README.html) for notes on how to get up and running.

### Getting blockchain data

:exclamation: terra.js can connect both terra-classic and terra network. If you want to communicate with classic chain, you have to set isClassic as `true`.
```ts
import { LCDClient, Coin } from '@terra-money/terra.js';

// connect to bombay testnet
const terra = new LCDClient({
URL: 'https://bombay-lcd.terra.dev',
chainID: 'bombay-12',
// connect to pisco testnet
const client = new LCDClient({
URL: 'https://pisco-lcd.terra.dev',
chainID: 'pisco-1',
isClassic: false // if it is unset, LCDClient assumes the flag is false.
});

// connect to columbus-5 terra classic network
const client = new LCDClient({
URL: 'https://columbus-lcd.terra.dev',
chainID: 'columbus-5',
isClassic: true // *set to true to connect terra-classic chain*
});

// To use LocalTerra
Expand All @@ -70,11 +78,9 @@ const terra = new LCDClient({
// chainID: 'localterra'
// });

// get the current swap rate from 1 TerraUSD to TerraKRW
const offerCoin = new Coin('uusd', '1000000');
terra.market.swapRate(offerCoin, 'ukrw').then(c => {
console.log(`${offerCoin.toString()} can be swapped for ${c.toString()}`);
});
// get the current balance of `terra1x46rqay4d3cssq8gxxvqz8xt6nwlz4td20k38v`
const balance = terra.bank.balance('terra1x46rqay4d3cssq8gxxvqz8xt6nwlz4td20k38v');
console.log(balance);
```

### Broadcasting transactions
Expand All @@ -92,8 +98,8 @@ const mk = new MnemonicKey({

// connect to bombay testnet
const terra = new LCDClient({
URL: 'https://bombay-lcd.terra.dev',
chainID: 'bombay-12',
URL: 'https://pisco-lcd.terra.dev',
chainID: 'pisco-1',
});

// To use LocalTerra
Expand All @@ -110,7 +116,7 @@ const wallet = terra.wallet(mk);
const send = new MsgSend(
'terra1x46rqay4d3cssq8gxxvqz8xt6nwlz4td20k38v',
'terra17lmam6zguazs5q5u6z5mmx76uj63gldnse2pdp',
{ uluna: 1000000, ukrw: 1230201, uusd: 1312029 }
{ uluna: 1200000}
);

wallet
Expand Down
29 changes: 21 additions & 8 deletions integration-tests/contract.ts
Expand Up @@ -7,21 +7,25 @@ import {
getCodeId,
getContractAddress,
} from '../src';
import { AccessConfig, AccessType } from '../src/core/wasm/AccessConfig';
import * as fs from 'fs';

const isLegacy = false;
const terra = new LocalTerra(isLegacy);

// test1 key from localterra accounts
const terra = new LocalTerra();
const { test1 } = terra.wallets;

async function main(): Promise<void> {
const storeCode = new MsgStoreCode(
test1.key.accAddress,
fs.readFileSync('contract.wasm').toString('base64')
fs.readFileSync(isLegacy ? 'contract.wasm.7' : 'contract.wasm.8').toString('base64'),
isLegacy ? undefined : new AccessConfig(AccessType.ACCESS_TYPE_EVERYBODY, "")
);
const storeCodeTx = await test1.createAndSignTx({
msgs: [storeCode],
});
const storeCodeTxResult = await terra.tx.broadcast(storeCodeTx);
const storeCodeTxResult = await terra.tx.broadcastBlock(storeCodeTx);

console.log(storeCodeTxResult);

Expand All @@ -38,13 +42,14 @@ async function main(): Promise<void> {
undefined,
+codeId, // code ID
{ count: 0, }, // InitMsg
{ uluna: 10000000, ukrw: 1000000 } // init coins
{ uluna: 1000000 }, // init coins
"testlabel",
);

const instantiateTx = await test1.createAndSignTx({
msgs: [instantiate],
});
const instantiateTxResult = await terra.tx.broadcast(instantiateTx);
const instantiateTxResult = await terra.tx.broadcastBlock(instantiateTx);

console.log(instantiateTxResult);

Expand All @@ -54,7 +59,7 @@ async function main(): Promise<void> {
);
}

const contractAddress = getContractAddress(instantiateTxResult);
const contractAddress = getContractAddress(instantiateTxResult, 0, isLegacy);

const execute = new MsgExecuteContract(
test1.key.accAddress, // sender
Expand All @@ -65,8 +70,16 @@ async function main(): Promise<void> {
const executeTx = await test1.createAndSignTx({
msgs: [execute],
});
const executeTxResult = await terra.tx.broadcast(executeTx);
const executeTxResult = await terra.tx.broadcastBlock(executeTx);
console.log(executeTxResult);

console.log(await terra.wasm.contractQuery(contractAddress, { "get_count": {} }));

const [history, _] = await terra.wasm.contractHistory(contractAddress);
console.log(history.map(h => h.toData()));
console.log(JSON.stringify(await terra.wasm.contractInfo(contractAddress)));
console.log(JSON.stringify(await terra.wasm.codeInfo(+codeId)));

}

main().then(console.log);
main().then(console.log).catch(console.log)
Binary file added integration-tests/contract.wasm.7
Binary file not shown.
Binary file added integration-tests/contract.wasm.8
Binary file not shown.
13 changes: 10 additions & 3 deletions integration-tests/send.ts
Expand Up @@ -9,8 +9,8 @@ async function main() {
});

const bombay = new LCDClient({
chainID: 'bombay-12',
URL: 'https://bombay-lcd.terra.dev',
chainID: 'localterra',
URL: 'http://localhost:1317',
});

// a wallet can be created out of any key
Expand All @@ -21,7 +21,7 @@ async function main() {
const send = new MsgSend(
'terra1x46rqay4d3cssq8gxxvqz8xt6nwlz4td20k38v',
'terra17lmam6zguazs5q5u6z5mmx76uj63gldnse2pdp',
{ uluna: 1312029 }
{ uluna: 1000 }
);

wallet
Expand All @@ -31,6 +31,13 @@ async function main() {
memo: 'test from terra.js!',
})
.then(tx => {
/*
tx.body.messages[0] = new MsgSend(
'terra1x46rqay4d3cssq8gxxvqz8xt6nwlz4td20k38v',
'terra17lmam6zguazs5q5u6z5mmx76uj63gldnse2pdp',
{ uluna: 2000 }
);
*/
return bombay.tx.broadcast(tx);
})
.then(result => {
Expand Down
30 changes: 17 additions & 13 deletions integration-tests/sendMultisend.ts
Expand Up @@ -9,31 +9,32 @@ async function main() {

const mk2 = new MnemonicKey({
mnemonic:
'arrest word woman erupt kiss tank neck achieve diagram gadget siren rare valve replace outside angry dance possible purchase extra yellow cruise pride august',
'quality vacuum heart guard buzz spike sight swarm shove special gym robust assume sudden deposit grid alcohol choice devote leader tilt noodle tide penalty'
});

const bombay = new LCDClient({
chainID: 'bombay-12',
URL: 'https://bombay-lcd.terra.dev',
gasPrices: { uusd: 0.38 },
chainID: 'localterra',
URL: 'http://localhost:1317',
gasPrices: { uluna: 0.38 },
isClassic: true
});

// create a simple message that moves coin balances
const send = new MsgMultiSend(
[
new MsgMultiSend.Input('terra1x46rqay4d3cssq8gxxvqz8xt6nwlz4td20k38v', {
uusd: 100000,
uluna: 100000,
}),
new MsgMultiSend.Input('terra1fqwsd6as9v7f93vja2u7yjs98enawcaq6ge2dx', {
uusd: 200000,
new MsgMultiSend.Input('terra17lmam6zguazs5q5u6z5mmx76uj63gldnse2pdp', {
uluna: 200000,
}),
],
[
new MsgMultiSend.Output('terra17lmam6zguazs5q5u6z5mmx76uj63gldnse2pdp', {
uusd: 150000,
new MsgMultiSend.Output('terra1757tkx08n0cqrw7p86ny9lnxsqeth0wgp0em95', {
uluna: 150000,
}),
new MsgMultiSend.Output('terra1gufrav46pnpwf03yu7xz76ylkmatsxtplrxnmc', {
uusd: 150000,
uluna: 150000,
}),
]
);
Expand All @@ -49,7 +50,7 @@ async function main() {
{
msgs: [send],
memo: 'memo',
gasPrices: { uusd: 0.456 },
gasPrices: { uluna: 0.456 },
gasAdjustment: 1.4,
}
);
Expand All @@ -61,8 +62,10 @@ async function main() {
accInfo.getSequenceNumber(),
tx.auth_info,
tx.body
)
),
bombay.config.isClassic
);
console.log(`accinfo1:${accInfo}`);

const sig2 = await mk2.createSignatureAmino(
new SignDoc(
Expand All @@ -71,7 +74,8 @@ async function main() {
accInfo2.getSequenceNumber(),
tx.auth_info,
tx.body
)
),
bombay.config.isClassic
);

tx.appendSignatures([sig1, sig2]);
Expand Down
9 changes: 6 additions & 3 deletions integration-tests/sendMultisig.ts
Expand Up @@ -25,6 +25,9 @@ async function main() {
mnemonic:
'shrug resist find inch narrow tumble knee fringe wide mandate angry sense grab rack fork snack family until bread lake bridge heavy goat want',
});
console.log(mk1.accAddress)
console.log(mk2.accAddress)
console.log(mk3.accAddress)

const multisigPubkey = new LegacyAminoMultisigPublicKey(2, [
mk1.publicKey as SimplePublicKey,
Expand All @@ -33,8 +36,8 @@ async function main() {
]);

const bombay = new LCDClient({
chainID: 'bombay-12',
URL: 'https://bombay-lcd.terra.dev',
chainID: 'localterra',
URL: 'http://localhost:1317',
gasPrices: { uusd: 0.38 },
});

Expand Down Expand Up @@ -94,7 +97,7 @@ async function main() {
),
]);
console.log(JSON.stringify(tx.toData()));
bombay.tx.broadcast(tx).then(console.log);
bombay.tx.broadcastBlock(tx).then(console.log);
}

main().catch(console.error);
15 changes: 8 additions & 7 deletions integration-tests/txSearch.ts
@@ -1,17 +1,18 @@
import { LCDClient } from '../src';

async function main() {
const bombay = new LCDClient({
chainID: 'bombay-12',
URL: 'https://bombay-lcd.terra.dev',
const terra = new LCDClient({
chainID: 'localterra',
URL: 'http://localhost:1317',
gasPrices: { uusd: 0.38 },
});

console.log(
`Txs Page 1: ${JSON.stringify(
(
await bombay.tx.search({
events: [{ key: 'tx.height', value: '5947667' }],
await terra.tx.search({
events: [{ key: 'tx.height', value: '8343' },
{key:'message.sender', value:'terra1x46rqay4d3cssq8gxxvqz8xt6nwlz4td20k38p'}],
'pagination.limit': '100',
})
).txs.map(tx => tx.txhash)
Expand All @@ -21,8 +22,8 @@ async function main() {
console.log(
`Txs Page 2: ${JSON.stringify(
(
await bombay.tx.search({
events: [{ key: 'tx.height', value: '5947667' }],
await terra.tx.search({
events: [{ key: 'tx.height', value: '8345' }],
'pagination.limit': '50',
'pagination.offset': '1',
})
Expand Down

0 comments on commit 870e74d

Please sign in to comment.