Example Usage

Remote Signing

The below example code displays the usage of customSigner to remotely sign a transaction that was generated by Anchor Earn.
For demonstration purposes, implementation of customSigner was done using Terra.js. For real usage customSigner should be connected with a remote signing solution used by the integrator (e.g. Ledger Hardware Wallet, Custodian APIs).
1
const anchorEarn = new AnchorEarn({
2
chain: CHAINS.TERRA,
3
network: NETWORKS.BOMBAY_12,
4
mnemonic:
5
'...',
6
});
7
8
// customSigner signs the generated unsigned tx
9
const customSigner = async (tx: Msg[]) => {
10
const account = new MnemonicKey({
11
mnemonic:
12
'...',
13
});
14
15
const wallet = new Wallet(
16
new LCDClient({
17
URL: 'https://bombay-lcd.terra.dev',
18
chainID: 'bombay-12',
19
}),
20
account,
21
);
22
23
return await wallet.createAndSignTx({
24
msgs: tx,
25
gasAdjustment: 2,
26
gasPrices: { uusd: 0.15 },
27
});
28
};
29
30
await anchorEarn.deposit({
31
amount: '0.01',
32
currency: DENOMS.UST,
33
log: (data) => {
34
console.log(data);
35
},
36
customSigner: customSigner,
37
});
Copied!

Remote Signing & Broadcasting

The below example code displays the usage of customBroadcaster to remotely sign and broadcast a transaction that was generated by Anchor Earn.
For demonstration purposes, implementation of customBroadcaster was done using Terra.js. For real usage customBroadcaster should be connected with a remote signing solution used by the integrator (e.g. Web Wallet Extension).
1
const anchorEarn = new AnchorEarn({
2
chain: CHAINS.TERRA,
3
network: NETWORKS.BOMBAY_12,
4
mnemonic:
5
'...',
6
});
7
8
// customBroadcaster signs and broadcasts the generated unsigned tx
9
const customBroadcaster = async (tx: Msg[]) => {
10
const lcd = new LCDClient({
11
URL: 'https://bombay-lcd.terra.dev',
12
chainID: 'bombay-12',
13
});
14
15
const wallet = new Wallet(
16
lcd,
17
new MnemonicKey({
18
mnemonic:
19
'...',
20
}),
21
);
22
23
const signedTx = await wallet.createAndSignTx({
24
msgs: tx,
25
gasAdjustment: 2,
26
gasPrices: { uusd: 0.15 },
27
});
28
29
return lcd.tx.broadcastSync(signedTx).then((result) => {
30
return result.txhash;
31
});
32
};
33
34
await anchorEarn.withdraw({
35
amount: '0.01',
36
currency: DENOMS.AUST,
37
log: (data) => {
38
console.log(data);
39
},
40
customBroadcaster: customBroadcaster
41
});
Copied!

Logging Transaction Progress

The below example code displays the usage of loggable to check on request progresses that were generated by Anchor Earn.
1
const deposit = await anchorEarn.deposit({
2
amount: '...',
3
currency: DENOMS.UST,
4
log: (data) => {
5
console.log(data);
6
}
7
});
Copied!
Last modified 2mo ago